Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
knight / Приложение.doc
Скачиваний:
9
Добавлен:
16.04.2013
Размер:
147.46 Кб
Скачать

Приложение 1

SQL-скрипт создания структуры базы данных:

drop domain T_DOMAIN_CHR;

create domain T_DOMAIN_CHR as CHAR(10) ;

drop domain T_DOMAIN_INT;

create domain T_DOMAIN_INT as NUMERIC ;

drop domain T_DOMAIN_CHR_SHORT;

create domain T_DOMAIN_CHR_SHORT as CHAR(1) ;

drop domain T_DOMAIN_CHR_LONG;

create domain T_DOMAIN_CHR_LONG as CHAR(50) ;

create table MODEL

(

ID CHAR(10),

REMARK CHAR(10),

PATH CHAR(10),

MFILE CHAR(10)

);

create table INDEXES

(

IID CHAR(10) not null,

MN T_DOMAIN_INT,

MX T_DOMAIN_INT,

STEP T_DOMAIN_INT,

constraint PK_INDEXES primary key (IID)

);

create table VERSION

(

NUM T_DOMAIN_INT not null,

REMARK T_DOMAIN_CHR_LONG,

constraint PK_VERSION primary key (NUM)

);

create table PARAMS

(

ID T_DOMAIN_CHR not null,

REMARK T_DOMAIN_CHR_LONG,

constraint PK_PARAMS primary key (ID)

);

create table PARAM

(

Id char(10),

IID CHAR(10),

IID_VALUE T_DOMAIN_INT,

NUM T_DOMAIN_INT not null,

constraint PK_PARAM primary key (NUM)

);

create ASC index IDX6 on PARAM (ID, NUM, IID);

create table INTER

(

Id t_domain_chr not null,

IID T_DOMAIN_CHR,

INFORM T_DOMAIN_INT,

KIND T_DOMAIN_CHR_SHORT

);

create table VAR_VALUE

(

Id char(10),

NUM T_DOMAIN_INT,

ID_VALUE T_DOMAIN_CHR_LONG,

VER T_DOMAIN_INT

);

alter table PARAM

add constraint FK_REF_3 foreign key (ID)

references PARAMS;

alter table PARAM

add constraint FK_REF_5 foreign key (IID)

references INDEXES;

alter table INTER

add constraint FK_REF_6 foreign key (ID)

references PARAMS;

alter table INTER

add constraint FK_REF_7 foreign key (IID)

references INDEXES;

alter table VAR_VALUE

add constraint FK_REF_2 foreign key (ID)

references PARAMS;

alter table VAR_VALUE

add constraint FK_REF_4 foreign key (VER)

references VERSION;

alter table VAR_VALUE

add constraint FK_REF_1 foreign key (NUM)

references PARAM;

Хранимые процедуры базы данных:

CREATE PROCEDURE DEL_OLD_VAL (

VER0 INTEGER

) AS

BEGIN

delete from var_value where ver=:ver0;

END

CREATE PROCEDURE INS_NEW_VAL (

ID_VAL CHAR(40),

ID0 CHAR(10),

NUM0 INTEGER,

Ver0 integer

) AS

BEGIN

insert into var_value (id,id_value,num,ver) values (:id0,:id_val,:num0,:ver0);

END

CREATE PROCEDURE KILL_OLD AS

BEGIN

delete from var_value where ver>0;

delete from version where num>0;

END

CREATE PROCEDURE DEL_PARAM (

PARAM CHAR(10)

) AS

BEGIN

delete from var_value where id=:param;

delete from inter where id=:param;

delete from params where id=:param;

delete from param where id=:param;

END

Процедура создания нового параметра:

if (eVar.Text='') or (lbVar.Items.Count=0) then exit;

if lbVar.Items.Count>2 then begin

MessageDlg('В переменной должно быть не больше двух индексов',mtError,[mbOk],0);

exit;

end;

IndexChance:=not IndexChance;

lbAll.Enabled:=not lbAll.Enabled;

lbVar.Enabled:=not lbVar.Enabled;

sgVar.Enabled:=not sgVar.Enabled;

eVar.Enabled:=not eVar.Enabled;

eRem.Enabled:=not eRem.Enabled;

sgVar.ShowHint:=not sgVar.ShowHint;

if lbVar.Items.Count=2 then Two:=True

else Two:=False;

if not IndexChance then begin

fmMain.qMain.SQL.Clear;

