2018-11-02 21:43:09 +01:00
|
|
|
// *************************************************************************** }
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2024-01-02 17:04:27 +01:00
|
|
|
// Copyright (c) 2010-2024 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
|
|
|
|
function CreateInsertSQL(
|
2023-11-02 17:36:19 +01:00
|
|
|
const TableMap: TMVCTableMap;
|
|
|
|
const ARInstance: TMVCActiveRecord): string; override;
|
2021-11-21 19:27:06 +01:00
|
|
|
function GetSequenceValueSQL(const PKFieldName: string;
|
|
|
|
const SequenceName: string;
|
|
|
|
const Step: Integer = 1): string; override;
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
MVCFramework.RQL.AST2FirebirdSQL;
|
|
|
|
|
2023-11-02 17:36:19 +01:00
|
|
|
function TMVCSQLGeneratorFirebird.CreateInsertSQL(const TableMap: TMVCTableMap;
|
|
|
|
const ARInstance: TMVCActiveRecord): string;
|
2018-11-02 21:43:09 +01:00
|
|
|
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;
|
2021-11-21 19:27:06 +01:00
|
|
|
lFieldName: String;
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
2023-11-02 17:36:19 +01:00
|
|
|
lPKInInsert := (not TableMap.fPrimaryKeyFieldName.IsEmpty) and (not(TMVCActiveRecordFieldOption.foAutoGenerated in TableMap.fPrimaryKeyOptions));
|
|
|
|
lPKInInsert := lPKInInsert and (not(TMVCActiveRecordFieldOption.foReadOnly in TableMap.fPrimaryKeyOptions));
|
2018-11-02 21:43:09 +01:00
|
|
|
lSB := TStringBuilder.Create;
|
|
|
|
try
|
2023-11-02 17:36:19 +01:00
|
|
|
lSB.Append('INSERT INTO ' + GetTableNameForSQL(TableMap.fTableName) + ' (');
|
2020-06-19 19:31:34 +02:00
|
|
|
if lPKInInsert then
|
2020-03-27 00:37:28 +01:00
|
|
|
begin
|
2023-11-02 17:36:19 +01:00
|
|
|
lSB.Append(GetFieldNameForSQL(TableMap.fPrimaryKeyFieldName) + ',');
|
2020-03-27 00:37:28 +01:00
|
|
|
end;
|
2021-11-21 19:27:06 +01:00
|
|
|
|
|
|
|
{partition}
|
|
|
|
for lFieldName in fPartitionInfo.FieldNames do
|
|
|
|
begin
|
|
|
|
lSB.Append(GetFieldNameForSQL(lFieldName) + ',');
|
|
|
|
end;
|
|
|
|
{end-partition}
|
|
|
|
|
2023-11-02 17:36:19 +01:00
|
|
|
for lKeyValue in TableMap.fMap do
|
2020-03-25 11:35:25 +01:00
|
|
|
begin
|
2021-11-21 19:27:06 +01:00
|
|
|
// if not(foTransient in lKeyValue.Value.FieldOptions) then
|
2024-03-13 16:45:09 +01:00
|
|
|
if lKeyValue.Value.Insertable then
|
2020-03-25 11:35:25 +01:00
|
|
|
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
|
2023-11-02 17:36:19 +01:00
|
|
|
lSB.Append(':' + GetParamNameForSQL(TableMap.fPrimaryKeyFieldName) + ',');
|
2020-03-27 00:37:28 +01:00
|
|
|
end;
|
2021-11-21 19:27:06 +01:00
|
|
|
|
|
|
|
{partition}
|
|
|
|
for lFieldName in fPartitionInfo.FieldNames do
|
|
|
|
begin
|
|
|
|
lSB.Append(':' + GetParamNameForSQL(lFieldName) + ',');
|
|
|
|
end;
|
|
|
|
{end-partition}
|
|
|
|
|
2023-11-02 17:36:19 +01:00
|
|
|
for lKeyValue in TableMap.fMap do
|
2020-03-25 11:35:25 +01:00
|
|
|
begin
|
2023-11-02 17:36:19 +01:00
|
|
|
if lKeyValue.Value.IsVersion then
|
|
|
|
begin
|
|
|
|
lSB.Append(OBJECT_VERSION_STARTING_VALUE + ',');
|
2024-03-13 16:45:09 +01:00
|
|
|
end else if lKeyValue.Value.Insertable then
|
2020-03-25 11:35:25 +01:00
|
|
|
begin
|
2022-01-26 23:00:32 +01: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(')');
|
|
|
|
|
2023-11-02 17:36:19 +01:00
|
|
|
if TMVCActiveRecordFieldOption.foAutoGenerated in TableMap.fPrimaryKeyOptions then
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
2023-11-02 17:36:19 +01:00
|
|
|
lSB.Append(' RETURNING ' + GetFieldNameForSQL(TableMap.fPrimaryKeyFieldName));
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
Result := lSB.ToString;
|
|
|
|
finally
|
|
|
|
lSB.Free;
|
|
|
|
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;
|
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
initialization
|
|
|
|
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.RegisterSQLGenerator('firebird', TMVCSQLGeneratorFirebird);
|
|
|
|
|
|
|
|
finalization
|
|
|
|
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.UnRegisterSQLGenerator('firebird');
|
|
|
|
|
|
|
|
end.
|