#1 2015-04-11 07:09:13

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Simple Interface based server with database !

Hi all;

I develop mormot server based on document and demos but I can't insert data to table with interface system;

thanks;

// interfaces.pas

unit Interfaces;

interface
uses SynCommons;
type
  INote = interface(IInvokable)
  ['{4C284438-8107-4237-9932-F844472A0369}']
    function addNote(Title,Body:RawUtf8):Boolean;
  end;

implementation

uses mORMot;

initialization
  TInterfaceFactory.RegisterInterfaces([TypeInfo(INote)]);
end.

// models.pas

unit Models;

interface

uses mOrmot,SynCommons;

type
  TSQLNote=class(TSQLRecord)
  private
    fTitle,fBody:RawUTF8;
  public
    function check:Boolean;
  published
    property Title:RawUTF8 read fTitle write fTitle;
    property Body:RawUTF8 read fBody write fBody;
  end;

  function CreateModel:TSQLModel;

implementation

  function CreateModel:TSQLModel;
  begin
    Result:=TSQLModel.Create([TSQLNote]);
  end;

{ TSQLNote }

function TSQLNote.check: Boolean;
begin
  Result:=True;
end;

end.

// main.pas

unit Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,mORMot,mORMotHttpServer,MormotSQLite3,SynCommons,Interfaces,SynSQLite3Static;

type

  TServiceNote=class(TInterfacedObject,INote)
  public
    function addNote(Title: RawUTF8; Body: RawUTF8): Boolean;
  end;

  TForm2 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    aModel: TSQLModel;
    aServer: TSQLRestServer;
    aHTTPServer: TSQLHttpServer;
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

uses Models;

procedure TForm2.FormCreate(Sender: TObject);
begin
  aModel:=CreateModel;
  aServer:=TSQLRESTServerDB.Create(aModel,'Database.db');
  aServer.ServiceDefine(TServiceNote,[INote],sicShared);
  aHTTPServer:=TSQLHttpServer.Create('8096',aServer,'+',useHttpApiRegisteringURI);
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  aModel.Free; aServer.Free;  aHTTPServer.Free;
end;

{ TServiceDemo }

function TServiceNote.addNote(Title, Body: RawUTF8): Boolean;
begin

end;

end.

Last edited by mohsenti (2015-04-11 08:18:52)

Offline

#2 2015-04-11 08:28:49

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

Re: Simple Interface based server with database !

Your code is not finished.

Offline

#3 2015-04-11 09:39:40

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: Simple Interface based server with database !

I don't know continuation and how to work with DB in interface based system.

Offline

#4 2015-04-12 03:02:54

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Simple Interface based server with database !

Do you mean something like:

myNode := TSQLNote.Create;
myNode.Title := 'no title';
aServer.Add(myNote);

?


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#5 2015-04-12 05:36:28

mohsenti
Member
Registered: 2015-04-11
Posts: 72

Re: Simple Interface based server with database !

edwinsn wrote:

Do you mean something like:

myNode := TSQLNote.Create;
myNode.Title := 'no title';
aServer.Add(myNote);

?

Yes but can't access aServer in TServiceNote

function TServiceNote.addNote(Title, Body: RawUTF8): Boolean;
begin
  // can't access aServer here
end;

Offline

#6 2015-04-12 06:26:39

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Simple Interface based server with database !

If your IServiceNote is supposed to be accessible in the clients, you can add a field of type TSQLRESTServerDB to TServicenote, and assign its value in the constructor, then in your AddNote() method you'll be able to access your REST DB SERVER.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#7 2015-04-12 06:27:34

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

Re: Simple Interface based server with database !

Use ServiceContext: TServiceRunningContext threadvar or the CurrentServiceContext function (from a package).

See http://synopse.info/files/html/api-1.18 … ICECONTEXT

Offline

#8 2015-04-12 09:37:09

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Simple Interface based server with database !

Oops! Sorry for provided the non-standard method, that what came from my mind when I read your question tongue


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#9 2015-05-31 14:20:12

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Simple Interface based server with database !

ab wrote:

Use ServiceContext: TServiceRunningContext threadvar or the CurrentServiceContext function (from a package).

See http://synopse.info/files/html/api-1.18 … ICECONTEXT

Hi Arnaud,

I'm using the latest rev. committed today, when being accessed on the server side, ServiceContext.Request is always nil. Any comments? Thanks.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

Board footer

Powered by FluxBB