qx::dao::count

Open discussion on QxOrm library

qx::dao::count

Postby andigor » Wed May 02, 2012 11:57 am

How can I get the QSqlError object when calling qx::dao::count function ?
andigor
 
Posts: 14
Joined: Wed May 02, 2012 11:53 am

Re: qx::dao::count

Postby QxOrm admin » Wed May 02, 2012 3:12 pm

How can I get the QSqlError object when calling qx::dao::count function ?

You can't !
qx::dao::count will return 0 if an error occured (and you can't get the QSqlError associated if an error occured).
Sorry for that, but SELECT Count(*) is a very basic SQL query, so the only way to have an error is a connexion error, or if you add a qx::QxSqlQuery parameter to the default query.
If you really want a QSqlError object executing a Count(*) SQL query, you have to use QtSql module directly.
QxOrm admin
 

Re: qx::dao::count

Postby andigor » Wed May 02, 2012 3:47 pm

Select count(*) seems very simple operation. But I use "where" clause with some additional conditions. And another question what about executing stored procedures and functions with QxOrm ? ;)
andigor
 
Posts: 14
Joined: Wed May 02, 2012 11:53 am

Re: qx::dao::count

Postby QxOrm admin » Thu May 03, 2012 7:16 am

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 ?
QxOrm admin
 

Re: qx::dao::count

Postby Guest » Thu May 03, 2012 8:50 am

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 ;)


Yes, but I don't know the reason, because if it was an error I want to show message, and don't do anything else in my program. So I changed a little your code: QSqlError qx::dao::count(long& c, const qx::QxSqlQuerry& ) Now it looks like other functions in qx::dao namespace

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 ?


If you provide QSqlError qx::dao::execute_query<T>(const qx::QxSqlQuery &, T &, QSqlDatabase * p = NULL) and expand qx::QxSqlQuery interace for calling proc and functions it will be nice because a lot of functionality can be used with that.
For unification reason seems to be better to use the registered class for result of the qx::dao::execute_query. Using real structs (not dynamic) is more easier (because of compiler and IDE help) and beautifully.

In general your style of qx::QSqlQuery is cool because mixing SQL and C++ code looks a little bit ugly. But it seems it do not provides all SQL functionality (math functions in where clause for example) because it very big work. From another side I planing to expand your reflection mechanism for all my program, because now it uses QMap<QString, QVariant> and have a lot of ugly code with converting and checking types and presence of data.
Guest
 

Re: qx::dao::count

Postby QxOrm admin » Thu May 03, 2012 9:20 am

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
 

Re: qx::dao::count

Postby Guest » Thu May 03, 2012 10:11 am

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.


I understand that modification of your code will close way for automatic updates with newer releases. But maybe you think about adding this functionality? All functions in qx::dao returns QSqlError and only one doesn't :roll:

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);

Ok. Thanks a lot.

So tell me please can we expect for the beta of QxOrm 1.2.4 ?
Guest
 

Re: qx::dao::count

Postby QxOrm admin » Fri May 04, 2012 7:05 am

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...
QxOrm admin
 

Re: qx::dao::count

Postby andigor » Fri May 04, 2012 8:41 am

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...


Thanks a lot.
andigor
 
Posts: 14
Joined: Wed May 02, 2012 11:53 am

Re: qx::dao::count

Postby QxOrm admin » Fri May 04, 2012 1:07 pm

Here is a new BETA version of QxOrm library 1.2.4 :
http://www.qxorm.com/version/QxOrm_1.2.4_BETA_16.zip

There is a new signature for qx::dao::count function :
Code: Select all
template <class T>
QSqlError count(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL);


And there is a new template function called qx::dao::execute_query :
Code: Select all
template <class T>
QSqlError execute_query(qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL);


I also created another function called qx::dao::call_query (without template) to call a custom SQL query or a stored procedure without passing a registered class.

Here is an example (added to qxBlog project to make some tests) :
Code: Select all
// 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);


And here is the documentation associated to qx::dao::execute_query function :
/*!
* \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
*/
QxOrm admin
 

Next

Return to QxOrm - Open discussion

Who is online

Users browsing this forum: No registered users and 5 guests