Skip to content

Commit

Permalink
Improved error message in case of duplicated con nection name in tmvc…
Browse files Browse the repository at this point in the history
…activerecord
  • Loading branch information
danieleteti committed Jan 7, 2025
1 parent f7a139a commit 85441a6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions sources/MVCFramework.ActiveRecord.pas
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ TMVCActiveRecord = class

class function GetScalar(const SQL: string; const Params: array of Variant): Variant;
class function CurrentConnection: TFDConnection;
class function GetConnectionByName(const ConnectionName: String): TFDConnection;
end;

IMVCUnitOfWork<T: TMVCActiveRecord> = interface
Expand Down Expand Up @@ -778,6 +779,7 @@ TMVCEntitiesRegistry = class(TInterfacedObject, IMVCEntitiesRegistry)
function GetCurrent(const RaiseExceptionIfNotAvailable: Boolean = True): TFDConnection;
function GetCurrentConnectionName(const RaiseExceptionIfNotAvailable: Boolean = False): String;
function GetCurrentBackend: string;
function GetByName(const aName: string): TFDConnection;
procedure SetDefault;
end;

Expand Down Expand Up @@ -1099,6 +1101,10 @@ procedure TMVCConnectionsRepository.AddConnection(const aName: string; const aCo

fMREW.BeginWrite;
try
if fConnectionsDict.ContainsKey(lConnKeyName) then
begin
raise EMVCActiveRecord.CreateFmt('Cannot add another connection with the same name for the same thread. Duplicated connection name is "%s"', [lName]);
end;
lConnHolder := TConnHolder.Create;
lConnHolder.Connection := aConnection;
lConnHolder.OwnsConnection := aOwns;
Expand Down Expand Up @@ -2288,6 +2294,11 @@ function TMVCActiveRecord.GetConnection: TFDConnection;
Result := fConn;
end;

class function TMVCActiveRecord.GetConnectionByName(const ConnectionName: String): TFDConnection;
begin
Result := ActiveRecordConnectionsRegistry.GetByName(ConnectionName);
end;

function TMVCActiveRecord.GetCustomTableName: String;
begin
Result := '';
Expand Down
25 changes: 25 additions & 0 deletions sources/MVCFramework.DataSet.Utils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ TDataSetHelper = class helper for TDataSet
function AsJSONArrayString: string; deprecated 'Use AsJSONArray';
function AsJSONObject(FieldNameCase: TMVCNameCase = TMVCNameCase.ncUseDefault;
const IgnoredFields: TArray<string> = nil): string;
function AsJDOJSONObject(FieldNameCase: TMVCNameCase = TMVCNameCase.ncUseDefault; const IgnoredFields: TArray<string> = nil): TJDOJsonObject;
function AsJSONObjectString: string; deprecated 'Use AsJSONObject';
procedure LoadFromJSONObject(const JSONObject: TJSONObject;
const FieldNameCase: TMVCNameCase); overload;
Expand Down Expand Up @@ -241,6 +242,30 @@ function TDataSetHelper.AsJDOJSONArray(FieldNameCase
end;
end;

function TDataSetHelper.AsJDOJSONObject(FieldNameCase: TMVCNameCase; const IgnoredFields: TArray<string>): TJDOJsonObject;
var
lSerializer: TMVCJsonDataObjectsSerializer;
lDSFields: TMVCDataSetFields;
begin
lSerializer := TMVCJsonDataObjectsSerializer.Create;
try
Result := TJDOJsonObject.Create;
try
lDSFields := lSerializer.GetDataSetFields(Self, TMVCIgnoredList(IgnoredFields), FieldNameCase);
try
lSerializer.DataSetToJsonObject(Self, Result, FieldNameCase, TMVCIgnoredList(IgnoredFields), lDSFields);
finally
lDSFields.Free;
end;
except
Result.Free;
raise;
end;
finally
lSerializer.Free;
end;
end;

function TDataSetHelper.AsJSONArrayOfValues: TJDOJsonArray;
var
lSerializer: TMVCJsonDataObjectsSerializer;
Expand Down

0 comments on commit 85441a6

Please sign in to comment.