{$IFNDEF CLR}
{$I LiteDac.inc}
unit LiteSQLGeneratorUni;
{$ENDIF}

interface

uses
  CRAccess, DASQLGenerator;

type
  TCustomLiteSQLGenerator = class(TDASQLGenerator)
  protected
    procedure GenerateInsertSQL(
      const KeyAndDataFields: TKeyAndDataFields;
      const ModifiedFieldsOnly: boolean;
      const Index: integer = -1); override;
  public
    function GenerateRecCountSQL(UseBaseSQL: boolean = False): string; override;
  end;

implementation

{ TCustomLiteSQLGenerator }

procedure TCustomLiteSQLGenerator.GenerateInsertSQL(
  const KeyAndDataFields: TKeyAndDataFields;
  const ModifiedFieldsOnly: boolean;
  const Index: integer);
begin
  inherited GenerateInsertSQL(KeyAndDataFields, ModifiedFieldsOnly, Index);

  if FFldSB.Length = 0 then begin
    Clear;
    inherited GenerateInsertSQL(KeyAndDataFields, False, Index);
  end;
end;

function TCustomLiteSQLGenerator.GenerateRecCountSQL(UseBaseSQL: boolean = False): string;
var
  SelectSQL: string;
begin
  if UseBaseSQL then
    SelectSQL := FService.BaseSQL
  else
    SelectSQL := FService.FinalSQL;
  SelectSQL := SQLSetOrderBy(SelectSQL, '');

  Clear;
  FHeaderSB.Append('SELECT count(*) FROM (');
  FMiddleSB.Append(SelectSQL);
  FFooterSB.Append(')');
  Result := AssembleSB();
end;

end.
