#1 2020-06-10 20:59:59

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Using Lazarus DBGrid with mORMot.

Hello people,
I seek a little code or sample illustrating a process of displaying a result of a Query ( SQL statement ) in a DBGrid using Lazarus/Fpc and mORMot.
I do not see any *LCL* unit that bind mORMot data.

Thank you.

Offline

#2 2020-06-11 06:52:56

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

Re: Using Lazarus DBGrid with mORMot.

Not yet...
Any input is welcome!

Offline

#3 2020-06-11 09:06:21

AOG
Member
Registered: 2014-02-24
Posts: 490

Re: Using Lazarus DBGrid with mORMot.

I am using the mORMot for filling DBGrids with Lazarus.
The TBufDataset is very suitable. It is used in SynDBMidasVCL.
Works very well.

ProjectDataset:=TSQLPropsToClientDataSet(Self,TSQLProject.RecordProps);
ProjectDataset.Active:=False;
ProjectDataset.Fields.Clear;
ProjectDataset.CreateDataset;
ProjectDataset.Active:=True;
DataSourceProject.DataSet:=ProjectDataset; //<----- this datasource is connected with a DBGrid
ProjectDataset.IndexFieldNames:='Code';
with TSQLProject.RecordProps do s:='ID,'+CSVFromFieldBits(FieldBitsFromExcludingCSV('Summary'));
Project := TSQLProject.CreateAndFillPrepare(DataBase,'',s);
T:=Project.FillTable;
if Assigned(T) then
begin
    ProjectDataset.DisableControls;
    ToClientDataSet(TClientDataSet(ProjectDataset),T);
    ProjectDataset.First;
    ProjectDataset.EnableControls;
end;
Project.Free;
ProjectDataset.BeforeScroll:=@DatasetBeforeScroll;
ProjectDataset.AfterScroll:=@DatasetAfterScroll;

Offline

#4 2020-06-11 15:47:51

ttomas
Member
Registered: 2013-03-08
Posts: 135

Re: Using Lazarus DBGrid with mORMot.

AOG wrote:

I am using the mORMot for filling DBGrids with Lazarus.
The TBufDataset is very suitable. It is used in SynDBMidasVCL.
Works very well.

@AOG I can't compile trunk mORMot SynDBMidasVCL.pas with Lazarus 2fix, fpc 3.2fix. Do you have custom version, can you upload your version. SynDBMidasVCL use SynDBVCL and first error is in PSGetTableName, function GetTableNameFromSQL come from dbcommon Delphi only, I change to:
result := {$ifdef fpc}'' {$else} GetTableNameFromSQL(fCommandText){$endif};
Next error is in SynDBMidasVCL line 281 function From don't exist.
Any hint how you use dataset in Lazarus.

Offline

#5 2020-06-11 17:46:57

AOG
Member
Registered: 2014-02-24
Posts: 490

Re: Using Lazarus DBGrid with mORMot.

Here you go.
https://github.com/LongDirtyAnimAlf/mOR … LExtra.pas
Please feel free to use and ask any question.

I will take a closer look, but AFAIK all the rest is vanilla mORMot latest of a couple of weeks ago. Compiled with FPC fixes 3.2

Last edited by AOG (2020-06-11 17:52:19)

Offline

#6 2020-06-11 18:43:31

ttomas
Member
Registered: 2013-03-08
Posts: 135

Re: Using Lazarus DBGrid with mORMot.

Thanks AOG, but this unit is not replacement for SynDBMidasVCL I need dataset from SynDB server to execute any query in existing not ORM Firebird DB.

Offline

#7 2020-06-11 19:09:11

AOG
Member
Registered: 2014-02-24
Posts: 490

Re: Using Lazarus DBGrid with mORMot.

Project := TSQLProject.CreateAndFillPrepare(DataBase,'Place your SQL here !',s);
Is the above not suitable ?

Offline

#8 2020-06-11 21:42:35

ttomas
Member
Registered: 2013-03-08
Posts: 135

Re: Using Lazarus DBGrid with mORMot.

AOG wrote:

Is the above not suitable ?

Not suitable this time. TSQLProject is TSQLRecord descendant, I don't use ORM in this db, it is existing ERP database. I use TSQLDBServerSockets from SynDBRemote on server side and TSQLDBSocketConnectionProperties on client. Now I load data in T..ObjectDynArray and display data in VirtualStringTree like a DBGrid. Works well and fast but need a lot of boilerplate code. You define dataset fields from TSQLRecord props. I will try from data in ISQLDBRows interface.

Offline

#9 2020-06-11 23:31:55

ttomas
Member
Registered: 2013-03-08
Posts: 135

Re: Using Lazarus DBGrid with mORMot.

