#1 2022-03-05 19:21:24

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 392

Exception with empty array parameter?

Following an advice I read somewhere in the forum, not to have orm endpoint accessible on Thttpserver in mormot2, I am using interface methods to access orm functions (to include authentication in future) like the following:

function TwbService.OrmRetrieveListJson1(Table: TOrmClass; const SqlWhere,  aCustomFieldsCsv: RawUtf8; aForceAjax: boolean): RawJson;
begin result:=server.Orm.RetrieveListJson(Table,SqlWhere,aCustomFieldsCsv,aForceAjax); end;

function TwbService.OrmRetrieveListJson2(Table: TOrmClass;  const FormatSqlWhere: RawUtf8; const BoundsSqlWhere: array of const;  const aCustomFieldsCsv: RawUtf8; aForceAjax: boolean): RawJson;
begin result:=server.Orm.RetrieveListJson(Table,FormatSqlWhere,BoundsSqlWhere,aCustomFieldsCsv,aForceAjax); end;

I noticed that the following gives exception:

service.OrmRetrieveListJson2(TmyORMclass,'',[],'*');

The following works OK:

service.OrmRetrieveListJson1(TmyORMclass,'','*');

Also the following works OK:

service.OrmRetrieveListJson2(TmyORMclass,'RowID>?',[0],'*');

Is the empty array of const causing the problem?

Offline

#2 2022-03-05 21:43:09

tbo
Member
Registered: 2015-04-20
Posts: 335

Re: Exception with empty array parameter?

Do not send SQL, but call a function with the required functionality.

TVGSCustomServiceObject = class(TInjectableObjectRest, ICustomService) ... end;

TArticleService = class(TVGSCustomServiceObject, IArticle)
public
  procedure GetAllItems(pmActiveStates: TActiveStates; out pmoList: TOrmArticleObjArray);
end;

procedure TArticleService.GetAllItems(pmActiveStates: TActiveStates; out pmoList: TOrmArticleObjArray);
var
  restServer: TVGSRestServerDB;
begin
  restServer := GetDBDataRestServer;
  if restServer <> Nil then
    restServer.Server.RetrieveListObjArray(pmoList, TOrmArticle, ActiveStatesToSqlWhere(pmActiveStates), []);
end;

For the service server, you can take TRestServerFullMemory and use the available authentication.

FRestAdminServer := TRestServerFullMemory.Create(MyModel, MyUserAuthentication);
FRestAdminServer.ServiceDefine(TArticleService, [IArticle], sicShared);
FRestAdminServer.Server.CreateMissingTables(0, [itoNoAutoCreateUsers]);
FRestAdminServer.Server.Add(MyOwnAuthUser, True);

Then call following in the client:

function TdmDB.CreateArticleList: TOrmArticleList;
var
  service: IArticle;
  dataArr: TOrmArticleObjArray;
begin
  if not dmDB.VGServerRestHttp.Resolve(IArticle, service) then Exit(Nil); //=>

  service.GetAllItems([asActive], dataArr);
  Result := TOrmArticleList.Create(dataArr);
end;

With best regards
Thomas

Last edited by tbo (2022-03-05 21:47:03)

Offline

#3 2022-03-05 23:25:40

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 392

Re: Exception with empty array parameter?

Thank you a lot for your response.
To be honest, after posting this thread I was wondering if array of const could be allowed  to be used.
I was coping-pasting orm methods and I found it working but the empty brackets did not. Probably it was working by chance

Do we need the "restServer := GetDBDataRestServer;" I found that the Server property from TInjectableObjectRest is working excellent
The TOrmArticleObjArray how it it defined? for the moment, I am using Json arrays to get data as they are more compatible with mobile devices.

I removed Tormclass procedure arguments as I am not sure if I can pass them safely. I am using tormclass.sqltable to transform it as a utf8string argument and back with "server.Model.Table[Table]" in order to use "generic" orm functions for multiple Tormclasses

My main problem now is how to pass and get generic Torm instances that they are usually passed as values in orm functions, e.g. OrmRetrieve(aID: TID; Value: TOrm; ForUpdate: boolean): boolean;
Probably the GetJsonValues can prepare a utf8string value and fillprepare can use it in the other side but I have not yet managed to use them successfully.
I have seen tdocvariant functions with variant as input/output but I have not find an example and I am not sure how to use them

probably a way to have custom authentication over the ORM interface is the most easy way to deal with the server, but as I want to use it from mobile and web clients json serialization/deserialization is the only way I am thinking of.

Offline

#4 2022-03-07 12:41:10

tbo
Member
Registered: 2015-04-20
Posts: 335

Re: Exception with empty array parameter?

dcoun wrote:

Do we need the "restServer := GetDBDataRestServer;"

Yes, in a multi-tenant version (Infos in this forum post):

function TVGSCustomServiceObject.GetDBDataRestServer: TVGSRestServerDB;
begin
  with TVGSRestAdminServer(Server) do
    Result := RestServerPool.FindRestServer(GetDBDataRestServerID(ServiceRunningContext.Request.Session));
end;

The TOrmArticleObjArray how it it defined?

TOrmArticleObjArray = array of TOrmArticle;

I have seen tdocvariant functions with variant as input/output but I have not find an example and I am not sure how to use them

Take a look at the MVC Blog example as a starting point: MVC Blog example source

With best regards
Thomas

Offline

#5 2022-03-07 14:44:22

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 392

Re: Exception with empty array parameter?

That was the post about having interface methods and not to expose ORM functions that I have read in the past and I need to revisit.
MVC example was not working in the past but I noticed some commits last days and I revisit it.
Thank you a lot. I will check it and I will return

Last edited by dcoun (2022-03-07 14:47:02)

Offline

Board footer

Powered by FluxBB