Recent

Author Topic: [SOLVED] Laz 2.0.10/fpc 3.2.0 and TBufDataset  (Read 600 times)

OH1KH

  • New Member
  • *
  • Posts: 43
[SOLVED] Laz 2.0.10/fpc 3.2.0 and TBufDataset
« on: March 02, 2021, 10:46:32 am »
HI !

Can someone again kindly advice me of reading (and understanding) the changes file. Thank you.

I see that Lazarus 2.0.12 is out, but it seems the fpc part has not changed (file names and zip sizes are same)
I will test that later when I have time so I assume the error still exist.

Now the problem is with  TBufDataset.

Code that perfectly works with laz2.0.8/fpc 3.0.4 does not work any more causing access violation error.
Code: Pascal  [Select][+][-]
  1. db := TBufDataset.Create(nil);
  2.         try
  3.           db.FieldDefs.Clear;
  4.           with db.FieldDefs do
  5.           begin
  6.             Add('loc', ftString, 4);
  7.             Add('cfm',ftBoolean)
  8.           end;
  9.           db.CreateDataset;
  10.           db.IndexDefs.Add('loc','loc',[ixPrimary]);
  11.  
  12.           db.Open;
  13.           wkd := 0;
  14.           while not dmData.Q1.Eof do
  15.           begin
  16.             db.Append;
  17.             db.Fields[0].AsString  := dmData.Q1.Fields[0].AsString;
  18.             db.Fields[1].AsBoolean := False;
  19.             db.Post;
  20.             inc(wkd);
  21.             dmData.Q1.Next
  22.           end;        
  23.  

When execution comes  first time to line dp.post Access violation error is created.

I tried to find what would be the difference reading changes documents but I could not find (understand) any reason.
« Last Edit: March 02, 2021, 02:42:58 pm by OH1KH »
--
Saku

LacaK

  • Hero Member
  • *****
  • Posts: 619
Re: Laz 2.0.10/fpc 3.2.0 and TBufDataset
« Reply #1 on: March 02, 2021, 10:58:00 am »
if you omit line 10 : db.IndexDefs.Add('loc','loc',[ixPrimary]);
then there is still AV ?

dseligo

  • Full Member
  • ***
  • Posts: 231
Re: Laz 2.0.10/fpc 3.2.0 and TBufDataset
« Reply #2 on: March 02, 2021, 11:35:25 am »
if you omit line 10 : db.IndexDefs.Add('loc','loc',[ixPrimary]);
then there is still AV ?
I also had this problem with IndexDefs.Add (and I just commented it out).
After "Open" I added:
Code: Pascal  [Select][+][-]
  1. IndexFieldNames:='myfield1;myfield2';

I don't know if that helps.

OH1KH

  • New Member
  • *
  • Posts: 43
Re: Laz 2.0.10/fpc 3.2.0 and TBufDataset
« Reply #3 on: March 02, 2021, 12:18:58 pm »
Thanks for quick reply!

Yes!
Commenting out line  db.IndexDefs.Add('loc','loc',[ixPrimary]); helps.

This part of code is not originally written by me so I have to clear out what IndexDefs.Add there stands for and could/needs this to be replaced with IndexFieldNames:='myfield1;myfield2';
Program seems to work without IndexFieldNames and with it. With quick look result is similar.

Funny that this it again one problem that arises when you update laz/fpc without touching the source you are going to compile.
And in this case I could not find anything from update changes file that I am able to connect to this problem.
--
Saku

PascalDragon

  • Hero Member
  • *****
  • Posts: 2969
  • Compiler Developer
Re: Laz 2.0.10/fpc 3.2.0 and TBufDataset
« Reply #4 on: March 02, 2021, 01:42:36 pm »
Yes!
Commenting out line  db.IndexDefs.Add('loc','loc',[ixPrimary]); helps.

You should use TCustomBufDataset.AddIndex instead. IndexDef should be considered as readonly.

cdbc

  • Sr. Member
  • ****
  • Posts: 356
    • http://www.cdbc.dk
Re: Laz 2.0.10/fpc 3.2.0 and TBufDataset
« Reply #5 on: March 02, 2021, 02:18:45 pm »
Code: [Select]
Hi
Try this:

