unit Aurelius.Sql.Exceptions;

{$I Aurelius.inc}

interface

uses
  SysUtils, TypInfo, DB,
  Aurelius.Global.Exceptions,
  Aurelius.Sql.BaseTypes,
  Aurelius.Sql.Interfaces;

type
  EUnexpectedWhereOperator = class(EOPFBaseException)
  public
    constructor Create(WhereOperator: TWhereOperator);
  end;

  EUnsupportedWhereOperator = class(EOPFBaseException)
  public
    constructor Create(ASQLGenerator: ISQLGenerator; WhereOperator: TWhereOperator);
  end;

  EUnexpectedAggregateFunction = class(EOPFBaseException)
  public
    constructor Create(AggregateFunction: TAggregateFunction);
  end;

  EUnsupportedAggregateFunction = class(EOPFBaseException)
  public
    constructor Create(ASQLGenerator: ISQLGenerator; AggregateFunction: TAggregateFunction);
  end;

  ESequencesNotSupported = class(EOPFBaseException)
  public
    constructor Create(ASQLGenerator: ISQLGenerator);
  end;

  EAutoGeneratedValuesNotSupported = class(EOPFBaseException)
  public
    constructor Create(ASQLGenerator: ISQLGenerator);
  end;

  EUnsupportedFieldType = class(EOPFBaseException)
  public
    constructor Create(ASQLGenerator: ISQLGenerator; FieldType: TFieldType);
  end;

  ECannotConvertFieldType = class(EOPFBaseException)
  public
    constructor Create(ASQLGenerator: ISQLGenerator; FromType, ToType: TFieldType);
  end;

  ESQLGeneratorNotFound = class(EOPFBaseException)
  public
    constructor Create(const SqlDialect: string);
  end;

  ESQLInternalError = class(EOPFInternalError)
  end;

  EUnsupportedLiteralType = class(EOPFInternalError)
    constructor Create(AType: TFieldType);
  end;

  ELimitsNotSupported = class(EOPFBaseException)
  public
    constructor Create(ASQLGenerator: ISQLGenerator);
  end;

implementation

{ EUnexpectedWhereOperator }

constructor EUnexpectedWhereOperator.Create(WhereOperator: TWhereOperator);
begin
  inherited CreateFmt('SQLGeneration: Unexpected where operator: %s.',
    [GetEnumName(TypeInfo(TWhereOperator), Ord(WhereOperator))]);
end;

{ EUnexpectedAggregateFunction }

constructor EUnexpectedAggregateFunction.Create(
  AggregateFunction: TAggregateFunction);
begin
  inherited CreateFmt('SQLGeneration: Unexpected aggregate function: %s.',
    [GetEnumName(TypeInfo(TAggregateFunction), Ord(AggregateFunction))]);
end;

{ EUnsupportedAggregateFunction }

constructor EUnsupportedAggregateFunction.Create(ASQLGenerator: ISQLGenerator;
  AggregateFunction: TAggregateFunction);
begin
  inherited CreateFmt('SQLGeneration: Aggregate function %s is not supported on %s.',
    [GetEnumName(TypeInfo(TAggregateFunction), Ord(AggregateFunction)),
    ASQLGenerator.GeneratorName]);
end;

{ EUnsupportedWhereOperator }

constructor EUnsupportedWhereOperator.Create(ASQLGenerator: ISQLGenerator;
  WhereOperator: TWhereOperator);
begin
  inherited CreateFmt('SQLGeneration: Where operator %s is not supported on %s.',
    [GetEnumName(TypeInfo(TWhereOperator), Ord(WhereOperator)),
    ASQLGenerator.GeneratorName]);
end;

{ ESequencesNotSupported }

constructor ESequencesNotSupported.Create(ASQLGenerator: ISQLGenerator);
begin
  inherited CreateFmt('SQLGeneration: Sequences are not supported on %s.',
    [ASQLGenerator.GeneratorName]);
end;

{ EAutoGeneratedValuesNotSupported }

constructor EAutoGeneratedValuesNotSupported.Create(ASQLGenerator: ISQLGenerator);
begin
  inherited CreateFmt(
    'SQLGeneration: Auto generated values are not supported on %s.',
    [ASQLGenerator.GeneratorName]);
end;

{ EUnsupportedFieldType }

constructor EUnsupportedFieldType.Create(ASQLGenerator: ISQLGenerator;
  FieldType: TFieldType);
begin
  inherited CreateFmt('SQLGeneration: Field type %s is not supported on %s.',
    [GetEnumName(TypeInfo(TFieldType), Ord(FieldType)),
    ASQLGenerator.GeneratorName]);
end;

{ ECannotConvertFieldType }

constructor ECannotConvertFieldType.Create(ASQLGenerator: ISQLGenerator; FromType, ToType: TFieldType);
begin
  inherited CreateFmt('SQLGeneration: Unable to convert value from type %s into %s on %s.',
    [GetEnumName(TypeInfo(TFieldType), Ord(FromType)),
    GetEnumName(TypeInfo(TFieldType), Ord(ToType)), ASQLGenerator.GeneratorName]);
end;

{ ESQLGeneratorNotFound }

constructor ESQLGeneratorNotFound.Create(const SqlDialect: string);
begin
  inherited CreateFmt('There is no SQLGenerator registered for SQL dialect "%s".'#13#10+
    'Add unit Aurelius.Sql.XXX to your project, where XXX is the SQL dialect you want to use', [SqlDialect]);
end;

{ EUnsupportedLiteralType }

constructor EUnsupportedLiteralType.Create(AType: TFieldType);
begin
  inherited CreateFmt('Unsupported literal type %d', [Ord(AType)]);
end;

{ ELimitsNotSupported }

constructor ELimitsNotSupported.Create(ASQLGenerator: ISQLGenerator);
begin
  inherited CreateFmt('SQLGeneration: Limits on SQL are not supported on %s.',
    [ASQLGenerator.GeneratorName]);
end;

end.
