Qx v0.7
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::SqlDatabase Class Reference

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)
 
SqlDatabaseoperator= (const SqlDatabase &other)
 
SqlDatabaseoperator= (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)
 

Detailed Description

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.

Multi-threading & Execution order

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.

Note
If you want to work with a single database via multiple connections within the same thread, create a new SqlDatabase instance for each connection.

The different types of queries may be created directly from an instance using the various SQL keyword-based methods, such as SELECT() and UPDATE().

Note
Currently, connection details cannot be changed after construction. If you need to alter these later, construct a new instance.

Constructor & Destructor Documentation

◆ SqlDatabase() [1/3]

Qx::SqlDatabase::SqlDatabase ( const QString & databaseName,
const QString & driver )
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.

◆ SqlDatabase() [2/3]

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.

◆ SqlDatabase() [3/3]

Qx::SqlDatabase::SqlDatabase ( SqlDatabase && other)
default

Move constructs this from other.

The new instance will continue to utilize the same connection(s) that other did.

◆ ~SqlDatabase()

Qx::SqlDatabase::~SqlDatabase ( )

Destroys the accessor, closing and removing any underlying connections across all threads in which it was used.

Member Function Documentation

◆ checkSchema()

template<QxSql::sql_struct First, QxSql::sql_struct... Rest>
SqlError Qx::SqlDatabase::checkSchema ( SqlSchemaReport & report,
SqlSchemaReport::StrictnessFlags strictness = SqlSchemaReport::Lenient )
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.

◆ closeConnection()

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.

◆ connect()

SqlError Qx::SqlDatabase::connect ( )

Forces the creation of the underlying connection for the current thread.

You generally will never need to call this function

◆ database()

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.

◆ databaseName()

QString Qx::SqlDatabase::databaseName ( ) const

Returns the database name that the accessor was configured to use.

◆ DELETE()

SqlDmlQuery Qx::SqlDatabase::DELETE ( )
inline

Returns a new DML style query that starts with a DELETE clause.

◆ driver()

QString Qx::SqlDatabase::driver ( ) const

Returns the driver that the accessor was configured to use.

◆ INSERT_INTO()

template<sql_stringable First, sql_stringable ... Rest>
SqlDmlQuery Qx::SqlDatabase::INSERT_INTO ( const SqlString & table,
First && fs,
Rest &&... s )
inline

Returns a new DML style query that starts with an INSERT INTO clause using table table and fs through s.

◆ isConnected()

bool Qx::SqlDatabase::isConnected ( ) const

Returns true if the accessor has setup a connection for the current thread; otherwise, returns false.

◆ MERGE_INTO()

template<sql_stringable First>
SqlDmlQuery Qx::SqlDatabase::MERGE_INTO ( First && fs)
inline

Returns a new DML style query that starts with a MERGE INTO clause using fs.

◆ operator=() [1/2]

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.

◆ operator=() [2/2]

SqlDatabase & Qx::SqlDatabase::operator= ( SqlDatabase && other)
default

Move assigns this from other.

The new instance will continue to utilize the same connection(s) that other did.

◆ SELECT() [1/2]

template<QxSql::sql_struct First, QxSql::sql_struct... Rest>
SqlDqlQuery Qx::SqlDatabase::SELECT ( )
inline

Returns a new DQL style query that starts with a SELECT clause using all of the fields from First through s.

◆ SELECT() [2/2]

template<sql_stringable First, sql_stringable ... Rest>
SqlDqlQuery Qx::SqlDatabase::SELECT ( First && fs,
Rest &&... s )
inline

Returns a new DQL style query that starts with a SELECT clause using fs through s.

◆ SELECT_DISTINCT() [1/2]

template<QxSql::sql_struct First, QxSql::sql_struct... Rest>
SqlDqlQuery Qx::SqlDatabase::SELECT_DISTINCT ( )
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.

◆ SELECT_DISTINCT() [2/2]

template<sql_stringable First, sql_stringable ... Rest>
SqlDqlQuery Qx::SqlDatabase::SELECT_DISTINCT ( First && fs,
Rest &&... s )
inline

Returns a new DQL style query that starts with a SELECT clause using fs through s, followed by the keyword DISTINCT.

◆ UPDATE() [1/2]

template<QxSql::sql_struct Struct>
SqlDmlQuery Qx::SqlDatabase::UPDATE ( )
inline

Returns a new DML style query that starts with an UPDATE clause using Struct.

◆ UPDATE() [2/2]

template<sql_stringable First>
SqlDmlQuery Qx::SqlDatabase::UPDATE ( First && fs)
inline

Returns a new DML style query that starts with an UPDATE clause using fs.


The documentation for this class was generated from the following files: