2018-11-02 21:43:09 +01:00
|
|
|
// *************************************************************************** }
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2023-01-17 08:52:26 +01:00
|
|
|
// Copyright (c) 2010-2023 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.MySQL;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
FireDAC.Phys.MySQLDef,
|
|
|
|
FireDAC.Phys.MySQL,
|
|
|
|
System.Rtti,
|
|
|
|
System.Generics.Collections,
|
|
|
|
MVCFramework.RQL.Parser,
|
|
|
|
MVCFramework.ActiveRecord,
|
2020-03-25 11:35:25 +01:00
|
|
|
MVCFramework.Commons;
|
2018-11-02 21:43:09 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
TMVCSQLGeneratorMySQL = class(TMVCSQLGenerator)
|
|
|
|
protected
|
|
|
|
function GetCompilerClass: TRQLCompilerClass; override;
|
|
|
|
public
|
|
|
|
function CreateInsertSQL(
|
|
|
|
const TableName: string;
|
2020-03-25 11:35:25 +01:00
|
|
|
const Map: TFieldsMap;
|
2018-11-02 21:43:09 +01:00
|
|
|
const PKFieldName: string;
|
|
|
|
const PKOptions: TMVCActiveRecordFieldOptions): string; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
MVCFramework.RQL.AST2MySQL;
|
|
|
|
|
2020-03-25 11:35:25 +01:00
|
|
|
function TMVCSQLGeneratorMySQL.CreateInsertSQL(const TableName: string; const Map: TFieldsMap;
|
2018-11-02 21:43:09 +01:00
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string;
|
|
|
|
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
|
2020-06-19 19:31:34 +02:00
|
|
|
lPKInInsert := (not PKFieldName.IsEmpty) and (not(TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions));
|
2020-08-11 00:54:42 +02:00
|
|
|
lPKInInsert := lPKInInsert and (not(TMVCActiveRecordFieldOption.foReadOnly in PKOptions));
|
2018-11-02 21:43:09 +01: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;
|
2021-11-21 19:27:06 +01:00
|
|
|
|
|
|
|
{partition}
|
|
|
|
for lFieldName in fPartitionInfo.FieldNames do
|
|
|
|
begin
|
|
|
|
lSB.Append(GetFieldNameForSQL(lFieldName) + ',');
|
|
|
|
end;
|
|
|
|
{end-partition}
|
|
|
|
|
2018-11-02 21:43:09 +01: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;
|
|
|
|
|
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
|
|
|
|
lSB.Append(':' + PKFieldName + ',');
|
|
|
|
end;
|
2021-11-21 19:27:06 +01:00
|
|
|
|
|
|
|
{partition}
|
|
|
|
for lFieldName in fPartitionInfo.FieldNames do
|
|
|
|
begin
|
|
|
|
lSB.Append(':' + GetParamNameForSQL(lFieldName) + ',');
|
|
|
|
end;
|
|
|
|
{end-partition}
|
|
|
|
|
2018-11-02 21:43:09 +01: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;
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
lSB.Remove(lSB.Length - 1, 1);
|
|
|
|
lSB.Append(')');
|
|
|
|
|
2018-12-09 23:03:06 +01:00
|
|
|
if TMVCActiveRecordFieldOption.foAutoGenerated in PKOptions then
|
2018-11-02 21:43:09 +01:00
|
|
|
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.
|