IDBConnection

interface OCP\IDBConnection
Interface IDBConnection
Implemented by:OC\DB\ConnectionAdapter
Source:lib/public/IDBConnection.php#53

Constants

ADD_MISSING_INDEXES_EVENT = self::class . ::ADD_MISSING_INDEXES
Source:lib/public/IDBConnection.php#57
Deprecated:22.0.0 this is an internal event
CHECK_MISSING_INDEXES_EVENT = self::class . ::CHECK_MISSING_INDEXES
Source:lib/public/IDBConnection.php#62
Deprecated:22.0.0 this is an internal event
ADD_MISSING_PRIMARY_KEYS_EVENT = self::class . ::ADD_MISSING_PRIMARY_KEYS
Source:lib/public/IDBConnection.php#67
Deprecated:22.0.0 this is an internal event
CHECK_MISSING_PRIMARY_KEYS_EVENT = self::class . ::CHECK_MISSING_PRIMARY_KEYS
Source:lib/public/IDBConnection.php#72
Deprecated:22.0.0 this is an internal event
ADD_MISSING_COLUMNS_EVENT = self::class . ::ADD_MISSING_COLUMNS
Source:lib/public/IDBConnection.php#77
Deprecated:22.0.0 this is an internal event
CHECK_MISSING_COLUMNS_EVENT = self::class . ::CHECK_MISSING_COLUMNS
Source:lib/public/IDBConnection.php#82
Deprecated:22.0.0 this is an internal event

Methods

public OCP\IDBConnection::getQueryBuilder()
Gets the QueryBuilder for the connection.
Source:lib/public/IDBConnection.php#90
Returns:\OCP\DB\QueryBuilder\IQueryBuilder
Since:8.2.0
public OCP\IDBConnection::prepare($sql, $limit=null, $offset=null)
Used to abstract the ownCloud database access away
Source:

lib/public/IDBConnection.php#103

Parameters:
  • $sql (string) the sql query with ? placeholder for params
  • $limit (int) the maximum number of rows
  • $offset (int) from which row we want to start
Returns:

\OCP\DB\IPreparedStatement The prepared statement.

Since:

6.0.0

Throws:

\OCP\DB\Exception since 21.0.0

public OCP\IDBConnection::executeQuery($sql, $params=[], $types=[])
Executes an, optionally parameterized, SQL query.
If the query is parameterized, a prepared statement is used.
If an SQLLogger is configured, the execution is logged.
Source:

lib/public/IDBConnection.php#120

Parameters:
  • $sql (string) The SQL query to execute.
  • $params (string[]) The parameters to bind to the query, if any.
  • $types (array) The types the previous parameters are in.
Returns:

\OCP\DB\IResult The executed statement.

Since:

8.0.0

Throws:

\OCP\DB\Exception since 21.0.0

public OCP\IDBConnection::executeUpdate($sql, $params=[], $types=[])
Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
and returns the number of affected rows.
This method supports PDO binding types as well as DBAL mapping types.
Source:

lib/public/IDBConnection.php#139

Parameters:
  • $sql (string) The SQL query.
  • $params (array) The query parameters.
  • $types (array) The parameter types.
Returns:

int The number of affected rows.

Since:

8.0.0

Throws:

\OCP\DB\Exception since 21.0.0

Deprecated:

21.0.0 use executeStatement

public OCP\IDBConnection::executeStatement($sql, $params=[], $types=[])
Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
and returns the number of affected rows.
This method supports PDO binding types as well as DBAL mapping types.
Source:

lib/public/IDBConnection.php#156

Parameters:
  • $sql (string) The SQL query.
  • $params (array) The query parameters.
  • $types (array) The parameter types.
Returns:

int The number of affected rows.

Since:

21.0.0

Throws:

\OCP\DB\Exception since 21.0.0

public OCP\IDBConnection::lastInsertId($table)
Used to get the id of the just inserted element
Source:

lib/public/IDBConnection.php#166

Parameters:
  • $table (string) the name of the table where we inserted the item
Returns:

int the id of the inserted element

Since:

6.0.0

Throws:

\OCP\DB\Exception since 21.0.0

Deprecated:

21.0.0 use \OCP\DB\QueryBuilder\IQueryBuilder::getLastInsertId

public OCP\IDBConnection::insertIfNotExist($table, $input, $compare=null)
Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
it is needed that there is also a unique constraint on the values. Then this method will
catch the exception and return 0.
Source:

lib/public/IDBConnection.php#183

Parameters:
  • $table (string) The table name (will replace PREFIX with the actual prefix)
  • $input (array) data that should be inserted into the table (column name => value)
  • $compare (array | null) List of values that should be checked for “if not exists”

If this is null or an empty array, all keys of $input will be compared Please note: text fields (clob) must not be used in the compare array

Returns:

int number of inserted rows

Throws:

\OCP\DB\Exception used to be the removed dbal exception, since 21.0.0 it’s \OCP\DB\Exception

Since:

6.0.0 - parameter $compare was added in 8.1.0, return type changed from boolean in 8.1.0

Deprecated:

15.0.0 - use unique index and “try { $db->insert() } catch (UniqueConstraintViolationException $e) }” instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371

public OCP\IDBConnection::insertIgnoreConflict($table, $values)
Insert a row if the row does not exist. Eventual conflicts during insert will be ignored.
Implementation is not fully finished and should not be used!
Source:

lib/public/IDBConnection.php#197

Parameters:
  • $table (string) The table name (will replace PREFIX with the actual prefix)
  • $values (array) data that should be inserted into the table (column name => value)
