#1 2014-06-13 10:45:54

pszawa
Member
Registered: 2014-06-05
Posts: 10

Local and remote database

Hi,

my program is available in two versions - with a local database and remote.

In the main application, it would look something like this:

{$IFDEF DBHTTP}
 _client := TSQLHttpClient.Create('server.com','7878',_model);
{$ELSE}
_server := TSQLRestServerDB.Create(_model, _pathToDb);
_server.ServiceRegister(TMyServices, [TypeInfo(IMyServices)], sicClientDriven);

_client := TSQLRestClientDB.Create(_model, nil, _server.DB, TSQLRestServerDB);
 {$ENDIF}

_client.ServiceRegisterClientDriven(TypeInfo(IMyServices), _myServices);
_model.Owner := _client;

only that there is a problem when I try to register IMyServices:

---------------------------
Debugger Exception Notification
---------------------------
Project App.exe raised exception class ESQLite3Exception with message 'near "[]": syntax error'.
---------------------------
Break   Continue   Help   
---------------------------

:76e6c42d KERNELBASE.RaiseException + 0x58
:006c26e6 HookedRaiseException + $66
SynSQLite3.sqlite3_check(95735112,1)
SynSQLite3.TSQLRequest.Prepare(95735112,'[]')
SynSQLite3.TSQLRequest.ExecuteAll(95735112,'[]')
SynSQLite3.TSQLDatabase.ExecuteAll('[]')
mORMotSQLite3.TSQLRestServerDB.EngineExecuteAll('[]')
mORMot.TSQLRestServerURIContext.ExecuteORMWrite
mORMot.TSQLRestServerURIContext.Execute(execORMWrite)
mORMot.TSQLRestServer.URI($18F683)
mORMotSQLite3.TSQLRestClientDB.InternalURI($18F683)
mORMot.TSQLRestClientURI.URI('root/MyServices._contract_','POST',$18F75C {''},$18F758 {''},$18F760 {'[]'})
mORMot.TServiceFactoryClient.InternalInvoke('_contract_','',$18F7DC {''},$18F7E0 {''},nil,nil)
mORMot.TServiceFactoryClient.Create($54AE4A0,$1009B88,sicClientDriven,'')
mORMot.TServiceContainer.AddInterface($1009B88,sicClientDriven,'')
mORMot.TSQLRestClientURI.ServiceRegister($1009B88,sicClientDriven,'')
mORMot.TSQLRestClientURI.ServiceRegisterClientDriven($1009B88,(no value),'')

How do I resolve this issue? I do not want to run a local HTTP server, because this is a drop in performance were unnecessary.

Offline

#2 2014-06-13 10:53:30

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Local and remote database

Ensure you downloaded the latest framework 1.18 source code and sqlite3*.obj files as stated by http://synopse.info/fossil/wiki?name=Get+the+source ?

For services to be available on the client side, you need to set an user on the client side, via a call to SetUser().
Please check the documentation and the samples.

Offline

#3 2014-06-13 11:49:34

pszawa
Member
Registered: 2014-06-05
Posts: 10

Re: Local and remote database

Yes, I have version 1.18

I checked the samples and on the basis of samples implemented the services. However, in the samples is always communication Client - HTTPClient - HTTPServer - Server and I would like to be able to skip HTTP.
The connection to the remote server works very well, but the problem I have when I want to do a local version.

Example project:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus,
  Vcl.StdCtrls, cxButtons, SynCommons, mORMotdb, SynDB, mORMot, mORMotSqlite3, SynSQLite3Static;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure Run;
  public
    { Public declarations }
  end;

  IMyServices = interface(IInvokable)
    ['{9A60C8ED-CEB2-4E09-87D4-4A16F496E5FE}']
    function Add(n1,n2: integer): integer;
  end;

  TMyServices = class(TInterfacedObject, IMyServices)
    public
        function Add(n1,n2: integer): integer;
  end;
var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TMyServices }

function TMyServices.Add(n1, n2: integer): integer;
begin
    Result := n1+n2;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    run;
end;

procedure TForm1.Run;
var
    server: TSQLRestServerDB;
    services: IMyServices;
    client: TSQLRestClientDB;
    model: TSQLModel;
begin
    model := TSQLModel.Create([]);
    server := TSQLRestServerDB.Create(model, 'db.db');
    server.ServiceRegister(TMyServices, [TypeInfo(IMyServices)], sicClientDriven);
    client := TSQLRestClientDB.Create(model, nil, server.DB, TSQLRestServerDB);
    client.ServiceRegisterClientDriven(TypeInfo(IMyServices),services); // booom
    client.SetUser('User','synopse');
end;

end.

Offline

#4 2014-06-13 13:37:45

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Local and remote database

Oups...

In fact, I was somewhat wrong.

You are using TSQLRestClientDB wrongly.
TSQLRestClientDB will host its own TSQLRestServerDB instance.
So your "server" variable is just not available.

Here is the correct way to do it:

var
    services: IMyServices;
    client: TSQLRestClientDB;
    model: TSQLModel;
begin
    model := TSQLModel.Create([]);
    client := TSQLRestClientDB.Create(model, nil, 'test.db3', TSQLRestServerDB, true); // true=handle authentication
    client.Server.CreateMissingTables; // we need User and Group tables for authentication
    client.Server.ServiceRegister(TMyServices, [TypeInfo(IMyServices)], sicClientDriven);
    client.SetUser('User','synopse');
    client.ServiceRegisterClientDriven(TypeInfo(IMyServices),services); // no booom
end;

Offline

#5 2014-06-13 14:53:53

pszawa
Member
Registered: 2014-06-05
Posts: 10

Re: Local and remote database

It's super, that is, on the client side I do not need a server.
It's even better than I thought.

Thank you for your help.
I am very pleased that when I started my project I chose mORMot. smile

Offline

#6 2014-06-13 22:31:58

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Local and remote database

You are welcome!

Thanks for your feedback.
smile

Offline

Board footer

Powered by FluxBB