Skip to content

Commit

Permalink
Added TMVCActiveRecord.TryGetSQLQuery<T> and TMVCActiveRecord.TryGetR…
Browse files Browse the repository at this point in the history
…QLQuery<T>
  • Loading branch information
danieleteti committed Oct 17, 2023
1 parent 62a1344 commit 7f6c3e2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
32 changes: 31 additions & 1 deletion sources/MVCFramework.ActiveRecord.pas
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,11 @@ TMVCActiveRecordHelper = class helper for TMVCActiveRecord
class function CountRQLByNamedQuery<T: TMVCActiveRecord, constructor>(
const QueryName: string;
const Params: array of const): Int64;

class function TryGetSQLQuery<T: TMVCActiveRecord, constructor>(
const QueryName: String;
out NamedSQLQuery: TSQLQueryWithName): Boolean; overload;
class function TryGetRQLQuery<T: TMVCActiveRecord, constructor>(
const QueryName: String; out NamedRQLQuery: TRQLQueryWithName): Boolean;
{ RTTI }
class function CreateMVCActiveRecord<T: TMVCActiveRecord>(AQualifiedClassName: string; const AParams: TArray<TValue> = nil): T;
end;
Expand Down Expand Up @@ -2125,6 +2129,32 @@ class function TMVCActiveRecordHelper.DeleteRQLByNamedQuery<T>(
end;
end;

class function TMVCActiveRecordHelper.TryGetSQLQuery<T>(
const QueryName: String; out NamedSQLQuery: TSQLQueryWithName): Boolean;
var
lT: T;
begin
lT := T.Create;
try
Result := lT.FindSQLQueryByName(QueryName, NamedSQLQuery);
finally
lT.Free;
end;
end;

class function TMVCActiveRecordHelper.TryGetRQLQuery<T>(
const QueryName: String; out NamedRQLQuery: TRQLQueryWithName): Boolean;
var
lT: T;
begin
lT := T.Create;
try
Result := lT.FindRQLQueryByName(QueryName, NamedRQLQuery);
finally
lT.Free;
end;
end;

class function TMVCActiveRecord.CurrentConnection: TFDConnection;
begin
Result := ActiveRecordConnectionsRegistry.GetCurrent;
Expand Down
19 changes: 19 additions & 0 deletions unittests/general/Several/ActiveRecordTestsU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ TTestActiveRecordBase = class(TObject)
[Test]
procedure TestNamedQuerySQL;
[Test]
procedure TestTryGetNamedQuery;
[Test]
procedure TestNamedQuerySQLByBackEnd;
[Test]
procedure TestStore;
Expand Down Expand Up @@ -1892,6 +1894,23 @@ procedure TTestActiveRecordBase.TestStore;

end;

procedure TTestActiveRecordBase.TestTryGetNamedQuery;
var
lTmpSQLQueryWithName: TSQLQueryWithName;
lTmpRQLQueryWithName: TRQLQueryWithName;
begin
Assert.IsTrue(TMVCActiveRecord.TryGetSQLQuery<TCustomer>('ByTwoCities', lTmpSQLQueryWithName));
Assert.AreEqual('ByTwoCities', lTmpSQLQueryWithName.Name);
Assert.IsNotEmpty(lTmpSQLQueryWithName.SQLText);
Assert.IsEmpty(lTmpSQLQueryWithName.BackEnd);
Assert.IsFalse(TMVCActiveRecord.TryGetSQLQuery<TCustomer>('DO_NOT_EXISTS', lTmpSQLQueryWithName));

Assert.IsTrue(TMVCActiveRecord.TryGetRQLQuery<TCustomer>('CityRomeOrLondon', lTmpRQLQueryWithName));
Assert.AreEqual('CityRomeOrLondon', lTmpRQLQueryWithName.Name);
Assert.IsNotEmpty(lTmpRQLQueryWithName.RQLText);
Assert.IsFalse(TMVCActiveRecord.TryGetRQLQuery<TCustomer>('DO_NOT_EXISTS', lTmpRQLQueryWithName));
end;

procedure TTestActiveRecordBase.TestUpdateIfNotFound;
var
lCustomer: TCustomer;
Expand Down

0 comments on commit 7f6c3e2

Please sign in to comment.