unit Aurelius.Id.AbstractGenerator;

{$I Aurelius.Inc}

interface

uses
  Aurelius.SQL.Interfaces;

type
  TAbstractGenerator = class abstract
  public
    // Must generate an ID for Entity. This method doesn't need to update the Entity itself,
    // just return a new valid id for the entity. Performer can be used to execute SQL statements
    // in the database, if needed (for example, to retrieve a sequence value).
    // If the id will be generated by the database and should not be used when inserting the entity,
    // this method should return an empty variant (Unassigned). This will cause the id to be ignored
    // on insert, and will make method RetrieveIdAfterInsert to be executed after the entity is saved.
    function GenerateId(Entity: TObject; Performer: ICommandPerformer): Variant; virtual; abstract;

    // This method is called after an entity is saved in database, and only if GenerateId returned an empty variant
    // Can be used to retrieve database-generated id, like identity
    function RetrieveIdAfterInsert(Entity: TObject; Performer: ICommandPerformer): Variant; virtual;
  end;

  TGeneratorClass = class of TAbstractGenerator;

implementation
uses
  Variants;

{ TAbstractGenerator }

function TAbstractGenerator.RetrieveIdAfterInsert(Entity: TObject;
  Performer: ICommandPerformer): Variant;
begin
  Result := Unassigned;
end;

end.
