2019-01-13 18:56:33 +01:00
|
|
|
// *************************************************************************** }
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2020-01-06 16:49:18 +01:00
|
|
|
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
|
2019-01-13 18:56:33 +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.PostgreSQL;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.Rtti,
|
|
|
|
System.Generics.Collections,
|
|
|
|
FireDAC.Phys.FB,
|
|
|
|
FireDAC.Phys.FBDef,
|
|
|
|
MVCFramework.ActiveRecord,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.RQL.Parser;
|
|
|
|
|
|
|
|
type
|
|
|
|
TMVCSQLGeneratorPostgreSQL = class(TMVCSQLGenerator)
|
|
|
|
protected
|
|
|
|
function GetCompilerClass: TRQLCompilerClass; override;
|
|
|
|
public
|
|
|
|
function CreateSelectSQL(
|
|
|
|
const TableName: string;
|
|
|
|
const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
|
|
|
function CreateInsertSQL(
|
|
|
|
const TableName: string;
|
|
|
|
const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
|
|
|
function CreateUpdateSQL(
|
|
|
|
const TableName: string;
|
|
|
|
const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
|
|
|
function CreateDeleteSQL(
|
|
|
|
const TableName: string;
|
|
|
|
const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions;
|
|
|
|
const PrimaryKeyValue: Int64): string; override;
|
2019-02-21 18:11:14 +01:00
|
|
|
function CreateDeleteAllSQL(
|
|
|
|
const TableName: string): string; override;
|
2019-01-13 18:56:33 +01:00
|
|
|
function CreateSelectByPKSQL(
|
|
|
|
const TableName: string;
|
|
|
|
const Map: TDictionary<TRttiField, string>; const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions;
|
|
|
|
const PrimaryKeyValue: Int64): string; override;
|
2019-02-21 18:11:14 +01:00
|
|
|
function CreateSQLWhereByRQL(
|
2019-01-13 18:56:33 +01:00
|
|
|
const RQL: string;
|
2019-02-21 18:11:14 +01:00
|
|
|
const Mapping: TMVCFieldsMapping;
|
|
|
|
const UseArtificialLimit: Boolean = True): string; override;
|
2019-01-13 19:18:57 +01:00
|
|
|
function CreateSelectCount(
|
|
|
|
const TableName: String): String; override;
|
2020-01-08 15:30:10 +01:00
|
|
|
function GetSequenceValueSQL(const PKFieldName: string;
|
|
|
|
const SequenceName: string;
|
|
|
|
const Step: Integer = 1): string; override;
|
2019-01-13 18:56:33 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{
|
|
|
|
All identifiers (including column names) that are not double-quoted are folded to
|
|
|
|
lower case in PostgreSQL. Column names that were created with double-quotes and thereby
|
|
|
|
retained upper-case letters (and/or other syntax violations) have to be double-quoted
|
|
|
|
for the rest of their life.
|
|
|
|
}
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
MVCFramework.RQL.AST2PostgreSQL;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorPostgreSQL.CreateInsertSQL(const TableName: string; const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
|
|
var
|
|
|
|
lKeyValue: TPair<TRttiField, string>;
|
|
|
|
lSB: TStringBuilder;
|
|
|
|
begin
|
|
|
|
lSB := TStringBuilder.Create;
|
|
|
|
try
|
2019-02-21 20:17:11 +01:00
|
|
|
lSB.Append('INSERT INTO ' + TableName + ' (');
|
2019-01-13 18:56:33 +01:00
|
|
|
for lKeyValue in Map do
|
|
|
|
lSB.Append(lKeyValue.value + ',');
|
|
|
|
lSB.Remove(lSB.Length - 1, 1);
|
|
|
|
lSB.Append(') values (');
|
|
|
|
for lKeyValue in Map do
|
|
|
|
begin
|
|
|
|
lSB.Append(':' + lKeyValue.value + ',');
|
|
|
|
end;
|
|
|
|
lSB.Remove(lSB.Length - 1, 1);
|
|
|
|
lSB.Append(')');
|
|
|
|
|
|
|
|
if TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions then
|
|
|
|
begin
|
|
|
|
lSB.Append(' RETURNING ' + PKFieldName);
|
|
|
|
// case GetBackEnd of
|
|
|
|
// cbPostgreSQL:
|
|
|
|
// begin
|
|
|
|
// lSB.Append(' RETURNING ' + fPrimaryKeyFieldName);
|
|
|
|
// end;
|
|
|
|
// cbMySQL:
|
|
|
|
// begin
|
|
|
|
// lSB.Append(';SELECT LAST_INSERT_ID() as ' + fPrimaryKeyFieldName);
|
|
|
|
// end;
|
|
|
|
// else
|
|
|
|
// raise EMVCActiveRecord.Create('Unsupported backend engine');
|
|
|
|
// end;
|
|
|
|
end;
|
|
|
|
Result := lSB.ToString;
|
|
|
|
finally
|
|
|
|
lSB.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorPostgreSQL.CreateSelectByPKSQL(
|
|
|
|
const TableName: string;
|
|
|
|
const Map: TDictionary<TRttiField, string>; const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions;
|
|
|
|
const PrimaryKeyValue: Int64): string;
|
|
|
|
begin
|
|
|
|
Result := CreateSelectSQL(TableName, Map, PKFieldName, PKOptions) + ' WHERE ' +
|
|
|
|
PKFieldName + '= :' + PKFieldName; // IntToStr(PrimaryKeyValue);
|
|
|
|
end;
|
|
|
|
|
2019-01-13 19:18:57 +01:00
|
|
|
function TMVCSQLGeneratorPostgreSQL.CreateSelectCount(
|
|
|
|
const TableName: String): String;
|
|
|
|
begin
|
2019-02-21 20:17:11 +01:00
|
|
|
Result := 'SELECT count(*) FROM ' + TableName;
|
2019-01-13 19:18:57 +01:00
|
|
|
end;
|
|
|
|
|
2019-01-13 18:56:33 +01:00
|
|
|
function TMVCSQLGeneratorPostgreSQL.CreateSelectSQL(const TableName: string;
|
|
|
|
const Map: TDictionary<TRttiField, string>; const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
|
|
begin
|
2019-02-21 20:17:11 +01:00
|
|
|
Result := 'SELECT ' + TableFieldsDelimited(Map, PKFieldName, ',') + ' FROM ' + TableName;
|
2019-01-13 18:56:33 +01:00
|
|
|
end;
|
|
|
|
|
2019-02-21 18:11:14 +01:00
|
|
|
function TMVCSQLGeneratorPostgreSQL.CreateSQLWhereByRQL(
|
2020-01-08 15:30:10 +01:00
|
|
|
const RQL: string;
|
|
|
|
const Mapping: TMVCFieldsMapping;
|
|
|
|
const UseArtificialLimit: Boolean): string;
|
2019-01-13 18:56:33 +01:00
|
|
|
var
|
|
|
|
lPostgreSQLCompiler: TRQLPostgreSQLCompiler;
|
|
|
|
begin
|
|
|
|
lPostgreSQLCompiler := TRQLPostgreSQLCompiler.Create(Mapping);
|
|
|
|
try
|
2019-02-21 18:11:14 +01:00
|
|
|
GetRQLParser.Execute(RQL, Result, lPostgreSQLCompiler, UseArtificialLimit);
|
2019-01-13 18:56:33 +01:00
|
|
|
finally
|
|
|
|
lPostgreSQLCompiler.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorPostgreSQL.CreateUpdateSQL(const TableName: string; const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
|
|
var
|
|
|
|
keyvalue: TPair<TRttiField, string>;
|
|
|
|
begin
|
2019-02-21 20:17:11 +01:00
|
|
|
Result := 'UPDATE ' + TableName + ' SET ';
|
2019-01-13 18:56:33 +01:00
|
|
|
for keyvalue in Map do
|
|
|
|
begin
|
|
|
|
Result := Result + keyvalue.value + ' = :' + keyvalue.value + ',';
|
|
|
|
end;
|
|
|
|
Result[Length(Result)] := ' ';
|
|
|
|
if not PKFieldName.IsEmpty then
|
|
|
|
begin
|
|
|
|
Result := Result + ' where ' + PKFieldName + '= :' + PKFieldName;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorPostgreSQL.GetCompilerClass: TRQLCompilerClass;
|
|
|
|
begin
|
|
|
|
Result := TRQLPostgreSQLCompiler;
|
|
|
|
end;
|
|
|
|
|
2020-01-08 15:30:10 +01:00
|
|
|
function TMVCSQLGeneratorPostgreSQL.GetSequenceValueSQL(const PKFieldName,
|
|
|
|
SequenceName: string; const Step: Integer): string;
|
|
|
|
begin
|
|
|
|
Result := Format('SELECT nextval(''%s'') %s', [SequenceName, PKFieldName]);
|
|
|
|
end;
|
|
|
|
|
2019-02-21 18:11:14 +01:00
|
|
|
function TMVCSQLGeneratorPostgreSQL.CreateDeleteAllSQL(
|
|
|
|
const TableName: string): string;
|
|
|
|
begin
|
2019-02-21 20:17:11 +01:00
|
|
|
Result := 'DELETE FROM ' + TableName;
|
2019-02-21 18:11:14 +01:00
|
|
|
end;
|
|
|
|
|
2019-01-13 18:56:33 +01:00
|
|
|
function TMVCSQLGeneratorPostgreSQL.CreateDeleteSQL(const TableName: string; const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions; const PrimaryKeyValue: Int64): string;
|
|
|
|
begin
|
2019-02-21 18:11:14 +01:00
|
|
|
Result := CreateDeleteAllSQL(TableName) + ' WHERE ' + PKFieldName + '=' + IntToStr(PrimaryKeyValue);
|
2019-01-13 18:56:33 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.RegisterSQLGenerator('postgresql', TMVCSQLGeneratorPostgreSQL);
|
|
|
|
|
|
|
|
finalization
|
|
|
|
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.UnRegisterSQLGenerator('postgresql');
|
|
|
|
|
|
|
|
end.
|