Akonadi::Server::QueryBuilder

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 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 Query::Condition &condition)
 
void addJoin (JoinType joinType, const QString &table, const QString &col1, const QString &col2)
 
void addSortColumn (const QString &column, Query::SortOrder order=Query::Ascending)
 
void addValueCondition (const QString &column, Query::CompareOperator op, const QVariant &value, ConditionType type=WhereCondition)
 
bool exec ()
 
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)
 
void setSubQueryMode (Query::LogicOperator op, ConditionType type=WhereCondition)
 

Detailed Description

Helper class to construct arbitrary SQL queries.

Definition at line 32 of file querybuilder.h.

Member Enumeration Documentation

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 57 of file querybuilder.h.

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 47 of file querybuilder.h.

Constructor & Destructor Documentation

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 89 of file querybuilder.cpp.

Member Function Documentation

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 471 of file querybuilder.cpp.

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 476 of file querybuilder.cpp.

void QueryBuilder::addColumn ( const QString col)

Adds the given column to a select query.

Parameters
colThe column to add.

Definition at line 459 of file querybuilder.cpp.

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 464 of file querybuilder.cpp.

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 139 of file querybuilder.cpp.

void QueryBuilder::addColumns ( const QStringList cols)

Adds the given columns to a select query.

Parameters
colsThe columns you want to select.

Definition at line 454 of file querybuilder.cpp.

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 553 of file querybuilder.cpp.

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 564 of file querybuilder.cpp.

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 570 of file querybuilder.cpp.

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 111 of file querybuilder.cpp.

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 126 of file querybuilder.cpp.

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 559 of file querybuilder.cpp.

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 133 of file querybuilder.cpp.

bool QueryBuilder::exec ( )

Executes the query, returns true on success.

Definition at line 337 of file querybuilder.cpp.

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 596 of file querybuilder.cpp.

QSqlQuery & QueryBuilder::query ( )

Returns the query, only valid after exec().

Definition at line 145 of file querybuilder.cpp.

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 576 of file querybuilder.cpp.

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 106 of file querybuilder.cpp.

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 581 of file querybuilder.cpp.

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 619 of file querybuilder.cpp.

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 591 of file querybuilder.cpp.

void QueryBuilder::setLimit ( int  limit)

Limits the amount of retrieved rows.

Parameters
limitthe maximum number of rows to retrieve.
Note
This has no effect on anything but SELECT queries.

Definition at line 586 of file querybuilder.cpp.

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 547 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-2020 The KDE developers.
Generated on Wed Aug 12 2020 23:16:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.