HDataChangeLogger is an object which can automatically log the data changes of a HTable into a separated sql table. Useful to store the history of a sql data table.
Every data field in a table has an attribute "logged". If this "logged" attribute is true and the HTable has a valid HDataChangeLogger object set, every real data modifications is automatically logged to an sql table.
(See HDataField::setLogging() and HDataField::resetLogging() to set the "logged" attribute.
And see HTable::setDataChangeLogger() and HTable::clearDataChangeLogger() to set this logger in a HTable)
You have to create a HTable instance which define the log sql table, and pass this HTable * to the HDataChangeLogger
The table has to have the following fields:
- HKey typed "dclkey" named (holds an autoincrement key)
- HSmallText typed "pos" named (holds the program position of the change. You define it.)
- HSmallText typed "cuser" named (holds the user who do the change. You define it.)
- HSmallText typed "sqltablename" named (holds the changed table name. Auto.)
- HSmallText typed "changedkey" named (holds the changed record's HKey. Auto.)
- HSmallText typed "sqlfieldname" named (holds the chaged fields sql name. Auto.)
- HSmallText typed "oldvalue" named (holds the changed fields old value. Auto.)
- HSmallText typed "newvalue" named (holds the changed fields new value. Auto.)
- HTimestamp typed "changetime" named (holds the changed fields mod. time. Auto.)
The table can contains any explain text, modifiers, comments or even more data fields.
An example log table (HFactory XML format):
<element name="datachange_log_table">
<table sqln="dclog">
<key sqln="dclkey">
<explain>Key</explain>
<title>Key</title>
<default>Generated</default>
<hide/>
</key>
<smalltext sqln="pos">
<explain>Position of changing</explain>
<title>Pos</title>
</smalltext>
<smalltext sqln="cuser">
<explain>Username</explain>
<title>Username</title>
</smalltext>
<smalltext sqln="sqltablename">
<explain>Sql table name which changed</explain>
<title>Tablename</title>
</smalltext>
<smalltext sqln="changedkey">
<explain>The changed records key</explain>
<title>Changedkey</title>
</smalltext>
<smalltext sqln="sqlfieldname">
<explain>Sql field name which changed</explain>
<title>Fieldname</title>
</smalltext>
<smalltext sqln="oldvalue">
<explain>The old value</explain>
<title>OldValue</title>
</smalltext>
<smalltext sqln="newvalue">
<explain>The new value</explain>
<title>NewValue</title>
</smalltext>
<timestamp sqln="changetime">
<explain>The changing time</explain>
<title>Changetime</title>
<default>NOW</default>
</timestamp>
</table>
</element>
Here is an example function of using this code in program. I have a mytable which is generated by the HFactory. I will edit this mytable ("t" variable) and I set up the logger to work on it. (Use the example XML above)
This function is edit (And optionally modify/log) a mytable record with "key"
int editMytableItem(QString key)
{
int result=0;
HTable *t = myfactory->genHTable(
"mytable");
HTable *logger_table = myfactory->genHTable(
"datachange_log_table");
...
"EscC|THoriz|Vert|StrToE|TB1Close|TB1AcceptClose|TB2Close",
tr("Modify the item"),tr("Modify"),tr("Cancel"),NULL);
if(dialog->exec())
{
{
result=1;
}
}
delete logger;
delete logger_table;
delete dialog;
delete t;
return result;
}
Of course you can create and set this logger object outer of this function. You can set the changed position and the user name everytime with the setPos() and setUser() functions.
Definition at line 3639 of file datalib.h.