Returns:

int number of inserted rows

Since:

16.0.0

public OCP\IDBConnection::setValues($table, $keys, $values, $updatePreconditionValues=[])
Insert or update a row value
Source:

lib/public/IDBConnection.php#211

Parameters:
  • $table (string)
  • $keys (array) (column name => value)
  • $values (array) (column name => value)
  • $updatePreconditionValues (array) ensure values match preconditions (column name => value)
Returns:

int number of new rows

Throws:

\OCP\DB\Exception used to be the removed dbal exception, since 21.0.0 it’s \OCP\DB\Exception

Throws:

\OCP\PreconditionNotMetException

Throws:

\OCP\DB\Exception used to be the removed dbal exception, since 21.0.0 it’s \OCP\DB\Exception

Throws:

\OCP\PreconditionNotMetException

Since:

9.0.0

public OCP\IDBConnection::lockTable($tableName)
Create an exclusive read+write lock on a table
Important Note: Due to the nature how locks work on different DBs, it is
only possible to lock one table at a time. You should also NOT start a
transaction while holding a lock.
Source:

lib/public/IDBConnection.php#224

Parameters:
  • $tableName (string)
Throws:

\OCP\DB\Exception since 21.0.0

Since:

9.1.0

public OCP\IDBConnection::unlockTable()
Release a previous acquired lock again
Source:lib/public/IDBConnection.php#232
Throws:\OCP\DB\Exception since 21.0.0
Since:9.1.0
public OCP\IDBConnection::beginTransaction()
Start a transaction
Source:lib/public/IDBConnection.php#239
Since:6.0.0
Throws:\OCP\DB\Exception since 21.0.0
public OCP\IDBConnection::inTransaction()
Check if a transaction is active
Source:lib/public/IDBConnection.php#247
Returns:bool
Since:8.2.0
public OCP\IDBConnection::commit()
Commit the database changes done during a transaction that is in progress
Source:lib/public/IDBConnection.php#254
Since:6.0.0
Throws:\OCP\DB\Exception since 21.0.0
public OCP\IDBConnection::rollBack()
Rollback the database changes done during a transaction that is in progress
Source:lib/public/IDBConnection.php#261
Since:6.0.0
Throws:\OCP\DB\Exception since 21.0.0
public OCP\IDBConnection::getError()
Gets the error code and message as a string for logging
Source:lib/public/IDBConnection.php#269
Returns:string
Since:6.0.0
Deprecated:21.0.0 doesn’t return anything meaningful
public OCP\IDBConnection::errorCode()
Fetch the SQLSTATE associated with the last database operation.
Source:lib/public/IDBConnection.php#278
Returns:int The last error code.
Since:8.0.0
Deprecated:21.0.0 doesn’t return anything anymore
public OCP\IDBConnection::errorInfo()
Fetch extended error information associated with the last database operation.
Source:lib/public/IDBConnection.php#287
Returns:array The last error information.
Since:8.0.0
Deprecated:21.0.0 doesn’t return anything anymore
public OCP\IDBConnection::connect()
Establishes the connection with the database.
Source:lib/public/IDBConnection.php#296
Returns:bool
Throws:\OCP\DB\Exception since 21.0.0
Since:8.0.0
public OCP\IDBConnection::close()
Close the database connection
Source:lib/public/IDBConnection.php#302
Since:8.0.0
public OCP\IDBConnection::quote($input, $type=\OCP\DB\QueryBuilder\IQueryBuilder::PARAM_STR)
Quotes a given input parameter.
Source:

lib/public/IDBConnection.php#312

Parameters:
  • $input (mixed) Parameter to be quoted.
  • $type (int) Type of the parameter.
Returns:

mixed The quoted parameter.

Since:

8.0.0

public OCP\IDBConnection::getDatabasePlatform()
Gets the DatabasePlatform instance that provides all the metadata about
the platform this driver connects to.
Source:lib/public/IDBConnection.php#321
Returns:\Doctrine\DBAL\Platforms\AbstractPlatform The database platform.
Since:8.0.0
public OCP\IDBConnection::dropTable($table)
Drop a table from the database if it exists
Source:

lib/public/IDBConnection.php#332

Parameters:
  • $table (string) table name without the prefix
Throws:

\OCP\DB\Exception since 21.0.0

Since:

8.0.0

public OCP\IDBConnection::tableExists($table)
Check if a table exists
Source:

lib/public/IDBConnection.php#342

Parameters:
  • $table (string) table name without the prefix
Returns:

bool

Throws:

\OCP\DB\Exception since 21.0.0

Since:

8.0.0

public OCP\IDBConnection::escapeLikeParameter($param)
Escape a parameter to be used in a LIKE query
Source:

lib/public/IDBConnection.php#351

Parameters:
  • $param (string)
Returns:

string

Since:

9.0.0

public OCP\IDBConnection::supports4ByteText()
Check whether or not the current database support 4byte wide unicode
Source:lib/public/IDBConnection.php#359
Returns:bool
Since:11.0.0
public OCP\IDBConnection::createSchema()
Create the schema of the connected database
Source:lib/public/IDBConnection.php#368
Returns:\Doctrine\DBAL\Schema\Schema
Throws:\OCP\DB\Exception since 21.0.0
Since:13.0.0
public OCP\IDBConnection::migrateToSchema($toSchema)
Migrate the database to the given schema
Source:

lib/public/IDBConnection.php#377

Parameters:
  • $toSchema (Doctrine\DBAL\Schema\Schema)
Throws:

\OCP\DB\Exception since 21.0.0

Since:

13.0.0