PDA

View Full Version : How to verify a QSqlRecord with PostgreSQL restrictions



Auryn
1st September 2008, 17:23
I have these connection:

connect((const QObject *)my_model, SIGNAL(beforeUpdate(int, QSqlRecord &)), this, SLOT(My_verify_record(int, QSqlRecord &)));
connect((const QObject *)my_model, SIGNAL(primeInsert(int, QSqlRecord &)), this, SLOT(My_verify_record(int, QSqlRecord &)));

This is my slot definition:

void My_class::My_verify_record(int my_row, QSqlRecord &my_record)
{
for(int my_column = 0; my_column < my_record.count(); my_column++)
{
// Verify that current column content fulfills the field definition in PostgreSQL
}
}

For example, if one field is defined as "NOT NULL" in PostgreSQL, I need to detect this fact inside the loop to warn the user.

wysota
1st September 2008, 19:08
And what is the problem?

jacek
1st September 2008, 23:54
For example, if one field is defined as "NOT NULL" in PostgreSQL, I need to detect this fact inside the loop to warn the user.
QSqlField provides some basing information, but you can query information_schema to get information about all constraints. The problem is that you will have to parse the contents of check_clause field from information_schema.check_constraints.

You can validate a record by starting a transaction, inserting data and rolling the transaction back, but that's not an efficient solution.

wysota
1st September 2008, 23:57
A much better is to implement static checks knowing the structure of the database.