Subject | Re: Inserting to a detail record |
---|---|
Author | Ed Dressel |
Post date | 2009-11-25T16:11:30Z |
> One or more of:Maybe I am missing something? I (think) am managing my own generators. Below is my slightly modified code--and even after a good night sleep, it seems that this should work?
>
> 1. Missing or wrong MasterLinks
> 2. Missing or wrong GeneratorLinks
> 3. Before Insert triggers that don't take into account that
> IBO fetches the new generated key value via GeneratorLinks.
Ed Dressel
function TdmRetireCalcs.CreateTrans(const aReadOnly: Boolean):
TIB_Transaction;
begin
result := TIB_Transaction.CreateForSession(nil, iboSession);
result.IB_Connection := ibConn;
result.ReadOnly := aReadOnly;
end;
function TdmRetireCalcs.CreateDSQL(const aTx: TIB_Transaction; const aSQL: String): TIB_DSQL;
begin
result := TIB_DSQL.CreateForSession(nil, iboSession);
result.IB_Connection := ibConn;
result.IB_Transaction := aTx;
result.SQL.Text := aSQL;
end;
procedure TdmRetireCalcs.AddClient(const aIPAddress, aEmail,
aPassword: AnsiString; out aErrorMsg: AnsiString);
var
lClientID: Integer;
lTx: TIB_Transaction;
ldsqlAdd: TIB_DSQL;
begin
lTx := CreateTrans(false);
ldsqlAdd := CreateDSQL(lTx, 'Insert into ClientInfo(Client_ID, ' +
Email, UserPassword, Admin_Rights) ' +
values (:Client_ID, :Email, :UserPassword, ''F'')');
try
try
ldsqlAdd.Prepare;
lClientID := ibConn.Gen_ID(STR_GEN_CLIENT_ID, 1);
ldsqlAdd.Params[0].AsInteger := lClientID;
ldsqlAdd.Params[1].AsString := aEmail;
ldsqlAdd.Params[2].AsString := aPassword;
ldsqlAdd.ExecSQL;
LogEvent(lTx, lClientID, ID_ADD_CLIENT, 0, aIPAddress, aEmail);
lTx.Commit;
except
lTx.Rollback;
raise;
end;
finally
ldsqlAdd.Free;
lTx.Free;
end;
end;
procedure TdmRetireCalcs.LogEvent(const aTx: TIB_Transaction;
const aClientID, aActionID, aSubActionID: Integer; const aIPAddress, aDetails: string);
var
ldsql: TIB_DSQL;
begin
ldsql := CreateDSQL(aTx, 'Insert into ClientActionLog ' +
'(Client_ID, ClientAction_ID, SubAction_ID, IP_Address, ' +
'Details) values ' +
'(:Client_ID, :ClientActoin_ID, :SubAction_ID, ' +
':IP_Address, :Details)');
try
ldsql.Prepare;
if aClientID > 0 then
ldsql.Params[0].AsInteger := aClientID;
ldsql.Params[1].AsInteger := aActionID;
if aSubActionID > 0 then
ldsql.Params[2].AsInteger := aSubActionID;
ldsql.Params[3].AsString := aIPAddress;
if Length(aDetails) > 0 then
ldsql.Params[4].AsString := aDetails;
ldsql.ExecSQL;
finally
ldsql.Free;
end;
end;