2018-09-25 15:36:53 +02:00
|
|
|
// *************************************************************************** }
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2020-01-04 12:53:53 +01:00
|
|
|
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
|
2018-09-25 15:36:53 +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.ActiveRecord;
|
|
|
|
|
2019-08-05 12:55:44 +02:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.Generics.Defaults,
|
|
|
|
System.Generics.Collections,
|
|
|
|
System.RTTI,
|
|
|
|
FireDAC.DApt,
|
|
|
|
Data.DB,
|
2018-10-14 18:23:20 +02:00
|
|
|
FireDAC.Comp.Client,
|
2019-09-30 00:05:46 +02:00
|
|
|
FireDAC.Stan.Def,
|
|
|
|
FireDAC.Stan.Pool,
|
|
|
|
FireDAC.Stan.Async,
|
2018-11-21 22:11:58 +01:00
|
|
|
FireDAC.Stan.Param,
|
2019-02-21 20:17:11 +01:00
|
|
|
System.SysUtils,
|
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.RQL.Parser,
|
2020-02-03 10:51:40 +01:00
|
|
|
MVCFramework.Serializer.Intf, MVCFramework.Serializer.Commons;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
EMVCActiveRecord = class(EMVCException)
|
|
|
|
public
|
|
|
|
constructor Create(const AMsg: string); reintroduce; { do not override!! }
|
|
|
|
end;
|
|
|
|
|
2019-01-18 18:11:27 +01:00
|
|
|
EMVCActiveRecordNotFound = class(EMVCActiveRecord)
|
2020-01-04 12:53:53 +01:00
|
|
|
public
|
|
|
|
procedure AfterConstruction; override;
|
2019-01-18 18:11:27 +01:00
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
TMVCActiveRecordClass = class of TMVCActiveRecord;
|
2019-03-05 20:55:37 +01:00
|
|
|
TMVCActiveRecordFieldOption = (foPrimaryKey, foAutoGenerated, foTransient);
|
2018-10-14 18:23:20 +02:00
|
|
|
TMVCActiveRecordFieldOptions = set of TMVCActiveRecordFieldOption;
|
|
|
|
TMVCEntityAction = (eaCreate, eaRetrieve, eaUpdate, eaDelete);
|
|
|
|
TMVCEntityActions = set of TMVCEntityAction;
|
2018-12-09 23:03:06 +01:00
|
|
|
TMVCActiveRecordLoadOption = (loIgnoreNotExistentFields);
|
|
|
|
TMVCActiveRecordLoadOptions = set of TMVCActiveRecordLoadOption;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
|
|
|
IMVCEntityProcessor = interface
|
|
|
|
['{E7CD11E6-9FF9-46D2-B7B0-DA5B38EAA14E}']
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure GetEntities(const Context: TWebContext; const Renderer: TMVCRenderer; const entityname: string;
|
2018-10-14 18:23:20 +02:00
|
|
|
var Handled: Boolean);
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure GetEntity(const Context: TWebContext; const Renderer: TMVCRenderer; const entityname: string;
|
|
|
|
const id: Integer; var Handled: Boolean);
|
|
|
|
procedure CreateEntity(const Context: TWebContext; const Renderer: TMVCRenderer; const entityname: string;
|
2018-10-14 18:23:20 +02:00
|
|
|
var Handled: Boolean);
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure UpdateEntity(const Context: TWebContext; const Renderer: TMVCRenderer; const entityname: string;
|
|
|
|
const id: Integer; var Handled: Boolean);
|
|
|
|
procedure DeleteEntity(const Context: TWebContext; const Renderer: TMVCRenderer; const entityname: string;
|
|
|
|
const id: Integer; var Handled: Boolean);
|
2018-10-14 18:23:20 +02:00
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
MVCActiveRecordCustomAttribute = class(TCustomAttribute)
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
MVCTableAttribute = class(MVCActiveRecordCustomAttribute)
|
2019-01-08 12:48:27 +01:00
|
|
|
public
|
2018-09-25 15:36:53 +02:00
|
|
|
Name: string;
|
2018-10-14 18:23:20 +02:00
|
|
|
constructor Create(aName: string);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
MVCTableFieldAttribute = class(MVCActiveRecordCustomAttribute)
|
2018-09-25 15:36:53 +02:00
|
|
|
public
|
|
|
|
FieldName: string;
|
2019-02-15 12:21:11 +01:00
|
|
|
FieldOptions: TMVCActiveRecordFieldOptions;
|
2020-01-08 15:30:10 +01:00
|
|
|
SequenceName: string;
|
|
|
|
constructor Create(const aFieldName: string; const aFieldOptions: TMVCActiveRecordFieldOptions;
|
|
|
|
const aSequenceName: String = ''); overload;
|
2019-02-15 12:21:11 +01:00
|
|
|
constructor Create(aFieldName: string); overload;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
MVCPrimaryKeyAttribute = MVCTableFieldAttribute deprecated '(ERROR) Use MVCTableFieldAttribute';
|
2018-09-25 15:36:53 +02:00
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
MVCEntityActionsAttribute = class(MVCActiveRecordCustomAttribute)
|
2018-10-14 18:23:20 +02:00
|
|
|
private
|
|
|
|
EntityAllowedActions: TMVCEntityActions;
|
|
|
|
public
|
|
|
|
constructor Create(const aEntityAllowedActions: TMVCEntityActions);
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
TMVCActiveRecord = class;
|
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
TMVCSQLGenerator = class;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
TMVCActiveRecordList = class(TObjectList<TMVCActiveRecord>)
|
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
TMVCActiveRecord = class
|
|
|
|
private
|
|
|
|
fConn: TFDConnection;
|
2018-11-02 21:43:09 +01:00
|
|
|
fSQLGenerator: TMVCSQLGenerator;
|
2018-09-25 15:36:53 +02:00
|
|
|
fPrimaryKeyFieldName: string;
|
2018-10-14 18:23:20 +02:00
|
|
|
fPrimaryKeyOptions: TMVCActiveRecordFieldOptions;
|
2020-01-08 15:30:10 +01:00
|
|
|
fPrimaryKeySequenceName: String;
|
2018-10-14 18:23:20 +02:00
|
|
|
fEntityAllowedActions: TMVCEntityActions;
|
2018-11-02 21:43:09 +01:00
|
|
|
fRQL2SQL: TRQL2SQL;
|
2020-01-06 16:49:18 +01:00
|
|
|
procedure MapTValueToParam(aValue: TValue; const aParam: TFDParam);
|
2020-01-04 12:53:53 +01:00
|
|
|
function MapNullableTValueToParam(aValue: TValue;
|
|
|
|
const aParam: TFDParam): Boolean;
|
2020-01-06 16:49:18 +01:00
|
|
|
function GetPrimaryKeyIsAutogenerated: Boolean;
|
|
|
|
procedure SetPrimaryKeyIsAutogenerated(const Value: Boolean);
|
2018-09-25 15:36:53 +02:00
|
|
|
protected
|
2020-02-03 10:51:40 +01:00
|
|
|
fRTTIType: TRttiInstanceType;
|
2018-09-25 15:36:53 +02:00
|
|
|
fProps: TArray<TRttiField>;
|
|
|
|
fObjAttributes: TArray<TCustomAttribute>;
|
|
|
|
fPropsAttributes: TArray<TCustomAttribute>;
|
|
|
|
fTableName: string;
|
|
|
|
fMap: TDictionary<TRttiField, string>;
|
2019-02-21 20:17:11 +01:00
|
|
|
fMapNonTransientFields: TDictionary<TRttiField, string>;
|
2018-09-25 15:36:53 +02:00
|
|
|
fPrimaryKey: TRttiField;
|
2018-11-02 21:43:09 +01:00
|
|
|
fBackendDriver: string;
|
|
|
|
fMapping: TMVCFieldsMapping;
|
|
|
|
function GetBackEnd: string;
|
2020-02-03 10:51:40 +01:00
|
|
|
function GetConnection: TFDConnection;
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure InitTableInfo;
|
2019-03-05 20:55:37 +01:00
|
|
|
class function ExecQuery(const SQL: string; const Values: array of Variant): TDataSet; overload;
|
|
|
|
class function ExecQuery(const SQL: string; const Values: array of Variant; const Connection: TFDConnection)
|
2018-11-24 16:56:21 +01:00
|
|
|
: TDataSet; overload;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
2020-01-08 15:30:10 +01:00
|
|
|
procedure FillPrimaryKey(const SequenceName: String);
|
2019-03-05 20:55:37 +01:00
|
|
|
function ExecNonQuery(const SQL: string; RefreshAutoGenerated: Boolean = false): int64; overload;
|
2018-09-25 15:36:53 +02:00
|
|
|
// load events
|
|
|
|
/// <summary>
|
|
|
|
/// Called everywhere before persist object into database
|
|
|
|
/// </summary>
|
|
|
|
procedure OnValidation; virtual;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called just after load the object state from database
|
|
|
|
/// </summary>
|
|
|
|
procedure OnAfterLoad; virtual;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called before load the object state from database
|
|
|
|
/// </summary>
|
|
|
|
procedure OnBeforeLoad; virtual;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called before insert the object state to database
|
|
|
|
/// </summary>
|
|
|
|
procedure OnBeforeInsert; virtual;
|
2018-10-23 16:18:34 +02:00
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
/// <summary>
|
2018-10-23 16:18:34 +02:00
|
|
|
/// Called after insert the object state to database
|
2018-09-25 15:36:53 +02:00
|
|
|
/// </summary>
|
2018-10-23 16:18:34 +02:00
|
|
|
procedure OnAfterInsert; virtual;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Called before update the object state to database
|
|
|
|
/// </summary>
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure OnBeforeUpdate; virtual;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Called after update the object state to database
|
|
|
|
/// </summary>
|
|
|
|
procedure OnAfterUpdate; virtual;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Called before delete object from database
|
|
|
|
/// </summary>
|
|
|
|
procedure OnBeforeDelete; virtual;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Called after delete object from database
|
|
|
|
/// </summary>
|
|
|
|
procedure OnAfterDelete; virtual;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Called before insert or update the object to the database
|
|
|
|
/// </summary>
|
|
|
|
procedure OnBeforeInsertOrUpdate; virtual;
|
|
|
|
|
2019-03-18 14:08:34 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Called before execute sql
|
|
|
|
/// </summary>
|
2019-05-09 20:53:52 +02:00
|
|
|
procedure OnBeforeExecuteSQL(var SQL: String); virtual;
|
2019-03-18 14:08:34 +01:00
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Called after insert or update the object to the database
|
|
|
|
/// </summary>
|
|
|
|
procedure OnAfterInsertOrUpdate; virtual;
|
|
|
|
|
2019-11-27 19:04:06 +01:00
|
|
|
procedure MapObjectToParams(const Params: TFDParams; var Handled: Boolean); virtual;
|
2020-01-04 12:53:53 +01:00
|
|
|
procedure MapDatasetToObject(const DataSet: TDataSet; const Options: TMVCActiveRecordLoadOptions;
|
|
|
|
var Handled: Boolean); virtual;
|
2019-11-27 19:04:06 +01:00
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
function GenerateSelectSQL: string;
|
2018-11-24 16:56:21 +01:00
|
|
|
|
|
|
|
function SQLGenerator: TMVCSQLGenerator;
|
2019-08-02 12:32:23 +02:00
|
|
|
|
|
|
|
function InternalCount(const RQL: String): int64;
|
|
|
|
function InternalSelectRQL(const RQL: string; const MaxRecordCount: Integer): TMVCActiveRecordList;
|
2018-09-25 15:36:53 +02:00
|
|
|
public
|
2018-11-24 16:56:21 +01:00
|
|
|
constructor Create(aLazyLoadConnection: Boolean); overload;
|
|
|
|
{ cannot be virtual! }
|
2018-09-28 13:01:46 +02:00
|
|
|
constructor Create; overload; virtual;
|
2018-09-25 15:36:53 +02:00
|
|
|
destructor Destroy; override;
|
2020-02-03 10:51:40 +01:00
|
|
|
procedure EnsureConnection;
|
2020-02-03 13:19:55 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Executes an Insert or an Update if primary key is defined or not
|
|
|
|
/// </summary>
|
|
|
|
procedure Store;
|
2019-03-05 20:55:37 +01:00
|
|
|
function CheckAction(const aEntityAction: TMVCEntityAction; const aRaiseException: Boolean = True): Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure Insert;
|
2018-09-28 13:01:46 +02:00
|
|
|
function GetMapping: TMVCFieldsMapping;
|
2018-10-23 16:18:34 +02:00
|
|
|
function LoadByPK(id: int64): Boolean; virtual;
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure Update;
|
|
|
|
procedure Delete;
|
|
|
|
function TableInfo: string;
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure LoadByDataset(const aDataSet: TDataSet; const aOptions: TMVCActiveRecordLoadOptions = []);
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure SetPK(const aValue: TValue);
|
|
|
|
function GetPK: TValue;
|
2020-02-03 13:19:55 +01:00
|
|
|
function TryGetPKValue(var Value: TValue; out IsNullableType: Boolean): Boolean;
|
2020-02-03 10:51:40 +01:00
|
|
|
[MVCDoNotSerialize]
|
2020-01-04 12:53:53 +01:00
|
|
|
property PrimaryKeyIsAutogenerated: Boolean read GetPrimaryKeyIsAutogenerated write SetPrimaryKeyIsAutogenerated;
|
|
|
|
class function GetByPK(const aClass: TMVCActiveRecordClass; const aValue: int64;
|
|
|
|
const RaiseExceptionIfNotFound: Boolean = True): TMVCActiveRecord; overload;
|
2019-03-05 20:55:37 +01:00
|
|
|
class function GetScalar(const SQL: string; const Params: array of Variant): Variant;
|
|
|
|
class function Select(const aClass: TMVCActiveRecordClass; const SQL: string; const Params: array of Variant)
|
2018-11-24 16:56:21 +01:00
|
|
|
: TMVCActiveRecordList; overload;
|
2019-03-05 20:55:37 +01:00
|
|
|
class function Select(const aClass: TMVCActiveRecordClass; const SQL: string; const Params: array of Variant;
|
2018-11-24 16:56:21 +01:00
|
|
|
const Connection: TFDConnection): TMVCActiveRecordList; overload;
|
2019-03-05 20:55:37 +01:00
|
|
|
class function SelectRQL(const aClass: TMVCActiveRecordClass; const RQL: string; const MaxRecordCount: Integer)
|
2018-11-24 16:56:21 +01:00
|
|
|
: TMVCActiveRecordList; overload;
|
2019-03-05 20:55:37 +01:00
|
|
|
class function DeleteRQL(const aClass: TMVCActiveRecordClass; const RQL: string): int64;
|
|
|
|
function SelectRQL(const RQL: string; const MaxRecordCount: Integer): TMVCActiveRecordList; overload;
|
|
|
|
class function Where(const aClass: TMVCActiveRecordClass; const SQLWhere: string; const Params: array of Variant)
|
2019-02-21 18:11:14 +01:00
|
|
|
: TMVCActiveRecordList; overload;
|
2019-03-05 20:55:37 +01:00
|
|
|
class function Where(const aClass: TMVCActiveRecordClass; const SQLWhere: string; const Params: array of Variant;
|
2018-11-02 21:43:09 +01:00
|
|
|
const Connection: TFDConnection): TMVCActiveRecordList; overload;
|
2019-03-05 20:55:37 +01:00
|
|
|
class function All(const aClass: TMVCActiveRecordClass): TObjectList<TMVCActiveRecord>; overload;
|
|
|
|
class function DeleteAll(const aClass: TMVCActiveRecordClass): int64; overload;
|
2019-05-09 20:53:52 +02:00
|
|
|
function Count(const RQL: String = ''): int64; overload;
|
|
|
|
class function Count(const aClass: TMVCActiveRecordClass; const RQL: String = ''): int64; overload;
|
2019-03-05 20:55:37 +01:00
|
|
|
class function SelectDataSet(const SQL: string; const Params: array of Variant): TDataSet;
|
2018-10-23 16:18:34 +02:00
|
|
|
class function CurrentConnection: TFDConnection;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2019-08-02 12:32:23 +02:00
|
|
|
TMVCActiveRecordHelper = class helper for TMVCActiveRecord
|
2020-01-04 12:53:53 +01:00
|
|
|
class function GetByPK<T: TMVCActiveRecord, constructor>(const aValue: int64;
|
|
|
|
const RaiseExceptionIfNotFound: Boolean = True): T; overload;
|
2019-08-02 12:32:23 +02:00
|
|
|
class function Select<T: TMVCActiveRecord, constructor>(const SQL: string; const Params: array of Variant;
|
|
|
|
const Options: TMVCActiveRecordLoadOptions = []): TObjectList<T>; overload;
|
|
|
|
class function SelectRQL<T: constructor, TMVCActiveRecord>(const RQL: string; const MaxRecordCount: Integer)
|
|
|
|
: TObjectList<T>; overload;
|
|
|
|
class function All<T: TMVCActiveRecord, constructor>: TObjectList<T>; overload;
|
|
|
|
class function Count<T: TMVCActiveRecord>(const RQL: String = ''): int64; overload;
|
|
|
|
class function Where<T: TMVCActiveRecord, constructor>(const SQLWhere: string; const Params: array of Variant)
|
|
|
|
: TObjectList<T>; overload;
|
|
|
|
class function GetOneByWhere<T: TMVCActiveRecord, constructor>(const SQLWhere: string;
|
|
|
|
const Params: array of Variant; const RaiseExceptionIfNotFound: Boolean = True): T;
|
|
|
|
class function GetFirstByWhere<T: TMVCActiveRecord, constructor>(const SQLWhere: string;
|
|
|
|
const Params: array of Variant; const RaiseExceptionIfNotFound: Boolean = True): T;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
IMVCEntitiesRegistry = interface
|
|
|
|
['{BB227BEB-A74A-4637-8897-B13BA938C07B}']
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure AddEntity(const aURLSegment: string; const aActiveRecordClass: TMVCActiveRecordClass);
|
|
|
|
procedure AddEntityProcessor(const aURLSegment: string; const aEntityProcessor: IMVCEntityProcessor);
|
2018-11-24 16:56:21 +01:00
|
|
|
function FindEntityClassByURLSegment(const aURLSegment: string;
|
|
|
|
out aMVCActiveRecordClass: TMVCActiveRecordClass): Boolean;
|
2019-03-05 20:55:37 +01:00
|
|
|
function FindProcessorByURLSegment(const aURLSegment: string; out aMVCEntityProcessor: IMVCEntityProcessor)
|
|
|
|
: Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
TMVCEntitiesRegistry = class(TInterfacedObject, IMVCEntitiesRegistry)
|
|
|
|
private
|
|
|
|
fEntitiesDict: TDictionary<string, TMVCActiveRecordClass>;
|
2018-10-14 18:23:20 +02:00
|
|
|
fProcessorsDict: TDictionary<string, IMVCEntityProcessor>;
|
2018-09-25 15:36:53 +02:00
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
destructor Destroy; override;
|
|
|
|
protected
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure AddEntityProcessor(const aURLSegment: string; const aEntityProcessor: IMVCEntityProcessor);
|
|
|
|
procedure AddEntity(const aURLSegment: string; const aActiveRecordClass: TMVCActiveRecordClass);
|
2018-11-24 16:56:21 +01:00
|
|
|
function FindEntityClassByURLSegment(const aURLSegment: string;
|
|
|
|
out aMVCActiveRecordClass: TMVCActiveRecordClass): Boolean;
|
2019-03-05 20:55:37 +01:00
|
|
|
function FindProcessorByURLSegment(const aURLSegment: string; out aMVCEntityProcessor: IMVCEntityProcessor)
|
|
|
|
: Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2018-09-28 13:01:46 +02:00
|
|
|
IMVCActiveRecordConnections = interface
|
2018-09-25 15:36:53 +02:00
|
|
|
['{7B87473C-1784-489F-A838-925E7DDD0DE2}']
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure AddConnection(const aName: string; const aConnection: TFDConnection; const Owns: Boolean = false);
|
2019-05-09 20:53:52 +02:00
|
|
|
procedure AddDefaultConnection(const aConnection: TFDConnection; const Owns: Boolean = false);
|
2018-10-14 18:23:20 +02:00
|
|
|
procedure RemoveConnection(const aName: string);
|
2019-05-09 20:53:52 +02:00
|
|
|
procedure RemoveDefaultConnection;
|
2018-10-14 18:23:20 +02:00
|
|
|
procedure SetCurrent(const aName: string);
|
2018-09-25 15:36:53 +02:00
|
|
|
function GetCurrent: TFDConnection;
|
2018-11-02 21:43:09 +01:00
|
|
|
function GetCurrentBackend: string;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
TMVCConnectionsRepository = class(TInterfacedObject, IMVCActiveRecordConnections)
|
2018-11-24 16:56:21 +01:00
|
|
|
private type
|
2018-11-09 18:11:59 +01:00
|
|
|
TConnHolder = class
|
2019-01-08 12:48:27 +01:00
|
|
|
public
|
2018-11-09 18:11:59 +01:00
|
|
|
Connection: TFDConnection;
|
|
|
|
OwnsConnection: Boolean;
|
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
2018-10-14 18:23:20 +02:00
|
|
|
fMREW: TMultiReadExclusiveWriteSynchronizer;
|
2018-11-09 18:11:59 +01:00
|
|
|
fConnectionsDict: TDictionary<string, TConnHolder>;
|
2018-10-14 18:23:20 +02:00
|
|
|
fCurrentConnectionsByThread: TDictionary<TThreadID, string>;
|
|
|
|
function GetKeyName(const aName: string): string;
|
2018-09-25 15:36:53 +02:00
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
destructor Destroy; override;
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure AddConnection(const aName: string; const aConnection: TFDConnection; const aOwns: Boolean = false);
|
2019-05-09 20:53:52 +02:00
|
|
|
procedure AddDefaultConnection(const aConnection: TFDConnection; const aOwns: Boolean = false);
|
2018-10-14 18:23:20 +02:00
|
|
|
procedure RemoveConnection(const aName: string);
|
2019-05-09 20:53:52 +02:00
|
|
|
procedure RemoveDefaultConnection;
|
2018-10-14 18:23:20 +02:00
|
|
|
procedure SetCurrent(const aName: string);
|
2018-09-25 15:36:53 +02:00
|
|
|
function GetCurrent: TFDConnection;
|
2018-10-14 18:23:20 +02:00
|
|
|
function GetByName(const aName: string): TFDConnection;
|
2018-11-02 21:43:09 +01:00
|
|
|
function GetCurrentBackend: string;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TMVCSQLGenerator = class abstract
|
|
|
|
private
|
|
|
|
fMapping: TMVCFieldsMapping;
|
|
|
|
fCompiler: TRQLCompiler;
|
|
|
|
fRQL2SQL: TRQL2SQL;
|
|
|
|
protected
|
|
|
|
function GetRQLParser: TRQL2SQL;
|
|
|
|
function GetCompiler: TRQLCompiler;
|
|
|
|
function GetCompilerClass: TRQLCompilerClass; virtual; abstract;
|
|
|
|
function GetMapping: TMVCFieldsMapping;
|
2019-03-05 20:55:37 +01:00
|
|
|
function TableFieldsDelimited(const Map: TDictionary<TRttiField, string>; const PKFieldName: string;
|
|
|
|
const Delimiter: string): string;
|
2018-11-02 21:43:09 +01:00
|
|
|
public
|
|
|
|
constructor Create(Mapping: TMVCFieldsMapping); virtual;
|
|
|
|
destructor Destroy; override;
|
2020-01-08 15:30:10 +01:00
|
|
|
// capabilities
|
|
|
|
function HasSequences: Boolean; virtual;
|
|
|
|
function HasReturning: Boolean; virtual;
|
|
|
|
// end-capabilities
|
2019-03-05 20:55:37 +01:00
|
|
|
function CreateSQLWhereByRQL(const RQL: string; const Mapping: TMVCFieldsMapping;
|
2019-02-21 20:17:11 +01:00
|
|
|
const UseArtificialLimit: Boolean = True): string; virtual; abstract;
|
2019-03-05 20:55:37 +01:00
|
|
|
function CreateSelectSQL(const TableName: string; const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string; virtual; abstract;
|
|
|
|
function CreateSelectByPKSQL(const TableName: string; const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions; const PrimaryKeyValue: int64): string;
|
2018-12-09 23:03:06 +01:00
|
|
|
virtual; abstract;
|
2019-03-05 20:55:37 +01:00
|
|
|
function CreateInsertSQL(const TableName: string; const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string; virtual; abstract;
|
|
|
|
function CreateUpdateSQL(const TableName: string; const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions): string; virtual; abstract;
|
|
|
|
function CreateDeleteSQL(const TableName: string; const Map: TDictionary<TRttiField, string>;
|
|
|
|
const PKFieldName: string; const PKOptions: TMVCActiveRecordFieldOptions; const PrimaryKeyValue: int64): string;
|
2018-12-09 23:03:06 +01:00
|
|
|
virtual; abstract;
|
2019-03-05 20:55:37 +01:00
|
|
|
function CreateDeleteAllSQL(const TableName: string): string; virtual; abstract;
|
|
|
|
function CreateSelectCount(const TableName: String): String; virtual; abstract;
|
2020-01-08 15:30:10 +01:00
|
|
|
function GetSequenceValueSQL(
|
|
|
|
const PKFieldName: String; const SequenceName: String; const Step: Integer = 1): String; virtual;
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
TMVCSQLGeneratorClass = class of TMVCSQLGenerator;
|
|
|
|
|
|
|
|
TMVCSQLGeneratorRegistry = class sealed
|
|
|
|
private
|
2018-11-09 18:11:59 +01:00
|
|
|
class var cInstance: TMVCSQLGeneratorRegistry;
|
2018-11-02 21:43:09 +01:00
|
|
|
|
|
|
|
class var
|
2018-11-09 18:11:59 +01:00
|
|
|
cLock: TObject;
|
2018-11-02 21:43:09 +01:00
|
|
|
fSQLGenerators: TDictionary<string, TMVCSQLGeneratorClass>;
|
|
|
|
protected
|
|
|
|
constructor Create;
|
|
|
|
public
|
|
|
|
destructor Destroy; override;
|
|
|
|
class function Instance: TMVCSQLGeneratorRegistry;
|
|
|
|
class destructor Destroy;
|
|
|
|
class constructor Create;
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure RegisterSQLGenerator(const aBackend: string; const aRQLBackendClass: TMVCSQLGeneratorClass);
|
2018-11-02 21:43:09 +01:00
|
|
|
procedure UnRegisterSQLGenerator(const aBackend: string);
|
|
|
|
function GetSQLGenerator(const aBackend: string): TMVCSQLGeneratorClass;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2018-09-28 13:01:46 +02:00
|
|
|
function ActiveRecordConnectionsRegistry: IMVCActiveRecordConnections;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
function ActiveRecordMappingRegistry: IMVCEntitiesRegistry;
|
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
function GetBackEndByConnection(aConnection: TFDConnection): string;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2018-09-28 18:33:19 +02:00
|
|
|
System.TypInfo,
|
|
|
|
System.IOUtils,
|
|
|
|
System.Classes,
|
2018-09-25 15:36:53 +02:00
|
|
|
MVCFramework.DataSet.Utils,
|
|
|
|
MVCFramework.Logger,
|
2020-01-04 12:53:53 +01:00
|
|
|
MVCFramework.Nullables,
|
2018-10-14 18:23:20 +02:00
|
|
|
FireDAC.Stan.Option,
|
2020-01-08 15:30:10 +01:00
|
|
|
Data.FmtBcd, System.Variants;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
var
|
2018-10-14 18:23:20 +02:00
|
|
|
gCtx: TRttiContext;
|
2018-09-25 15:36:53 +02:00
|
|
|
gEntitiesRegistry: IMVCEntitiesRegistry;
|
2018-10-14 18:23:20 +02:00
|
|
|
gConnections: IMVCActiveRecordConnections;
|
2018-09-25 15:36:53 +02:00
|
|
|
gLock: TObject;
|
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
function GetBackEndByConnection(aConnection: TFDConnection): string;
|
|
|
|
begin
|
2019-08-02 12:32:23 +02:00
|
|
|
case Ord(aConnection.RDBMSKind) of
|
2018-11-02 21:43:09 +01:00
|
|
|
0:
|
|
|
|
Exit('unknown');
|
|
|
|
1:
|
|
|
|
Exit('oracle');
|
|
|
|
2:
|
2018-11-09 18:11:59 +01:00
|
|
|
Exit('mssql');
|
2018-11-02 21:43:09 +01:00
|
|
|
3:
|
|
|
|
Exit('msaccess');
|
|
|
|
4:
|
|
|
|
Exit('mysql');
|
|
|
|
5:
|
|
|
|
Exit('db2');
|
|
|
|
6:
|
|
|
|
Exit('sqlanywhere');
|
|
|
|
7:
|
|
|
|
Exit('advantage');
|
|
|
|
8:
|
|
|
|
Exit('interbase');
|
|
|
|
9:
|
|
|
|
Exit('firebird');
|
|
|
|
10:
|
|
|
|
Exit('sqlite');
|
|
|
|
11:
|
|
|
|
Exit('postgresql');
|
|
|
|
12:
|
|
|
|
Exit('nexusdb');
|
|
|
|
13:
|
|
|
|
Exit('dataSnap');
|
|
|
|
14:
|
|
|
|
Exit('informix');
|
|
|
|
15:
|
|
|
|
Exit('teradata');
|
|
|
|
16:
|
|
|
|
Exit('mongodb');
|
|
|
|
17:
|
|
|
|
Exit('other');
|
|
|
|
else
|
|
|
|
raise EMVCActiveRecord.Create('Unknown RDBMS Kind');
|
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
end;
|
|
|
|
|
2018-09-28 13:01:46 +02:00
|
|
|
function ActiveRecordConnectionsRegistry: IMVCActiveRecordConnections;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
if gConnections = nil then // double check here
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
TMonitor.Enter(gLock);
|
|
|
|
try
|
|
|
|
if gConnections = nil then
|
|
|
|
begin
|
|
|
|
gConnections := TMVCConnectionsRepository.Create;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
TMonitor.Exit(gLock);
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
Result := gConnections;
|
|
|
|
end;
|
|
|
|
|
2020-01-08 15:30:10 +01:00
|
|
|
function IntToNullableInt(const Value: Integer): NullableInt32;
|
|
|
|
begin
|
|
|
|
Result.SetValue(Value);
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
{ TConnectionsRepository }
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure TMVCConnectionsRepository.AddConnection(const aName: string; const aConnection: TFDConnection;
|
|
|
|
const aOwns: Boolean = false);
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lName: string;
|
2018-10-14 18:23:20 +02:00
|
|
|
lConnKeyName: string;
|
2018-11-09 18:11:59 +01:00
|
|
|
lConnHolder: TConnHolder;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
lName := aName.ToLower;
|
|
|
|
lConnKeyName := GetKeyName(lName);
|
|
|
|
|
|
|
|
fMREW.BeginWrite;
|
|
|
|
try
|
2018-11-09 18:11:59 +01:00
|
|
|
lConnHolder := TConnHolder.Create;
|
|
|
|
lConnHolder.Connection := aConnection;
|
|
|
|
lConnHolder.OwnsConnection := aOwns;
|
2018-11-24 16:56:21 +01:00
|
|
|
fConnectionsDict.Add(lConnKeyName, lConnHolder);
|
|
|
|
// raise exception on duplicates
|
2019-03-05 20:55:37 +01:00
|
|
|
if (lName = 'default') and (not fCurrentConnectionsByThread.ContainsKey(TThread.CurrentThread.ThreadID)) then
|
2018-10-14 18:23:20 +02:00
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
fCurrentConnectionsByThread.AddOrSetValue(TThread.CurrentThread.ThreadID, lName);
|
2018-10-14 18:23:20 +02:00
|
|
|
end;
|
|
|
|
finally
|
|
|
|
fMREW.EndWrite;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-05-09 20:53:52 +02:00
|
|
|
procedure TMVCConnectionsRepository.AddDefaultConnection(
|
|
|
|
const aConnection: TFDConnection; const aOwns: Boolean);
|
|
|
|
begin
|
|
|
|
AddConnection('default', aConnection, aOwns);
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
constructor TMVCConnectionsRepository.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
2018-10-14 18:23:20 +02:00
|
|
|
fMREW := TMultiReadExclusiveWriteSynchronizer.Create;
|
2018-11-09 18:11:59 +01:00
|
|
|
fConnectionsDict := TDictionary<string, TConnHolder>.Create;
|
2018-10-14 18:23:20 +02:00
|
|
|
fCurrentConnectionsByThread := TDictionary<TThreadID, string>.Create;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMVCConnectionsRepository.Destroy;
|
|
|
|
begin
|
|
|
|
fConnectionsDict.Free;
|
2018-10-14 18:23:20 +02:00
|
|
|
fCurrentConnectionsByThread.Free;
|
|
|
|
fMREW.Free;
|
2018-09-25 15:36:53 +02:00
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
function TMVCConnectionsRepository.GetByName(const aName: string): TFDConnection;
|
2018-10-14 18:23:20 +02:00
|
|
|
var
|
|
|
|
lKeyName: string;
|
2018-11-09 18:11:59 +01:00
|
|
|
lConnHolder: TConnHolder;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2020-02-21 20:14:15 +01:00
|
|
|
{$IF not Defined(TokyoOrBetter)}
|
2019-08-02 12:32:23 +02:00
|
|
|
Result := nil;
|
2019-08-05 12:55:44 +02:00
|
|
|
{$ENDIF}
|
2018-10-14 18:23:20 +02:00
|
|
|
lKeyName := GetKeyName(aName.ToLower);
|
|
|
|
fMREW.BeginRead;
|
|
|
|
try
|
2018-11-09 18:11:59 +01:00
|
|
|
if not fConnectionsDict.TryGetValue(lKeyName, lConnHolder) then
|
2018-10-14 18:23:20 +02:00
|
|
|
raise Exception.CreateFmt('Unknown connection %s', [aName]);
|
2018-11-09 18:11:59 +01:00
|
|
|
Result := lConnHolder.Connection;
|
2018-10-14 18:23:20 +02:00
|
|
|
finally
|
|
|
|
fMREW.EndRead;
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCConnectionsRepository.GetCurrent: TFDConnection;
|
2018-10-14 18:23:20 +02:00
|
|
|
var
|
|
|
|
lName: string;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2020-02-21 20:14:15 +01:00
|
|
|
{$IF not Defined(TokyoOrBetter)}
|
2019-08-02 12:32:23 +02:00
|
|
|
Result := nil;
|
2019-08-05 12:55:44 +02:00
|
|
|
{$ENDIF}
|
2018-10-14 18:23:20 +02:00
|
|
|
fMREW.BeginRead;
|
|
|
|
try
|
2019-08-02 12:32:23 +02:00
|
|
|
if fCurrentConnectionsByThread.TryGetValue(TThread.CurrentThread.ThreadID, lName) then
|
2018-10-14 18:23:20 +02:00
|
|
|
begin
|
|
|
|
Result := GetByName(lName);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
raise EMVCActiveRecord.Create('No current connection for thread');
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
fMREW.EndRead;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
function TMVCConnectionsRepository.GetCurrentBackend: string;
|
|
|
|
begin
|
|
|
|
Result := GetBackEndByConnection(GetCurrent);
|
|
|
|
end;
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
function TMVCConnectionsRepository.GetKeyName(const aName: string): string;
|
|
|
|
begin
|
2019-08-02 12:32:23 +02:00
|
|
|
Result := Format('%10.10d::%s', [TThread.CurrentThread.ThreadID, aName]);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
procedure TMVCConnectionsRepository.RemoveConnection(const aName: string);
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lName: string;
|
2018-10-14 18:23:20 +02:00
|
|
|
lKeyName: string;
|
2018-11-09 18:11:59 +01:00
|
|
|
lConnHolder: TConnHolder;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
lName := aName.ToLower;
|
|
|
|
lKeyName := GetKeyName(lName);
|
|
|
|
|
|
|
|
fMREW.BeginWrite;
|
2018-09-25 15:36:53 +02:00
|
|
|
try
|
2018-11-09 18:11:59 +01:00
|
|
|
if not fConnectionsDict.TryGetValue(lKeyName, lConnHolder) then
|
2018-10-14 18:23:20 +02:00
|
|
|
raise Exception.CreateFmt('Unknown connection %s', [aName]);
|
|
|
|
fConnectionsDict.Remove(lKeyName);
|
|
|
|
try
|
2018-11-09 18:11:59 +01:00
|
|
|
FreeAndNil(lConnHolder);
|
2018-10-14 18:23:20 +02:00
|
|
|
except
|
|
|
|
on E: Exception do
|
|
|
|
begin
|
|
|
|
LogE('ActiveRecord: ' + E.ClassName + ' > ' + E.Message);
|
|
|
|
raise;
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
finally
|
|
|
|
fMREW.EndWrite;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-05-09 20:53:52 +02:00
|
|
|
procedure TMVCConnectionsRepository.RemoveDefaultConnection;
|
|
|
|
begin
|
|
|
|
RemoveConnection('default');
|
|
|
|
end;
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
procedure TMVCConnectionsRepository.SetCurrent(const aName: string);
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lName: string;
|
2018-10-14 18:23:20 +02:00
|
|
|
lKeyName: string;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
lName := aName.ToLower;
|
|
|
|
lKeyName := GetKeyName(lName);
|
|
|
|
|
|
|
|
fMREW.BeginWrite;
|
|
|
|
try
|
|
|
|
if not fConnectionsDict.ContainsKey(lKeyName) then
|
|
|
|
raise Exception.CreateFmt('Unknown connection %s', [aName]);
|
2019-08-02 12:32:23 +02:00
|
|
|
fCurrentConnectionsByThread.AddOrSetValue(TThread.CurrentThread.ThreadID, lName);
|
2018-10-14 18:23:20 +02:00
|
|
|
finally
|
|
|
|
fMREW.EndWrite;
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function ActiveRecordMappingRegistry: IMVCEntitiesRegistry;
|
|
|
|
begin
|
|
|
|
if gEntitiesRegistry = nil then
|
|
|
|
begin
|
|
|
|
TMonitor.Enter(gLock);
|
|
|
|
try
|
|
|
|
if gEntitiesRegistry = nil then
|
|
|
|
begin
|
|
|
|
gEntitiesRegistry := TMVCEntitiesRegistry.Create;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
TMonitor.Exit(gLock);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Result := gEntitiesRegistry;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TableFieldAttribute }
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
constructor MVCTableFieldAttribute.Create(aFieldName: string);
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-02-15 12:21:11 +01:00
|
|
|
Create(aFieldName, []);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ TableAttribute }
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
constructor MVCTableAttribute.Create(aName: string);
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2018-10-14 18:23:20 +02:00
|
|
|
name := aName;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ TActiveRecord }
|
|
|
|
|
|
|
|
destructor TMVCActiveRecord.Destroy;
|
|
|
|
begin
|
|
|
|
fMap.Free;
|
2019-02-21 20:17:11 +01:00
|
|
|
fMapNonTransientFields.Free;
|
2018-11-02 21:43:09 +01:00
|
|
|
fSQLGenerator.Free;
|
|
|
|
fRQL2SQL.Free;
|
|
|
|
fConn := nil; // do not free it!!
|
2018-09-25 15:36:53 +02:00
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2020-02-03 10:51:40 +01:00
|
|
|
procedure TMVCActiveRecord.EnsureConnection;
|
|
|
|
begin
|
|
|
|
GetConnection;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
function TMVCActiveRecord.ExecNonQuery(const SQL: string; RefreshAutoGenerated: Boolean = false): int64;
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lQry: TFDQuery;
|
|
|
|
lPar: TFDParam;
|
|
|
|
lPair: TPair<TRttiField, string>;
|
|
|
|
lValue: TValue;
|
2019-05-09 20:53:52 +02:00
|
|
|
lSQL: String;
|
2019-11-27 19:04:06 +01:00
|
|
|
lHandled: Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
lQry := TFDQuery.Create(nil);
|
|
|
|
try
|
2020-02-03 10:51:40 +01:00
|
|
|
lQry.Connection := GetConnection;
|
2019-03-18 14:08:34 +01:00
|
|
|
lSQL := SQL;
|
|
|
|
OnBeforeExecuteSQL(lSQL);
|
|
|
|
lQry.SQL.Text := lSQL;
|
2019-11-27 19:04:06 +01:00
|
|
|
|
2020-01-04 12:53:53 +01:00
|
|
|
lHandled := false;
|
|
|
|
// lQry.Prepare;
|
2019-11-27 19:04:06 +01:00
|
|
|
MapObjectToParams(lQry.Params, lHandled);
|
|
|
|
if not lHandled then
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-11-27 19:04:06 +01:00
|
|
|
for lPair in fMap do
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2020-01-04 12:53:53 +01:00
|
|
|
lPar := lQry.FindParam(lPair.Value);
|
2019-11-27 19:04:06 +01:00
|
|
|
if lPar <> nil then
|
|
|
|
begin
|
|
|
|
lValue := lPair.Key.GetValue(Self);
|
|
|
|
MapTValueToParam(lValue, lPar);
|
|
|
|
end
|
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
2019-11-27 19:04:06 +01:00
|
|
|
// check if it's the primary key
|
|
|
|
lPar := lQry.FindParam(fPrimaryKeyFieldName);
|
|
|
|
if lPar <> nil then
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-11-27 19:04:06 +01:00
|
|
|
if lPar.DataType = ftUnknown then
|
|
|
|
begin
|
|
|
|
{ TODO -oDanieleT -cGeneral : Let's find a smarter way to do this if the engine cannot recognize parameter's datatype }
|
|
|
|
lPar.DataType := ftLargeint;
|
|
|
|
end;
|
|
|
|
MapTValueToParam(fPrimaryKey.GetValue(Self), lPar);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-01-08 15:30:10 +01:00
|
|
|
if RefreshAutoGenerated and (TMVCActiveRecordFieldOption.foAutoGenerated in fPrimaryKeyOptions) and
|
|
|
|
fPrimaryKeySequenceName.IsEmpty then
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2020-01-04 12:53:53 +01:00
|
|
|
lValue := fPrimaryKey.GetValue(Self);
|
2020-01-08 15:30:10 +01:00
|
|
|
lQry.Open;
|
2020-02-03 10:51:40 +01:00
|
|
|
|
2020-01-08 15:30:10 +01:00
|
|
|
if (lValue.Kind = tkRecord) then
|
2020-01-04 12:53:53 +01:00
|
|
|
begin
|
2020-02-05 23:46:38 +01:00
|
|
|
MapDataSetFieldToNullableRTTIField(lValue, lQry.Fields[0], fPrimaryKey, Self);
|
2020-02-03 10:51:40 +01:00
|
|
|
// if SameText(lValue.TypeInfo.Name, 'Nullable<System.Integer>') then
|
|
|
|
// begin
|
|
|
|
// lInteger :=lQry.FieldByName(fPrimaryKeyFieldName).AsInteger;
|
|
|
|
// TValue.MakeWithoutCopy(@lInteger, TypeInfo(NullableInteger), lValue);
|
|
|
|
// //lValue := TValue.From<NullableInteger>(Nullable<Integer>(lQry.FieldByName(fPrimaryKeyFieldName).AsInteger))
|
|
|
|
// // if lValue.IsType<NullableInt32> then
|
|
|
|
// // lValue := TValue.From<NullableInt32>(NullableInt32(lQry.FieldByName(fPrimaryKeyFieldName).AsInteger))
|
|
|
|
// end
|
|
|
|
// else if SameText(lValue.TypeInfo.Name, 'Nullable<System.Int64>') then
|
|
|
|
// begin
|
|
|
|
/// / lLargeInt :=lQry.FieldByName(fPrimaryKeyFieldName).AsLargeInt;
|
|
|
|
/// / TValue.MakeWithoutCopy(@lLargeInt, TypeInfo(NullableInt64), lValue);
|
|
|
|
// lNullableInt64.Value := lQry.FieldByName(fPrimaryKeyFieldName).AsLargeInt;
|
|
|
|
/// / fPrimaryKey.SetValue(Self, TValue.From<Nullable<System.Int64>>(lNullableInt64));
|
|
|
|
// TValue.Make(@lNullableInt64, TypeInfo(NullableInt64), lOutValue);
|
|
|
|
// fPrimaryKey.SetValue(Self,3);
|
|
|
|
// //fPrimaryKey.SetValue(Self,lOutValue);
|
|
|
|
//
|
|
|
|
/// / TValue.MakeWithoutCopy(@lNullableInt64, TypeInfo(NullableInt64), lValue);
|
|
|
|
// //lValue := TValue.From<NullableInt64>(lQry.FieldByName(fPrimaryKeyFieldName).AsLargeInt)
|
|
|
|
// // else if lValue.IsType<NullableInt64> then
|
|
|
|
// // lValue := TValue.From<NullableInt64>(NullableInt64(lQry.FieldByName(fPrimaryKeyFieldName).AsLargeInt))
|
|
|
|
// end
|
|
|
|
// else if SameText(lValue.TypeInfo.Name, 'Nullable<System.UInt32>') then
|
|
|
|
// lValue := lQry.FieldByName(fPrimaryKeyFieldName).AsInteger
|
|
|
|
/// / else if lValue.IsType<NullableUInt32> then
|
|
|
|
/// / lValue := TValue.From<NullableUInt32>(NullableUInt32(lQry.FieldByName(fPrimaryKeyFieldName).AsInteger))
|
|
|
|
// else if lValue.IsType<NullableUInt64> then
|
|
|
|
// lValue := TValue.From<NullableUInt64>(NullableUInt64(lQry.FieldByName(fPrimaryKeyFieldName).AsLargeInt))
|
|
|
|
// else if lValue.IsType<NullableInt16> then
|
|
|
|
// lValue := TValue.From<NullableInt16>(NullableInt16(lQry.FieldByName(fPrimaryKeyFieldName).AsInteger))
|
|
|
|
// else if lValue.IsType<NullableUInt16> then
|
|
|
|
// lValue := TValue.From<NullableUInt16>(NullableUInt16(lQry.FieldByName(fPrimaryKeyFieldName).AsInteger))
|
|
|
|
// else
|
|
|
|
// raise EMVCActiveRecord.Create('Invalid type for primary key');
|
2020-01-04 12:53:53 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
lValue := lQry.FieldByName(fPrimaryKeyFieldName).AsInteger;
|
2020-02-03 10:51:40 +01:00
|
|
|
fPrimaryKey.SetValue(Self, lValue);
|
2020-01-04 12:53:53 +01:00
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2019-03-18 14:08:34 +01:00
|
|
|
lQry.ExecSQL(lSQL);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
Result := lQry.RowsAffected;
|
2018-10-14 18:23:20 +02:00
|
|
|
finally
|
2018-09-25 15:36:53 +02:00
|
|
|
lQry.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.ExecQuery(const SQL: string; const Values: array of Variant;
|
|
|
|
const Connection: TFDConnection): TDataSet;
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lQry: TFDQuery;
|
|
|
|
begin
|
|
|
|
lQry := TFDQuery.Create(nil);
|
|
|
|
try
|
2019-05-09 20:53:52 +02:00
|
|
|
lQry.FetchOptions.Unidirectional := false; // True;
|
2018-09-28 13:01:46 +02:00
|
|
|
if Connection = nil then
|
|
|
|
begin
|
|
|
|
lQry.Connection := ActiveRecordConnectionsRegistry.GetCurrent;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
lQry.Connection := Connection;
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
lQry.SQL.Text := SQL;
|
2018-10-14 18:23:20 +02:00
|
|
|
// lQry.Prepare;
|
2018-09-25 15:36:53 +02:00
|
|
|
lQry.Open(SQL, Values);
|
|
|
|
Result := lQry;
|
|
|
|
except
|
|
|
|
lQry.Free;
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-01-08 15:30:10 +01:00
|
|
|
procedure TMVCActiveRecord.FillPrimaryKey(const SequenceName: String);
|
|
|
|
var
|
|
|
|
lDS: TDataSet;
|
|
|
|
lSQL: string;
|
|
|
|
begin
|
|
|
|
if not SequenceName.IsEmpty then
|
|
|
|
begin
|
|
|
|
lSQL := SQLGenerator.GetSequenceValueSQL(fPrimaryKeyFieldName, SequenceName);
|
|
|
|
if lSQL.IsEmpty then
|
|
|
|
begin
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
lDS := ExecQuery(lSQL, []);
|
|
|
|
try
|
2020-02-05 23:46:38 +01:00
|
|
|
MapDataSetFieldToRTTIField(lDS.Fields[0], fPrimaryKey, Self);
|
2020-01-08 15:30:10 +01:00
|
|
|
finally
|
|
|
|
lDS.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.ExecQuery(const SQL: string; const Values: array of Variant): TDataSet;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-09-28 13:01:46 +02:00
|
|
|
Result := ExecQuery(SQL, Values, nil);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecord.InitTableInfo;
|
|
|
|
var
|
2018-10-14 18:23:20 +02:00
|
|
|
lAttribute: TCustomAttribute;
|
|
|
|
lRTTIField: TRttiField;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
fEntityAllowedActions := [TMVCEntityAction.eaCreate, TMVCEntityAction.eaRetrieve, TMVCEntityAction.eaUpdate,
|
2018-11-02 21:43:09 +01:00
|
|
|
TMVCEntityAction.eaDelete];
|
2018-09-25 15:36:53 +02:00
|
|
|
fTableName := '';
|
2020-02-03 10:51:40 +01:00
|
|
|
fRTTIType := gCtx.GetType(Self.ClassInfo) as TRttiInstanceType;
|
2018-09-25 15:36:53 +02:00
|
|
|
fObjAttributes := fRTTIType.GetAttributes;
|
2018-10-14 18:23:20 +02:00
|
|
|
for lAttribute in fObjAttributes do
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
if lAttribute is MVCTableAttribute then
|
2018-10-14 18:23:20 +02:00
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
fTableName := MVCTableAttribute(lAttribute).Name;
|
2018-10-14 18:23:20 +02:00
|
|
|
continue;
|
|
|
|
end;
|
2018-10-23 16:18:34 +02:00
|
|
|
if lAttribute is MVCEntityActionsAttribute then
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
fEntityAllowedActions := MVCEntityActionsAttribute(lAttribute).EntityAllowedActions;
|
2018-09-25 15:36:53 +02:00
|
|
|
Break;
|
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
if fTableName = '' then
|
|
|
|
raise Exception.Create('Cannot find TableNameAttribute');
|
|
|
|
|
|
|
|
fProps := fRTTIType.GetFields;
|
2018-10-14 18:23:20 +02:00
|
|
|
for lRTTIField in fProps do
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
fPropsAttributes := lRTTIField.GetAttributes;
|
2018-09-25 15:36:53 +02:00
|
|
|
if Length(fPropsAttributes) = 0 then
|
2018-10-14 18:23:20 +02:00
|
|
|
continue;
|
|
|
|
for lAttribute in fPropsAttributes do
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
if lAttribute is MVCTableFieldAttribute then
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
if foPrimaryKey in MVCTableFieldAttribute(lAttribute).FieldOptions then
|
|
|
|
begin
|
|
|
|
fPrimaryKey := lRTTIField;
|
|
|
|
fPrimaryKeyFieldName := MVCTableFieldAttribute(lAttribute).FieldName;
|
|
|
|
fPrimaryKeyOptions := MVCTableFieldAttribute(lAttribute).FieldOptions;
|
2020-01-08 15:30:10 +01:00
|
|
|
fPrimaryKeySequenceName := MVCTableFieldAttribute(lAttribute).SequenceName;
|
2019-03-05 20:55:37 +01:00
|
|
|
continue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
fMap.Add(lRTTIField, { fTableName + '.' + } MVCTableFieldAttribute(lAttribute).FieldName);
|
|
|
|
if not(foTransient in MVCTableFieldAttribute(lAttribute).FieldOptions) then
|
|
|
|
fMapNonTransientFields.Add(lRTTIField, MVCTableFieldAttribute(lAttribute).FieldName);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecord.Insert;
|
|
|
|
var
|
|
|
|
SQL: string;
|
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
CheckAction(TMVCEntityAction.eaCreate);
|
2018-09-25 15:36:53 +02:00
|
|
|
OnValidation;
|
|
|
|
OnBeforeInsert;
|
|
|
|
OnBeforeInsertOrUpdate;
|
2019-02-21 20:17:11 +01:00
|
|
|
if fMapNonTransientFields.Count = 0 then
|
|
|
|
begin
|
|
|
|
raise EMVCActiveRecord.CreateFmt
|
2019-03-05 20:55:37 +01:00
|
|
|
('Cannot insert an entity if all fields are transient. Class [%s] mapped on table [%s]', [ClassName, fTableName]);
|
2019-02-21 20:17:11 +01:00
|
|
|
end;
|
2020-01-08 15:30:10 +01:00
|
|
|
if (foAutoGenerated in fPrimaryKeyOptions) then
|
|
|
|
begin
|
|
|
|
if not SQLGenerator.HasReturning then
|
|
|
|
begin
|
|
|
|
if not SQLGenerator.HasSequences then
|
|
|
|
begin
|
|
|
|
raise EMVCActiveRecord.Create
|
|
|
|
('Cannot use AutoGenerated primary keys if the engine doesn''t support returning clause nor sequences');
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
if fPrimaryKeySequenceName.IsEmpty then
|
|
|
|
begin
|
|
|
|
raise EMVCActiveRecord.Create('SequenceName is empty for entity ' + ClassName + ' but ' + GetBackEnd +
|
|
|
|
' requires it');
|
|
|
|
end;
|
|
|
|
FillPrimaryKey(fPrimaryKeySequenceName);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
SQL := SQLGenerator.CreateInsertSQL(fTableName, fMapNonTransientFields, fPrimaryKeyFieldName, fPrimaryKeyOptions);
|
2018-09-25 15:36:53 +02:00
|
|
|
ExecNonQuery(SQL, True);
|
2018-10-23 16:18:34 +02:00
|
|
|
OnAfterInsert;
|
|
|
|
OnAfterInsertOrUpdate;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2019-08-02 12:32:23 +02:00
|
|
|
function TMVCActiveRecord.InternalCount(const RQL: String): int64;
|
|
|
|
var
|
|
|
|
lSQL: string;
|
|
|
|
begin
|
|
|
|
lSQL := Self.SQLGenerator.CreateSelectCount(fTableName);
|
|
|
|
if not RQL.IsEmpty then
|
|
|
|
begin
|
|
|
|
lSQL := lSQL + fSQLGenerator.CreateSQLWhereByRQL(RQL, GetMapping, false);
|
|
|
|
end;
|
|
|
|
Result := GetScalar(lSQL, []);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCActiveRecord.InternalSelectRQL(const RQL: string;
|
|
|
|
const MaxRecordCount: Integer): TMVCActiveRecordList;
|
|
|
|
var
|
|
|
|
lSQL: string;
|
|
|
|
begin
|
|
|
|
lSQL := SQLGenerator.CreateSQLWhereByRQL(RQL, GetMapping);
|
|
|
|
LogD(Format('RQL [%s] => SQL [%s]', [RQL, lSQL]));
|
|
|
|
Result := Where(TMVCActiveRecordClass(Self.ClassType), lSQL, []);
|
|
|
|
end;
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
constructor TMVCActiveRecord.Create(aLazyLoadConnection: Boolean);
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-09-28 13:01:46 +02:00
|
|
|
inherited Create;
|
|
|
|
fConn := nil;
|
2018-11-02 21:43:09 +01:00
|
|
|
SetLength(fMapping, 0);
|
|
|
|
{ TODO -oDanieleT -cGeneral : Consider lazyconnection }
|
2020-02-03 10:51:40 +01:00
|
|
|
if not aLazyLoadConnection then
|
|
|
|
begin
|
|
|
|
GetConnection;
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
fMap := TDictionary<TRttiField, string>.Create;
|
2019-02-21 20:17:11 +01:00
|
|
|
fMapNonTransientFields := TDictionary<TRttiField, string>.Create;
|
2018-09-25 15:36:53 +02:00
|
|
|
InitTableInfo;
|
|
|
|
end;
|
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
function TMVCActiveRecord.GenerateSelectSQL: string;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
Result := SQLGenerator.CreateSelectSQL(fTableName, fMap, fPrimaryKeyFieldName, fPrimaryKeyOptions);
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCActiveRecord.GetBackEnd: string;
|
|
|
|
begin
|
|
|
|
if fBackendDriver.IsEmpty then
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2020-02-03 10:51:40 +01:00
|
|
|
fBackendDriver := GetBackEndByConnection(GetConnection);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
2018-11-02 21:43:09 +01:00
|
|
|
Result := fBackendDriver;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2020-01-04 12:53:53 +01:00
|
|
|
class function TMVCActiveRecord.GetByPK(const aClass: TMVCActiveRecordClass; const aValue: int64;
|
|
|
|
const RaiseExceptionIfNotFound: Boolean): TMVCActiveRecord;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
Result := aClass.Create;
|
2019-01-18 18:11:27 +01:00
|
|
|
if not Result.LoadByPK(aValue) then
|
|
|
|
begin
|
|
|
|
Result.Free;
|
2020-01-04 12:53:53 +01:00
|
|
|
if RaiseExceptionIfNotFound then
|
|
|
|
begin
|
|
|
|
raise EMVCActiveRecordNotFound.Create('Data not found');
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
end;
|
2019-01-18 18:11:27 +01:00
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2020-01-04 12:53:53 +01:00
|
|
|
class function TMVCActiveRecordHelper.GetByPK<T>(const aValue: int64;
|
|
|
|
const RaiseExceptionIfNotFound: Boolean = True): T;
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lActiveRecord: TMVCActiveRecord;
|
|
|
|
begin
|
|
|
|
Result := T.Create;
|
|
|
|
lActiveRecord := TMVCActiveRecord(Result);
|
2019-01-18 18:11:27 +01:00
|
|
|
if not lActiveRecord.LoadByPK(aValue) then
|
|
|
|
begin
|
|
|
|
Result.Free;
|
2020-01-04 12:53:53 +01:00
|
|
|
if RaiseExceptionIfNotFound then
|
|
|
|
begin
|
|
|
|
raise EMVCActiveRecordNotFound.Create('Data not found');
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
end;
|
2019-01-18 18:11:27 +01:00
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2019-08-02 12:32:23 +02:00
|
|
|
class function TMVCActiveRecordHelper.GetFirstByWhere<T>(const SQLWhere: string; const Params: array of Variant;
|
2019-03-05 20:55:37 +01:00
|
|
|
const RaiseExceptionIfNotFound: Boolean): T;
|
2018-10-23 16:18:34 +02:00
|
|
|
var
|
|
|
|
lList: TObjectList<T>;
|
|
|
|
begin
|
|
|
|
lList := Where<T>(SQLWhere, Params);
|
|
|
|
try
|
|
|
|
if lList.Count = 0 then
|
|
|
|
begin
|
|
|
|
if RaiseExceptionIfNotFound then
|
|
|
|
raise EMVCActiveRecord.Create('Got 0 rows');
|
|
|
|
Exit(nil);
|
|
|
|
end;
|
2020-02-05 23:46:38 +01:00
|
|
|
Result := lList.Extract(lList.First);
|
2018-10-23 16:18:34 +02:00
|
|
|
finally
|
|
|
|
lList.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-09-28 13:01:46 +02:00
|
|
|
function TMVCActiveRecord.GetMapping: TMVCFieldsMapping;
|
|
|
|
var
|
|
|
|
lPair: TPair<TRttiField, string>;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
2018-11-02 21:43:09 +01:00
|
|
|
if Length(fMapping) = 0 then
|
2018-09-28 13:01:46 +02:00
|
|
|
begin
|
2018-11-02 21:43:09 +01:00
|
|
|
if not fPrimaryKeyFieldName.IsEmpty then
|
2019-02-05 18:08:21 +01:00
|
|
|
begin
|
|
|
|
SetLength(fMapping, fMap.Count + 1);
|
|
|
|
fMapping[0].InstanceFieldName := fPrimaryKey.Name.Substring(1).ToLower;
|
|
|
|
fMapping[0].DatabaseFieldName := fPrimaryKeyFieldName;
|
|
|
|
i := 1;
|
|
|
|
end
|
2018-11-02 21:43:09 +01:00
|
|
|
else
|
2019-02-05 18:08:21 +01:00
|
|
|
begin
|
2018-11-02 21:43:09 +01:00
|
|
|
SetLength(fMapping, fMap.Count);
|
2019-02-05 18:08:21 +01:00
|
|
|
i := 0;
|
|
|
|
end;
|
2018-09-28 13:01:46 +02:00
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
for lPair in fMap do
|
|
|
|
begin
|
|
|
|
fMapping[i].InstanceFieldName := lPair.Key.Name.Substring(1).ToLower;
|
2020-01-04 12:53:53 +01:00
|
|
|
fMapping[i].DatabaseFieldName := lPair.Value;
|
2018-11-02 21:43:09 +01:00
|
|
|
inc(i);
|
|
|
|
end;
|
2018-09-28 13:01:46 +02:00
|
|
|
end;
|
2018-11-02 21:43:09 +01:00
|
|
|
Result := fMapping;
|
2018-09-28 13:01:46 +02:00
|
|
|
end;
|
|
|
|
|
2019-08-02 12:32:23 +02:00
|
|
|
class function TMVCActiveRecordHelper.GetOneByWhere<T>(const SQLWhere: string; const Params: array of Variant;
|
2019-03-05 20:55:37 +01:00
|
|
|
const RaiseExceptionIfNotFound: Boolean): T;
|
2018-10-23 16:18:34 +02:00
|
|
|
begin
|
|
|
|
Result := GetFirstByWhere<T>(SQLWhere, Params, false);
|
|
|
|
if Result = nil then
|
|
|
|
begin
|
|
|
|
if RaiseExceptionIfNotFound then
|
|
|
|
raise EMVCActiveRecord.Create('Got 0 rows when exactly 1 was expected');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
function TMVCActiveRecord.GetPK: TValue;
|
2020-02-03 13:19:55 +01:00
|
|
|
var
|
|
|
|
lIsNullableType: Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2020-02-03 13:19:55 +01:00
|
|
|
if not TryGetPKValue(Result, lIsNullableType) then
|
2020-01-04 12:53:53 +01:00
|
|
|
begin
|
2020-02-03 13:19:55 +01:00
|
|
|
if not lIsNullableType then
|
|
|
|
begin
|
|
|
|
raise EMVCActiveRecord.Create('Primary key not available');
|
|
|
|
end;
|
2020-01-04 12:53:53 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCActiveRecord.GetPrimaryKeyIsAutogenerated: Boolean;
|
|
|
|
begin
|
|
|
|
Result := foAutoGenerated in fPrimaryKeyOptions;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.GetScalar(const SQL: string; const Params: array of Variant): Variant;
|
2019-01-08 12:48:27 +01:00
|
|
|
begin
|
|
|
|
Result := CurrentConnection.ExecSQLScalar(SQL, Params);
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
function TMVCActiveRecord.CheckAction(const aEntityAction: TMVCEntityAction; const aRaiseException: Boolean): Boolean;
|
2018-10-14 18:23:20 +02:00
|
|
|
begin
|
|
|
|
Result := aEntityAction in fEntityAllowedActions;
|
|
|
|
if (not Result) and aRaiseException then
|
|
|
|
raise EMVCActiveRecord.CreateFmt('Action not allowed on "%s"', [ClassName]);
|
|
|
|
end;
|
|
|
|
|
2019-05-09 20:53:52 +02:00
|
|
|
class function TMVCActiveRecord.Count(const aClass: TMVCActiveRecordClass; const RQL: String): int64;
|
2019-01-13 19:18:57 +01:00
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
|
|
|
begin
|
|
|
|
lAR := aClass.Create;
|
|
|
|
try
|
2020-01-04 12:53:53 +01:00
|
|
|
// Up to 10.1 BErlin, here the compiler try to call the Count<T> introduced by the class helper
|
|
|
|
// Instead of the Count() which exists in "TMVCActiveRecord"
|
2019-08-02 12:32:23 +02:00
|
|
|
Result := lAR.InternalCount(RQL);
|
2019-01-13 19:18:57 +01:00
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-01-04 12:53:53 +01:00
|
|
|
function TMVCActiveRecord.Count(const RQL: String = ''): int64;
|
2019-05-09 20:53:52 +02:00
|
|
|
begin
|
2019-08-02 12:32:23 +02:00
|
|
|
Result := InternalCount(RQL);
|
2019-05-09 20:53:52 +02:00
|
|
|
end;
|
|
|
|
|
2019-08-02 12:32:23 +02:00
|
|
|
class function TMVCActiveRecordHelper.Count<T>(const RQL: String = ''): int64;
|
2019-05-09 20:53:52 +02:00
|
|
|
begin
|
|
|
|
Result := TMVCActiveRecord.Count(TMVCActiveRecordClass(T), RQL);
|
2019-01-13 19:18:57 +01:00
|
|
|
end;
|
|
|
|
|
2018-11-24 16:56:21 +01:00
|
|
|
class function TMVCActiveRecord.CurrentConnection: TFDConnection;
|
2018-10-23 16:18:34 +02:00
|
|
|
begin
|
|
|
|
Result := ActiveRecordConnectionsRegistry.GetCurrent;
|
|
|
|
end;
|
|
|
|
|
2020-02-03 10:51:40 +01:00
|
|
|
function TMVCActiveRecord.GetConnection: TFDConnection;
|
2018-09-28 13:01:46 +02:00
|
|
|
begin
|
|
|
|
if fConn = nil then
|
|
|
|
begin
|
|
|
|
fConn := ActiveRecordConnectionsRegistry.GetCurrent;
|
|
|
|
end;
|
|
|
|
Result := fConn;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TMVCActiveRecord.Create;
|
|
|
|
begin
|
2020-02-03 10:51:40 +01:00
|
|
|
Create(True);
|
2018-09-28 13:01:46 +02:00
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure TMVCActiveRecord.Delete;
|
|
|
|
var
|
|
|
|
SQL: string;
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
OnBeforeDelete;
|
2018-09-25 15:36:53 +02:00
|
|
|
if not Assigned(fPrimaryKey) then
|
2019-03-05 20:55:37 +01:00
|
|
|
raise Exception.CreateFmt('Cannot delete %s without a primary key', [ClassName]);
|
|
|
|
SQL := SQLGenerator.CreateDeleteSQL(fTableName, fMap, fPrimaryKeyFieldName, fPrimaryKeyOptions,
|
2020-01-04 12:53:53 +01:00
|
|
|
GetPK.AsInt64);
|
2018-09-25 15:36:53 +02:00
|
|
|
ExecNonQuery(SQL, false);
|
2018-10-23 16:18:34 +02:00
|
|
|
OnAfterDelete;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.DeleteAll(const aClass: TMVCActiveRecordClass): int64;
|
2019-02-21 18:11:14 +01:00
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
|
|
|
begin
|
|
|
|
lAR := aClass.Create;
|
|
|
|
try
|
2019-03-05 20:55:37 +01:00
|
|
|
Result := lAR.ExecNonQuery(lAR.SQLGenerator.CreateDeleteAllSQL(lAR.fTableName));
|
2019-02-21 18:11:14 +01:00
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.DeleteRQL(const aClass: TMVCActiveRecordClass; const RQL: string): int64;
|
2019-02-21 18:11:14 +01:00
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
|
|
|
begin
|
|
|
|
lAR := aClass.Create(True);
|
|
|
|
try
|
2019-03-05 20:55:37 +01:00
|
|
|
Result := lAR.ExecNonQuery(lAR.SQLGenerator.CreateDeleteAllSQL(lAR.fTableName) +
|
|
|
|
lAR.SQLGenerator.CreateSQLWhereByRQL(RQL, lAR.GetMapping, false));
|
2019-02-21 18:11:14 +01:00
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
|
2019-11-27 19:04:06 +01:00
|
|
|
procedure TMVCActiveRecord.MapDatasetToObject(const DataSet: TDataSet;
|
|
|
|
const Options: TMVCActiveRecordLoadOptions;
|
|
|
|
var Handled: Boolean);
|
|
|
|
begin
|
2020-01-04 12:53:53 +01:00
|
|
|
// do nothing
|
2019-11-27 19:04:06 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecord.MapObjectToParams(const Params: TFDParams;
|
|
|
|
var Handled: Boolean);
|
|
|
|
begin
|
2020-01-04 12:53:53 +01:00
|
|
|
// do nothing
|
2019-11-27 19:04:06 +01:00
|
|
|
end;
|
|
|
|
|
2020-01-04 12:53:53 +01:00
|
|
|
function TMVCActiveRecord.MapNullableTValueToParam(aValue: TValue; const aParam: TFDParam): Boolean;
|
|
|
|
begin
|
|
|
|
Assert(aValue.Kind = tkRecord);
|
|
|
|
Result := false;
|
|
|
|
if aValue.IsType(TypeInfo(NullableString)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableString>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := ftString;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := aValue.AsType<NullableString>().Value;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if aValue.IsType(TypeInfo(NullableInt32)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableInt32>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := ftInteger;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := aValue.AsType<NullableInt32>().Value;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
2020-02-03 10:51:40 +01:00
|
|
|
else if aValue.IsType(TypeInfo(NullableTDate)) then
|
2020-01-04 12:53:53 +01:00
|
|
|
begin
|
2020-02-03 10:51:40 +01:00
|
|
|
if not aValue.AsType<NullableTDate>().HasValue then
|
2020-01-04 12:53:53 +01:00
|
|
|
begin
|
|
|
|
aParam.DataType := ftDate;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2020-02-03 10:51:40 +01:00
|
|
|
aValue := TValue.From<TDate>(aValue.AsType<NullableTDate>().Value);
|
2020-01-04 12:53:53 +01:00
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
2020-02-03 10:51:40 +01:00
|
|
|
else if aValue.IsType(TypeInfo(NullableTTime)) then
|
2020-01-04 12:53:53 +01:00
|
|
|
begin
|
2020-02-03 10:51:40 +01:00
|
|
|
if not aValue.AsType<NullableTTime>().HasValue then
|
2020-01-04 12:53:53 +01:00
|
|
|
begin
|
|
|
|
aParam.DataType := ftTime;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2020-02-03 10:51:40 +01:00
|
|
|
aValue := TValue.From<TTime>(aValue.AsType<NullableTTime>().Value);
|
2020-01-04 12:53:53 +01:00
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
2020-02-03 10:51:40 +01:00
|
|
|
else if aValue.IsType(TypeInfo(NullableTDateTime)) then
|
2020-01-04 12:53:53 +01:00
|
|
|
begin
|
2020-02-03 10:51:40 +01:00
|
|
|
if not aValue.AsType<NullableTDateTime>().HasValue then
|
2020-01-04 12:53:53 +01:00
|
|
|
begin
|
|
|
|
aParam.DataType := ftDateTime;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2020-02-03 10:51:40 +01:00
|
|
|
aValue := TValue.From<TDateTime>(aValue.AsType<NullableTDateTime>().Value);
|
2020-01-04 12:53:53 +01:00
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if aValue.IsType(TypeInfo(NullableUInt32)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableUInt32>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := ftInteger;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := aValue.AsType<NullableUInt32>().Value;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if aValue.IsType(TypeInfo(NullableInt64)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableInt64>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := ftLargeint;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := aValue.AsType<NullableInt64>().Value;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if aValue.IsType(TypeInfo(NullableInt16)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableInt16>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := ftInteger;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := aValue.AsType<NullableInt16>().Value;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if aValue.IsType(TypeInfo(NullableUInt64)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableUInt64>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := ftLargeint;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := aValue.AsType<NullableUInt64>().Value;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if aValue.IsType(TypeInfo(NullableUInt16)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableUInt16>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := ftInteger;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := aValue.AsType<NullableUInt16>().Value;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if aValue.IsType(TypeInfo(NullableBoolean)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableBoolean>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := ftBoolean;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := TValue.From<Boolean>(aValue.AsType<NullableBoolean>().Value);
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if aValue.IsType(TypeInfo(NullableSingle)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableSingle>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := TFieldType.ftSingle;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := aValue.AsType<NullableSingle>().Value;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if aValue.IsType(TypeInfo(NullableDouble)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableDouble>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := TFieldType.ftFloat;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := aValue.AsType<NullableDouble>().Value;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if aValue.IsType(TypeInfo(NullableCurrency)) then
|
|
|
|
begin
|
|
|
|
if not aValue.AsType<NullableCurrency>().HasValue then
|
|
|
|
begin
|
|
|
|
aParam.DataType := TFieldType.ftCurrency;
|
|
|
|
aParam.Clear;
|
|
|
|
Exit(True);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aValue := aValue.AsType<NullableCurrency>().Value;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
if Result then
|
|
|
|
begin
|
|
|
|
MapTValueToParam(aValue, aParam);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecord.MapTValueToParam(aValue: TValue; const aParam: TFDParam);
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lStream: TStream;
|
2019-03-13 12:38:04 +01:00
|
|
|
lName: String;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-05-09 20:53:52 +02:00
|
|
|
{$IFDEF NEXTGEN}
|
|
|
|
lName := aValue.TypeInfo.NameFld.ToString;
|
|
|
|
{$ELSE}
|
|
|
|
lName := String(aValue.TypeInfo.Name);
|
|
|
|
{$ENDIF}
|
2020-01-04 12:53:53 +01:00
|
|
|
if (aValue.TypeInfo.Kind = tkRecord) then
|
|
|
|
begin
|
|
|
|
if MapNullableTValueToParam(aValue, aParam) then
|
|
|
|
begin
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
case aValue.TypeInfo.Kind of
|
|
|
|
tkString, tkUString:
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-12-17 14:52:11 +01:00
|
|
|
case aParam.DataType of
|
2020-01-04 12:53:53 +01:00
|
|
|
ftUnknown, ftString, ftWideString:
|
|
|
|
begin
|
|
|
|
aParam.AsString := aValue.AsString;
|
|
|
|
end;
|
|
|
|
ftWideMemo:
|
|
|
|
begin
|
|
|
|
aParam.AsWideMemo := aValue.AsString;
|
|
|
|
end;
|
|
|
|
ftMemo:
|
|
|
|
begin
|
|
|
|
aParam.AsMemo := AnsiString(aValue.AsString);
|
|
|
|
end;
|
2019-12-17 14:52:11 +01:00
|
|
|
else
|
|
|
|
begin
|
2019-12-17 17:34:23 +01:00
|
|
|
raise EMVCActiveRecord.CreateFmt('Invalid parameter type for (tkString, tkUString) [%s]', [lName]);
|
2019-12-17 14:52:11 +01:00
|
|
|
end;
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
2019-08-02 12:32:23 +02:00
|
|
|
{$IF Defined(SeattleOrBetter)}
|
2018-10-14 18:23:20 +02:00
|
|
|
tkWideString:
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
aParam.AsWideString := aValue.AsString;
|
|
|
|
end;
|
2019-08-02 12:32:23 +02:00
|
|
|
{$ENDIF}
|
2018-10-14 18:23:20 +02:00
|
|
|
tkInt64:
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
aParam.AsLargeInt := aValue.AsInt64;
|
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
tkInteger:
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
aParam.AsInteger := aValue.AsInteger;
|
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
tkEnumeration:
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
if aValue.TypeInfo = TypeInfo(System.Boolean) then
|
|
|
|
begin
|
|
|
|
aParam.AsBoolean := aValue.AsBoolean;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aParam.AsInteger := Ord(aValue.AsInteger);
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
tkFloat:
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-05-09 20:53:52 +02:00
|
|
|
if lName = 'TDate' then
|
2018-10-14 18:23:20 +02:00
|
|
|
begin
|
|
|
|
aParam.AsDate := Trunc(aValue.AsExtended);
|
|
|
|
end
|
2018-11-02 21:43:09 +01:00
|
|
|
else
|
2019-05-09 20:53:52 +02:00
|
|
|
if lName = 'TDateTime' then
|
2020-01-04 12:53:53 +01:00
|
|
|
begin
|
|
|
|
aParam.AsDateTime := aValue.AsExtended;
|
|
|
|
end
|
|
|
|
else
|
2020-01-06 16:49:18 +01:00
|
|
|
if lName = 'TTime' then
|
2020-01-04 12:53:53 +01:00
|
|
|
begin
|
|
|
|
aParam.AsTime := aValue.AsExtended;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if lName = 'Currency' then
|
|
|
|
begin
|
|
|
|
aParam.AsCurrency := aValue.AsCurrency;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
aParam.AsFloat := aValue.AsExtended;
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
tkClass:
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2020-01-06 16:49:18 +01:00
|
|
|
if (aValue.AsObject <> nil) and (not aValue.IsInstanceOf(TStream)) then
|
2019-03-05 20:55:37 +01:00
|
|
|
raise EMVCActiveRecord.CreateFmt('Unsupported reference type for param %s: %s',
|
2018-11-24 16:56:21 +01:00
|
|
|
[aParam.Name, aValue.AsObject.ClassName]);
|
2020-01-04 12:53:53 +01:00
|
|
|
{$IF Defined(SeattleOrBetter)}
|
|
|
|
lStream := aValue.AsType<TStream>();
|
|
|
|
{$ELSE}
|
2019-08-02 12:32:23 +02:00
|
|
|
lStream := aValue.AsType<TStream>();
|
2020-01-04 12:53:53 +01:00
|
|
|
{$ENDIF}
|
2018-09-25 15:36:53 +02:00
|
|
|
if Assigned(lStream) then
|
|
|
|
begin
|
|
|
|
lStream.Position := 0;
|
|
|
|
aParam.LoadFromStream(lStream, ftBlob);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2020-01-06 16:49:18 +01:00
|
|
|
aParam.DataType := TFieldType.ftBlob;
|
2018-09-25 15:36:53 +02:00
|
|
|
aParam.Clear;
|
|
|
|
end;
|
|
|
|
end;
|
2019-03-05 20:55:37 +01:00
|
|
|
tkRecord:
|
|
|
|
begin
|
|
|
|
if aValue.IsType(TypeInfo(TGUID)) then
|
|
|
|
begin
|
|
|
|
aParam.AsGuid := aValue.AsType<TGUID>;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
raise Exception.CreateFmt('Unsupported Record TypeKind (%d) for param %s',
|
|
|
|
[Ord(aValue.TypeInfo.Kind), aParam.Name]);
|
|
|
|
end;
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
else
|
2019-03-05 20:55:37 +01:00
|
|
|
raise Exception.CreateFmt('Unsupported TypeKind (%d) for param %s', [Ord(aValue.TypeInfo.Kind), aParam.Name]);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
|
|
|
// case aParam.DataType of
|
|
|
|
// ftUnknown:
|
|
|
|
// begin
|
|
|
|
// { aParam.DataType could be pkUndefined for some RDBMS (es. MySQL), so we rely on Variant }
|
|
|
|
// if (aValue.TypeInfo.Kind = tkClass) then
|
|
|
|
// begin
|
|
|
|
// if not aValue.IsInstanceOf(TStream) then
|
|
|
|
// raise EMVCActiveRecord.CreateFmt('Unsupported type for param %s', [aParam.Name]);
|
|
|
|
// lStream := aValue.AsType<TStream>(false);
|
|
|
|
// if Assigned(lStream) then
|
|
|
|
// begin
|
|
|
|
// lStream.Position := 0;
|
|
|
|
// aParam.LoadFromStream(lStream, ftBlob);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// begin
|
|
|
|
// aParam.Clear;
|
|
|
|
// end;
|
|
|
|
//
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// begin
|
|
|
|
// aParam.value := aValue.AsVariant;
|
|
|
|
// end;
|
|
|
|
// end;
|
|
|
|
// ftString:
|
|
|
|
// begin
|
|
|
|
// aParam.AsString := aValue.AsString;
|
|
|
|
// end;
|
|
|
|
// ftWideString:
|
|
|
|
// begin
|
|
|
|
// aParam.AsWideString := aValue.AsString;
|
|
|
|
// end;
|
|
|
|
// ftLargeint:
|
|
|
|
// begin
|
|
|
|
// aParam.AsLargeInt := aValue.AsInt64;
|
|
|
|
// end;
|
|
|
|
// ftSmallint:
|
|
|
|
// begin
|
|
|
|
// aParam.AsSmallInt := aValue.AsInteger;
|
|
|
|
// end;
|
|
|
|
// ftInteger:
|
|
|
|
// begin
|
|
|
|
// aParam.AsInteger := aValue.AsInteger;
|
|
|
|
// end;
|
|
|
|
// ftLongWord:
|
|
|
|
// begin
|
|
|
|
// aParam.AsLongWord := aValue.AsInteger;
|
|
|
|
// end;
|
|
|
|
// ftWord:
|
|
|
|
// begin
|
|
|
|
// aParam.AsWord := aValue.AsInteger;
|
|
|
|
// end;
|
|
|
|
// ftDate:
|
|
|
|
// begin
|
|
|
|
// aParam.AsDate := Trunc(aValue.AsExtended);
|
|
|
|
// end;
|
|
|
|
// ftDateTime:
|
|
|
|
// begin
|
|
|
|
// aParam.AsDateTime := aValue.AsExtended;
|
|
|
|
// end;
|
|
|
|
// ftBoolean:
|
|
|
|
// begin
|
|
|
|
// aParam.AsBoolean := aValue.AsBoolean;
|
|
|
|
// end;
|
|
|
|
// ftMemo:
|
|
|
|
// begin
|
|
|
|
// aParam.AsMemo := AnsiString(aValue.AsString);
|
|
|
|
// end;
|
|
|
|
// ftWideMemo:
|
|
|
|
// begin
|
|
|
|
// aParam.AsWideMemo := aValue.AsString;
|
|
|
|
// end;
|
|
|
|
// ftBCD:
|
|
|
|
// begin
|
|
|
|
// aParam.AsBCD := aValue.AsCurrency;
|
|
|
|
// end;
|
|
|
|
// ftBlob:
|
|
|
|
// begin
|
|
|
|
// lStream := aValue.AsType<TStream>(false);
|
|
|
|
// if Assigned(lStream) then
|
|
|
|
// begin
|
|
|
|
// lStream.Position := 0;
|
|
|
|
// aParam.LoadFromStream(lStream, ftBlob);
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// begin
|
|
|
|
// aParam.Clear;
|
|
|
|
// end;
|
|
|
|
// end;
|
|
|
|
// else
|
|
|
|
// raise Exception.CreateFmt('Unsupported FieldType (%d) for param %s', [Ord(aParam.DataType), aParam.Name]);
|
|
|
|
// end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure TMVCActiveRecord.LoadByDataset(const aDataSet: TDataSet; const aOptions: TMVCActiveRecordLoadOptions);
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lItem: TPair<TRttiField, string>;
|
2018-12-09 23:03:06 +01:00
|
|
|
lField: TField;
|
2019-11-27 19:04:06 +01:00
|
|
|
lHandled: Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
CheckAction(TMVCEntityAction.eaRetrieve);
|
2018-09-25 15:36:53 +02:00
|
|
|
OnBeforeLoad;
|
2019-11-27 19:04:06 +01:00
|
|
|
|
2020-01-04 12:53:53 +01:00
|
|
|
lHandled := false;
|
2019-11-27 19:04:06 +01:00
|
|
|
MapDatasetToObject(aDataSet, aOptions, lHandled);
|
|
|
|
if not lHandled then
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-11-27 19:04:06 +01:00
|
|
|
for lItem in fMap do
|
2019-11-05 16:57:22 +01:00
|
|
|
begin
|
2019-11-27 19:04:06 +01:00
|
|
|
if lItem.Value.IsEmpty then
|
|
|
|
begin
|
2020-01-04 12:53:53 +01:00
|
|
|
continue;
|
2019-11-27 19:04:06 +01:00
|
|
|
end;
|
2020-01-04 12:53:53 +01:00
|
|
|
lField := aDataSet.FindField(lItem.Value);
|
2019-11-27 19:04:06 +01:00
|
|
|
if lField = nil then
|
|
|
|
begin
|
|
|
|
if TMVCActiveRecordLoadOption.loIgnoreNotExistentFields in aOptions then
|
|
|
|
continue
|
|
|
|
else
|
|
|
|
raise EMVCActiveRecord.CreateFmt
|
2020-01-04 12:53:53 +01:00
|
|
|
('Field [%s] not found in dataset. [HINT] If you dont need it, use loIgnoreNotExistentFields',
|
|
|
|
[lItem.Value]);
|
2019-11-27 19:04:06 +01:00
|
|
|
end;
|
2020-02-05 23:46:38 +01:00
|
|
|
MapDataSetFieldToRTTIField(lField, lItem.Key, Self);
|
2019-11-05 16:57:22 +01:00
|
|
|
end;
|
2019-11-27 19:04:06 +01:00
|
|
|
if not fPrimaryKeyFieldName.IsEmpty then
|
2018-12-09 23:03:06 +01:00
|
|
|
begin
|
2020-02-05 23:46:38 +01:00
|
|
|
MapDataSetFieldToRTTIField(aDataSet.FieldByName(fPrimaryKeyFieldName), fPrimaryKey, Self);
|
2018-12-09 23:03:06 +01:00
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
OnAfterLoad;
|
|
|
|
end;
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
function TMVCActiveRecord.LoadByPK(id: int64): Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
SQL: string;
|
|
|
|
lDataSet: TDataSet;
|
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
CheckAction(TMVCEntityAction.eaRetrieve);
|
2019-03-05 20:55:37 +01:00
|
|
|
SQL := SQLGenerator.CreateSelectByPKSQL(fTableName, fMap, fPrimaryKeyFieldName, fPrimaryKeyOptions, id);
|
2020-02-03 10:51:40 +01:00
|
|
|
lDataSet := ExecQuery(SQL, [id], GetConnection);
|
2018-09-25 15:36:53 +02:00
|
|
|
try
|
|
|
|
Result := not lDataSet.Eof;
|
|
|
|
if Result then
|
|
|
|
begin
|
|
|
|
LoadByDataset(lDataSet);
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lDataSet.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
procedure TMVCActiveRecord.OnAfterDelete;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecord.OnAfterInsert;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecord.OnAfterInsertOrUpdate;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure TMVCActiveRecord.OnAfterLoad;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
procedure TMVCActiveRecord.OnAfterUpdate;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure TMVCActiveRecord.OnBeforeDelete;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2019-05-09 20:53:52 +02:00
|
|
|
procedure TMVCActiveRecord.OnBeforeExecuteSQL(var SQL: String);
|
2019-03-18 14:08:34 +01:00
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure TMVCActiveRecord.OnBeforeInsert;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecord.OnBeforeInsertOrUpdate;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecord.OnBeforeLoad;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecord.OnBeforeUpdate;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecord.OnValidation;
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.Select(const aClass: TMVCActiveRecordClass; const SQL: string;
|
|
|
|
const Params: array of Variant): TMVCActiveRecordList;
|
2018-09-28 13:01:46 +02:00
|
|
|
begin
|
|
|
|
Result := Select(aClass, SQL, Params, nil);
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.Select(const aClass: TMVCActiveRecordClass; const SQL: string;
|
|
|
|
const Params: array of Variant; const Connection: TFDConnection): TMVCActiveRecordList;
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lDataSet: TDataSet;
|
|
|
|
lAR: TMVCActiveRecord;
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
Result := TMVCActiveRecordList.Create;
|
2018-09-25 15:36:53 +02:00
|
|
|
try
|
2018-09-28 13:01:46 +02:00
|
|
|
lDataSet := ExecQuery(SQL, Params, Connection);
|
2018-09-25 15:36:53 +02:00
|
|
|
try
|
|
|
|
while not lDataSet.Eof do
|
|
|
|
begin
|
|
|
|
lAR := aClass.Create;
|
|
|
|
Result.Add(lAR);
|
|
|
|
lAR.LoadByDataset(lDataSet);
|
|
|
|
lDataSet.Next;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lDataSet.Free;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
Result.Free;
|
|
|
|
raise;
|
|
|
|
end;
|
2018-09-28 13:01:46 +02:00
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2019-08-02 12:32:23 +02:00
|
|
|
class function TMVCActiveRecordHelper.Select<T>(const SQL: string; const Params: array of Variant;
|
2019-03-05 20:55:37 +01:00
|
|
|
const Options: TMVCActiveRecordLoadOptions): TObjectList<T>;
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lDataSet: TDataSet;
|
|
|
|
lAR: TMVCActiveRecord;
|
2019-11-27 19:04:06 +01:00
|
|
|
lHandled: Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
Result := TObjectList<T>.Create(True);
|
|
|
|
try
|
|
|
|
lDataSet := ExecQuery(SQL, Params);
|
|
|
|
try
|
|
|
|
while not lDataSet.Eof do
|
|
|
|
begin
|
|
|
|
lAR := T.Create;
|
|
|
|
Result.Add(lAR);
|
2018-12-09 23:03:06 +01:00
|
|
|
lAR.LoadByDataset(lDataSet, Options);
|
2018-09-25 15:36:53 +02:00
|
|
|
lDataSet.Next;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lDataSet.Free;
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
Result.Free;
|
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.SelectDataSet(const SQL: string; const Params: array of Variant): TDataSet;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
Result := TMVCActiveRecord.ExecQuery(SQL, Params);
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
function TMVCActiveRecord.SelectRQL(const RQL: string; const MaxRecordCount: Integer): TMVCActiveRecordList;
|
2018-09-27 12:26:50 +02:00
|
|
|
begin
|
2019-08-02 12:32:23 +02:00
|
|
|
Result := InternalSelectRQL(RQL, MaxRecordCount);
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
2019-08-02 12:32:23 +02:00
|
|
|
class function TMVCActiveRecordHelper.SelectRQL<T>(const RQL: string; const MaxRecordCount: Integer): TObjectList<T>;
|
2018-11-09 18:11:59 +01:00
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
|
|
|
lSQL: string;
|
|
|
|
begin
|
|
|
|
lAR := T.Create;
|
|
|
|
try
|
2019-02-21 18:11:14 +01:00
|
|
|
lSQL := lAR.SQLGenerator.CreateSQLWhereByRQL(RQL, lAR.GetMapping).Trim;
|
2019-01-13 19:18:57 +01:00
|
|
|
// LogD(Format('RQL [%s] => SQL [%s]', [RQL, lSQL]));
|
2018-11-09 18:11:59 +01:00
|
|
|
if lSQL.StartsWith('where', True) then
|
|
|
|
lSQL := lSQL.Remove(0, 5).Trim;
|
|
|
|
Result := Where<T>(lSQL, []);
|
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.SelectRQL(const aClass: TMVCActiveRecordClass; const RQL: string;
|
|
|
|
const MaxRecordCount: Integer): TMVCActiveRecordList;
|
2018-11-02 21:43:09 +01:00
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
|
|
|
begin
|
|
|
|
lAR := aClass.Create(True);
|
2018-09-27 12:26:50 +02:00
|
|
|
try
|
2019-08-02 12:32:23 +02:00
|
|
|
Result := lAR.InternalSelectRQL(RQL, MaxRecordCount);
|
2018-09-27 12:26:50 +02:00
|
|
|
finally
|
2018-11-02 21:43:09 +01:00
|
|
|
lAR.Free;
|
2018-09-27 12:26:50 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure TMVCActiveRecord.SetPK(const aValue: TValue);
|
2020-01-08 15:30:10 +01:00
|
|
|
var
|
|
|
|
lPKValue: TValue;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
if fPrimaryKeyFieldName.IsEmpty then
|
2020-01-08 15:30:10 +01:00
|
|
|
begin
|
2018-09-25 15:36:53 +02:00
|
|
|
raise Exception.Create('No primary key defined');
|
2020-01-08 15:30:10 +01:00
|
|
|
end;
|
|
|
|
if fPrimaryKey.GetValue(Self).Kind = tkRecord then
|
|
|
|
begin
|
|
|
|
lPKValue := fPrimaryKey.GetValue(Self);
|
|
|
|
if lPKValue.IsType<NullableInt32> then
|
|
|
|
begin
|
|
|
|
if aValue.IsType<UInt32> then
|
|
|
|
begin
|
|
|
|
lPKValue := TValue.From<NullableInt32>(IntToNullableInt(aValue.AsInteger));
|
|
|
|
end;
|
|
|
|
//
|
|
|
|
// if aValue.AsType<NullableInt32>().HasValue then
|
|
|
|
// begin
|
|
|
|
// lPKValue := aValue;
|
|
|
|
// end
|
|
|
|
// else
|
|
|
|
// begin
|
|
|
|
// lPKValue.AsType<NullableInt32>().Clear;
|
|
|
|
// end;
|
|
|
|
end
|
|
|
|
else if lPKValue.IsType<NullableInt64> then
|
|
|
|
begin
|
|
|
|
if aValue.AsType<NullableInt64>().HasValue then
|
|
|
|
begin
|
|
|
|
lPKValue := aValue;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
lPKValue.AsType<NullableInt64>().Clear;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if lPKValue.IsType<NullableUInt32> then
|
|
|
|
begin
|
|
|
|
if aValue.AsType<NullableUInt32>().HasValue then
|
|
|
|
begin
|
|
|
|
lPKValue := aValue;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
lPKValue.AsType<NullableUInt32>().Clear;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else if lPKValue.IsType<NullableUInt64> then
|
|
|
|
begin
|
|
|
|
if aValue.AsType<NullableUInt64>().HasValue then
|
|
|
|
begin
|
|
|
|
lPKValue := aValue;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
lPKValue.AsType<NullableUInt64>().Clear;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise EMVCActiveRecord.Create('Invalid type for primary key');
|
|
|
|
fPrimaryKey.SetValue(Self, lPKValue);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
fPrimaryKey.SetValue(Self, aValue)
|
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2020-01-04 12:53:53 +01:00
|
|
|
procedure TMVCActiveRecord.SetPrimaryKeyIsAutogenerated(const Value: Boolean);
|
|
|
|
begin
|
|
|
|
if Value then
|
|
|
|
begin
|
|
|
|
Include(fPrimaryKeyOptions, foAutoGenerated);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Exclude(fPrimaryKeyOptions, foAutoGenerated);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-11-24 16:56:21 +01:00
|
|
|
function TMVCActiveRecord.SQLGenerator: TMVCSQLGenerator;
|
|
|
|
begin
|
|
|
|
if not Assigned(fSQLGenerator) then
|
|
|
|
begin
|
2020-02-03 10:51:40 +01:00
|
|
|
GetConnection.Connected := True;
|
2019-03-05 20:55:37 +01:00
|
|
|
fSQLGenerator := TMVCSQLGeneratorRegistry.Instance.GetSQLGenerator(GetBackEnd).Create(GetMapping);
|
2018-11-24 16:56:21 +01:00
|
|
|
end;
|
|
|
|
Result := fSQLGenerator;
|
|
|
|
end;
|
|
|
|
|
2020-02-03 13:19:55 +01:00
|
|
|
procedure TMVCActiveRecord.Store;
|
|
|
|
var
|
|
|
|
lValue: TValue;
|
|
|
|
lRes: Boolean;
|
|
|
|
lIsNullableType: Boolean;
|
|
|
|
begin
|
|
|
|
lRes := TryGetPKValue(lValue, lIsNullableType);
|
|
|
|
if not lIsNullableType then
|
|
|
|
begin
|
|
|
|
raise EMVCActiveRecord.Create('Store can be used only with nullable PKs [HINT] Use NullableInt64 as PK');
|
|
|
|
end;
|
|
|
|
if lRes then
|
|
|
|
begin
|
|
|
|
Update;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Insert;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
function TMVCActiveRecord.TableInfo: string;
|
|
|
|
var
|
|
|
|
keyvalue: TPair<TRttiField, string>;
|
|
|
|
begin
|
|
|
|
Result := 'Table Name: ' + fTableName;
|
|
|
|
for keyvalue in fMap do
|
2020-01-04 12:53:53 +01:00
|
|
|
Result := Result + sLineBreak + #9 + keyvalue.Key.Name + ' = ' + keyvalue.Value;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2020-02-03 13:19:55 +01:00
|
|
|
function TMVCActiveRecord.TryGetPKValue(var Value: TValue; out IsNullableType: Boolean): Boolean;
|
|
|
|
begin
|
|
|
|
IsNullableType := false;
|
|
|
|
if fPrimaryKeyFieldName.IsEmpty then
|
|
|
|
raise Exception.Create('No primary key defined');
|
|
|
|
Value := fPrimaryKey.GetValue(Self);
|
|
|
|
if Value.Kind = tkRecord then
|
|
|
|
begin
|
|
|
|
if Value.IsType<NullableInt32>() then
|
|
|
|
begin
|
|
|
|
Result := Value.AsType<NullableInt32>().HasValue;
|
|
|
|
if Result then
|
|
|
|
Value := Value.AsType<NullableInt32>().Value;
|
|
|
|
end
|
|
|
|
else if Value.IsType<NullableInt64>() then
|
|
|
|
begin
|
|
|
|
Result := Value.AsType<NullableInt64>().HasValue;
|
|
|
|
if Result then
|
|
|
|
Value := Value.AsType<NullableInt64>().Value;
|
|
|
|
end
|
|
|
|
else if Value.IsType<NullableUInt32>() then
|
|
|
|
begin
|
|
|
|
Result := Value.AsType<NullableUInt32>().HasValue;
|
|
|
|
if Result then
|
|
|
|
Value := Value.AsType<NullableUInt32>().Value;
|
|
|
|
end
|
|
|
|
else if Value.IsType<NullableUInt64>() then
|
|
|
|
begin
|
|
|
|
Result := Value.AsType<NullableUInt64>().HasValue;
|
|
|
|
if Result then
|
|
|
|
Value := Value.AsType<NullableUInt64>().Value;
|
|
|
|
end
|
|
|
|
else if Value.IsType<NullableInt16>() then
|
|
|
|
begin
|
|
|
|
Result := Value.AsType<NullableInt16>().HasValue;
|
|
|
|
if Result then
|
|
|
|
Value := Value.AsType<NullableInt16>().Value;
|
|
|
|
end
|
|
|
|
else if Value.IsType<NullableUInt16>() then
|
|
|
|
begin
|
|
|
|
Result := Value.AsType<NullableUInt16>().HasValue;
|
|
|
|
if Result then
|
|
|
|
Value := Value.AsType<NullableUInt16>().Value;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise EMVCActiveRecord.Create('Invalid primary key type [HINT: Use Int64 or NullableInt64, so that Store method is available too.]');
|
|
|
|
IsNullableType := True;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := not Value.IsEmpty;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
procedure TMVCActiveRecord.Update;
|
|
|
|
var
|
|
|
|
SQL: string;
|
|
|
|
begin
|
2018-10-14 18:23:20 +02:00
|
|
|
CheckAction(TMVCEntityAction.eaUpdate);
|
2018-09-25 15:36:53 +02:00
|
|
|
OnValidation;
|
|
|
|
OnBeforeUpdate;
|
|
|
|
OnBeforeInsertOrUpdate;
|
2019-02-21 20:17:11 +01:00
|
|
|
if fMapNonTransientFields.Count = 0 then
|
|
|
|
begin
|
|
|
|
raise EMVCActiveRecord.CreateFmt
|
2019-03-05 20:55:37 +01:00
|
|
|
('Cannot update an entity if all fields are transient. Class [%s] mapped on table [%s]', [ClassName, fTableName]);
|
2019-02-21 20:17:11 +01:00
|
|
|
end;
|
2019-03-05 20:55:37 +01:00
|
|
|
SQL := SQLGenerator.CreateUpdateSQL(fTableName, fMapNonTransientFields, fPrimaryKeyFieldName, fPrimaryKeyOptions);
|
2018-09-25 15:36:53 +02:00
|
|
|
ExecNonQuery(SQL, false);
|
2018-10-23 16:18:34 +02:00
|
|
|
OnAfterUpdate;
|
|
|
|
OnAfterInsertOrUpdate;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.All(const aClass: TMVCActiveRecordClass): TObjectList<TMVCActiveRecord>;
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
|
|
|
begin
|
|
|
|
lAR := aClass.Create;
|
|
|
|
try
|
2018-11-02 21:43:09 +01:00
|
|
|
Result := Select(aClass, lAR.GenerateSelectSQL, []);
|
2018-09-25 15:36:53 +02:00
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-08-02 12:32:23 +02:00
|
|
|
class function TMVCActiveRecordHelper.All<T>: TObjectList<T>;
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
|
|
|
begin
|
|
|
|
lAR := T.Create;
|
|
|
|
try
|
2018-11-02 21:43:09 +01:00
|
|
|
Result := Select<T>(lAR.GenerateSelectSQL, []);
|
2018-09-25 15:36:53 +02:00
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.Where(const aClass: TMVCActiveRecordClass; const SQLWhere: string;
|
|
|
|
const Params: array of Variant): TMVCActiveRecordList;
|
2018-09-28 13:01:46 +02:00
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
Result := Where(aClass, SQLWhere, Params, nil);
|
2018-09-28 13:01:46 +02:00
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
class function TMVCActiveRecord.Where(const aClass: TMVCActiveRecordClass; const SQLWhere: string;
|
|
|
|
const Params: array of Variant; const Connection: TFDConnection): TMVCActiveRecordList;
|
2018-09-27 12:26:50 +02:00
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
|
|
|
begin
|
|
|
|
lAR := aClass.Create;
|
|
|
|
try
|
2019-03-05 20:55:37 +01:00
|
|
|
Result := Select(aClass, lAR.GenerateSelectSQL + SQLWhere, Params, Connection);
|
2018-09-27 12:26:50 +02:00
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-08-02 12:32:23 +02:00
|
|
|
class function TMVCActiveRecordHelper.Where<T>(const SQLWhere: string; const Params: array of Variant): TObjectList<T>;
|
2018-09-25 15:36:53 +02:00
|
|
|
var
|
|
|
|
lAR: TMVCActiveRecord;
|
|
|
|
begin
|
|
|
|
lAR := T.Create;
|
|
|
|
try
|
2019-03-05 20:55:37 +01:00
|
|
|
if SQLWhere.Trim.IsEmpty() or SQLWhere.Trim.StartsWith('/*limit*/') or SQLWhere.Trim.StartsWith('/*sort*/') then
|
2018-09-27 12:26:50 +02:00
|
|
|
begin
|
2018-12-17 00:39:29 +01:00
|
|
|
Result := Select<T>(lAR.GenerateSelectSQL + SQLWhere, Params);
|
2018-09-27 12:26:50 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2018-12-17 00:39:29 +01:00
|
|
|
Result := Select<T>(lAR.GenerateSelectSQL + ' WHERE ' + SQLWhere, Params);
|
2018-09-27 12:26:50 +02:00
|
|
|
end;
|
2018-09-25 15:36:53 +02:00
|
|
|
finally
|
|
|
|
lAR.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
{ PrimaryKeyAttribute }
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
// constructor MVCPrimaryKeyAttribute.Create(const aFieldName: string);
|
|
|
|
// begin
|
|
|
|
// Create(aFieldName, []);
|
|
|
|
// end;
|
|
|
|
//
|
|
|
|
// constructor MVCPrimaryKeyAttribute.Create(const aFieldName: string; const aFieldOptions: TMVCActiveRecordFieldOptions);
|
|
|
|
// begin
|
|
|
|
// inherited Create;
|
|
|
|
// FieldName := aFieldName;
|
|
|
|
// FieldOptions := aFieldOptions;
|
|
|
|
// end;
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
{ TMVCEntitiesRegistry }
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure TMVCEntitiesRegistry.AddEntity(const aURLSegment: string; const aActiveRecordClass: TMVCActiveRecordClass);
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
|
|
|
fEntitiesDict.AddOrSetValue(aURLSegment.ToLower, aActiveRecordClass);
|
|
|
|
end;
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
procedure TMVCEntitiesRegistry.AddEntityProcessor(const aURLSegment: string;
|
|
|
|
const aEntityProcessor: IMVCEntityProcessor);
|
|
|
|
begin
|
|
|
|
fProcessorsDict.Add(aURLSegment, aEntityProcessor);
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
constructor TMVCEntitiesRegistry.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
fEntitiesDict := TDictionary<string, TMVCActiveRecordClass>.Create;
|
2018-10-14 18:23:20 +02:00
|
|
|
fProcessorsDict := TDictionary<string, IMVCEntityProcessor>.Create;
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMVCEntitiesRegistry.Destroy;
|
|
|
|
begin
|
|
|
|
fEntitiesDict.Free;
|
2018-10-14 18:23:20 +02:00
|
|
|
fProcessorsDict.Free;
|
2018-09-25 15:36:53 +02:00
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
function TMVCEntitiesRegistry.FindEntityClassByURLSegment(const aURLSegment: string;
|
|
|
|
out aMVCActiveRecordClass: TMVCActiveRecordClass): Boolean;
|
2018-09-25 15:36:53 +02:00
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
Result := fEntitiesDict.TryGetValue(aURLSegment.ToLower, aMVCActiveRecordClass);
|
2018-10-14 18:23:20 +02:00
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
function TMVCEntitiesRegistry.FindProcessorByURLSegment(const aURLSegment: string;
|
|
|
|
out aMVCEntityProcessor: IMVCEntityProcessor): Boolean;
|
2018-10-14 18:23:20 +02:00
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
Result := fProcessorsDict.TryGetValue(aURLSegment.ToLower, aMVCEntityProcessor);
|
2018-09-25 15:36:53 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ EMVCActiveRecord }
|
|
|
|
|
|
|
|
constructor EMVCActiveRecord.Create(const AMsg: string);
|
|
|
|
begin
|
|
|
|
inherited Create(http_status.BadRequest, AMsg);
|
|
|
|
end;
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
{ EntityActionsAttribute }
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
constructor MVCEntityActionsAttribute.Create(const aEntityAllowedActions: TMVCEntityActions);
|
2018-10-14 18:23:20 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
EntityAllowedActions := aEntityAllowedActions;
|
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
{ TMVCActiveRecordList }
|
|
|
|
|
|
|
|
constructor TMVCActiveRecordList.Create;
|
|
|
|
begin
|
|
|
|
inherited Create(True);
|
|
|
|
end;
|
|
|
|
|
2018-11-02 21:43:09 +01:00
|
|
|
{ TMVCSQLGeneratorRegistry }
|
|
|
|
|
|
|
|
constructor TMVCSQLGeneratorRegistry.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
fSQLGenerators := TDictionary<string, TMVCSQLGeneratorClass>.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
class constructor TMVCSQLGeneratorRegistry.Create;
|
|
|
|
begin
|
2018-11-09 18:11:59 +01:00
|
|
|
cLock := TObject.Create;
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
class destructor TMVCSQLGeneratorRegistry.Destroy;
|
|
|
|
begin
|
2018-11-09 18:11:59 +01:00
|
|
|
cLock.Free;
|
|
|
|
cInstance.Free;
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMVCSQLGeneratorRegistry.Destroy;
|
|
|
|
begin
|
|
|
|
fSQLGenerators.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
function TMVCSQLGeneratorRegistry.GetSQLGenerator(const aBackend: string): TMVCSQLGeneratorClass;
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
|
|
|
if not fSQLGenerators.TryGetValue(aBackend, Result) then
|
|
|
|
begin
|
2019-03-05 20:55:37 +01:00
|
|
|
raise ERQLCompilerNotFound.CreateFmt('SQLGenerator not found for "%s"', [aBackend]);
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-11-24 16:56:21 +01:00
|
|
|
class function TMVCSQLGeneratorRegistry.Instance: TMVCSQLGeneratorRegistry;
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
2018-11-09 18:11:59 +01:00
|
|
|
if not Assigned(cInstance) then
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
2018-11-09 18:11:59 +01:00
|
|
|
TMonitor.Enter(cLock);
|
2018-11-02 21:43:09 +01:00
|
|
|
try
|
2018-11-09 18:11:59 +01:00
|
|
|
if not Assigned(cInstance) then
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
2018-11-09 18:11:59 +01:00
|
|
|
cInstance := TMVCSQLGeneratorRegistry.Create;
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
finally
|
2018-11-09 18:11:59 +01:00
|
|
|
TMonitor.Exit(cLock);
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
end;
|
2018-11-09 18:11:59 +01:00
|
|
|
Result := cInstance;
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCSQLGeneratorRegistry.RegisterSQLGenerator(const aBackend: string;
|
|
|
|
const aRQLBackendClass: TMVCSQLGeneratorClass);
|
|
|
|
begin
|
2018-11-24 16:56:21 +01:00
|
|
|
fSQLGenerators.AddOrSetValue(aBackend, aRQLBackendClass);
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure TMVCSQLGeneratorRegistry.UnRegisterSQLGenerator(const aBackend: string);
|
2018-11-02 21:43:09 +01:00
|
|
|
begin
|
|
|
|
fSQLGenerators.Remove(aBackend);
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TMVCSQLGenerator }
|
|
|
|
|
|
|
|
constructor TMVCSQLGenerator.Create(Mapping: TMVCFieldsMapping);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fMapping := Mapping;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGenerator.GetMapping: TMVCFieldsMapping;
|
|
|
|
begin
|
|
|
|
Result := fMapping;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMVCSQLGenerator.Destroy;
|
|
|
|
begin
|
|
|
|
fCompiler.Free;
|
|
|
|
fRQL2SQL.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGenerator.GetCompiler: TRQLCompiler;
|
|
|
|
begin
|
|
|
|
if fCompiler = nil then
|
|
|
|
begin
|
|
|
|
fCompiler := GetCompilerClass.Create(fMapping);
|
|
|
|
end;
|
|
|
|
Result := fCompiler;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGenerator.GetRQLParser: TRQL2SQL;
|
|
|
|
begin
|
|
|
|
if fRQL2SQL = nil then
|
|
|
|
begin
|
2019-05-09 20:53:52 +02:00
|
|
|
fRQL2SQL := TRQL2SQL.Create; // (20);
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
Result := fRQL2SQL;
|
|
|
|
end;
|
|
|
|
|
2020-01-08 15:30:10 +01:00
|
|
|
function TMVCSQLGenerator.GetSequenceValueSQL(
|
|
|
|
const PKFieldName: String; const SequenceName: String; const Step: Integer = 1): String;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGenerator.HasReturning: Boolean;
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMVCSQLGenerator.HasSequences: Boolean;
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
function TMVCSQLGenerator.TableFieldsDelimited(const Map: TDictionary<TRttiField, string>; const PKFieldName: string;
|
2018-11-02 21:43:09 +01:00
|
|
|
const Delimiter: string): string;
|
|
|
|
var
|
|
|
|
lPair: TPair<TRttiField, string>;
|
|
|
|
begin
|
|
|
|
for lPair in Map do
|
|
|
|
begin
|
2019-11-05 16:57:22 +01:00
|
|
|
if not lPair.Value.IsEmpty then
|
|
|
|
begin
|
2020-01-04 12:53:53 +01:00
|
|
|
Result := Result + lPair.Value + Delimiter;
|
2019-11-05 16:57:22 +01:00
|
|
|
end;
|
2018-11-02 21:43:09 +01:00
|
|
|
end;
|
|
|
|
Result := Copy(Result, 1, Length(Result) - Length(Delimiter));
|
|
|
|
if not PKFieldName.IsEmpty then
|
|
|
|
begin
|
|
|
|
Result := PKFieldName + ',' + Result;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2018-11-09 18:11:59 +01:00
|
|
|
{ TMVCConnectionsRepository.TConnHolder }
|
|
|
|
|
|
|
|
destructor TMVCConnectionsRepository.TConnHolder.Destroy;
|
|
|
|
begin
|
|
|
|
if OwnsConnection then
|
2019-03-18 14:08:34 +01:00
|
|
|
Begin
|
2019-05-09 20:53:52 +02:00
|
|
|
if Connection.Connected then
|
|
|
|
Connection.Connected := false;
|
2018-11-09 18:11:59 +01:00
|
|
|
FreeAndNil(Connection);
|
2019-03-18 14:08:34 +01:00
|
|
|
End;
|
2018-11-09 18:11:59 +01:00
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2020-01-08 15:30:10 +01:00
|
|
|
constructor MVCTableFieldAttribute.Create(const aFieldName: string; const aFieldOptions: TMVCActiveRecordFieldOptions;
|
|
|
|
const aSequenceName: String);
|
2019-02-15 12:21:11 +01:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FieldName := aFieldName;
|
|
|
|
FieldOptions := aFieldOptions;
|
2020-01-08 15:30:10 +01:00
|
|
|
SequenceName := aSequenceName;
|
2019-02-15 12:21:11 +01:00
|
|
|
end;
|
|
|
|
|
2020-01-04 12:53:53 +01:00
|
|
|
{ EMVCActiveRecordNotFound }
|
|
|
|
|
|
|
|
procedure EMVCActiveRecordNotFound.AfterConstruction;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
fHttpErrorCode := http_status.NotFound;
|
|
|
|
end;
|
|
|
|
|
2018-09-25 15:36:53 +02:00
|
|
|
initialization
|
|
|
|
|
|
|
|
gLock := TObject.Create;
|
2018-10-14 18:23:20 +02:00
|
|
|
gCtx := TRttiContext.Create;
|
|
|
|
gCtx.FindType('');
|
2018-09-25 15:36:53 +02:00
|
|
|
|
|
|
|
finalization
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
gCtx.Free;
|
2018-09-25 15:36:53 +02:00
|
|
|
gLock.Free;
|
|
|
|
|
|
|
|
end.
|