Page 1 of 1

Printing queries

PostPosted: Tue May 14, 2019 11:47 am
by xormwolf
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!

Re: Printing queries

PostPosted: Tue May 14, 2019 6:54 pm
by qxorm
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)

Re: Printing queries

PostPosted: Tue May 14, 2019 7:34 pm
by qxorm
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);

Re: Printing queries

PostPosted: Wed May 15, 2019 9:26 am
by xormwolf
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

Re: Printing queries

PostPosted: Wed May 15, 2019 11:28 am
by qxorm
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.

Re: Printing queries

PostPosted: Thu May 16, 2019 11:44 am
by xormwolf
hi again, thank you for your answers, they guided us in the correct direction!

Re: Printing queries

PostPosted: Thu May 16, 2019 2:45 pm
by qxorm
they guided us in the correct direction!

Great !