How can I get the QSqlError object when calling qx::dao::count function ?
Select count(*) seems very simple operation. But I use "where" clause with some additional conditions.
what about executing stored procedures and functions with QxOrm ?
QxOrm admin wrote:Select count(*) seems very simple operation. But I use "where" clause with some additional conditions.
If your additional condition contains an error, there is no result associated to your query.
So, returning 0, qx::dao::count function doesn't have a bad behaviour, since there is no result corresponding to your query
what about executing stored procedures and functions with QxOrm ?
Sorry, it is not supported yet by QxOrm library 1.2.3 (you have to use QtSql module directly).
But I think it's quite easy to implement using introspection engine of QxOrm library : if you have a C++ class (registered into QxOrm context) to map the result of a stored procedure, it would be easy to implement a generic function.
If you are available to make some tests, I can provide you quite quickly a new BETA version of QxOrm 1.2.4...
And I can include into this BETA a function called : QSqlError qx::dao::execute_query<T>(const qx::QxSqlQuery &, T &, QSqlDatabase * p = NULL); (or something like this).
With this function, you could execute your own query or function or stored procedure.
And if the resultset can be mapped with T class, then all properties will be fetched automatically.
We could also provide a generic structure containing all the results of the query without having to provide a registered class : something like QList<QList<QVariant>> to store all results of the query.
What do you think about it ?
So I changed a little your code: QSqlError qx::dao::count(long& c, const qx::QxSqlQuerry& )
But it seems it do not provides all SQL functionality (math functions in where clause for example)
QxOrm admin wrote:So I changed a little your code: QSqlError qx::dao::count(long& c, const qx::QxSqlQuerry& )
I understand your point of vue, but I don't recommend to modify the source code of QxOrm library (this is not a bug, just a new feature you want to add for your case).
So you can create your own function into your project that you can include into qx::dao namespace if you want.
But it seems it do not provides all SQL functionality (math functions in where clause for example)
You can use this method to add math or other functions building your SQL query :
qx::QxSqlQuery & qx::QxSqlQuery::freeText(const QString & text);
QxOrm admin wrote:I think I could provide a new BETA version of QxOrm 1.2.4 next week...
And I will include also your qx::dao::count request adding a function returning a QSqlError instance...
template <class T>
QSqlError count(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL);
template <class T>
QSqlError execute_query(qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL);
// Call a custom SQL query or a stored procedure
qx_query testStoredProc("SELECT * FROM author");
daoError = qx::dao::call_query(testStoredProc);
qAssert(! daoError.isValid());
testStoredProc.dumpSqlResult();
// Call a custom SQL query or a stored procedure and fetch automatically properties (with a collection of items)
qx_query testStoredProcBis("SELECT * FROM author");
authorX.clear();
daoError = qx::dao::execute_query(testStoredProcBis, authorX);
qAssert(! daoError.isValid()); qAssert(authorX.count() > 0);
qx::dump(authorX);
// Call a custom SQL query or a stored procedure and fetch automatically properties
qx_query testStoredProcThird("SELECT name, category_id FROM category");
category_ptr category_tmp = category_ptr(new category());
daoError = qx::dao::execute_query(testStoredProcThird, category_tmp);
qAssert(! daoError.isValid()); qAssert(category_tmp->m_id != 0);
qx::dump(category_tmp);
/*!
* \ingroup QxDao
* \brief Execute a custom SQL query or a stored procedure, all columns that can be mapped to the instance of type T will be fetched automatically
* \param query Define a custom SQL query or a stored procedure to call
* \param t Instance of type T, all columns that can be mapped to this instance will be fetched automatically
* \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter)
* \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query
*/
Return to QxOrm - Open discussion
Users browsing this forum: No registered users and 2 guests