Recent

Author Topic: Easy to use memory dataset  (Read 26435 times)

cdbc

  • Hero Member
  • *****
  • Posts: 1971
    • http://www.cdbc.dk
Re: Easy to use memory dataset
« Reply #15 on: February 29, 2016, 03:46:11 pm »
Hi
Cool, while you're at it, have a look at "Locate" in TDbf  ;) :D
I've implemented my own cliendataset with delta and delayed updates, but I just needed support for SQLite backend....
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #16 on: February 29, 2016, 08:19:26 pm »
I add this procedures to TBufDataset but I cant compile its package and Lazarus still uses old one,how can I compile it to use new changes?
Code: Pascal  [Select][+][-]
  1.     procedure Clear;
  2.     Procedure CopyFromDataset(DataSet : TDataSet;CopyData : Boolean=True);
Code: Pascal  [Select][+][-]
  1. procedure TCustomBufDataset.Clear;
  2. begin
  3.  ClearIndexes;
  4.  ClearBuffers;
  5.  ClearFields;
  6. end;
  7.  
  8. procedure TCustomBufDataset.CopyFromDataset(DataSet: TDataSet; CopyData: Boolean);
  9. Var
  10.   I  : Integer;
  11.   F,F1,F2 : TField;
  12.   L1,L2  : TList;
  13.   N : String;
  14.   OriginalPosition: TBookMark;
  15. begin
  16.  Clear;
  17.  For I:=0 to Dataset.FieldCount-1 do
  18.    begin
  19.    F:=Dataset.Fields[I];
  20.    TFieldDef.Create(FieldDefs,F.FieldName,F.DataType,F.Size,F.Required,F.FieldNo);
  21.    end;
  22.  CreateDataset;
  23.    If CopyData then
  24.   begin
  25.     Open;
  26.     L1:=TList.Create;
  27.     Try
  28.       L2:=TList.Create;
  29.       Try
  30.         For I:=0 to FieldDefs.Count-1 do
  31.           begin
  32.           N:=FieldDefs[I].Name;
  33.           F1:=FieldByName(N);
  34.           F2:=DataSet.FieldByName(N);
  35.           L1.Add(F1);
  36.           L2.Add(F2);
  37.           end;
  38.         DisableControls;
  39.         Dataset.DisableControls;
  40.         OriginalPosition:=Dataset.GetBookmark;
  41.         Try
  42.           Dataset.Open;
  43.           Dataset.First;
  44.           While not Dataset.EOF do
  45.             begin
  46.             Append;
  47.             For I:=0 to L1.Count-1 do
  48.               begin
  49.               F1:=TField(L1[i]);
  50.               F2:=TField(L2[I]);
  51.               Case F1.DataType of
  52.                 ftFixedChar,
  53.                 ftString   : F1.AsString:=F2.AsString;
  54.                 ftBoolean  : F1.AsBoolean:=F2.AsBoolean;
  55.                 ftFloat    : F1.AsFloat:=F2.AsFloat;
  56.                 ftLargeInt : F1.AsInteger:=F2.AsInteger;
  57.                 ftSmallInt : F1.AsInteger:=F2.AsInteger;
  58.                 ftInteger  : F1.AsInteger:=F2.AsInteger;
  59.                 ftDate     : F1.AsDateTime:=F2.AsDateTime;
  60.                 ftTime     : F1.AsDateTime:=F2.AsDateTime;
  61.                 ftDateTime : F1.AsDateTime:=F2.AsDateTime;
  62.                 else         F1.AsString:=F2.AsString;
  63.               end;
  64.               end;
  65.             Try
  66.               Post;
  67.             except
  68.               Cancel;
  69.               Raise;
  70.             end;
  71.             Dataset.Next;
  72.             end;
  73.         Finally
  74.           DataSet.GotoBookmark(OriginalPosition); //Return to original record
  75.           Dataset.EnableControls;
  76.           EnableControls;
  77.         end;
  78.       finally
  79.         L2.Free;
  80.       end;
  81.     finally
  82.       l1.Free;
  83.     end;
  84.   end;
  85. end;

balazsszekely

  • Guest
Re: Easy to use memory dataset
« Reply #17 on: February 29, 2016, 08:45:50 pm »
Quote
I add this procedures to TBufDataset but I cant compile its package and Lazarus still uses old one,how can I compile it to use new changes?
Bufdataset is a FPC unit, for the changes to take effect you need to recompile FPC.

Thaddy

  • Hero Member
  • *****
  • Posts: 16673
  • Kallstadt seems a good place to evict Trump to.
Re: Easy to use memory dataset
« Reply #18 on: February 29, 2016, 09:05:09 pm »
The code looks good. Why not submit it as a patch?
But I am sure they don't want the Trumps back...

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #19 on: February 29, 2016, 09:34:54 pm »
Quote
I add this procedures to TBufDataset but I cant compile its package and Lazarus still uses old one,how can I compile it to use new changes?
Bufdataset is a FPC unit, for the changes to take effect you need to recompile FPC.
How?I try what I found but keep getting weird errors. What should I do if I want to debug it?I never done that.

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #20 on: February 29, 2016, 09:42:17 pm »
The code looks good. Why not submit it as a patch?
Honestly I dont know how and one of the problems was getting and keep Lazarus trunk version OK in my computer, I discussed my problems in another topic.

