2018-11-02 21:43:09 +01:00
|
|
|
// *************************************************************************** }
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2021-08-15 18:39:55 +02:00
|
|
|
// Copyright (c) 2010-2021 Daniele Teti and the DMVCFramework Team
|
2018-11-02 21:43:09 +01:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
|
|
|
|
unit MVCFramework.SQLGenerators.Firebird;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.Rtti,
|
|
|
|
System.Generics.Collections,
|
|
|
|
FireDAC.Phys.FB,
|
|
|
|
FireDAC.Phys.FBDef,
|
|
|
|
MVCFramework.ActiveRecord,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.RQL.Parser;
|
|
|
|
|
|
|
|
type
|
|
|
|
TMVCSQLGeneratorFirebird = class(TMVCSQLGenerator)
|
2019-02-21 18:11:14 +01:00
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
protected
|
|
|
|
function GetCompilerClass: TRQLCompilerClass; override;
|
|
|
|
public
|
2020-01-08 15:30:10 +01:00
|
|
|
function GetSequenceValueSQL(
|
2020-03-25 11:35:25 +01:00
|
|
|
const PKFieldName: string; const SequenceName: string; const Step: Integer): string; override;
|
2018-11-02 21:43:09 +01:00
|
|
|
function CreateSelectSQL(
|
|
|
|
const TableName: string;
|
2020-03-25 11:35:25 +01:00
|
|
|
const Map: TFieldsMap;
|
2018-11-02 21:43:09 +01:00
|
|
|
const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
|
|
|
function CreateInsertSQL(
|
|
|
|
const TableName: string;
|
2020-03-25 11:35:25 +01:00
|
|
|
const Map: TFieldsMap;
|
2018-11-02 21:43:09 +01:00
|
|
|
const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
|
|
|
function CreateUpdateSQL(
|
|
|
|
const TableName: string;
|
2020-03-25 11:35:25 +01:00
|
|
|
const Map: TFieldsMap;
|
2018-11-02 21:43:09 +01:00
|
|
|
const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
|
|
|
function CreateDeleteSQL(
|
|
|
|
const TableName: string;
|
2020-03-25 11:35:25 +01:00
|
|
|
const Map: TFieldsMap;
|
2018-11-02 21:43:09 +01:00
|
|
|
const PKFieldName: string;
|
2020-03-31 16:23:22 +02:00
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
2019-02-21 18:11:14 +01:00
|
|
|
function CreateDeleteAllSQL(
|
|
|
|
const TableName: string): string; override;
|
2018-11-02 21:43:09 +01:00
|
|
|
function CreateSelectByPKSQL(
|
|
|
|
const TableName: string;
|
2020-03-25 11:35:25 +01:00
|
|
|
const Map: TFieldsMap; const PKFieldName: string;
|
2020-03-31 16:23:22 +02:00
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
2019-02-21 18:11:14 +01:00
|
|
|
function CreateSQLWhereByRQL(
|
2020-06-19 19:31:34 +02:00
|
|
|
const RQL: string; const Mapping: TMVCFieldsMapping;
|
|
|
|
const UseArtificialLimit: Boolean = True;
|
2020-09-30 11:16:10 +02:00
|
|
|
const UseFilterOnly: Boolean = False;
|
2021-11-18 00:49:12 +01:00
|
|
|
const MaxRecordCount: Int32 = TMVCConstants.MAX_RECORD_COUNT): string; override;
|
2019-01-13 19:18:57 +01:00
|
|
|
function CreateSelectCount(
|
2020-03-25 11:35:25 +01:00
|
|
|
const TableName: string): string; override;
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
MVCFramework.RQL.AST2FirebirdSQL;
|
|
|
|
|
2020-03-25 11:35:25 +01:00
|
|
|
function TMVCSQLGeneratorFirebird.CreateInsertSQL(const TableName: string; const Map: TFieldsMap;
|
2018-11-02 21:43:09 +01:00
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
|
|
var
|
2020-03-25 11:35:25 +01:00
|
|
|
lKeyValue: TPair<TRttiField, TFieldInfo>;
|
2018-11-02 21:43:09 +01:00
|
|
|
lSB: TStringBuilder;
|
2020-06-19 19:31:34 +02:00
|
|
|
lPKInInsert: Boolean;
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
2020-06-19 19:31:34 +02:00
|
|
|
lPKInInsert := (not PKFieldName.IsEmpty) and (not(TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions));
|
2020-08-11 00:54:42 +02:00
|
|
|
lPKInInsert := lPKInInsert and (not(TMVCActiveRecordFieldOption.foReadOnly in PKOptions));
|
2018-11-02 21:43:09 +01:00
|
|
|
lSB := TStringBuilder.Create;
|
|
|
|
try
|
2021-04-05 19:35:46 +02:00
|
|
|
lSB.Append('INSERT INTO ' + GetTableNameForSQL(TableName) + ' (');
|
2020-06-19 19:31:34 +02:00
|
|
|
if lPKInInsert then
|
2020-03-27 00:37:28 +01:00
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
lSB.Append(GetFieldNameForSQL(PKFieldName) + ',');
|
2020-03-27 00:37:28 +01:00
|
|
|
end;
|
2018-11-02 21:43:09 +01:00
|
|
|
for lKeyValue in Map do
|
2020-03-25 11:35:25 +01:00
|
|
|
begin
|
|
|
|
if lKeyValue.Value.Writeable then
|
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
lSB.Append(GetFieldNameForSQL(lKeyValue.Value.FieldName) + ',');
|
2020-03-25 11:35:25 +01:00
|
|
|
end;
|
|
|
|
end;
|
2019-04-20 14:32:31 +02:00
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
lSB.Remove(lSB.Length - 1, 1);
|
|
|
|
lSB.Append(') values (');
|
2020-06-19 19:31:34 +02:00
|
|
|
if lPKInInsert then
|
2020-03-27 00:37:28 +01:00
|
|
|
begin
|
2021-04-07 19:40:23 +02:00
|
|
|
lSB.Append(':' + GetParamNameForSQL(PKFieldName) + ',');
|
2020-03-27 00:37:28 +01:00
|
|
|
end;
|
2018-11-02 21:43:09 +01:00
|
|
|
for lKeyValue in Map do
|
2020-03-25 11:35:25 +01:00
|
|
|
begin
|
|
|
|
if lKeyValue.Value.Writeable then
|
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
lSB.Append(':' + GetParamNameForSQL(lKeyValue.Value.FieldName) + ',');
|
2020-03-25 11:35:25 +01:00
|
|
|
end;
|
|
|
|
end;
|
2019-04-20 14:32:31 +02:00
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
lSB.Remove(lSB.Length - 1, 1);
|
|
|
|
lSB.Append(')');
|
|
|
|
|
2018-12-09 23:03:06 +01:00
|
|
|
if TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions then
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
lSB.Append(' RETURNING ' + GetFieldNameForSQL(PKFieldName));
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
Result := lSB.ToString;
|
|
|
|
finally
|
|
|
|
lSB.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorFirebird.CreateSelectByPKSQL(
|
|
|
|
const TableName: string;
|
2020-03-25 11:35:25 +01:00
|
|
|
const Map: TFieldsMap; const PKFieldName: string;
|
2020-03-31 16:23:22 +02:00
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string;
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
|
|
|
Result := CreateSelectSQL(TableName, Map, PKFieldName, PKOptions) + ' WHERE ' +
|
2021-04-05 19:35:46 +02:00
|
|
|
GetFieldNameForSQL(PKFieldName) + '= :' + GetParamNameForSQL(PKFieldName); // IntToStr(PrimaryKeyValue);
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
2019-01-13 19:18:57 +01:00
|
|
|
function TMVCSQLGeneratorFirebird.CreateSelectCount(
|
2020-03-25 11:35:25 +01:00
|
|
|
const TableName: string): string;
|
2019-01-13 19:18:57 +01:00
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
Result := 'SELECT count(*) FROM ' + GetTableNameForSQL(TableName);
|
2019-01-13 19:18:57 +01:00
|
|
|
end;
|
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
function TMVCSQLGeneratorFirebird.CreateSelectSQL(const TableName: string;
|
2020-03-25 11:35:25 +01:00
|
|
|
const Map: TFieldsMap; const PKFieldName: string;
|
2018-11-02 21:43:09 +01:00
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
Result := 'SELECT ' + TableFieldsDelimited(Map, PKFieldName, ',') + ' FROM ' + GetTableNameForSQL(TableName);
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
2019-02-21 18:11:14 +01:00
|
|
|
function TMVCSQLGeneratorFirebird.CreateSQLWhereByRQL(
|
2020-06-19 19:31:34 +02:00
|
|
|
const RQL: string; const Mapping: TMVCFieldsMapping;
|
2020-09-30 11:16:10 +02:00
|
|
|
const UseArtificialLimit: Boolean = True;
|
|
|
|
const UseFilterOnly: Boolean = False;
|
2021-11-18 00:49:12 +01:00
|
|
|
const MaxRecordCount: Int32 = TMVCConstants.MAX_RECORD_COUNT): string;
|
2018-11-02 21:43:09 +01:00
|
|
|
var
|
|
|
|
lFirebirdCompiler: TRQLFirebirdCompiler;
|
|
|
|
begin
|
|
|
|
lFirebirdCompiler := TRQLFirebirdCompiler.Create(Mapping);
|
|
|
|
try
|
2021-11-18 00:49:12 +01:00
|
|
|
GetRQLParser.Execute(RQL, Result, lFirebirdCompiler,
|
|
|
|
UseArtificialLimit, UseFilterOnly, MaxRecordCount);
|
2018-11-02 21:43:09 +01:00
|
|
|
finally
|
|
|
|
lFirebirdCompiler.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-03-25 11:35:25 +01:00
|
|
|
function TMVCSQLGeneratorFirebird.CreateUpdateSQL(const TableName: string; const Map: TFieldsMap;
|
2018-11-02 21:43:09 +01:00
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
|
|
var
|
2020-03-25 11:35:25 +01:00
|
|
|
lKeyValue: TPair<TRttiField, TFieldInfo>;
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
Result := 'UPDATE ' + GetTableNameForSQL(TableName) + ' SET ';
|
2020-03-25 11:35:25 +01:00
|
|
|
for lKeyValue in Map do
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
2020-03-25 11:35:25 +01:00
|
|
|
if lKeyValue.Value.Writeable then
|
2019-11-05 16:57:22 +01:00
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
Result := Result + GetFieldNameForSQL(lKeyValue.Value.FieldName) + ' = :' + GetParamNameForSQL(lKeyValue.Value.FieldName) + ',';
|
2019-11-05 16:57:22 +01:00
|
|
|
end;
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
Result[Length(Result)] := ' ';
|
|
|
|
if not PKFieldName.IsEmpty then
|
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
Result := Result + ' where ' + GetFieldNameForSQL(PKFieldName) + '= :' + GetParamNameForSQL(PKFieldName);
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorFirebird.GetCompilerClass: TRQLCompilerClass;
|
|
|
|
begin
|
|
|
|
Result := TRQLFirebirdCompiler;
|
|
|
|
end;
|
|
|
|
|
2020-01-08 15:30:10 +01:00
|
|
|
function TMVCSQLGeneratorFirebird.GetSequenceValueSQL(
|
2020-03-25 11:35:25 +01:00
|
|
|
const PKFieldName: string; const SequenceName: string; const Step: Integer): string;
|
2020-01-08 15:30:10 +01:00
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
Result := Format('select gen_id(%s,%d) %s from rdb$database', [GetFieldNameForSQL(SequenceName), Step, GetFieldNameForSQL(PKFieldName)]);
|
2020-01-08 15:30:10 +01:00
|
|
|
end;
|
|
|
|
|
2019-02-21 18:11:14 +01:00
|
|
|
function TMVCSQLGeneratorFirebird.CreateDeleteAllSQL(
|
|
|
|
const TableName: string): string;
|
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
Result := 'DELETE FROM ' + GetTableNameForSQL(TableName);
|
2019-02-21 18:11:14 +01:00
|
|
|
end;
|
|
|
|
|
2020-03-25 11:35:25 +01:00
|
|
|
function TMVCSQLGeneratorFirebird.CreateDeleteSQL(const TableName: string; const Map: TFieldsMap;
|
2020-03-31 16:23:22 +02:00
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
2021-04-05 19:35:46 +02:00
|
|
|
Result := CreateDeleteAllSQL(TableName) + ' WHERE ' + GetFieldNameForSQL(PKFieldName) + '=:' + GetParamNameForSQL(PKFieldName);
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.RegisterSQLGenerator('firebird', TMVCSQLGeneratorFirebird);
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.RegisterSQLGenerator('interbase', TMVCSQLGeneratorFirebird);
|
|
|
|
|
|
|
|
finalization
|
|
|
|
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.UnRegisterSQLGenerator('firebird');
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.UnRegisterSQLGenerator('interbase');
|
|
|
|
|
|
|
|
end.
|