PDA

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



Auryn
1st September 2008, 18: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, 20:08
And what is the problem?

jacek
2nd September 2008, 00: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
2nd September 2008, 00:57
A much better is to implement static checks knowing the structure of the database.