Recent

Author Topic: tvplanit - how to save an iniDatastore?  (Read 2042 times)

Nicole

  • Hero Member
  • *****
  • Posts: 970
tvplanit - how to save an iniDatastore?
« on: December 09, 2022, 03:00:09 pm »
If I save my events in an ini-file, all my changes are saved at close of the software.
How to save them at sudden?

Background:
I wrote a utility, which allows to delete data as groups.
E.g. "delete all events before the year 2021 containing 42"

You want to check at sudden, if the data are gone and may want to delete e.g. all which contain e.g. "Ladybug". Who knows ;-)

What I need is something, what may look alike.

existing:
Code: Pascal  [Select][+][-]
  1. Var res: TVpResource;

wanted
Code: Pascal  [Select][+][-]
  1. res.SaveAllEvents;

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: tvplanit - how to save an iniDatastore?
« Reply #1 on: December 15, 2022, 03:46:14 pm »
Sorry for the long delay, your post must have been swept away too fast from the "Recent" list.

When you want to save all events of a specific resource, there must be a function for it in the most general datastore, TVpCustomDatastore, because the resource does not know to which datastore it belongs and, thus, the save must not be specific to TIniDatastore.

There is a method TVpCustomDatastore.PostEvents which seems to be usable for this purpose. In TVpCustomDatastore it is just abstract, but in the most general database datastore it is implemented such that it seeks the currently active resource and posts all the related event records back to the database.

The filebased datastores, however, are not optimized for saving of records fulfilling specific filter criteria (records of events of a specific resource), they only can write all data at once. Therefore the PostEvents method almost does nothing here; it just iterates over the internal event list and removes those events which are marked for deletion - nothing is written to file. This only happens at the end, before the datastore is disconnected: the IniDatastore calls a method WriteToIni, the XmlDatastore a method WriteToXml, and the JsonDatastore a method WriteJson. All these write the full file with all records.

These WriteXXX methods are "protected" at the moment, I could make them "public". But this would only be a store-type specific solution. If you'd once move to another datastore type it would not work any more, and you would have to modify your sourcecode at deeply buried places...

I am in doubt whether the IniDatastore is the correct datastore for your needs. I'd recommend to move to a database-based datastore, at least TBufDSDatastore which has no external dependencies, but gives you all the advantages of a databaser.

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: tvplanit - how to save an iniDatastore?
« Reply #2 on: December 16, 2022, 05:03:35 pm »
Thank you for your reply.

Just to take a snipet:

Code: Pascal  [Select][+][-]
  1. Var res: TVpResource;
  2.  
  3. begin
  4.   res := VpControlLink_my.Datastore.Resource;    // war VpControlLink1  
  5. ...
  6.  
Am I right, that
Code: Pascal  [Select][+][-]
  1. res.postevents;
will do the job?

I only have one datastore.
What I like is, that the ini-file is that slim. No database and driver needed.


wp

  • Hero Member
  • *****
  • Posts: 11912
Re: tvplanit - how to save an iniDatastore?
« Reply #3 on: December 16, 2022, 05:23:14 pm »
Am I right, that
Code: Pascal  [Select][+][-]
  1. res.postevents;
will do the job?
For the ini datastore, I don't think so. I tried to explain above that PostEvents for the file datastores (ini, xml, json) only cleans the internal list of events from deleted records, but does not write anything to the file itself.

What I like is, that the ini-file is that slim. No database and driver needed.
But partial writing to the ini file is not possible. Therefore, my recommendation of a database datastore. Use TBufDSDatastore which does not require a driver either.

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: tvplanit - how to save an iniDatastore?
« Reply #4 on: December 16, 2022, 06:11:02 pm »
I have put too much work in this ini-file structure already.

If I close down the software and restart it, - the ini-file is saved and the change is visible next open.
So there shall be a way to do this process at runtime as well?

I re-read your posting.
It shall be "WriteToIni"?
How to call it best?

It is fine to me, if all is written.
Those cleaning sessions will be rare, about once a year to get rid of old data.

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: tvplanit - how to save an iniDatastore?
« Reply #5 on: December 16, 2022, 06:58:34 pm »
It shall be "WriteToIni"?
How to call it best?
One possibility is to write a class helper for the IniDatastore:
Code: Pascal  [Select][+][-]
  1. type
  2.   TVpIniDatastoreHelper = class helper for TVpIniDatastore
  3.   public
  4.     procedure WriteIni;
  5.   end;  
  6.  
  7. procedure TVpIniDatastoreHelper.WriteIni;
  8. begin
  9.   WriteToIni;
  10. end;

With that your IniDataStore seems to have a new public method WriteIni (note the missing "To"), and you can rewrite the ini file at any time by calling this helper method, for example by means of a button click:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   VpIniDatastore1.WriteIni;
  4. end;

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: tvplanit - how to save an iniDatastore?
« Reply #6 on: December 17, 2022, 06:33:03 pm »
Thank you so much!!
I have saved your text and it waits for my next programming session of this module.

 

TinyPortal © 2005-2018