balazsszekely

  • Guest
Re: Easy to use memory dataset
« Reply #21 on: February 29, 2016, 09:44:27 pm »

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #22 on: February 29, 2016, 09:49:17 pm »
@GetMem I read it but I still cant compile it.
I give it another try

Thaddy

  • Hero Member
  • *****
  • Posts: 16673
  • Kallstadt seems a good place to evict Trump to.
Re: Easy to use memory dataset
« Reply #23 on: February 29, 2016, 09:53:38 pm »
The code looks good. Why not submit it as a patch?
Honestly I dont know how and one of the problems was getting and keep Lazarus trunk version OK in my computer, I discussed my problems in another topic.

I can likely turn it into a patch that you can submit.
But I am sure they don't want the Trumps back...

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #24 on: February 29, 2016, 09:57:59 pm »
Quote
I can likely turn it into a patch that you can submit.
It will be very good. Thanks.
Im still trying to compile and check it my self.

balazsszekely

  • Guest
Re: Easy to use memory dataset
« Reply #25 on: February 29, 2016, 10:08:37 pm »
What about this? You can run a few tests without recompile:
Code: Pascal  [Select][+][-]
  1. type
  2.   TBufDatasetHelper = class helper for TCustomBufDataset
  3.   private
  4.     procedure Clear;
  5.     procedure CopyFromDataset(DataSet: TDataSet; CopyData: Boolean);
  6.   end;
  7.  
  8. procedure TBufDatasetHelper.Clear;
  9. begin
  10.   ClearIndexes;
  11.   ClearBuffers;
  12.   ClearFields;
  13. end;
  14.  
  15. procedure TBufDatasetHelper.CopyFromDataset(DataSet: TDataSet; CopyData: Boolean);
  16. Var
  17.   I  : Integer;
  18.   F,F1,F2 : TField;
  19.   L1,L2  : TList;
  20.   N : String;
  21.   OriginalPosition: TBookMark;
  22. begin
  23.  Clear;
  24.  For I:=0 to Dataset.FieldCount-1 do
  25.    begin
  26.    F:=Dataset.Fields[I];
  27.    TFieldDef.Create(FieldDefs,F.FieldName,F.DataType,F.Size,F.Required,F.FieldNo);
  28.    end;
  29.  CreateDataset;
  30.    If CopyData then
  31.   begin
  32.     Open;
  33.     L1:=TList.Create;
  34.     Try
  35.       L2:=TList.Create;
  36.       Try
  37.         For I:=0 to FieldDefs.Count-1 do
  38.           begin
  39.           N:=FieldDefs[I].Name;
  40.           F1:=FieldByName(N);
  41.           F2:=DataSet.FieldByName(N);
  42.           L1.Add(F1);
  43.           L2.Add(F2);
  44.           end;
  45.         DisableControls;
  46.         Dataset.DisableControls;
  47.         OriginalPosition:=Dataset.GetBookmark;
  48.         Try
  49.           Dataset.Open;
  50.           Dataset.First;
  51.           While not Dataset.EOF do
  52.             begin
  53.             Append;
  54.             For I:=0 to L1.Count-1 do
  55.               begin
  56.               F1:=TField(L1[i]);
  57.               F2:=TField(L2[I]);
  58.               Case F1.DataType of
  59.                 ftFixedChar,
  60.                 ftString   : F1.AsString:=F2.AsString;
  61.                 ftBoolean  : F1.AsBoolean:=F2.AsBoolean;
  62.                 ftFloat    : F1.AsFloat:=F2.AsFloat;
  63.                 ftLargeInt : F1.AsInteger:=F2.AsInteger;
  64.                 ftSmallInt : F1.AsInteger:=F2.AsInteger;
  65.                 ftInteger  : F1.AsInteger:=F2.AsInteger;
  66.                 ftDate     : F1.AsDateTime:=F2.AsDateTime;
  67.                 ftTime     : F1.AsDateTime:=F2.AsDateTime;
  68.                 ftDateTime : F1.AsDateTime:=F2.AsDateTime;
  69.                 else         F1.AsString:=F2.AsString;
  70.               end;
  71.               end;
  72.             Try
  73.               Post;
  74.             except
  75.               Cancel;
  76.               Raise;
  77.             end;
  78.             Dataset.Next;
  79.             end;
  80.         Finally
  81.           DataSet.GotoBookmark(OriginalPosition); //Return to original record
  82.           Dataset.EnableControls;
  83.           EnableControls;
  84.         end;
  85.       finally
  86.         L2.Free;
  87.       end;
  88.     finally
  89.       l1.Free;
  90.     end;
  91.   end;
  92. end;        
  93.  
  94. //...
  95. var
  96.   BufDataset: TBufDataset;
  97. begin
  98.   BufDataset := TBufDataset.Create(nil);
  99.   BufDataset.CopyFromDataset();
  100. end;
  101.                  
  102.  

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #26 on: February 29, 2016, 10:17:00 pm »
I tried to recompile but I give this error:
Quote
C:\lazarus\fpc\3.0.0\source\packages\fcl-db>C:\lazarus\fpc\3.0.0\bin\i386-win32\make.exe  clean all install INSTALL_PREFIX="C:\lazarus\fpc\3.0.0"
make: Nothing to be done for `clean'.
C:/lazarus/fpc/3.0.0/bin/i386-win32/ppc386.exe fpmake.pp -n -Fu../../rtl -Fu../../packages/paszlib -Fu../../packages/fcl-process -Fu../../packages/hash -Fu../../packages/libtar -Fu../../packages/fpmkunit
Fatal: Can't find unit system used by fpmake
Fatal: Compilation aborted
make: *** [fpmake.exe] Error 1
Also I tried to see if it can find fpc.cfg and it seems it can. for thsi I tried fpc.exe -va command .


balazsszekely

  • Guest
Re: Easy to use memory dataset
« Reply #27 on: February 29, 2016, 10:29:23 pm »
Quote
@aradeonas
I tried to recompile but I give this error.
It works fine at my side. Did you try the class helper solution?

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Easy to use memory dataset
« Reply #28 on: February 29, 2016, 10:31:17 pm »
Good call @GetMem. I wanted to do that but recompiling dpc distract me.
Thanks to you I test it and add First to it and now it seems working good enough. I just dont like its memory usage. it takes about triple of simple query that I just copy if its a problem its about TBufDataset and I will look at it.
Here the changed code :
Code: Pascal  [Select][+][-]
  1. var
  2.   I: integer;
  3.   F, F1, F2: TField;
  4.   L1, L2: TList;
  5.   N: string;
  6.   OriginalPosition: TBookMark;
  7. begin
  8.   Clear;
  9.   for I := 0 to Dataset.FieldCount - 1 do
  10.   begin
  11.     F := Dataset.Fields[I];
  12.     TFieldDef.Create(FieldDefs, F.FieldName, F.DataType, F.Size, F.Required, F.FieldNo);
  13.   end;
  14.   CreateDataset;
  15.   if CopyData then
  16.   begin
  17.     Open;
  18.     L1 := TList.Create;
  19.     try
  20.       L2 := TList.Create;
  21.       try
  22.         for I := 0 to FieldDefs.Count - 1 do
  23.         begin
  24.           N := FieldDefs[I].Name;
  25.           F1 := FieldByName(N);
  26.           F2 := DataSet.FieldByName(N);
  27.           L1.Add(F1);
  28.           L2.Add(F2);
  29.         end;
  30.         DisableControls;
  31.         Dataset.DisableControls;
  32.         OriginalPosition := Dataset.GetBookmark;
  33.         try
  34.           Dataset.Open;
  35.           Dataset.First;
  36.           while not Dataset.EOF do
  37.           begin
  38.             Append;
  39.             for I := 0 to L1.Count - 1 do
  40.             begin
  41.               F1 := TField(L1[i]);
  42.               F2 := TField(L2[I]);
  43.               case F1.DataType of
  44.                 ftFixedChar,
  45.                 ftString: F1.AsString := F2.AsString;
  46.                 ftBoolean: F1.AsBoolean := F2.AsBoolean;
  47.                 ftFloat: F1.AsFloat := F2.AsFloat;
  48.                 ftLargeInt: F1.AsInteger := F2.AsInteger;
  49.                 ftSmallInt: F1.AsInteger := F2.AsInteger;
  50.                 ftInteger: F1.AsInteger := F2.AsInteger;
  51.                 ftDate: F1.AsDateTime := F2.AsDateTime;
  52.                 ftTime: F1.AsDateTime := F2.AsDateTime;
  53.                 ftDateTime: F1.AsDateTime := F2.AsDateTime;
  54.                 else
  55.                   F1.AsString := F2.AsString;
  56.               end;
  57.             end;
  58.             try
  59.               Post;
  60.             except
  61.               Cancel;
  62.               raise;
  63.             end;
  64.             Dataset.Next;
  65.           end;
  66.         finally
  67.           DataSet.GotoBookmark(OriginalPosition); //Return to original record
  68.           Dataset.EnableControls;
  69.           EnableControls;
  70.         end;
  71.       finally
  72.         L2.Free;
  73.       end;
  74.     finally
  75.       l1.Free;
  76.     end;
  77.     First;
  78.   end;
  79. end;

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4565
  • I like bugs.
Re: Easy to use memory dataset
« Reply #29 on: February 29, 2016, 10:31:46 pm »
Quote
I can likely turn it into a patch that you can submit.
It will be very good. Thanks.
Im still trying to compile and check it my self.

Oh, you should really learn to make patches yourself. Besides, it is extremely easy. Just one "svn diff" command or in TortoiseSVN "Create patch". Doesn't get much simpler.
I understood you are making code for the online package manager. Then you surely must create patches.

BTW, what problems you had with Lazarus trunk version in your computer? Where was it discussed?
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

 

TinyPortal © 2005-2018