Using a generator for non Id fields?

Thank you for the contribution, @Weetch_Russell. Also note that there is no need to use the TFDQuery component directly, you can use Aurelius interfaces directly and thus the code is not dependent on a specific component:

class function TConnectionUtils.GetNextSequenceValue(AConnection: IDBConnection; const SequenceName: string): Int64;
const
  SQL = 'SELECT NEXT VALUE FOR %s FROM RDB$DATABASE';
var
  Query: IDBStatement;
  ResultSet: IDBResultSet;
begin
  Query := AConnection.CreateStatement;
  Statement.SetSQLCommand(Format(SQL, [SequenceName.ToUpper]));
  ResultSet := Statement.ExecuteQuery;
  if ResultSet.Next then
    Result := ResultSet.GetFieldValue(0)
  else
    Result := -1;
end;

I'd also recommend not using resourcestring for SQL statements.

1 Like