Skip to content

Commit

Permalink
Aligned sample data database for activerecorddb
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Apr 22, 2024
1 parent bdfb74a commit 2559a00
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 51 deletions.
66 changes: 32 additions & 34 deletions samples/activerecord_showcase/MainFormU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -847,46 +847,44 @@ procedure TMainForm.btnMultiThreadingClick(Sender: TObject);

lConnParams := FDConnection1.Params.Text;
lProc := procedure
var
lConn: TFDConnection;
lCustomer: TCustomer;
I: Integer;
begin
lConn := TFDConnection.Create(nil);
try
lConn.ConnectionDefName := CON_DEF_NAME;
ActiveRecordConnectionsRegistry.AddDefaultConnection(lConn, True);
lConn.Params.Text := lConnParams;
lConn.Open;
for I := 1 to 30 do
begin
lCustomer := TCustomer.Create;
try
lCustomer.Code := Format('%5.5d', [TThread.CurrentThread.ThreadID, I]);
lCustomer.City := Cities[Random(high(Cities) + 1)];
lCustomer.CompanyName :=
Format('%s %s %s', [lCustomer.City, Stuff[Random(high(Stuff) + 1)],
CompanySuffix[Random(high(CompanySuffix) + 1)]]);
lCustomer.Note := lCustomer.CompanyName + ' is from ' + lCustomer.City;
lCustomer.Insert;
finally
lCustomer.Free;
end;
end;
finally
ActiveRecordConnectionsRegistry.RemoveDefaultConnection;
end;
end;

lTasks := [TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
var
lCustomer: TCustomer;
I: Integer;
begin
ActiveRecordConnectionsRegistry.AddDefaultConnection(CON_DEF_NAME);
try
lCustomer := TCustomer.Create;
try
for I := 1 to 50 do
begin
lCustomer.ID.Clear;
lCustomer.Code := Format('%5.5d', [TThread.CurrentThread.ThreadID, I]);
lCustomer.City := Cities[Random(high(Cities) + 1)];
lCustomer.CompanyName :=
Format('%s %s %s', [lCustomer.City, Stuff[Random(high(Stuff) + 1)],
CompanySuffix[Random(high(CompanySuffix) + 1)]]);
lCustomer.Note := lCustomer.CompanyName + ' is from ' + lCustomer.City;
lCustomer.Insert;
end;
finally
lCustomer.Free;
end;
finally
ActiveRecordConnectionsRegistry.RemoveDefaultConnection;
end;
end;

lTasks := [
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc),
TTask.Run(lProc)];
TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc), TTask.Run(lProc)
];
TTask.WaitForAll(lTasks);

ShowMessage('Just inserted ' + TMVCActiveRecord.Count(TCustomer,
'in(City,["Rome","New York","London","Melbourne","Berlin"])').ToString + ' records');
'in(City,["Rome","New York","London","Melbourne","Berlin"])').ToString + ' records by ' + Length(lTasks).ToString + ' threads');
end;

procedure TMainForm.btnNamedQueryClick(Sender: TObject);
Expand Down
Binary file modified samples/data/ACTIVERECORDDB.FDB
Binary file not shown.
Binary file modified samples/data/activerecorddb.db
Binary file not shown.
1 change: 1 addition & 0 deletions samples/data/activerecorddb_firebird_script.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CREATE TABLE customers (
description varchar(200),
city varchar(200),
rating INTEGER,
last_contact_timestamp timestamp,
NOTE BLOB SUB_TYPE 1,
CONSTRAINT customers_pk PRIMARY KEY (id)
);
Expand Down
3 changes: 2 additions & 1 deletion samples/data/activerecorddb_postgresql_script.sql
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ CREATE TABLE public.customers (
description character varying(200),
city character varying(200),
note text,
rating integer
rating integer,
last_contact_timestamp timestamptz NULL
);

ALTER TABLE public.customers OWNER TO postgres;
Expand Down
41 changes: 25 additions & 16 deletions sources/MVCFramework.RESTAdapter.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ***************************************************************************
// ***************************************************************************
//
// Delphi MVC Framework
//
Expand Down Expand Up @@ -155,19 +155,9 @@ TAsynchRequest = class(TInterfacedObject, IAsynchRequest)
implementation

uses
// ObjectsMappers,
JsonDataObjects,
MVCFramework.Serializer.Commons,
MVCFramework.Serializer.Defaults,

{$IFDEF SYSTEMJSON}
System.JSON,

{$ELSE}
Data.DBXJSON,
Data.SqlExpr,
DBXCommon,

{$ENDIF}
MVCFramework.Rtti.Utils,
MVCFramework.DuckTyping,
Generics.Collections,
Expand Down Expand Up @@ -274,6 +264,16 @@ procedure TRESTAdapter<T>.DoInvoke(aMethod: TRttiMethod; const aArgs: TArray<TVa
lResp := fRESTClient.Post(lURL, lBody);
httpDELETE:
lResp := fRESTClient.Delete(lURL);
httpPATCH:
lResp := fRESTClient.Patch(lURL, lBody);
httpOPTIONS:
lResp := fRESTClient.Options(lURL);
httpHEAD:
lResp := fRESTClient.Head(lURL);
else
begin
raise Exception.Create('Error Message');
end;
end;

// if is a procedure no need a return type
Expand Down Expand Up @@ -347,8 +347,12 @@ function TRESTAdapter<T>.GetURL(aMethod: TRttiMethod; const aArgs: TArray<TValue
lURLDict := TDictionary<string, string>.Create;
try
for lSplit in lSplitUrl do
begin
if not lSplit.IsEmpty then
begin
lURLDict.Add(lSplit, lSplit);
end;
end;
lParameters := aMethod.GetParameters;
// lArg := aArgs[I+1] because
// aArgs RTTI for the arguments of the interface method that has been called. The first argument (located at index 0) represents the interface instance itself.
Expand All @@ -362,12 +366,17 @@ function TRESTAdapter<T>.GetURL(aMethod: TRttiMethod; const aArgs: TArray<TValue
end;

for lSplit in lSplitUrl do
begin
if not lSplit.IsEmpty then
begin
Result := Result + URL_SEPARATOR + lURLDict[lSplit];
end;
end;

if lURL.EndsWith(URL_SEPARATOR) and not (Result.EndsWith(URL_SEPARATOR)) then
begin
Result := Result + URL_SEPARATOR;

end;
finally
lURLDict.Free;
end;
Expand All @@ -387,12 +396,12 @@ procedure TRESTAdapter<T>.MapResult(aResp: IMVCRESTResponse; aMethod: TRttiMetho
GetDefaultSerializer.DeserializeCollection(aResp.Content, aResult.AsObject, lAttrListOf.Value);
end
// JSONValue
else if aRttiType.AsInstance.MetaclassType.InheritsFrom(TJSONValue) then
else if aRttiType.AsInstance.MetaclassType.InheritsFrom(TJsonBaseObject) then
begin
aResult := TJSONObject.ParseJSONValue(aResp.Content);
// Object
aResult := TJsonBaseObject.Parse(aResp.Content);
end
else
// Object
begin
aResult := TRttiUtils.CreateObject(aRttiType.QualifiedName);
GetDefaultSerializer.DeserializeObject(aResp.Content, aResult.AsObject);
Expand Down

0 comments on commit 2559a00

Please sign in to comment.