#include <qdataschema.h>
Public Types | |
enum | ConnectionType { CT_INTERNAL, CT_MYSQL, CT_POSTGRESQL } |
Signals | |
void | verifyMessage (QString op, QString value, QString oldvalue) |
Public Member Functions | |
QDataSchema (ConnectionType ctype, const char *objname=0) | |
Create and initialise new object. | |
~QDataSchema () | |
Close connection and release object resources. | |
bool | open (const QString &dbname, const QString &dbuser="", const QString &dbpass="", const QString &dbhost="localhost", int dbport=0) |
Open and initialise database server connection. | |
bool | open () |
Open and init database connection. | |
void | close () |
Closes database server connection. | |
QSqlDatabase * | db () |
Returns database connection. | |
bool | databaseCreate (const QString &dbadmuser, const QString &dbadmpass) |
Create new database. | |
bool | databaseDrop (const QString &dbadmuser, const QString &dbadmpass) |
Drop current database. | |
int | execList (const QStringList &query, bool inTransaction=false) |
QSqlCursor * | table (const QString &name=QString::null) |
Creates QSqlCursor object for database table. | |
QString | tableName (const QString &name) |
QString | tableNameStripPrefix (const QString &name) |
QString | nameSpace () const |
void | setNameSpace (const QString &namesp) |
bool | tableExists (const QString &name) |
Checks table existence. | |
QDataSchemaDriver * | driver () |
Return current QDataSchema driver. | |
void | setDataDictionary (const QStringList &dd_new) |
Set data dictionary for application. | |
QStringList | dataDictionary () |
Return data dictionary. | |
QStringList | updateStructureQuery () |
Return queries list for database update. | |
QStringList | verifyLog () |
Return list of differents. | |
int | verifyStructure () |
Check structure. | |
int | updateStructure () |
virtual int | databaseExport (const QString &filename) |
Unload information from database. | |
virtual int | databaseImport (const QString &filename, bool updateStruct=false) |
Load information into data base. | |
Static Public Member Functions | |
static QStringList | drivers () |
Return a drivers list. | |
static bool | isDriverAvailable (const QString &name) |
Return a true if dataschema driver Name available. | |
Protected Member Functions | |
void | verifyLogRecord (QString op, QString value, QString oldvalue="") |
Register record of change in database. | |
QString | ddRecord (int idx, bool d_sql=false) |
int | ddCount (const QString &etype, bool d_sql=false) |
int | ddIndex (const QString &etype, int num, bool d_sql=false) |
int | ddIndex (const QString &etype, const QString &name, bool d_sql=false) |
int | ddCountSub (int idx, const QString &etype, bool d_sql=false) |
int | ddIndexSub (int idx, const QString &etype, int num, bool d_sql=false) |
int | ddIndexSub (int idx, const QString &etype, const QString &name, bool d_sql=false) |
QString | ddRecSection (const QString &rec, int secnum) |
Return dictionary element record section. | |
QString | ddRecType (const QString &rec) |
Return type of dictionary element record. | |
QString | ddRecName (const QString &rec) |
Return name of dictionary element record. | |
void | joinLists (QStringList &list, const QStringList &add) |
Append one string list to another. | |
int | checkSqlError (QSqlQuery &query) |
Check query result. | |
QString | fieldTypeSql (const QString &tdef) |
virtual int | createSystables () |
Used for support syncronous data structure in application and database server. Application data structure describes in database server independed context. In depends of the database server, chose method for structure updates if structure of the application data was changed ( for example, we wer updated application version ) and we need updates data base structure with save information, that was collected earlier.
In addition to tables, described in the application, QDataSchema creates additional table of data dictionary with name dictionaryTableName() ( default "dd" ) for storing information about current structure of the data base for maximum correct updates of the information structure.
Dictionary creates automaticaly with folowing parameters:
T=dd|Data dictionary|S F=id|Line number|I|0|0| F=value|Data value|C|250|0|
QDataSchema *qds; QString dbname = "qds", dbuser = "", dbpasswd = "", dbserver = "localhost"; int dbport = 0; QStringList dd; qds = new QDataSchema( QDataSchema::CT_INTERNAL ); // Application data schema description dd<<"D=QDS|Description|UTF-8|UID"; dd<<"T=T1|Description|U"; dd<<"F=F1|Description|I|0|0|PSN"; dd<<"F=F2|Description|D|0|0|"; dd<<"F=F3|Description|C|200|0|"; dd<<"F=F4|Description|N|10|2|"; dd<<"F=F5|Description|I|0|0|"; dd<<"F=F2|Description|L|0|0|"; dd<<"I=I1|Description|F2,F3|"; dd<<"T=T2|Description|U"; dd<<"F=F1|Description|I|0|0|PSN"; dd<<"F=F2|Description|C|10|0|"; qds->setDataDictionary( dd ); if ( qds->init( dbname,dbuser,dbpasswd,dbserver,dbport ) ) { if ( !qds->verifyStructure() ){ // need to update printf("verify log:\n%s\n", ( const char * ) adb.verifyLog().join("\n") ); printf("update structure query:\n%s\n", ( const char * ) adb.updateStructureQuery().join("\n") ); qds->updateStructure(); } } else printf("ERROR INIT DATABASE\n");
Objectives of the QDataSchema class is support actualy SQL database server tables structure automaticaly if database structure changed in applications, using QDataSchema. QDataSchema uses tables structure description.
Theare are tow tables structure descriptions: 1) Tables structure on database server 2) Tables structure, loaded by application on class initialisation.
varifyStructure() method check correspondence betwin that structures and if there different, prepare 3 differents lists:
Tables structure is a strings list in format:
* # Commentaries in (UTF-8) * # --- Data dictionary * # Name|Description|Charset|UID * D=DATASCHEMA|Description of the data schema|UTF-8|XXXCCCWWW * # --- Table * # Name | Descr | Type[A/S/U]| DBName|Flags * T=TableName|Table description|A|DBTable| * # --- Fields * # Name| Description|Type [D/C/N/I]|Length|Precision| * F=CURDATE|Current date|D|8|0| * #--- Indexes * # Name| Description |Unique [0/1]|Index fields|DBName * I=IDD|Index of ID|0|ID,DATE,DESCR(UPPER)|IDD * # END *
QDataSchema::QDataSchema | ( | ConnectionType | ct, | |
const char * | objname = 0 | |||
) |
Create and initialise new object.
bool QDataSchema::open | ( | const QString & | dbname, | |
const QString & | dbuser = "" , |
|||
const QString & | dbpass = "" , |
|||
const QString & | dbhost = "localhost" , |
|||
int | dbport = 0 | |||
) |
Open and initialise database server connection.
dbname | (in) - Database name. | |
dbuser | (in) - Database user name. | |
dbpass | (in) - Database password. | |
dbhost | (in) - Database host. | |
dbport | (in) - Database connection port. |
bool QDataSchema::open | ( | ) |
Open and init database connection.
QSqlDatabase * QDataSchema::db | ( | ) |
Returns database connection.
bool QDataSchema::databaseCreate | ( | const QString & | dbadmuser, | |
const QString & | dbadmpass | |||
) |
Create new database.
Create new database with name was seted before. For create database operation you are need to know database server administrator name and password.
dbadmuser | (in) Database server administrator name | |
dbadmpass | (in) Database server administrator password |
bool QDataSchema::databaseDrop | ( | const QString & | dbadmuser, | |
const QString & | dbadmpass | |||
) |
Drop current database.
Drop database with name was seted before. For database drop operation you are need to know database server administrator name and password.
dbadmuser | (in) Database server administrator name | |
dbadmpass | (in) Database server administrator password |
QSqlCursor * QDataSchema::table | ( | const QString & | name = QString::null |
) |
Creates QSqlCursor object for database table.
Returned object can be used for browsing and editing table in current database.
name | (in) - sql table name. |
bool QDataSchema::tableExists | ( | const QString & | name | ) |
Checks table existence.
name | (in) - table name to check for. |
QStringList QDataSchema::drivers | ( | ) | [static] |
Return a drivers list.
bool QDataSchema::isDriverAvailable | ( | const QString & | name | ) | [static] |
Return a true if dataschema driver Name available.
void QDataSchema::setDataDictionary | ( | const QStringList & | dd_new | ) |
Set data dictionary for application.
QStringList QDataSchema::dataDictionary | ( | ) |
Return data dictionary.
QStringList QDataSchema::updateStructureQuery | ( | ) |
Return queries list for database update.
Queries list prepares while verifyStructure() executes.
QStringList QDataSchema::verifyLog | ( | ) |
Return list of differents.
It is show differents in data base and application dictionaries.
int QDataSchema::databaseExport | ( | const QString & | filename | ) | [virtual] |
Unload information from database.
filename | (in) name of file for data exchange |
int QDataSchema::databaseImport | ( | const QString & | filename, | |
bool | updateStruct = false | |||
) | [virtual] |
Load information into data base.
filename | (in) name of file for data exchange | |
updateStruct | (in) true - update structure, if it is different from existence, false - to return error code. |
void QDataSchema::verifyLogRecord | ( | QString | op, | |
QString | value, | |||
QString | oldvalue = "" | |||
) | [protected] |
Register record of change in database.
Calls when differenses list forms on execution verifyStructure(). Generates verifyMessage() signal.
op | (in) operation: "+" - add, "-" - delete, "&" - change | |
value | (in) new value of changed dictionary element | |
oldvalue | (in) old value of changed dictionary element. Empty for add and delete operations. |
QString QDataSchema::ddRecSection | ( | const QString & | rec, | |
int | secnum | |||
) | [protected] |
Return dictionary element record section.
rec | (in) record (line) of the dictionary | |
secnum | (in) section number, starts from 0. Sections divide by "|" symbol. |
QString QDataSchema::ddRecType | ( | const QString & | rec | ) | [protected] |
Return type of dictionary element record.
rec | (in) record (line) of the dictionary |
QString QDataSchema::ddRecName | ( | const QString & | rec | ) | [protected] |
Return name of dictionary element record.
rec | (in) record (line) of the dictionary |
void QDataSchema::joinLists | ( | QStringList & | list, | |
const QStringList & | add | |||
) | [protected] |
Append one string list to another.
list | (in) string list for expansion | |
add | (in) string list for add to "list" |