fmMain.qMain.SQL.Add('SELECT * from Indexes where ID='+#39+lbVar.Items.Strings[0]+#39+';');

fmMain.qMain.Open;

sgVar.ColCount:=round((fmMain.qMain.FieldByName('Mx').AsInteger-fmMain.qMain.FieldByName('Mn').AsInteger) div fmMain.qMain.FieldByName('Step').AsInteger+1)+1;

if Two then begin

fmMain.qSec.SQL.Clear;

fmMain.qSec.SQL.Add('SELECT * from Indexes where ID='+#39+lbVar.Items.Strings[1]+#39+';');

fmMain.qSec.Open;

sgVar.RowCount:=round((fmMain.qSec.FieldByName('Mx').AsInteger-fmMain.qSec.FieldByName('Mn').AsInteger) div fmMain.qSec.FieldByName('Step').AsInteger+1)+1;

end;

sgVar.FixedRows:=1;

sgVar.FixedCols:=1;

for i:=1 to sgVar.ColCount-1 do

sgVar.Cells[i,0]:=IntToStr(fmMain.qMain.FieldByName('Mn').AsInteger+(i-1)*fmMain.qMain.FieldByName('Step').AsInteger);

if Two then begin

for i:=1 to sgVar.RowCount-1 do

sgVar.Cells[0,i]:=IntToStr(fmMain.qSec.FieldByName('Mn').AsInteger+(i-1)*fmMain.qSec.FieldByName('Step').AsInteger);

sgVar.Cells[0,0]:=fmMain.qSec.FieldByName('Id').AsString+'\'+fmMain.qMain.FieldByName('Id').AsString;

end

else sgVar.Cells[0,0]:=fmMain.qMain.FieldByName('Id').AsString;

fmMain.qMain.Close;

fmMain.qSec.Close;

{ fmMain.qMain.SQL.Add('SELECT Mn,Mx,Step from Indexes where');

for i:=0 to lbVar.Items.Count-1 do begin

if lbVar.Items.Count=1

then fmMain.qMain.SQL.Add('ID='+#39+lbVar.Items.Strings[i]+#39+';')

else begin

if i=0 then fmMain.qMain.SQL.Add('ID='+#39+lbVar.Items.Strings[i]+#39 );

if i=lbVar.Items.Count-1 then fmMain.qMain.SQL.Add('or ID='+#39+lbVar.Items.Strings[i]+#39+';');

if not ((i=0) or (i=lbVar.Items.Count-1))

then fmMain.qMain.SQL.Add('or ID='+#39+lbVar.Items.Strings[i]+#39);

end;

end;

fmMain.qMain.Open;

iTmp:=1;

for i:=0 to fmMain.qMain.RecordCount-1 do begin

iTmp:=iTmp*round((fmMain.qMain.FieldByName('Mx').AsInteger-fmMain.qMain.FieldByName('Mn').AsInteger) div fmMain.qMain.FieldByName('Step').AsInteger+1);

if i<>fmMain.qMain.RecordCount-1 then fmMain.qMain.Next;

end;

sgVar.ColCount:=iTmp;}

end

else begin

for i:=0 to lbVar.Items.Count-1 do begin

fmMain.qMain.SQL.Clear;

fmMain.qMain.SQL.Add('INSERT INTO Inter VALUES('+#39+eVar.Text+#39+','+#39+lbVar.Items.Strings[i]+#39+',1,'+#39+'v'+#39+');');

fmMain.qMain.ExecSQL;

end;

num:=0;

for i:=1 to sgVar.ColCount-1 do

for j:=1 to sgVar.RowCount-1 do begin

fmMain.qMain.SQL.Clear;

inc(num);

fmMain.qMain.SQL.Add('INSERT INTO Param VALUES('+#39+eVar.Text+#39+','+#39+lbVar.Items.Strings[0]+#39+','+#39+sgVar.Cells[i,0]+#39+','+IntToStr(num)+');');

fmMain.qMain.ExecSQL;

if Two then begin

fmMain.qMain.SQL.Clear;

fmMain.qMain.SQL.Add('INSERT INTO Param VALUES('+#39+eVar.Text+#39+','+#39+lbVar.Items.Strings[1]+#39+','+#39+sgVar.Cells[0,j]+#39+','+IntToStr(num)+');');

fmMain.qMain.ExecSQL;

end;

fmMain.qMain.SQL.Clear;

fmMain.qMain.SQL.Add('INSERT INTO Var_value VALUES('+#39+eVar.Text+#39+','+IntToStr(num)+','+#39+sgVar.Cells[i,j]+#39+','+'0);');

fmMain.qMain.ExecSQL;

fmMain.qMain.SQL.Clear;

fmMain.qMain.SQL.Add('INSERT INTO Var_value VALUES('+#39+eVar.Text+#39+','+IntToStr(num)+'," ",'+'-1);');

fmMain.qMain.ExecSQL;

end;

fmMain.qMain.SQL.Clear;

fmMain.qMain.SQL.Add('INSERT INTO Params (id,remark) VALUES('+#39+eVar.Text+#39+','+#39+eRem.Text+#39+')');// changed for MDI

fmMain.qMain.ExecSQL;

// Modify & Add by Knight

if not(TfmForm1(fmMain.ActiveMDIChild).tv.Selected.HasChildren or not(TfmForm1(fmMain.ActiveMDIChild).tv.Selected.Parent<>nil )) then TfmForm1(fmMain.ActiveMDIChild).tv.Items.AddChild(TfmForm1(fmMain.ActiveMDIChild).tv.Selected.Parent,eVar.Text)

else TfmForm1(fmMain.ActiveMDIChild).tv.Items.AddChild(TfmForm1(fmMain.ActiveMDIChild).tv.Selected,eVar.Text);

// End

Close;

end;

end;

Процедура сохранения новой версии расчетов:

procedure TfmMain.UpdateTable(model:string;valuesList:TStringList);

var

var_name,var_name0, strSelect:string;

i,j,count_idx,num:integer;

strList:TStringList;

// =====================

function getIndexes(name:string):TStringList;

var

slist:TStringList;

str:String;

begin

slist:=TStringList.Create;

str:=copy(name,pos('[',name)+1,pos(']',name)-pos('[',name)-1);

if pos(',',str)=0 then slist.add(str)

else begin

str:=str+',';

while pos(',',str)<>0 do begin

slist.add(copy(str,1,pos(',',str)-1));

delete(str,1,pos(',',str));

end;

end;

getIndexes:=slist;

end;

// =====================

function getNum(strList:TstringList):longint;

var

i,num:longint;

results:Boolean;

begin

qMain.Filter:=qmain.Fields[2].fieldname+'='+#39+strList[0]+#39;

for i:=2 to strList.Count do

qMain.Filter:=qMain.Filter+' and '+qmain.Fields[2*i].name+'='+#39+strList[i-1]+#39;

qMain.Filtered:=true;

getNum:=qMain.fieldbyname('num').asInteger;

qMain.filter:='';

qMain.Filtered:=false;

end;

// =====================

begin

// qMain.DatabaseName:=model;

database.StartTransaction;

fmProgress.show;

fmProgress.ProgressBar1.Position:=0;

fmProgress.Caption:='Анализ выходных данных';

fmProgress.ProgressBar1.Max:=valueslist.Count+1;

fmProgress.ProgressBar1.Position:=fmProgress.ProgressBar1.Position+1;

{ qMain.sql.clear;

qMain.sql.Add('delete from var_value where ver='+inttostr(curversion));

qMain.ExecSQL;

}

sp.UnPrepare;

sp.StoredProcName:='del_old_val';

sp.Prepare;

sp.Params.parambyname('ver0').asInteger:=curVersion;

sp.ExecProc;

qMain.close;

qMain.sql.Clear;

qMain.sql.add('select * from params');

qMain.open;

i:=0;

while i<valueslist.Count do begin

fmProgress.ProgressBar1.Position:=fmProgress.ProgressBar1.Position+1;

var_name:=copy(valuesList.Names[i],1,pos('[',valuesList.names[i])-1);

if var_name<>'' then qMain.Filter:='id='+#39+var_name+#39;

qMain.Filtered:=true;

if (qMain.RecordCount=0)or(var_name='') then valueslist.Delete(i)

else begin

inc(i);

end;

qMain.Filtered:=false;

end;

qMain.filter:='';

qMain.close;

i:=0;

strList:=TStringList.Create;

num:=0;

fmProgress.ProgressBar1.Position:=0;

fmProgress.Caption:='Запись данных в базу';

fmProgress.ProgressBar1.Max:=valueslist.Count;

while (i<valuesList.Count) do begin

var_name0:=copy(valuesList.Names[i],1,pos('[',valuesList.names[i])-1);

var_name:=var_name0;

strList:=CreateSelect(var_name);

qMain.SQL.Clear;

qMain.SQL:=strList;

qMain.open;

qSec.close;

while (i<valuesList.Count) and

(copy(valuesList.Names[i],1,pos('[',valuesList.names[i])-1)=var_name0) do begin

fmProgress.ProgressBar1.Position:=fmProgress.ProgressBar1.Position+1;

strList:=getIndexes(valuesList.Names[i]); // возвращает значение индексов

num:=getNum(strList);// возвращает значение num из таблицы param в соотв-ии со значениями индексов из strList

sp.UnPrepare;

sp.StoredProcName:='ins_new_val';

sp.Prepare;

sp.Params.parambyname('id0').asString:=var_name0;

sp.Params.parambyname('num0').asInteger:=num;

sp.Params.parambyname('ver0').asInteger:=curversion;

sp.Params.parambyname('id_val').asString:=trim(valuesList.Values[valuesList.Names[i]]);

sp.ExecProc;

inc(i);

end;

qSec.close;

qMain.close;

end;

strList.Free;

database.Commit;

// обновление таблицы

{ for i:=0 to fmForm1.Count_grids-1 do

begin

for j:=0 to fmForm1.tblgrids[i].rowcount-2 do

begin

if fmForm1.tblgrids[i].ver[j]=inttostr(curversion) then

begin

fmForm1.ActiveStringGrid:=fmForm1.tblgrids[i];

fmForm1.ActiveStringGrid.row:=j+1;

fmForm1.N7Click(fmMain);

end;

end;

end;

// обновление графиков

{ for i:=0 to fmForm1.countgraph-1 do

if model=TfmForm1(fmMain.ActiveMDIChild).graphlist[i].model

then TfmForm1(fmMain.ActiveMDIChild).GraphList[i].N3Click(fmMain);

}

fmProgress.close;

end;

Соседние файлы в папке knight