Printing queries

Forum for posting problems using QxOrm library

Printing queries

Postby xormwolf » Tue May 14, 2019 11:47 am

Hello,
I have been implementing QxOrm in our c++ project and now that I am transforming more complex sql (string) statements into the QxOrm format, I need to have QxOrm print the generated /called queries.
we need these prints for validation.

In the examples from the blog there are automatic prints to command line but I am not able to get them.

After some investigation, I can see that the QX_SQL_BUILDER_INIT_FCT and QX_SQL_BUILDER_INIT_FCT_WITH_RELATION hold the QString sql variable.
Is there a simple command to just dump the generated query from the QxSqlQueryBuilder?

Thank you!
xormwolf
 
Posts: 4
Joined: Tue May 14, 2019 11:40 am

Re: Printing queries

Postby qxorm » Tue May 14, 2019 6:54 pm

Hello,

By default, QxOrm library prints all SQL queries in output console.
You can use the following option to make logs more "readable" :
Code: Select all
qx::QxSqlDatabase::getSingleton()->setFormatSqlQueryBeforeLogging(true);


If you want to redirect all logs to an output file, then you have to use this Qt feature (qInstallMessageHandler) :
Code: Select all
qInstallMessageHandler(myMessageOutput);

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
// write all logs to an output file...
}

More details here : https://stackoverflow.com/questions/495 ... etc-output

QxOrm QxSqlDatabase singleton class provides other options about SQL queries :
- setTraceSqlQuery() : enable or disable SQL queries logs
- setTraceSqlRecord() : trace all SQL result rows values
- setTraceSqlBoundValues() : trace SQL queries placeholder values (? for example)
- setTraceSqlBoundValuesOnError() : trace SQL queries placeholder values (? for example) only when an error occurred
- setFormatSqlQueryBeforeLogging() : add some break line for pretty printing SQL queries
- setTraceSqlOnlySlowQueriesDatabase() : trace only slow SQL queries (response time from database)
- setTraceSqlOnlySlowQueriesTotal() : trace only slow SQL queries (C++ parsing + response time from database)
qxorm
Site Admin
 
Posts: 483
Joined: Mon Apr 12, 2010 7:45 am

Re: Printing queries

Postby qxorm » Tue May 14, 2019 7:34 pm

You can also create your own SQL generator : for example, if you are working with a MySQL database, you can create a class which inherits from qx::dao::detail::QxSqlGenerator_MySQL.

In your custom class (for example named MySQLGenerator), just override onBeforeSqlPrepare() and/or formatSqlQuery() methods (you will have access to the SQL generated by QxOrm library, and you can log or modify it if you want) :
Code: Select all
virtual void onBeforeSqlPrepare(IxDao_Helper * pDaoHelper, QString & sql) const;
virtual void formatSqlQuery(IxDao_Helper * pDaoHelper, QString & sql) const;


Then register your class like this :
Code: Select all
std::shared_ptr<MySQLGenerator> pGenerator = std::make_shared<MySQLGenerator>();
qx::QxSqlDatabase::getSingleton()->setSqlGenerator(pGenerator);
qxorm
Site Admin
 
Posts: 483
Joined: Mon Apr 12, 2010 7:45 am

Re: Printing queries

Postby xormwolf » Wed May 15, 2019 9:26 am

Thank you for your prompt reply, qxorm.

The issue arises from qDebug that does not print anything. We have already tried subclassing but no luck.

Also, we checked if QT_NO_DEBUG_OUTPUT is set somewhere so qDebug will not print, but can not find it.
We tried doing #undef QT_NO_DEBUG_OUTPUT in several places but also no luck there.

However, qInfo and qWarning work. We are working with oracle linux and qt5.

Kind regards
xormwolf
 
Posts: 4
Joined: Tue May 14, 2019 11:40 am

Re: Printing queries

Postby qxorm » Wed May 15, 2019 11:28 am

If qDebug doesn't log anything, even in output console, then you should ask in a Qt forum (this is not a QxOrm issue because it uses qDebug like any other Qt programs).

So you should try to create your custom SQL generator class (which inherits from qx::dao::detail::QxSqlGenerator_Oracle in your case).
This way, you can handle onBeforeSqlPrepare() and/or formatSqlQuery() methods without your qDebug issue.
qxorm
Site Admin
 
Posts: 483
Joined: Mon Apr 12, 2010 7:45 am

Re: Printing queries

Postby xormwolf » Thu May 16, 2019 11:44 am

hi again, thank you for your answers, they guided us in the correct direction!
xormwolf
 
Posts: 4
Joined: Tue May 14, 2019 11:40 am

Re: Printing queries

Postby qxorm » Thu May 16, 2019 2:45 pm

they guided us in the correct direction!

Great !
qxorm
Site Admin
 
Posts: 483
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 6 guests

cron