unit Aurelius.Sql.Firebird3;

interface

uses
  Aurelius.Sql.Interfaces,
  Aurelius.Sql.Metadata,
  Aurelius.Sql.Register,
  Aurelius.Sql.Firebird;

type
  TFirebird3SQLGenerator = class(TFirebirdSQLGenerator)
  private
    FUseBoolean: Boolean;
    FUseIdentity: Boolean;
  protected
    procedure DefineColumnType(Column: TColumnMetadata); override;
    function GetSqlDialect: string; override;
    function GetSupportedFieldTypes: TFieldTypeSet; override;
    function GetSupportedFeatures: TDBFeatures; override;
  public
    constructor Create;
    property UseBoolean: Boolean read FUseBoolean write FUseBoolean;
    property UseIdentity: Boolean read FUseIdentity write FUseIdentity;
  end;

implementation

uses
  DB;

{ TFirebird3SQLGenerator }

constructor TFirebird3SQLGenerator.Create;
begin
  inherited Create;
  FUseBoolean := True;
  FUseIdentity := True;
end;

procedure TFirebird3SQLGenerator.DefineColumnType(Column: TColumnMetadata);
begin
  DefineNumericColumnType(Column);
  if Column.DataType <> '' then
    Exit;

  case Column.FieldType of
    ftBoolean:
      Column.DataType := 'BOOLEAN';
  else
    inherited DefineColumnType(Column);
  end;
  if Column.AutoGenerated then
    Column.DataType := Column.DataType + ' GENERATED BY DEFAULT AS IDENTITY (START WITH 1)';
end;

function TFirebird3SQLGenerator.GetSqlDialect: string;
begin
  Result := 'Firebird3';
end;

function TFirebird3SQLGenerator.GetSupportedFeatures: TDBFeatures;
begin
  if UseIdentity then
    Result := AllDBFeatures - [TDBFeature.Sequences, TDBFeature.Schemas]
  else
    Result := AllDBFeatures - [TDBFeature.AutoGenerated, TDBFeature.RetrieveIdOnInsert,
      TDBFeature.Schemas];
end;

function TFirebird3SQLGenerator.GetSupportedFieldTypes: TFieldTypeSet;
begin
  Result := inherited GetSupportedFieldTypes;
  if UseBoolean then
    Result := Result + [ftBoolean];
end;

initialization
  TSQLGeneratorRegister.GetInstance.RegisterGenerator(TFirebird3SQLGenerator.Create);

end.
