Akonadi::Server::QueryBuilder

Search for usage in LXR

Akonadi::Server::QueryBuilder Class Reference

#include <querybuilder.h>

Inheritance diagram for Akonadi::Server::QueryBuilder:

Public Types

enum  ConditionType { WhereCondition , HavingCondition , NUM_CONDITIONS }
 
enum  JoinType { InnerJoin , LeftJoin }
 
enum  QueryType { Select , Insert , Update , Delete }
 

Public Member Functions

 QueryBuilder (const QSqlQuery &tableQuery, const QString &tableQueryAlias)
 
 QueryBuilder (const QString &table, QueryType type=Select)
 
 QueryBuilder (DataStore *store, const QSqlQuery &tableQuery, const QString &tableQueryAlias)
 
 QueryBuilder (DataStore *store, const QString &table, QueryType type=Select)
 
void addAggregation (const QString &col, const QString &aggregate)
 
void addAggregation (const Query::Case &caseStmt, const QString &aggregate)
 
void addColumn (const QString &col)
 
void addColumn (const Query::Case &caseStmt)
 
void addColumnCondition (const QString &column, Query::CompareOperator op, const QString &column2, ConditionType type=WhereCondition)
 
void addColumns (const QStringList &cols)
 
void addCondition (const Query::Condition &condition, ConditionType type=WhereCondition)
 
void addGroupColumn (const QString &column)
 
void addGroupColumns (const QStringList &columns)
 
void addJoin (JoinType joinType, const QString &table, const QString &col1, const QString &col2)
 
void addJoin (JoinType joinType, const QString &table, const Query::Condition &condition)
 
void addSortColumn (const QString &column, Query::SortOrder order=Query::Ascending)
 
void addValueCondition (const QString &column, Query::CompareOperator op, const QList< qint64 > &value, ConditionType type=WhereCondition)
 
void addValueCondition (const QString &column, Query::CompareOperator op, const QVariant &value, ConditionType type=WhereCondition)
 
bool exec ()
 
QString getTable () const
 
QString getTableWithColumn (const QString &column) const
 
qint64 insertId ()
 
QSqlQueryquery ()
 
void setColumnValue (const QString &column, const QVariant &value)
 
void setDatabaseType (DbType::Type type)
 
void setDistinct (bool distinct)
 
void setForUpdate (bool forUpdate=true)
 
void setIdentificationColumn (const QString &column)
 
void setLimit (int limit, int offset=-1)
 
void setSubQueryMode (Query::LogicOperator op, ConditionType type=WhereCondition)
 

Protected Member Functions

DataStoredataStore () const
 

Detailed Description

Helper class to construct arbitrary SQL queries.

Definition at line 31 of file querybuilder.h.

Member Enumeration Documentation

◆ ConditionType

Defines the place at which a condition should be evaluated.

Enumerator
WhereCondition 

add condition to WHERE part of the query

HavingCondition 

add condition to HAVING part of the query NOTE: only supported for SELECT queries

Definition at line 56 of file querybuilder.h.

◆ JoinType

When the same table gets joined as both, Inner- and LeftJoin, it will be merged into a single InnerJoin since it is more restrictive.

Enumerator
InnerJoin 

NOTE: only supported for UPDATE and SELECT queries.

LeftJoin 

NOTE: only supported for SELECT queries.

Definition at line 46 of file querybuilder.h.

◆ QueryType

enum Akonadi::Server::QueryBuilder::QueryType

Definition at line 34 of file querybuilder.h.

Constructor & Destructor Documentation

◆ QueryBuilder() [1/4]

QueryBuilder::QueryBuilder ( const QString & table,
QueryBuilder::QueryType type = Select )
explicit

Creates a new query builder.

Parameters
tableThe main table to operate on.

Definition at line 109 of file querybuilder.cpp.

◆ QueryBuilder() [2/4]

QueryBuilder::QueryBuilder ( DataStore * store,
const QString & table,
QueryBuilder::QueryType type = Select )

Definition at line 114 of file querybuilder.cpp.

◆ QueryBuilder() [3/4]

QueryBuilder::QueryBuilder ( const QSqlQuery & tableQuery,
const QString & tableQueryAlias )
explicit

Creates a new query builder with subquery in FROM clause for SELECT queries.

Parameters
tableQuerymust be a valid select query
tableQueryAliasalias name for table query

Definition at line 132 of file querybuilder.cpp.

