Créer une table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| procedure NewTable(UneTable: String; UIBdatabase: tuibdatabase);
var
q: TUIBQuery;
t: TUIBTransaction;
begin
if UIBdatabase <> nil then
begin
q := TUIBQuery.Create(nil);
t := TUIBTransaction.Create(nil);
q.Transaction := t;
t.Database := UIBdatabase;
UneTable:=UpperCase(UneTable);
try
with q do
begin
close;
sql.clear;
sql.add('select 1 from RDB$RELATIONS where RDB$RELATION_NAME = :UneTable');
Params.ByNameAsString['UneTable'] := UneTable;
open;
if Fields.ByNameAsInteger['CONSTANT'] = 1 then
ELSE begin
///////////////////////////////////////////////////////////
close;
sql.clear;
sql.Text := ' CREATE GENERATOR GEN_' + UneTable + '_ID;';
try
ExecSQL;
Except
On E: Exception do
ShowMessage('Création Imposible :' + E.Message);
end;
///////////////////////////////////////////////////////////
close;
sql.clear;
sql.Text := 'CREATE TABLE ' + UneTable + ' ' + '(ID INTEGER) ;';
try
ExecSQL;
Except
On E: Exception do
ShowMessage('Création Imposible :' + E.Message);
end;
///////////////////////////////////////////////////////////
close;
sql.clear;
sql.Text := 'CREATE UNIQUE INDEX ' + UneTable + '_IDX1 on '+UneTable+' (ID)';
try
ExecSQL;
Except
On E: Exception do
ShowMessage('Création Imposible :' + E.Message);
end;
/////////////////////////////////////////////////////////////
close;
sql.clear;
sql.Text := 'CREATE OR ALTER TRIGGER ' + UneTable +
'_BI FOR ' + UneTable + ' ACTIVE BEFORE INSERT POSITION 0 ' + 'as ' +
'begin ' + ' if (new.id is null) then' +
' new.id = gen_id(gen_' + UneTable + '_id,1);' + 'end';
try
ExecSQL;
Except
On E: Exception do
ShowMessage('Création Imposible :' + E.Message);
end;
///////////////////////////////////////////////////////////
end;
end;
finally
q.Free;
t.Free;
end;
end;
end; |
Creer un champ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| procedure NewField(UneTable:String; UnChamp: String; UnType:String; UIBdatabase: tuibdatabase);
var
q : TUIBQuery;
t : TUIBTransaction;
begin
if UIBDatabase<>nil then
begin
UneTable:=UpperCase(UneTable);
UnChamp:=UpperCase(UnChamp);
q:=TUIBQuery.Create(nil);
t:=TUIBTransaction.Create(nil);
q.Transaction:=t;
t.Database:=UIBDatabase;
try
with q do
begin
close;
sql.clear;
sql.add('select 1 from rdb$relation_fields where rdb$relation_name =:UneTable and RDB$FIELD_NAME =:UnChamp;');
Params.ByNameAsString['UneTable']:=UneTable;
Params.ByNameAsString['UnChamp']:=UnChamp;
open;
if Fields.ByNameAsInteger['CONSTANT']=1 then ELSE
begin
close;
sql.Clear;
Sql.Text := Format('ALTER TABLE %s ADD %s %s;', [UneTable, UnChamp, UnType]);
try
ExecSQL;
Except
On E:Exception do ShowMessage('Création Imposible :'+E.Message);
end;
end;
end;
finally
q.Free;
t.Free;
end;
end;
end; |
récupérer et incrémenté le générateur format IBExpert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| function GetID(aType:string; Uibdatabase:Tuibdatabase):Integer;
var
q : TUIBQuery;
t : TUIBTransaction;
begin
q:=TUIBQuery.Create(nil);
t:=TUIBTransaction.Create(nil);
q.Transaction:=t;
t.DataBase:=Uibdatabase;
try
with q do
begin
SQL.Add('SELECT GEN_ID (GEN_'+aType+'_ID, 1) from rdb$database');
Open;
Result:=q.Fields.AsInteger[0];
Close;
end;
finally
FreeAndNil(q);
FreeAndNil(t);
end;
end; |
Typiquement on appelle les les procédures
j'ai pas d'exemple sous la main pour newtable mais bon, c'est facile a appellé
Newfiel
NewField('FACTURES','V_TELEPHONE','VARCHAR(20) CHARACTER SET ISO8859_1',dmVulcain.UIBdbVulcain);
get ID
1 2
| if (FieldByName('ID').IsNull) or ( FieldByName('ID').AsInteger<1)
then FieldByName('ID').AsInteger:= GetID('FACTURES', UIBDatabase); |
Avec ces procedures dans un fichier .pas, je me prends plus la tête pour la syntaxe, je recommande quand même de tester ses tables avec un outil comme IbExpert, flamerobin etc etc..
Cordialement
André
Pendant que j'y suis, dans l'état ou je l'ai laissé il y a trois semaine, travaillé sur nzn.delphi changer la longeur d'un champ char ou varchar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| procedure AlterFieldStringLength(db: TUIBDatabase; const TableName, FieldName: string;
ExpectedLenght: Integer);
var
T: TUIBTransaction;
begin
T := TUIBTransaction.create(nil);
T.DataBase := db;
with TUIBQuery.Create(nil) do
begin
Transaction := T;
SQL.Text :=
'select f.RDB$FIELD_LENGTH '
+ 'from RDB$RELATION_FIELDS r '
+ 'join RDB$FIELDS f on (r.RDB$FIELD_SOURCE=f.RDB$FIELD_NAME) '
+ 'where (r.RDB$RELATION_NAME=?) and (r.RDB$FIELD_NAME=?)';
try
Params.AsString[0] := TableName;
Params.AsString[1] := FieldName;
Open;
if (not EOF) and (Fields.AsInteger[0] < ExpectedLenght) then
T.ExecuteImmediate(
'ALTER TABLE ' + TableName
+ ' ALTER ' + FieldName
+ ' TYPE VARCHAR('+IntToStr(ExpectedLenght)+')'
);
if (not EOF) and (Fields.AsInteger[0] > ExpectedLenght) then
begin
T.ExecuteImmediate('ALTER TABLE '+TableName+' ADD c_temp VARCHAR('+IntToStr(ExpectedLenght)+')') ;
T.Commit;
T.ExecuteImmediate('UPDATE '+TableName+' SET c_temp = '+FieldName+'');
T.ExecuteImmediate('ALTER TABLE '+TableName+' DROP '+FieldName+'');
T.ExecuteImmediate('ALTER TABLE '+TableName+' ALTER c_temp TO '+FieldName+'');
end;
finally
Free;
T.Free;
end;
end;
end; |
Partager