2019-06-24 20:59:33 +02:00
|
|
|
// *************************************************************************** }
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2020-01-06 16:49:18 +01:00
|
|
|
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
|
2019-06-24 20:59:33 +02: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.Sqlite;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.Rtti,
|
|
|
|
System.Generics.Collections,
|
|
|
|
MVCFramework.ActiveRecord,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.RQL.Parser;
|
|
|
|
|
|
|
|
type
|
|
|
|
TMVCSQLGeneratorSQLite = class(TMVCSQLGenerator)
|
|
|
|
protected
|
|
|
|
function GetCompilerClass: TRQLCompilerClass; override;
|
|
|
|
public
|
|
|
|
function CreateSelectSQL(
|
|
|
|
const TableName: string;
|
2020-03-25 11:35:25 +01:00
|
|
|
const Map: TFieldsMap;
|
2019-06-24 20:59:33 +02: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;
|
2019-06-24 20:59:33 +02: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;
|
2019-06-24 20:59:33 +02: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;
|
2019-06-24 20:59:33 +02:00
|
|
|
const PKFieldName: string;
|
2020-03-31 16:23:22 +02:00
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
2019-06-24 20:59:33 +02:00
|
|
|
function CreateDeleteAllSQL(
|
|
|
|
const TableName: string): string; override;
|
|
|
|
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-06-24 20:59:33 +02:00
|
|
|
function CreateSQLWhereByRQL(
|
2020-06-19 19:31:34 +02:00
|
|
|
const RQL: string; const Mapping: TMVCFieldsMapping;
|
|
|
|
const UseArtificialLimit: Boolean = True;
|
|
|
|
const UseFilterOnly: Boolean = False): string; override;
|
2019-06-24 20:59:33 +02:00
|
|
|
function CreateSelectCount(
|
2020-03-25 11:35:25 +01:00
|
|
|
const TableName: string): string; override;
|
2020-01-08 15:30:10 +01:00
|
|
|
function HasSequences: Boolean; override;
|
2019-06-24 20:59:33 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
MVCFramework.RQL.AST2SQLite;
|
|
|
|
|
2020-03-25 11:35:25 +01:00
|
|
|
function TMVCSQLGeneratorSQLite.CreateInsertSQL(const TableName: string; const Map: TFieldsMap;
|
2019-06-24 20:59:33 +02:00
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
|
|
var
|
2020-03-25 11:35:25 +01:00
|
|
|
lKeyValue: TPair<TRttiField, TFieldInfo>;
|
2019-06-24 20:59:33 +02:00
|
|
|
lSB: TStringBuilder;
|
2020-06-19 19:31:34 +02:00
|
|
|
lPKInInsert: Boolean;
|
2019-06-24 20:59:33 +02:00
|
|
|
begin
|
2020-06-19 19:31:34 +02:00
|
|
|
lPKInInsert := (not PKFieldName.IsEmpty) and (not(TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions));
|
2019-06-24 20:59:33 +02:00
|
|
|
lSB := TStringBuilder.Create;
|
|
|
|
try
|
|
|
|
lSB.Append('INSERT INTO ' + TableName + ' (');
|
2020-06-19 19:31:34 +02:00
|
|
|
if lPKInInsert then
|
2020-03-27 00:37:28 +01:00
|
|
|
begin
|
|
|
|
lSB.Append(PKFieldName + ',');
|
|
|
|
end;
|
2019-06-24 20:59:33 +02:00
|
|
|
for lKeyValue in Map do
|
2020-03-25 11:35:25 +01:00
|
|
|
begin
|
|
|
|
if lKeyValue.Value.Writeable then
|
|
|
|
begin
|
|
|
|
lSB.Append(lKeyValue.Value.FieldName + ',');
|
|
|
|
end;
|
|
|
|
end;
|
2019-06-24 20:59:33 +02: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
|
|
|
|
lSB.Append(':' + PKFieldName + ',');
|
|
|
|
end;
|
2019-06-24 20:59:33 +02:00
|
|
|
for lKeyValue in Map do
|
|
|
|
begin
|
2020-03-25 11:35:25 +01:00
|
|
|
if lKeyValue.Value.Writeable then
|
|
|
|
begin
|
|
|
|
lSB.Append(':' + lKeyValue.Value.FieldName + ',');
|
|
|
|
end;
|
2019-06-24 20:59:33 +02:00
|
|
|
end;
|
|
|
|
lSB.Remove(lSB.Length - 1, 1);
|
|
|
|
lSB.Append(')');
|
|
|
|
|
|
|
|
if TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions then
|
|
|
|
begin
|
|
|
|
lSB.Append(' ; SELECT last_insert_rowid() as ' + PKFieldName + ';');
|
|
|
|
end;
|
|
|
|
Result := lSB.ToString;
|
|
|
|
finally
|
|
|
|
lSB.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorSQLite.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;
|
2019-06-24 20:59:33 +02:00
|
|
|
begin
|
|
|
|
Result := CreateSelectSQL(TableName, Map, PKFieldName, PKOptions) + ' WHERE ' +
|
|
|
|
PKFieldName + '= :' + PKFieldName; // IntToStr(PrimaryKeyValue);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorSQLite.CreateSelectCount(
|
2020-03-25 11:35:25 +01:00
|
|
|
const TableName: string): string;
|
2019-06-24 20:59:33 +02:00
|
|
|
begin
|
|
|
|
Result := 'SELECT count(*) FROM ' + TableName;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorSQLite.CreateSelectSQL(const TableName: string;
|
2020-03-25 11:35:25 +01:00
|
|
|
const Map: TFieldsMap; const PKFieldName: string;
|
2019-06-24 20:59:33 +02:00
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
|
|
begin
|
|
|
|
Result := 'SELECT ' + TableFieldsDelimited(Map, PKFieldName, ',') + ' FROM ' + TableName;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorSQLite.CreateSQLWhereByRQL(
|
2020-06-19 19:31:34 +02:00
|
|
|
const RQL: string; const Mapping: TMVCFieldsMapping;
|
|
|
|
const UseArtificialLimit: Boolean;
|
|
|
|
const UseFilterOnly: Boolean): string;
|
2019-06-24 20:59:33 +02:00
|
|
|
var
|
|
|
|
lSQLiteCompiler: TRQLSQLiteCompiler;
|
|
|
|
begin
|
|
|
|
lSQLiteCompiler := TRQLSQLiteCompiler.Create(Mapping);
|
|
|
|
try
|
2020-06-19 19:31:34 +02:00
|
|
|
GetRQLParser.Execute(RQL, Result, lSQLiteCompiler, UseArtificialLimit, UseFilterOnly);
|
2019-06-24 20:59:33 +02:00
|
|
|
finally
|
|
|
|
lSQLiteCompiler.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-03-25 11:35:25 +01:00
|
|
|
function TMVCSQLGeneratorSQLite.CreateUpdateSQL(const TableName: string; const Map: TFieldsMap;
|
2019-06-24 20:59:33 +02:00
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
|
|
var
|
2020-03-25 11:35:25 +01:00
|
|
|
lKeyValue: TPair<TRttiField, TFieldInfo>;
|
2019-06-24 20:59:33 +02:00
|
|
|
begin
|
|
|
|
Result := 'UPDATE ' + TableName + ' SET ';
|
2020-03-25 11:35:25 +01:00
|
|
|
for lKeyValue in Map do
|
2019-06-24 20:59:33 +02:00
|
|
|
begin
|
2020-03-25 11:35:25 +01:00
|
|
|
if lKeyValue.Value.Writeable then
|
|
|
|
begin
|
|
|
|
Result := Result + lKeyValue.Value.FieldName + ' = :' + lKeyValue.Value.FieldName + ',';
|
|
|
|
end;
|
2019-06-24 20:59:33 +02:00
|
|
|
end;
|
|
|
|
Result[Length(Result)] := ' ';
|
|
|
|
if not PKFieldName.IsEmpty then
|
|
|
|
begin
|
|
|
|
Result := Result + ' where ' + PKFieldName + '= :' + PKFieldName;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGeneratorSQLite.GetCompilerClass: TRQLCompilerClass;
|
|
|
|
begin
|
|
|
|
Result := TRQLSQLiteCompiler;
|
|
|
|
end;
|
|
|
|
|
2020-01-08 15:30:10 +01:00
|
|
|
function TMVCSQLGeneratorSQLite.HasSequences: Boolean;
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
2019-06-24 20:59:33 +02:00
|
|
|
function TMVCSQLGeneratorSQLite.CreateDeleteAllSQL(
|
|
|
|
const TableName: string): string;
|
|
|
|
begin
|
|
|
|
Result := 'DELETE FROM ' + TableName;
|
|
|
|
end;
|
|
|
|
|
2020-03-25 11:35:25 +01:00
|
|
|
function TMVCSQLGeneratorSQLite.CreateDeleteSQL(const TableName: string; const Map: TFieldsMap;
|
2020-03-31 16:23:22 +02:00
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
|
2019-06-24 20:59:33 +02:00
|
|
|
begin
|
2020-03-31 16:23:22 +02:00
|
|
|
Result := CreateDeleteAllSQL(TableName) + ' WHERE ' + PKFieldName + '=:' + PKFieldName;
|
2019-06-24 20:59:33 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.RegisterSQLGenerator('sqlite', TMVCSQLGeneratorSQLite);
|
|
|
|
|
|
|
|
finalization
|
|
|
|
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.UnRegisterSQLGenerator('sqlite');
|
|
|
|
|
|
|
|
end.
|