delphimvcframework/sources/MVCFramework.SQLGenerators.PostgreSQL.pas

242 lines
7.9 KiB
ObjectPascal
Raw Normal View History

2019-01-13 18:56:33 +01:00
// *************************************************************************** }
//
// Delphi MVC Framework
//
// 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: TFieldsMap;
2019-01-13 18:56:33 +01:00
const PKFieldName: string;
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
function CreateInsertSQL(
const TableName: string;
const Map: TFieldsMap;
2019-01-13 18:56:33 +01:00
const PKFieldName: string;
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
function CreateUpdateSQL(
const TableName: string;
const Map: TFieldsMap;
2019-01-13 18:56:33 +01:00
const PKFieldName: string;
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
function CreateDeleteSQL(
const TableName: string;
const Map: TFieldsMap;
2019-01-13 18:56:33 +01:00
const PKFieldName: string;
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
function CreateDeleteAllSQL(
const TableName: string): string; override;
2019-01-13 18:56:33 +01:00
function CreateSelectByPKSQL(
const TableName: string;
const Map: TFieldsMap; const PKFieldName: string;
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
function CreateSQLWhereByRQL(
2019-01-13 18:56:33 +01:00
const RQL: string;
const Mapping: TMVCFieldsMapping;
const UseArtificialLimit: Boolean = True;
const UseFilterOnly: Boolean = False): string; override;
2019-01-13 19:18:57 +01:00
function CreateSelectCount(
const TableName: string): string; override;
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: TFieldsMap;
2019-01-13 18:56:33 +01:00
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
var
lKeyValue: TPair<TRttiField, TFieldInfo>;
2019-01-13 18:56:33 +01:00
lSB: TStringBuilder;
lPKInInsert: Boolean;
2019-01-13 18:56:33 +01:00
begin
lPKInInsert := (not PKFieldName.IsEmpty) and (not(TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions));
lPKInInsert := lPKInInsert and (not(TMVCActiveRecordFieldOption.foReadOnly in PKOptions));
2019-01-13 18:56:33 +01:00
lSB := TStringBuilder.Create;
try
lSB.Append('INSERT INTO ' + GetTableNameForSQL(TableName) + ' (');
if lPKInInsert then
begin
lSB.Append(GetFieldNameForSQL(PKFieldName) + ',');
end;
2019-01-13 18:56:33 +01:00
for lKeyValue in Map do
begin
// if not(foTransient in lKeyValue.Value.FieldOptions) then
if lKeyValue.Value.Writeable then
begin
lSB.Append(GetFieldNameForSQL(lKeyValue.Value.FieldName) + ',');
end;
end;
2019-01-13 18:56:33 +01:00
lSB.Remove(lSB.Length - 1, 1);
lSB.Append(') values (');
if lPKInInsert then
begin
lSB.Append(':' + GetParamNameForSQL(PKFieldName) + ',');
end;
2019-01-13 18:56:33 +01:00
for lKeyValue in Map do
begin
if lKeyValue.Value.Writeable then
begin
lSB.Append(':' + GetParamNameForSQL(lKeyValue.Value.FieldName) + ',');
end;
2019-01-13 18:56:33 +01:00
end;
lSB.Remove(lSB.Length - 1, 1);
lSB.Append(')');
if TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions then
begin
lSB.Append(' RETURNING ' + GetFieldNameForSQL(PKFieldName));
2019-01-13 18:56:33 +01:00
end;
Result := lSB.ToString;
finally
lSB.Free;
end;
end;
function TMVCSQLGeneratorPostgreSQL.CreateSelectByPKSQL(
const TableName: string;
const Map: TFieldsMap; const PKFieldName: string;
const PKOptions: TMVCActiveRecordFieldOptions): string;
2019-01-13 18:56:33 +01:00
begin
if PKFieldName.IsEmpty then
begin
raise EMVCActiveRecord.Create('No primary key provided. [HINT] Define a primary key field adding foPrimaryKey in field options.');
end;
2019-01-13 18:56:33 +01:00
Result := CreateSelectSQL(TableName, Map, PKFieldName, PKOptions) + ' WHERE ' +
GetFieldNameForSQL(PKFieldName) + '= :' + GetParamNameForSQL(PKFieldName); // IntToStr(PrimaryKeyValue);
2019-01-13 18:56:33 +01:00
end;
2019-01-13 19:18:57 +01:00
function TMVCSQLGeneratorPostgreSQL.CreateSelectCount(
const TableName: string): string;
2019-01-13 19:18:57 +01:00
begin
Result := 'SELECT count(*) FROM ' + GetTableNameForSQL(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: TFieldsMap; const PKFieldName: string;
2019-01-13 18:56:33 +01:00
const PKOptions: TMVCActiveRecordFieldOptions): string;
begin
Result := 'SELECT ' + TableFieldsDelimited(Map, PKFieldName, ',') + ' FROM ' + GetTableNameForSQL(TableName);
2019-01-13 18:56:33 +01:00
end;
function TMVCSQLGeneratorPostgreSQL.CreateSQLWhereByRQL(
const RQL: string;
const Mapping: TMVCFieldsMapping;
const UseArtificialLimit: Boolean;
const UseFilterOnly: Boolean): string;
2019-01-13 18:56:33 +01:00
var
lPostgreSQLCompiler: TRQLPostgreSQLCompiler;
begin
lPostgreSQLCompiler := TRQLPostgreSQLCompiler.Create(Mapping);
try
GetRQLParser.Execute(RQL, Result, lPostgreSQLCompiler, UseArtificialLimit, UseFilterOnly);
2019-01-13 18:56:33 +01:00
finally
lPostgreSQLCompiler.Free;
end;
end;
function TMVCSQLGeneratorPostgreSQL.CreateUpdateSQL(const TableName: string; const Map: TFieldsMap;
2019-01-13 18:56:33 +01:00
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
var
lPair: TPair<TRttiField, TFieldInfo>;
2019-01-13 18:56:33 +01:00
begin
Result := 'UPDATE ' + GetTableNameForSQL(TableName) + ' SET ';
for lPair in Map do
2019-01-13 18:56:33 +01:00
begin
if lPair.Value.Writeable then
begin
Result := Result + GetFieldNameForSQL(lPair.Value.FieldName) + ' = :' +
GetParamNameForSQL(lPair.Value.FieldName) + ',';
end;
2019-01-13 18:56:33 +01:00
end;
Result[Length(Result)] := ' ';
if not PKFieldName.IsEmpty then
begin
Result := Result + ' where ' + GetFieldNameForSQL(PKFieldName) + '= :' + GetParamNameForSQL(PKFieldName);
2019-01-13 18:56:33 +01:00
end;
end;
function TMVCSQLGeneratorPostgreSQL.GetCompilerClass: TRQLCompilerClass;
begin
Result := TRQLPostgreSQLCompiler;
end;
function TMVCSQLGeneratorPostgreSQL.GetSequenceValueSQL(const PKFieldName,
SequenceName: string; const Step: Integer): string;
begin
Result := Format('SELECT nextval(''%s'') %s', [SequenceName, GetFieldNameForSQL(PKFieldName)]);
end;
function TMVCSQLGeneratorPostgreSQL.CreateDeleteAllSQL(
const TableName: string): string;
begin
Result := 'DELETE FROM ' + GetTableNameForSQL(TableName);
end;
function TMVCSQLGeneratorPostgreSQL.CreateDeleteSQL(const TableName: string; const Map: TFieldsMap;
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
2019-01-13 18:56:33 +01:00
begin
Result := CreateDeleteAllSQL(TableName) + ' WHERE ' + GetFieldNameForSQL(PKFieldName) + '=:' +
GetParamNameForSQL(PKFieldName);
2019-01-13 18:56:33 +01:00
end;
initialization
TMVCSQLGeneratorRegistry.Instance.RegisterSQLGenerator('postgresql', TMVCSQLGeneratorPostgreSQL);
finalization
TMVCSQLGeneratorRegistry.Instance.UnRegisterSQLGenerator('postgresql');
end.