@ab
I just investigate SynDBMidasVCL and find a bug for fpc to work.
{$endif FPC} in line 193 must be moved to line 226. First 3 ToClientDataSet functions are Delphi midas dependent. Also exclude add {$ifndef fpc} in implementation for first 3. Last 2 functions works fine with fpc and TBufDataset.

Offline

#10 2020-08-08 21:49:59

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Re: Using Lazarus DBGrid with mORMot.

ttomas wrote:

@ab
I just investigate SynDBMidasVCL and find a bug for fpc to work.
{$endif FPC} in line 193 must be moved to line 226. First 3 ToClientDataSet functions are Delphi midas dependent. Also exclude add {$ifndef fpc} in implementation for first 3. Last 2 functions works fine with fpc and TBufDataset.

hello.
Could you provide a unit complete source code for this.
I Urge to use it in my lazarus project.

I use this code :

procedure TForm1.Button1Click(Sender: TObject);
begin
  if ds1.DataSet <> nil then
    ds1.DataSet.Free;
  //
  if f_Prox <> nil then
    FreeAndNil(f_Prox);
  //
  f_Prox := TSQLDBWinHTTPConnectionProperties.Create('127.0.0.1:'+edtPort.Text, 'Medec1',
                   'user', 'pass');
  //
  if f_Prox = nil then
    raise Exception.Create(' httpConnection not !!! ');
  //


  //
  stmt := f_Prox.NewThreadSafeStatement;
  try
    stmt.Execute('select * from uc_fzchs',True);
    //ds1.DataSet := ToClientDataSet(self,stmt)
    ds1.DataSet :=  SQLProToClientDataSet(Self, f_Prox); // ToDataSet(self,stmt);   // in this stage it does not even compile.
  finally
    FreeAndNil(stmt);
    FreeAndNil(f_Prox);
  end;

end;     

Offline

#11 2020-08-09 06:25:37

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

Re: Using Lazarus DBGrid with mORMot.

Use local interface variables, not class variables.
Then you don't need to use Free.
Ensure you release the stmt first by explicitely call stmt := nil.
Please check the docs.

Offline

#12 2020-08-10 10:38:08

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Re: Using Lazarus DBGrid with mORMot.

ab wrote:

Use local interface variables, not class variables.
Then you don't need to use Free.
Ensure you release the stmt first by explicitely call stmt := nil.
Please check the docs.

Thanks, but could you recommande me an exemple or providence a portion of code to just begin
Thanks

Offline

#13 2020-08-10 12:39:41

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

Re: Using Lazarus DBGrid with mORMot.

Offline

#14 2020-08-11 08:31:55

ttomas
Member
Registered: 2013-03-08
Posts: 135

Re: Using Lazarus DBGrid with mORMot.

Bsaidus wrote:

hello.
Could you provide a unit complete source code for this.
I Urge to use it in my lazarus project.

Already merged in trunk https://github.com/synopse/mORMot/pull/327
This is my code how I open/create TBufDataset

function TDBConn.OpenAttOpt(aId: integer): TBufDataset;
var Stmt: ISQLDBRows;
begin
  Stmt := fProps.Execute('SELECT ID_ATT, WWVALUE, LABEL '+
     'FROM WWATT_OPT '+
     'WHERE ID_ATT=? ORDER BY ID_ATT, WWVALUE', [aID]);
  if not Assigned(fAttOptDataSet) then begin
     fAttOptDataSet := TBufDataset.Create(nil);
     fAttOptDataSet.FieldDefs.Add('ID_ATT',ftInteger);
     fAttOptDataSet.FieldDefs.Add('WWVALUE',ftString, 25);
     fAttOptDataSet.FieldDefs.Add('LABEL',ftString, 40);
     fAttOptDataSet.CreateDataSet;
     fAttOptDataSet.Fields[0].Visible:=False;
     fAttOptDataSet.Fields[1].DisplayLabel:='Value';
     fAttOptDataSet.Fields[1].DisplayWidth:=25;
     fAttOptDataSet.Fields[2].DisplayLabel:='Label';
     fAttOptDataSet.Fields[2].DisplayWidth:=40;
  end;
  if ToClientDataSet(fAttOptDataSet, Stmt.Instance, 0, cdsReplace, false) then 
     result := fAttOptDataSet
  else
     result := nil;
end; 

Note: If SQL is empty autocreated fields is Bad, I always create fields manually

Last edited by ttomas (2020-08-11 08:41:00)

Offline

#15 2020-12-06 21:39:44

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Re: Using Lazarus DBGrid with mORMot.

Hello all,
I've posted a situation in lazarus/fpc forum, in case you can help.
https://forum.lazarus.freepascal.org/in … ic=52420.0
Thank you.

Offline

Board footer

Powered by FluxBB