![]() |
cutelyst 5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
|
Helper methods to perform and work with database queries. More...

Classes | |
| class | Cutelyst::Sql::Transaction |
| This is a helper class to create scoped transactions. More... | |
Macros | |
| #define | CPreparedSqlQuery(str) |
| #define | CPreparedSqlQueryFO(str) |
| #define | CPreparedSqlQueryForDatabase(str, db) |
| #define | CPreparedSqlQueryForDatabaseFO(str, db) |
| #define | CPreparedSqlQueryThread(str) |
| #define | CPreparedSqlQueryThreadFO(str) |
| #define | CPreparedSqlQueryThreadForDB(str, db) |
| #define | CPreparedSqlQueryThreadForDBFO(str, db) |
Functions | |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT void | Cutelyst::Sql::bindParamsToQuery (QSqlQuery &query, const Cutelyst::ParamsMultiMap ¶ms, bool htmlEscaped=true) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QString | Cutelyst::Sql::databaseNameThread (const QString &dbName={}) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QSqlDatabase | Cutelyst::Sql::databaseThread (const QString &dbName={}) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QSqlQuery | Cutelyst::Sql::preparedQuery (const QString &query, QSqlDatabase db=QSqlDatabase(), bool forwardOnly=false) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QSqlQuery | Cutelyst::Sql::preparedQueryThread (const QString &query, const QString &dbName={}, bool forwardOnly=false) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantList | Cutelyst::Sql::queryToHashList (QSqlQuery &query) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantHash | Cutelyst::Sql::queryToHashObject (QSqlQuery &query) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantHash | Cutelyst::Sql::queryToIndexedHash (QSqlQuery &query, const QString &key) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonObject | Cutelyst::Sql::queryToIndexedJsonObject (QSqlQuery &query, const QString &key) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonArray | Cutelyst::Sql::queryToJsonArray (QSqlQuery &query) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonObject | Cutelyst::Sql::queryToJsonObject (QSqlQuery &query) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonArray | Cutelyst::Sql::queryToJsonObjectArray (QSqlQuery &query) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantList | Cutelyst::Sql::queryToList (QSqlQuery &query) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantList | Cutelyst::Sql::queryToMapList (QSqlQuery &query) |
| CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantMap | Cutelyst::Sql::queryToMapObject (QSqlQuery &query) |
The Sql plugin provides methods and classes in the Sql namespace to help with performing database queries and to handle the query results.
Setup the database and the per thread database connection in your application class. Database connections should be established in your reimplementation of Application::postFork(). Creating the database can be done in your reimplementation of Application::init(). You could also use external tools to create the database layout. On bigger project you will mostly want to use some kind of database migration helper. In this example we will use a simple SQLite database that will be created in our init method if not already existing.
myapp.h:
Implementation in myapp.cpp:
Use our database in a controller listing available users:
| #define CPreparedSqlQuery | ( | str | ) |
Constructs a static QSqlQuery with query str on the default database using Cutelyst::Sql::preparedQuery(). The created QSqlQuery object will be returned.
| #define CPreparedSqlQueryFO | ( | str | ) |
Constructs a static QSqlQuery with query str on the default database using Cutelyst::Sql::preparedQuery() with forwardOnly set to true. The crated query will be returned.
| #define CPreparedSqlQueryForDatabase | ( | str, | |
| db ) |
Constructs a static thread local QSqlQuery with query str on database db using Cutelyst::Sql::preparedQuery() method. The created QSqlQuery object will be returned.
| #define CPreparedSqlQueryForDatabaseFO | ( | str, | |
| db ) |
Constructs a static thread local QSqlQuery with query str on database db using Cutelyst::Sql::preparedQueryThread() with forwardOnly set to true. The created QSqlQuery will be returned.
| #define CPreparedSqlQueryThread | ( | str | ) |
Constructs a static thread local QSqlQuery with query str on the database for the current thread using Cutelyst::Sql::preparedQueryThread(). The created QSqlQuery object will be returned.
| #define CPreparedSqlQueryThreadFO | ( | str | ) |
Constructs a static thread local QSqlQuery with query str on the database for the current thread using Cutelyst::Sql::preparedQueryThread() with forwardOnly set to true. The created QSqlQuery object will be returned.
| #define CPreparedSqlQueryThreadForDB | ( | str, | |
| db ) |
Constructs a static thread local QSqlQuery with query str on database db using Cutelyst::Sql::preparedQueryThread(). The created QSqlQuery object will be returned.
| #define CPreparedSqlQueryThreadForDBFO | ( | str, | |
| db ) |
Constructs a static thread local QSqlQuery with query str on database db using Cutelyst::Sql::preparedQueryThread() with forwardOnly set to true. The created QSqlQuery object will be returned.
| void Cutelyst::Sql::bindParamsToQuery | ( | QSqlQuery & | query, |
| const Cutelyst::ParamsMultiMap & | params, | ||
| bool | htmlEscaped = true ) |
| QString Cutelyst::Sql::databaseNameThread | ( | const QString & | dbName = {} | ) |
Returns a string with as "dbName-threadNumber" to be used for connecting
Definition at line 260 of file sql.cpp.
Referenced by databaseThread(), and preparedQueryThread().
| QSqlDatabase Cutelyst::Sql::databaseThread | ( | const QString & | dbName = {} | ) |
Returns a QSqlDatabase named as "dbName-threadNumber" to be used for QSqlQuery
Definition at line 265 of file sql.cpp.
References databaseNameThread().
Referenced by Cutelyst::Sql::Transaction::Transaction().
| QSqlQuery Cutelyst::Sql::preparedQuery | ( | const QString & | query, |
| QSqlDatabase | db = QSqlDatabase(), | ||
| bool | forwardOnly = false ) |
Returns a QSqlQuery object prepared with query using the database db. This is specially useful to avoid pointers to prepared queries.
For applications that use default QSqlDatabase() connection, not thread-safe: QSqlQuery query = CPreparedSqlQuery("SELECT * FROM");
For applications that do not use default QSqlDatabase(), the returned QSqlQuery is a CUTELYST_PLUGIN_UTILS_SQL_EXPORT thread_local which glues QSqlQuery to the current thread but you must have a per thread QSqlDatabase() connection for this to be completely safe: QSqlQuery query = CPreparedSqlQueryForDatabase("SELECT * FROM", QSqlDatabase::data);
The returned object is set to forward only and you must use a different database connection and thread_local on CUTELYST_PLUGIN_UTILS_SQL_EXPORT objects to be thread-safe.
| QSqlQuery Cutelyst::Sql::preparedQueryThread | ( | const QString & | query, |
| const QString & | dbName = {}, | ||
| bool | forwardOnly = false ) |
Returns a QSqlQuery object prepared with query using the dbName database which will automatically get's in the form of databaseNameThread(). This is specially useful to avoid pointers to prepered queries.
For applications that uses default QSqlDatabase() connection, not thread-safe: QSqlQuery query = CPreparedSqlQuery("SELECT * FROM");
For applications that do not use default QSqlDatabase(), the returned QSqlQuery is a CUTELYST_PLUGIN_UTILS_SQL_EXPORT thread_local which glues QSqlQuery to the current thread but you must have a per thread QSqlDatabase() connection for this to be completely safe: QSqlQuery query = CPreparedSqlQueryForDatabase("SELECT * FROM", QSqlDatabase::data);
The returned object is set to forward only and you must use a different database connection and thread_local on CUTELYST_PLUGIN_UTILS_SQL_EXPORT objects to be thread-safe.
Definition at line 249 of file sql.cpp.
References databaseNameThread().
| QVariantList Cutelyst::Sql::queryToHashList | ( | QSqlQuery & | query | ) |
| QVariantHash Cutelyst::Sql::queryToHashObject | ( | QSqlQuery & | query | ) |
| QVariantHash Cutelyst::Sql::queryToIndexedHash | ( | QSqlQuery & | query, |
| const QString & | key ) |
| QJsonObject Cutelyst::Sql::queryToIndexedJsonObject | ( | QSqlQuery & | query, |
| const QString & | key ) |
| QJsonArray Cutelyst::Sql::queryToJsonArray | ( | QSqlQuery & | query | ) |
| QJsonObject Cutelyst::Sql::queryToJsonObject | ( | QSqlQuery & | query | ) |
| QJsonArray Cutelyst::Sql::queryToJsonObjectArray | ( | QSqlQuery & | query | ) |
| QVariantList Cutelyst::Sql::queryToList | ( | QSqlQuery & | query | ) |
| QVariantList Cutelyst::Sql::queryToMapList | ( | QSqlQuery & | query | ) |