Qx v0.7
Qt Extensions Library
|
The SqlDatabase class provides straightforward access to an SQL database. More...
#include <qx/sql/qx-sqldatabase.h>
Public Member Functions | |
SqlDatabase (const QString &databaseName, const QString &driver) | |
SqlDatabase (const SqlDatabase &other) | |
SqlDatabase (SqlDatabase &&other) | |
~SqlDatabase () | |
template<QxSql::sql_struct First, QxSql::sql_struct... Rest> | |
SqlError | checkSchema (SqlSchemaReport &report, SqlSchemaReport::StrictnessFlags strictness=SqlSchemaReport::Lenient) |
bool | closeConnection () |
SqlError | connect () |
SqlError | database (QSqlDatabase &db, bool connect=true) |
QString | databaseName () const |
SqlDmlQuery | DELETE () |
QString | driver () const |
template<sql_stringable First, sql_stringable ... Rest> | |
SqlDmlQuery | INSERT_INTO (const SqlString &table, First &&fs, Rest &&... s) |
bool | isConnected () const |
template<sql_stringable First> | |
SqlDmlQuery | MERGE_INTO (First &&fs) |
SqlDatabase & | operator= (const SqlDatabase &other) |
SqlDatabase & | operator= (SqlDatabase &&other) |
template<QxSql::sql_struct First, QxSql::sql_struct... Rest> | |
SqlDqlQuery | SELECT () |
template<sql_stringable First, sql_stringable ... Rest> | |
SqlDqlQuery | SELECT (First &&fs, Rest &&... s) |
template<QxSql::sql_struct First, QxSql::sql_struct... Rest> | |
SqlDqlQuery | SELECT_DISTINCT () |
template<sql_stringable First, sql_stringable ... Rest> | |
SqlDqlQuery | SELECT_DISTINCT (First &&fs, Rest &&... s) |
template<QxSql::sql_struct Struct> | |
SqlDmlQuery | UPDATE () |
template<sql_stringable First> | |
SqlDmlQuery | UPDATE (First &&fs) |
SqlDatabase acts as a somewhat higher level version of QSqlDatabase and allows iteration with a database in a more streamlined fashion. Although it does not provide the same depth of capabilities as using QSqlDatabase directly, numerous common SQL tasks are greatly simplified with its use, as many of the more meticulous steps of database communication are handled for you transparently.
Unlike QSqlDatabase there are no issues or catches with storing SqlDatabase as a class member.
Each instance represents a single-connection to a database per-thread. Although it is possible to manually open/close the underlying connection with the database, one generally does not need to do so; SqlDatabase will automatically create and open a thread specific connection with the specified database as necessary, and reuse that connection throughout its lifetime for any work that occurs in that same thread. This means that one instance can safely be used by multiple threads, though each thread will access the database via its own connection, so there may be additional limitations imposed by specific database drivers related to concurrent connections.
The different types of queries may be created directly from an instance using the various SQL keyword-based methods, such as SELECT() and UPDATE().
|
explicit |
Creates a database accessor for a database with name databaseName, using driver driver.
No underlying connection initially exists, but will automatically be created and opened as soon as it's needed.
Qx::SqlDatabase::SqlDatabase | ( | const SqlDatabase & | other | ) |
Creates a copy of other.
The copy will not share the connection(s) of the original and instead use its own, making this constructor convenient for preparing multiple connections to the same database after having specified the connection details once.
|
default |
Move constructs this from other.
The new instance will continue to utilize the same connection(s) that other did.
Qx::SqlDatabase::~SqlDatabase | ( | ) |
Destroys the accessor, closing and removing any underlying connections across all threads in which it was used.
|
inline |
Evaluates if the database matches the schema provided by First through Rest in accordance with strictness, and sets report to a report noting any deviations from this schema.
Each specified SQL-tied struct type is treated as a table, with it's members representing that tables fields. The presence of each of those tables and their corresponding fields is checked for, with some exceptions depending on the value of strictness.
Returns a valid error if there was an issue inspecting the schema.
bool Qx::SqlDatabase::closeConnection | ( | ) |
Closes the underlying connection to the database for the current thread, if present.
Returns true
if there was originally a connection open; otherwise, returns false
.
SqlError Qx::SqlDatabase::connect | ( | ) |
Forces the creation of the underlying connection for the current thread.
You generally will never need to call this function
SqlError Qx::SqlDatabase::database | ( | QSqlDatabase & | db, |
bool | connect = true ) |
Provides access to the underlying database connection via db, and returns a valid error object if there was a problem connecting to the database.
If connect is false
, then the connection will only be provided if the accessor had already setup the connection, with db instead set to an invalid instance if not.
QString Qx::SqlDatabase::databaseName | ( | ) | const |
Returns the database name that the accessor was configured to use.
|
inline |
Returns a new DML style query that starts with a DELETE
clause.
QString Qx::SqlDatabase::driver | ( | ) | const |
Returns the driver that the accessor was configured to use.
|
inline |
Returns a new DML style query that starts with an INSERT INTO
clause using table table and fs through s.
bool Qx::SqlDatabase::isConnected | ( | ) | const |
Returns true
if the accessor has setup a connection for the current thread; otherwise, returns false
.
|
inline |
Returns a new DML style query that starts with a MERGE INTO
clause using fs.
SqlDatabase & Qx::SqlDatabase::operator= | ( | const SqlDatabase & | other | ) |
Creates a copy of other.
The copy will not share the connection(s) of the original and instead use its own, making this operator convenient for preparing multiple connections to the same database after having specified the connection details once.
|
default |
Move assigns this from other.
The new instance will continue to utilize the same connection(s) that other did.
|
inline |
Returns a new DQL style query that starts with a SELECT
clause using all of the fields from First through s.
|
inline |
Returns a new DQL style query that starts with a SELECT
clause using fs through s.
|
inline |
Returns a new DQL style query that starts with a SELECT
clause using all of the fields from First through Rest, followed by the keyword DISTINCT
.
|
inline |
Returns a new DQL style query that starts with a SELECT
clause using fs through s, followed by the keyword DISTINCT
.
|
inline |
Returns a new DML style query that starts with an UPDATE
clause using Struct.
|
inline |
Returns a new DML style query that starts with an UPDATE
clause using fs.