zMemTable - remove fields and unwanted records

The offical for ZeosLib 7.3 Report problems, ask for help, post proposals for the new version of Zeoslib 7.3/v8
Quick Info:
-We made two new drivers: odbc(raw and unicode version) and oledb
-GUID domain/field-defined support for FB
-extended error infos of Firebird
-performance ups are still in queue
In future some more feature will arrive, so stay tuned and don't hassitate to help
Post Reply
kjteng
Senior Boarder
Senior Boarder
Posts: 54
Joined: 10.05.2015, 15:02

zMemTable - remove fields and unwanted records

Post by kjteng »

Hi,
I wanted to remove some fields and filtered records from a zMemTable at runtime (but not to alter the file that I originally loaded the data from).
Current I do it by copying the fields/records that I want to temporary memtable and than clone it to the original memtable (see code below). Long cut ... because I dont know how to deleted fields.
Any advice
would be much appreciated.

Code: Select all

procedure AltTable(mt: TZMemTable; ls: array of integer); // ls contains the fields (number) that I want to keep
var tmp: TZMemTable; ii: integer;
begin
  tmp := TZMemTable.Create(nil);
  try
    for ii := 0 to high(ls) do
      with mt.Fields[ls[ii]] do
        tmp.FieldDefs.Add(FieldName, DataType, Size, Required);
    tmp.Open;
    mt.First;
    while not mt.eof do
      with tmp do
      begin
        Append;
        for ii := 0 to FieldCount -1 do
          Fields[ii].Value := mt.Fields[ls[ii]].Value;
        Post;
        mt.Next;
      end;
    mt.Close;
    mt.CloneDataFrom(tmp); 
 {  tmp.SaveTofile('tmp');
    mt.LoadFromFile('tmp'); }
  finally
    tmp.free
  end;
end;     
User avatar
aehimself
Zeos Dev Team
Zeos Dev Team
Posts: 796
Joined: 18.11.2018, 17:37
Location: Hungary

Re: zMemTable - remove fields and unwanted records

Post by aehimself »

A bit hacky but it might work... try Memtable.Fieldbyname('somefield').Free ?
For records, simply use MemTable.Delete;
Delphi 12.2, Zeos 8 from latest GIT snapshot
Using:
- MySQL server 8.0.18; libmysql.dll 8.0.40 x64 5.7.19 x68, libmariadb.dll 3.3.11
- Oracle server 11.2.0, 12.1.0, 19.0.0; oci.dll 21.15
- MSSQL 2012, 2019; sybdb.dll FreeTDS_3102
- SQLite 3.47
kjteng
Senior Boarder
Senior Boarder
Posts: 54
Joined: 10.05.2015, 15:02

Re: zMemTable - remove fields and unwanted records

Post by kjteng »

aehimself:
No luck.
Memtable.Fieldbyname('somefield').Free results in 'Access violation' error.
MemTable.Delete can delete a record that is visible (i.e. not filtered out). My question is how to delete all records that are filtered out (not visible). I use CloneDataFrom because it would copy records that are filtered out.
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1956
Joined: 17.01.2011, 14:17

Re: zMemTable - remove fields and unwanted records

Post by marsupilami »

Hello kjteng,

I think that TZMemTable is not made for removing fields and the like. Your best bet will be to have some kind of data pump that copies the data.

Best regards,

Jan
miab3
Zeos Test Team
Zeos Test Team
Posts: 1310
Joined: 11.05.2012, 12:32
Location: Poland

Re: zMemTable - remove fields and unwanted records

Post by miab3 »

Hi kjteng, all,

You can operate between many different ZMemTable.

It would be more interesting and relatively simple to write/read parts (from-to) of the ZMemTable to/from disk, and merge data.
Though it might actually be useful to copy the definitions of the specified fields and data to the ZMemTable.

Michał
kjteng
Senior Boarder
Senior Boarder
Posts: 54
Joined: 10.05.2015, 15:02

Re: zMemTable - remove fields and unwanted records

Post by kjteng »

miab3 wrote: 14.11.2021, 10:03 Hi kjteng, all,

You can operate between many different ZMemTable.

It would be more interesting and relatively simple to write/read parts (from-to) of the ZMemTable to/from disk, and merge data.
Though it might actually be useful to copy the definitions of the specified fields and data to the ZMemTable.

Michał
Thanks for your reply. Your suggestion is the similar to what I was trying to do with the code above. Right?
miab3
Zeos Test Team
Zeos Test Team
Posts: 1310
Joined: 11.05.2012, 12:32
Location: Poland

Re: zMemTable - remove fields and unwanted records

Post by miab3 »

Hi kjteng,

Note that you can copy the structure without some fields.
As well as adding new fields.
And copy the data in a traditional way, indicating the correct ones.
In fact, a library of such routines, the ZMemTableUtils, would come in handy.

Michał
kjteng
Senior Boarder
Senior Boarder
Posts: 54
Joined: 10.05.2015, 15:02

Re: zMemTable - remove fields and unwanted records

Post by kjteng »

miab3 wrote: 17.11.2021, 10:04 Hi kjteng,
...
As well as adding new fields.
And copy the data in a traditional way, indicating the correct ones.
In fact, a library of such routines, the ZMemTableUtils, would come in handy.

Michał
OK I see your point. Thank you for the valuable advice.
Where can I find ZMemTableUtils.pas ?
miab3
Zeos Test Team
Zeos Test Team
Posts: 1310
Joined: 11.05.2012, 12:32
Location: Poland

Re: zMemTable - remove fields and unwanted records

Post by miab3 »

kjteng wrote: 17.11.2021, 11:56 Where can I find ZMemTableUtils.pas ?
I thought you would write.

Michał
kjteng
Senior Boarder
Senior Boarder
Posts: 54
Joined: 10.05.2015, 15:02

Re: zMemTable - remove fields and unwanted records

Post by kjteng »

miab3 wrote: 17.11.2021, 12:44
kjteng wrote: 17.11.2021, 11:56 Where can I find ZMemTableUtils.pas ?
I thought you would write.

Michał
Haha. Okok. I will include it in my helper file.
miab3
Zeos Test Team
Zeos Test Team
Posts: 1310
Joined: 11.05.2012, 12:32
Location: Poland

Re: zMemTable - remove fields and unwanted records

Post by miab3 »

Hi kjteng,

I was hoping that since you use this, you would write some useful procedures and share with us.

Michał
kjteng
Senior Boarder
Senior Boarder
Posts: 54
Joined: 10.05.2015, 15:02

Re: zMemTable - remove fields and unwanted records

Post by kjteng »

miab3 wrote: 18.11.2021, 07:02 Hi kjteng,

I was hoping that since you use this, you would write some useful procedures and share with us.

Michał
Hi Michal,
I have uploaded to github. see viewtopic.php?f=50&t=148850.
miab3
Zeos Test Team
Zeos Test Team
Posts: 1310
Joined: 11.05.2012, 12:32
Location: Poland

Re: zMemTable - remove fields and unwanted records

Post by miab3 »

Hi kjteng,

Thanks.
I see that you solved the problem of copying some fields and data.
If in the future you have something interesting with ZMemTable, publish it.

Michał
Post Reply