Page 1 of 1

PostPosted: Tue Feb 04, 2003 8:05 pm
by sgibson
I have a situation where I have a struct that I am writing to each record in the database. The struct contains several ints, floats, strings, a couple Dates, etc. Plus I added some "placeholder" variables to the struct that aren't used because my thought was they would "pad" the record structure so that I could add functionality without screwing up existing data. So, I have something like this:
Code: Select all
struct myRecord {
   int blah;
   int slime;
   float foo;
   float bar;
   string bah;
   string humbug;
   Date the_date;
   int placeholderA;
   float placeholderB;
   string placeholderC;

My primary question is this: if I want to add a Date to the structure can I simply replace one of the placeholder ints with a Date? I'm not sure what the size of a Date is versus an int when they get written to the database. I'm guessing ints are 32 bits (4 bytes), but I don't know that for certain. How big are Dates?

My secondary question is: is it even necessary to use these placeholders? Or will things "just work" if I append any new data to the end of the struct? My concern is that older records will be shorter in length than newer records and may give unexpected results when I try to read an old record into the new structure.

Thanks in advance for any insight,

perl -e 'print $i=pack(c5,(41*2),sqrt(7056),(unpack(c,H)-2),oct(115),10);'

PostPosted: Tue Feb 04, 2003 8:39 pm
by jims

Using placeholders will only work if the "sizeof" the item is the same.

I've just always added new data to the end of the structure and have never had a problem. You just have to be careful to initialize the new data if necessary for those records that did not have anything.

Also, for more complicated changes I save the "DatabaseVersion" in appPrefs, then if I need to change the database structure I can write a simple routine to read the old structure and write the new structure.


PostPosted: Wed Feb 05, 2003 3:03 am
by dewey
In general it you cannot put a native object into a database, because they are usually just an id referring to an internal structure. Luckily Date is different, because it is actually stored as the number of seconds since 1904 (IIRC).

If you call with a typeof(myRecord) which contains more fields than exist in the database record, the call to read() will return a lower value than expected (see docs). As Jim said, be sure to initialise the rest of the fields to something meaningful.