generatorname from a field

In this forum we will discuss things relating the ZEOSLib 6.6.x stable versions

Moderators: gto, EgonHugeist

Post Reply
slai
Senior Boarder
Senior Boarder
Posts: 53
Joined: 07.07.2006, 10:45
Location: office

generatorname from a field

Post by slai »

Hy there

How can i get the generatorname from a field in a table?

i use firebird 2

thanks for reply
seawolf
Zeos Dev Team *
Zeos Dev Team *
Posts: 385
Joined: 04.06.2008, 19:50
Contact:

Post by seawolf »

Usually

- create a generator
- create a trigger BeforeInsert tor your table

CREATE TRIGGER trgTTEST_BI for TTEST
active before insert position 0
as
begin
if ((new.id is null) or (new.id = 0)) then
begin
new.id = gen_id( gidTest, 1 );
end
end

where id is the field name
slai
Senior Boarder
Senior Boarder
Posts: 53
Joined: 07.07.2006, 10:45
Location: office

Post by slai »

hy seawolf thanks for your reply.

i now had a look at the metadata tables from firebird and there is a table called dependencies now if i made a trigger for the generater i can read the generatorname from the table.


something like that

Code: Select all

function GetGeneratorNameFromTableAndField(sTableName, sFieldName: String): String;
var
   tempquery : TZQuery;
   sTriggerName : String;
begin

     tempquery := TZQuery.Create(nil);
     tempquery.Connection := DBDataMod.ZCDB;
     tempquery.SQL.Add('Select * From RDB$DEPENDENCIES where RDB$DEPENDED_ON_NAME = '''+sTableName+''' and RDB$FIELD_NAME = '''+sFieldName+'''');
     tempquery.Open;

     if tempquery.RecordCount > 0 then begin    //RDB$DEPENDENT_NAME
         sTriggerName := tempquery.FieldByName('RDB$DEPENDENT_NAME').AsString;
     end else begin
         sTriggerName:='';
     end;

     if sTriggerName = '' then begin
        result := '';
     end else begin
       tempquery.Close;
       tempquery.SQL.Clear;
       tempquery.SQL.Add('Select CAST(RDB$DEPENDED_ON_NAME as VARCHAR(255)) gen From RDB$DEPENDENCIES where RDB$DEPENDENT_NAME = '''+sTriggerName+''' and RDB$FIELD_NAME is NULL');
       tempquery.Open;
       if tempquery.RecordCount = 0 then begin
           result := '';
       end else begin
           result := tempquery.FieldByName('GEN').AsString;
       end;
     end;

     tempquery.Close;
     tempquery.Free;
end;
Post Reply