◆ QueryBuilder() [4/4]

QueryBuilder::QueryBuilder ( DataStore * store,
const QSqlQuery & tableQuery,
const QString & tableQueryAlias )

Definition at line 137 of file querybuilder.cpp.

Member Function Documentation

◆ addAggregation() [1/2]

void QueryBuilder::addAggregation ( const QString & col,
const QString & aggregate )

Adds an aggregation statement.

Parameters
colThe column to aggregate on
aggregateThe aggregation function.

Definition at line 525 of file querybuilder.cpp.

◆ addAggregation() [2/2]

void QueryBuilder::addAggregation ( const Query::Case & caseStmt,
const QString & aggregate )

Adds and aggregation statement with CASE.

Parameters
caseStmtThe case statement to aggregate on
aggregateThe aggregation function.

Definition at line 530 of file querybuilder.cpp.

◆ addColumn() [1/2]

void QueryBuilder::addColumn ( const QString & col)

Adds the given column to a select query.

Parameters
colThe column to add.

Definition at line 513 of file querybuilder.cpp.

◆ addColumn() [2/2]

void QueryBuilder::addColumn ( const Query::Case & caseStmt)

Adds the given case statement to a select query.

Parameters
caseStmtThe case statement to add.

Definition at line 518 of file querybuilder.cpp.

◆ addColumnCondition()

void QueryBuilder::addColumnCondition ( const QString & column,
Query::CompareOperator op,
const QString & column2,
ConditionType type = WhereCondition )

Add a WHERE or HAVING condition which compares a column with another column.

Parameters
columnThe column that should be compared.
opThe operator used for comparison.
column2The column column is compared to.
typeDefines whether this condition should be part of the WHERE or the HAVING part of the query. Defaults to WHERE.

Definition at line 194 of file querybuilder.cpp.

◆ addColumns()

void QueryBuilder::addColumns ( const QStringList & cols)

Adds the given columns to a select query.

Parameters
colsThe columns you want to select.

Definition at line 508 of file querybuilder.cpp.

◆ addCondition()

void QueryBuilder::addCondition ( const Query::Condition & condition,
ConditionType type = WhereCondition )

Add a WHERE condition.

Use this to build hierarchical conditions.

Parameters
conditionThe condition that the resultset should satisfy.
typeDefines whether this condition should be part of the WHERE or the HAVING part of the query. Defaults to WHERE.

Definition at line 632 of file querybuilder.cpp.

◆ addGroupColumn()

void QueryBuilder::addGroupColumn ( const QString & column)

Add a GROUP BY column.

NOTE: Only supported in SELECT queries.

Parameters
columnName of the column to use for grouping.

Definition at line 643 of file querybuilder.cpp.

◆ addGroupColumns()

void QueryBuilder::addGroupColumns ( const QStringList & columns)

Add list of columns to GROUP BY.

NOTE: Only supported in SELECT queries.

Parameters
columnsNames of columns to use for grouping.

Definition at line 649 of file querybuilder.cpp.

◆ addJoin() [1/2]

void QueryBuilder::addJoin ( JoinType joinType,
const QString & table,
const QString & col1,
const QString & col2 )

Join a table to the query.

This is a convenience method to create simple joins like e.g. 'LEFT JOIN t ON c1 = c2'.

NOTE: make sure the JoinType is supported by the current QueryType

Parameters
joinTypeThe type of JOIN you want to add.
tableThe table to join.
col1The first column for the ON statement.
col2The second column for the ON statement.

Definition at line 175 of file querybuilder.cpp.

◆ addJoin() [2/2]

void QueryBuilder::addJoin ( JoinType joinType,
const QString & table,
const Query::Condition & condition )

Join a table to the query.

NOTE: make sure the JoinType is supported by the current QueryType

Parameters
joinTypeThe type of JOIN you want to add.
tableThe table to join.
conditionthe ON condition for this join.

Definition at line 161 of file querybuilder.cpp.

◆ addSortColumn()

void QueryBuilder::addSortColumn ( const QString & column,
Query::SortOrder order = Query::Ascending )

Add sort column.

Parameters
columnName of the column to sort.
orderSort order

Definition at line 638 of file querybuilder.cpp.

◆ addValueCondition() [1/2]

void QueryBuilder::addValueCondition ( const QString & column,
Query::CompareOperator op,
const QList< qint64 > & value,
ConditionType type = WhereCondition )

