Constants

PARAM_NULL

PARAM_NULL

PARAM_BOOL

PARAM_BOOL

PARAM_INT

PARAM_INT

PARAM_STR

PARAM_STR

PARAM_LOB

PARAM_LOB

PARAM_DATE

PARAM_DATE

PARAM_INT_ARRAY

PARAM_INT_ARRAY

PARAM_STR_ARRAY

PARAM_STR_ARRAY

Methods

automaticTablePrefix()

automaticTablePrefix(boolean  $enabled) 

Enable/disable automatic prefixing of table names with the oc_ prefix

Parameters

boolean $enabled

If set to true table names will be prefixed with the owncloud database prefix automatically.

expr()

expr() : \OCP\DB\QueryBuilder\IExpressionBuilder

Gets an ExpressionBuilder used for object-oriented construction of query expressions.

This producer method is intended for convenient inline usage. Example:

$qb = $conn->getQueryBuilder() ->select('u') ->from('users', 'u') ->where($qb->expr()->eq('u.id', 1));

For more complex expression construction, consider storing the expression builder object in a local variable.

Returns

\OCP\DB\QueryBuilder\IExpressionBuilder

getType()

getType() : integer

Gets the type of the currently built query.

Returns

integer

getConnection()

getConnection() : \OCP\IDBConnection

Gets the associated DBAL Connection for this query builder.

Returns

\OCP\IDBConnection

getState()

getState() : integer

Gets the state of this query builder instance.

Returns

integer —

Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN.

execute()

execute() : \Doctrine\DBAL\Driver\Statement|integer

Executes this query using the bound parameters and their types.

Uses \Connection::executeQuery for select statements and \Connection::executeUpdate for insert, update and delete statements.

Returns

\Doctrine\DBAL\Driver\Statement|integer

getSQL()

getSQL() : string

Gets the complete SQL string formed by the current specifications of this QueryBuilder.

$qb = $conn->getQueryBuilder() ->select('u') ->from('User', 'u') echo $qb->getSQL(); // SELECT u FROM User u

Returns

string —

The SQL query string.

setParameter()

