mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-15 15:55:54 +01:00
02de5b11ea
- Added partitioning for `TMVCActiveRecord descendants` (more info ASAP) - After a big refactoring (*"I love to delete code" -- cit. Daniele Teti*), support a new SQLGenerator is just 2 (two) methods away!
137 lines
3.6 KiB
ObjectPascal
137 lines
3.6 KiB
ObjectPascal
// *************************************************************************** }
|
|
//
|
|
// Delphi MVC Framework
|
|
//
|
|
// Copyright (c) 2010-2021 Daniele Teti and the DMVCFramework Team
|
|
//
|
|
// 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.MySQL;
|
|
|
|
interface
|
|
|
|
uses
|
|
FireDAC.Phys.MySQLDef,
|
|
FireDAC.Phys.MySQL,
|
|
System.Rtti,
|
|
System.Generics.Collections,
|
|
MVCFramework.RQL.Parser,
|
|
MVCFramework.ActiveRecord,
|
|
MVCFramework.Commons;
|
|
|
|
type
|
|
TMVCSQLGeneratorMySQL = class(TMVCSQLGenerator)
|
|
protected
|
|
function GetCompilerClass: TRQLCompilerClass; override;
|
|
public
|
|
function CreateInsertSQL(
|
|
const TableName: string;
|
|
const Map: TFieldsMap;
|
|
const PKFieldName: string;
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.SysUtils,
|
|
MVCFramework.RQL.AST2MySQL;
|
|
|
|
function TMVCSQLGeneratorMySQL.CreateInsertSQL(const TableName: string; const Map: TFieldsMap;
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
var
|
|
lKeyValue: TPair<TRttiField, TFieldInfo>;
|
|
lSB: TStringBuilder;
|
|
lPKInInsert: Boolean;
|
|
lFieldName: String;
|
|
begin
|
|
lPKInInsert := (not PKFieldName.IsEmpty) and (not(TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions));
|
|
lPKInInsert := lPKInInsert and (not(TMVCActiveRecordFieldOption.foReadOnly in PKOptions));
|
|
lSB := TStringBuilder.Create;
|
|
try
|
|
lSB.Append('INSERT INTO ' + TableName + '(');
|
|
if lPKInInsert then
|
|
begin
|
|
lSB.Append(PKFieldName + ',');
|
|
end;
|
|
|
|
{partition}
|
|
for lFieldName in fPartitionInfo.FieldNames do
|
|
begin
|
|
lSB.Append(GetFieldNameForSQL(lFieldName) + ',');
|
|
end;
|
|
{end-partition}
|
|
|
|
for lKeyValue in Map do
|
|
begin
|
|
if lKeyValue.Value.Writeable then
|
|
begin
|
|
lSB.Append(lKeyValue.Value.FieldName + ',');
|
|
end;
|
|
end;
|
|
|
|
lSB.Remove(lSB.Length - 1, 1);
|
|
lSB.Append(') values (');
|
|
if lPKInInsert then
|
|
begin
|
|
lSB.Append(':' + PKFieldName + ',');
|
|
end;
|
|
|
|
{partition}
|
|
for lFieldName in fPartitionInfo.FieldNames do
|
|
begin
|
|
lSB.Append(':' + GetParamNameForSQL(lFieldName) + ',');
|
|
end;
|
|
{end-partition}
|
|
|
|
for lKeyValue in Map do
|
|
begin
|
|
if lKeyValue.Value.Writeable then
|
|
begin
|
|
lSB.Append(':' + lKeyValue.Value.FieldName + ',');
|
|
end;
|
|
end;
|
|
lSB.Remove(lSB.Length - 1, 1);
|
|
lSB.Append(')');
|
|
|
|
if TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions then
|
|
begin
|
|
lSB.Append(';SELECT LAST_INSERT_ID() as ' + PKFieldName);
|
|
end;
|
|
Result := lSB.ToString;
|
|
finally
|
|
lSB.Free;
|
|
end;
|
|
end;
|
|
|
|
function TMVCSQLGeneratorMySQL.GetCompilerClass: TRQLCompilerClass;
|
|
begin
|
|
Result := TRQLMySQLCompiler;
|
|
end;
|
|
|
|
initialization
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.RegisterSQLGenerator('mysql', TMVCSQLGeneratorMySQL);
|
|
|
|
finalization
|
|
|
|
TMVCSQLGeneratorRegistry.Instance.UnRegisterSQLGenerator('mysql');
|
|
|
|
end.
|