Add a WHERE or HAVING condition which compares a column with a given value.

This is an overload specially for passing a list of IDs, which is a fairly common case in Akonadi.

Parameters
columnThe column that should be compared.
opThe operator used for comparison.
valueThe value column is compared to.
typeDefines whether this condition should be part of the WHERE or the HAVING part of the query. Defaults to WHERE.

Definition at line 188 of file querybuilder.cpp.

◆ addValueCondition() [2/2]

void QueryBuilder::addValueCondition ( const QString & column,
Query::CompareOperator op,
const QVariant & value,
ConditionType type = WhereCondition )

Add a WHERE or HAVING condition which compares a column with a given value.

Parameters
columnThe column that should be compared.
opThe operator used for comparison.
valueThe value column is compared to.
typeDefines whether this condition should be part of the WHERE or the HAVING part of the query. Defaults to WHERE.

Definition at line 182 of file querybuilder.cpp.

◆ dataStore()

DataStore * Akonadi::Server::QueryBuilder::dataStore ( ) const
inlineprotected

Definition at line 297 of file querybuilder.h.

◆ exec()

bool QueryBuilder::exec ( )

Executes the query, returns true on success.

Definition at line 396 of file querybuilder.cpp.

◆ getTable()

QString QueryBuilder::getTable ( ) const

Returns the name of the main table or subquery.

Definition at line 704 of file querybuilder.cpp.

◆ getTableWithColumn()

QString QueryBuilder::getTableWithColumn ( const QString & column) const

Returns concatenated table name with column name.

Parameters
columnColumn name.
Note
Pass only column that are not prefixed by table name.

Definition at line 709 of file querybuilder.cpp.

◆ insertId()

qint64 QueryBuilder::insertId ( )

Returns the ID of the newly created record (only valid for INSERT queries)

Note
This will assert when being used with setIdentificationColumn() called with an empty string.
Returns
-1 if invalid

Definition at line 676 of file querybuilder.cpp.

◆ query()

QSqlQuery & QueryBuilder::query ( )

Returns the query, only valid after exec().

Definition at line 200 of file querybuilder.cpp.

◆ setColumnValue()

void QueryBuilder::setColumnValue ( const QString & column,
const QVariant & value )

Sets a column to the given value (only valid for INSERT and UPDATE queries).

Parameters
columnColumn to change.
valueThe value column should be set to.

Definition at line 655 of file querybuilder.cpp.

◆ setDatabaseType()

void QueryBuilder::setDatabaseType ( DbType::Type type)

Sets the database which should execute the query.

Unfortunately the SQL "standard" is not interpreted in the same way everywhere...

Definition at line 156 of file querybuilder.cpp.

◆ setDistinct()

void QueryBuilder::setDistinct ( bool distinct)

Specify whether duplicates should be included in the result.

Parameters
distincttrue to remove duplicates, false is the default

Definition at line 660 of file querybuilder.cpp.

◆ setForUpdate()

void QueryBuilder::setForUpdate ( bool forUpdate = true)

Indicate to the database to acquire an exclusive lock on the rows already during SELECT statement.

Only makes sense in SELECT queries.

Definition at line 699 of file querybuilder.cpp.

◆ setIdentificationColumn()

void QueryBuilder::setIdentificationColumn ( const QString & column)

Sets the column used for identification in an INSERT statement.

The default is "id", only change this on tables without such a column (usually n:m helper tables).

Parameters
columnName of the identification column, empty string to disable this.
Note
This only affects PostgreSQL.
See also
insertId()

Definition at line 671 of file querybuilder.cpp.

◆ setLimit()

void QueryBuilder::setLimit ( int limit,
int offset = -1 )

Limits the amount of retrieved rows.

Parameters
limitthe maximum number of rows to retrieve.
offsetoffset of the first row to retrieve. The default value for offset is -1, indicating no offset.
Note
This has no effect on anything but SELECT queries.

Definition at line 665 of file querybuilder.cpp.

◆ setSubQueryMode()

void QueryBuilder::setSubQueryMode ( Query::LogicOperator op,
ConditionType type = WhereCondition )

Define how WHERE or HAVING conditions are combined.

Todo
Give this method a better name.
Parameters
opThe logical operator that should be used to combine the conditions.
typeDefines whether the operator should be used for WHERE or for HAVING conditions. Defaults to WHERE conditions.

Definition at line 626 of file querybuilder.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:54:41 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.