setParameter(string|integer  $key, mixed  $value, string|null  $type = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Sets a query parameter for the query being constructed.

$qb = $conn->getQueryBuilder() ->select('u') ->from('users', 'u') ->where('u.id = :user_id') ->setParameter(':user_id', 1);

Parameters

string|integer $key

The parameter position or name.

mixed $value

The parameter value.

string|null $type

One of the IQueryBuilder::PARAM_* constants.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

setParameters()

setParameters(array  $params, array  $types = array()) : \OCP\DB\QueryBuilder\IQueryBuilder

Sets a collection of query parameters for the query being constructed.

$qb = $conn->getQueryBuilder() ->select('u') ->from('users', 'u') ->where('u.id = :user_id1 OR u.id = :user_id2') ->setParameters(array( ':user_id1' => 1, ':user_id2' => 2 ));

Parameters

array $params

The query parameters to set.

array $types

The query parameters types to set.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

getParameters()

getParameters() : array

Gets all defined query parameters for the query being constructed indexed by parameter index or name.

Returns

array —

The currently defined query parameters indexed by parameter index or name.

getParameter()

getParameter(mixed  $key) : mixed

Gets a (previously set) query parameter of the query being constructed.

Parameters

mixed $key

The key (index or name) of the bound parameter.

Returns

mixed —

The value of the bound parameter.

getParameterTypes()

getParameterTypes() : array

Gets all defined query parameter types for the query being constructed indexed by parameter index or name.

Returns

array —

The currently defined query parameter types indexed by parameter index or name.

getParameterType()

getParameterType(mixed  $key) : mixed

Gets a (previously set) query parameter type of the query being constructed.

Parameters

mixed $key

The key (index or name) of the bound parameter type.

Returns

mixed —

The value of the bound parameter type.

setFirstResult()

setFirstResult(integer  $firstResult) : \OCP\DB\QueryBuilder\IQueryBuilder

Sets the position of the first result to retrieve (the "offset").

Parameters

integer $firstResult

The first result to return.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

getFirstResult()

getFirstResult() : integer

Gets the position of the first result the query object was set to retrieve (the "offset").

Returns NULL if \setFirstResult was not applied to this QueryBuilder.

Returns

integer —

The position of the first result.

setMaxResults()

setMaxResults(integer  $maxResults) : \OCP\DB\QueryBuilder\IQueryBuilder

Sets the maximum number of results to retrieve (the "limit").

Parameters

integer $maxResults

The maximum number of results to retrieve.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

getMaxResults()

getMaxResults() : integer

Gets the maximum number of results the query object was set to retrieve (the "limit").

Returns NULL if \setMaxResults was not applied to this query builder.

Returns

integer —

The maximum number of results.

select()

select(mixed  $select = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Specifies an item that is to be returned in the query result.

Replaces any previously specified selections, if any.

$qb = $conn->getQueryBuilder() ->select('u.id', 'p.id') ->from('users', 'u') ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');

Parameters

mixed $select

The selection expressions.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

selectAlias()

selectAlias(mixed  $select, string  $alias) : \OCP\DB\QueryBuilder\IQueryBuilder

Specifies an item that is to be returned with a different name in the query result.

$qb = $conn->getQueryBuilder() ->selectAlias('u.id', 'user_id') ->from('users', 'u') ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');

Parameters

mixed $select

The selection expressions.

string $alias

The column alias used in the constructed query.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

selectDistinct()

selectDistinct(mixed  $select) : \OCP\DB\QueryBuilder\IQueryBuilder

Specifies an item that is to be returned uniquely in the query result.

$qb = $conn->getQueryBuilder() ->selectDistinct('type') ->from('users');

Parameters

mixed $select

The selection expressions.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

addSelect()

addSelect(mixed  $select = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Adds an item that is to be returned in the query result.

$qb = $conn->getQueryBuilder() ->select('u.id') ->addSelect('p.id') ->from('users', 'u') ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id');

Parameters

mixed $select

The selection expression.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

delete()

delete(string  $delete = null, string  $alias = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Turns the query being built into a bulk delete query that ranges over a certain table.

$qb = $conn->getQueryBuilder() ->delete('users', 'u') ->where('u.id = :user_id'); ->setParameter(':user_id', 1);

Parameters

string $delete

The table whose rows are subject to the deletion.

string $alias

The table alias used in the constructed query.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

update()

update(string  $update = null, string  $alias = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Turns the query being built into a bulk update query that ranges over a certain table

$qb = $conn->getQueryBuilder() ->update('users', 'u') ->set('u.password', md5('password')) ->where('u.id = ?');

Parameters

string $update

The table whose rows are subject to the update.

string $alias

The table alias used in the constructed query.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

insert()

insert(string  $insert = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Turns the query being built into an insert query that inserts into a certain table

$qb = $conn->getQueryBuilder() ->insert('users') ->values( array( 'name' => '?', 'password' => '?' ) );

Parameters

string $insert

The table into which the rows should be inserted.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

from()

from(string  $from, string|null  $alias = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Creates and adds a query root corresponding to the table identified by the given alias, forming a cartesian product with any existing query roots.

$qb = $conn->getQueryBuilder() ->select('u.id') ->from('users', 'u')

Parameters

string $from

The table.

string|null $alias

The alias of the table.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

join()

join(string  $fromAlias, string  $join, string  $alias, string  $condition = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Creates and adds a join to the query.

$qb = $conn->getQueryBuilder() ->select('u.name') ->from('users', 'u') ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1');

Parameters

string $fromAlias

The alias that points to a from clause.

string $join

The table name to join.

string $alias

The alias of the join table.

string $condition

The condition for the join.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

innerJoin()

innerJoin(string  $fromAlias, string  $join, string  $alias, string  $condition = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Creates and adds a join to the query.

$qb = $conn->getQueryBuilder() ->select('u.name') ->from('users', 'u') ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');

Parameters

string $fromAlias

The alias that points to a from clause.

string $join

The table name to join.

string $alias

The alias of the join table.

string $condition

The condition for the join.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

leftJoin()

leftJoin(string  $fromAlias, string  $join, string  $alias, string  $condition = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Creates and adds a left join to the query.

$qb = $conn->getQueryBuilder() ->select('u.name') ->from('users', 'u') ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');

Parameters

string $fromAlias

The alias that points to a from clause.

string $join

The table name to join.

string $alias

The alias of the join table.

string $condition

The condition for the join.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

rightJoin()

rightJoin(string  $fromAlias, string  $join, string  $alias, string  $condition = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Creates and adds a right join to the query.

$qb = $conn->getQueryBuilder() ->select('u.name') ->from('users', 'u') ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');

Parameters

string $fromAlias

The alias that points to a from clause.

string $join

The table name to join.

string $alias

The alias of the join table.

string $condition

The condition for the join.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

set()

set(string  $key, string  $value) : \OCP\DB\QueryBuilder\IQueryBuilder

Sets a new value for a column in a bulk update query.

$qb = $conn->getQueryBuilder() ->update('users', 'u') ->set('u.password', md5('password')) ->where('u.id = ?');

Parameters

string $key

The column to set.

string $value

The value, expression, placeholder, etc.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

where()

where(mixed  $predicates) : \OCP\DB\QueryBuilder\IQueryBuilder

Specifies one or more restrictions to the query result.

Replaces any previously specified restrictions, if any.

$qb = $conn->getQueryBuilder() ->select('u.name') ->from('users', 'u') ->where('u.id = ?');

// You can optionally programatically build and/or expressions
$qb = $conn->getQueryBuilder();

$or = $qb->expr()->orx();
$or->add($qb->expr()->eq('u.id', 1));
$or->add($qb->expr()->eq('u.id', 2));

$qb->update('users', 'u')
    ->set('u.password', md5('password'))
    ->where($or);

Parameters

mixed $predicates

The restriction predicates.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

andWhere()

andWhere(mixed  $where) : \OCP\DB\QueryBuilder\IQueryBuilder

Adds one or more restrictions to the query results, forming a logical conjunction with any previously specified restrictions.

$qb = $conn->getQueryBuilder() ->select('u') ->from('users', 'u') ->where('u.username LIKE ?') ->andWhere('u.is_active = 1');

Parameters

mixed $where

The query restrictions.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

orWhere()

orWhere(mixed  $where) : \OCP\DB\QueryBuilder\IQueryBuilder

Adds one or more restrictions to the query results, forming a logical disjunction with any previously specified restrictions.

$qb = $conn->getQueryBuilder() ->select('u.name') ->from('users', 'u') ->where('u.id = 1') ->orWhere('u.id = 2');

Parameters

mixed $where

The WHERE statement.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

groupBy()

groupBy(mixed  $groupBy) : \OCP\DB\QueryBuilder\IQueryBuilder

Specifies a grouping over the results of the query.

Replaces any previously specified groupings, if any.

$qb = $conn->getQueryBuilder() ->select('u.name') ->from('users', 'u') ->groupBy('u.id');

Parameters

mixed $groupBy

The grouping expression.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

addGroupBy()

addGroupBy(mixed  $groupBy) : \OCP\DB\QueryBuilder\IQueryBuilder

Adds a grouping expression to the query.

$qb = $conn->getQueryBuilder() ->select('u.name') ->from('users', 'u') ->groupBy('u.lastLogin'); ->addGroupBy('u.createdAt')

Parameters

mixed $groupBy

The grouping expression.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

setValue()

setValue(string  $column, string  $value) : \OCP\DB\QueryBuilder\IQueryBuilder

Sets a value for a column in an insert query.

$qb = $conn->getQueryBuilder() ->insert('users') ->values( array( 'name' => '?' ) ) ->setValue('password', '?');

Parameters

string $column

The column into which the value should be inserted.

string $value

The value that should be inserted into the column.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

values()

values(array  $values) : \OCP\DB\QueryBuilder\IQueryBuilder

Specifies values for an insert query indexed by column names.

Replaces any previous values, if any.

$qb = $conn->getQueryBuilder() ->insert('users') ->values( array( 'name' => '?', 'password' => '?' ) );

Parameters

array $values

The values to specify for the insert query indexed by column names.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

having()

having(mixed  $having) : \OCP\DB\QueryBuilder\IQueryBuilder

Specifies a restriction over the groups of the query.

Replaces any previous having restrictions, if any.

Parameters

mixed $having

The restriction over the groups.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

andHaving()

andHaving(mixed  $having) : \OCP\DB\QueryBuilder\IQueryBuilder

Adds a restriction over the groups of the query, forming a logical conjunction with any existing having restrictions.

Parameters

mixed $having

The restriction to append.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

orHaving()

orHaving(mixed  $having) : \OCP\DB\QueryBuilder\IQueryBuilder

Adds a restriction over the groups of the query, forming a logical disjunction with any existing having restrictions.

Parameters

mixed $having

The restriction to add.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

orderBy()

orderBy(string  $sort, string  $order = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Specifies an ordering for the query results.

Replaces any previously specified orderings, if any.

Parameters

string $sort

The ordering expression.

string $order

The ordering direction.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

addOrderBy()

addOrderBy(string  $sort, string  $order = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Adds an ordering to the query results.

Parameters

string $sort

The ordering expression.

string $order

The ordering direction.

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

getQueryPart()

getQueryPart(string  $queryPartName) : mixed

Gets a query part by its name.

Parameters

string $queryPartName

Returns

mixed

getQueryParts()

getQueryParts() : array

Gets all query parts.

Returns

array

resetQueryParts()

resetQueryParts(array|null  $queryPartNames = null) : \OCP\DB\QueryBuilder\IQueryBuilder

Resets SQL parts.

Parameters

array|null $queryPartNames

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

resetQueryPart()

resetQueryPart(string  $queryPartName) : \OCP\DB\QueryBuilder\IQueryBuilder

Resets a single SQL part.

Parameters

string $queryPartName

Returns

\OCP\DB\QueryBuilder\IQueryBuilder

This QueryBuilder instance.

createNamedParameter()

createNamedParameter(mixed  $value, mixed  $type = self::PARAM_STR, string  $placeHolder = null) : \OCP\DB\QueryBuilder\IParameter

Creates a new named parameter and bind the value $value to it.

This method provides a shortcut for PDOStatement::bindValue when using prepared statements.

The parameter $value specifies the value that you want to bind. If $placeholder is not provided bindValue() will automatically create a placeholder for you. An automatic placeholder will be of the name ':dcValue1', ':dcValue2' etc.

For more information see http://php.net/pdostatement-bindparam

Example: $value = 2; $q->eq( 'id', $q->bindValue( $value ) ); $stmt = $q->executeQuery(); // executed with 'id = 2'

Parameters

mixed $value
mixed $type
string $placeHolder

The name to bind with. The string must start with a colon ':'.

Returns

\OCP\DB\QueryBuilder\IParameter

createPositionalParameter()

createPositionalParameter(mixed  $value, integer  $type = self::PARAM_STR) : \OCP\DB\QueryBuilder\IParameter

Creates a new positional parameter and bind the given value to it.

Attention: If you are using positional parameters with the query builder you have to be very careful to bind all parameters in the order they appear in the SQL statement , otherwise they get bound in the wrong order which can lead to serious bugs in your code.

Example: $qb = $conn->getQueryBuilder(); $qb->select('u.*') ->from('users', 'u') ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR))

Parameters

mixed $value
integer $type

Returns

\OCP\DB\QueryBuilder\IParameter

createParameter()

createParameter(string  $name) : \OCP\DB\QueryBuilder\IParameter

Creates a new parameter

Example: $qb = $conn->getQueryBuilder(); $qb->select('u.*') ->from('users', 'u') ->where('u.username = ' . $qb->createParameter('name')) ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR))

Parameters

string $name

Returns

\OCP\DB\QueryBuilder\IParameter

createFunction()

createFunction(string  $call) : \OCP\DB\QueryBuilder\IQueryFunction

Creates a new function

Attention: Column names inside the call have to be quoted before hand

Example: $qb = $conn->getQueryBuilder(); $qb->select($qb->createFunction('COUNT()')) ->from('users', 'u') echo $qb->getSQL(); // SELECT COUNT() FROM users u $qb = $conn->getQueryBuilder(); $qb->select($qb->createFunction('COUNT(column)')) ->from('users', 'u') echo $qb->getSQL(); // SELECT COUNT(column) FROM users u

Parameters

string $call

Returns

\OCP\DB\QueryBuilder\IQueryFunction

getLastInsertId()

getLastInsertId() : integer

Used to get the id of the last inserted element

Throws

\BadMethodCallException

When being called before an insert query has been run.

Returns

integer

getTableName()

getTableName(string  $table) : string

Returns the table name quoted and with database prefix as needed by the implementation

Parameters

string $table

Returns

string

getColumnName()

getColumnName(string  $column, string  $tableAlias = '') : string

Returns the column name quoted and with table alias prefix as needed by the implementation

Parameters

string $column
string $tableAlias

Returns

string