[code=pascal]
Procedure TBufDataset.CopyFromDataset(DataSet : TDataSet; CopyData : Boolean);
Var
  I  : Integer;
  F,F1,F2 : TField;
  L1,L2  : TList;
  N : String;
begin
  Clear(True);
  // NOT from fielddefs. The data may not be available in buffers !!
  for I:= 0 to Dataset.FieldCount-1 do begin
    F:= Dataset.Fields[I];
    // ææ special case for ftAutoInc fields 16.02.2020 /bc
    if F.DataType = ftAutoInc then TFieldDef.Create(FieldDefs,F.FieldName,ftInteger,F.Size,F.Required,F.FieldNo)
    else TFieldDef.Create(FieldDefs,F.FieldName,F.DataType,F.Size,F.Required,F.FieldNo);
  end;
  CreateTable;
  if CopyData then begin
    Open;
    L1:= TList.Create;
    try
      L2:= TList.Create;
      try
        for I:= 0 to FieldDefs.Count-1 do begin
          N:= FieldDefs[I].Name;
          F1:= FieldByName(N);
          F2:= DataSet.FieldByName(N);
          L1.Add(F1);
          L2.Add(F2);
        end;
        Dataset.DisableControls;
        try
          Dataset.Open;
          while not Dataset.EOF do begin
            Append;
            for I:= 0 to L1.Count-1 do begin
              F1:= TField(L1[i]);
              F2:= TField(L2[I]);
              case F1.DataType of
                ftString   : F1.AsString:= F2.AsString;
                ftBoolean  : F1.AsBoolean:= F2.AsBoolean;
                ftFloat    : F1.AsFloat:= F2.AsFloat;
                ftLargeInt : F1.AsInteger:= F2.AsInteger;
                ftSmallInt : F1.AsInteger:= F2.AsInteger;
                ftInteger  : F1.AsInteger:= F2.AsInteger;
                ftDate     : F1.AsDateTime:= F2.AsDateTime;
                ftTime     : F1.AsDateTime:= F2.AsDateTime;
                ftDateTime : F1.AsDateTime:= F2.AsDateTime;
              end;
            end;
            try Post; except Cancel; Raise; end;
            Dataset.Next;
          end;
        finally Dataset.EnableControls; end;
      finally L2.Free; end;
    finally L1.Free; end;
  end;
end;
Implement this in TBufDatset and maybe locate, I dono.
Indexes i dono  %)
As you can see it's 'stolen' from TMemDataset.
 ;)
Regards Benny
« Last Edit: March 02, 2021, 02:36:29 pm by cdbc »
If it ain't broke, don't fix it ;)

OH1KH

  • New Member
  • *
  • Posts: 43
[SOLVED] Laz 2.0.10/fpc 3.2.0 and TBufDataset
« Reply #6 on: March 02, 2021, 02:42:26 pm »
Change code like this makes it work:

Code: Pascal  [Select][+][-]
  1.    db := TBufDataset.Create(nil);
  2.         try
  3.           db.FieldDefs.Clear;
  4.           //with db.FieldDefs do
  5.           //begin
  6.             db.FieldDefs.Add('loc', ftString, 4);
  7.             db.IndexDefs.Add('loc','loc',[ixPrimary]);
  8.             db.FieldDefs.Add('cfm',ftBoolean);
  9.           //end;
  10.           db.CreateDataset;
  11.           //db.IndexDefs.Add('loc','loc',[ixPrimary]);
  12.           db.Open;
  13.           wkd := 0;
  14.           while not dmData.Q1.Eof do
  15.           begin
  16.             db.Append;
  17.             db.Fields[0].AsUTF8String  := dmData.Q1.Fields[0].AsString;
  18.             db.Fields[1].AsBoolean := False;
  19.             db.Post;
  20.             inc(wkd);
  21.             dmData.Q1.Next
  22.           end;                
  23.  

IndexDefs should be done before CreateDataset, then no error happen in db.post any more.
« Last Edit: March 02, 2021, 02:44:25 pm by OH1KH »
--
Saku

 

TinyPortal © 2005-2018