Database
class Database
Abstract database connectivity class.
Sub-classes of this implement the actual database connection libraries
Constants
PARTIAL_QUERY |
|
FULL_QUERY |
|
Methods
Get the current connector
Returns the current schema manager
Returns the current query builder
Execute the given SQL parameterised query with the specified arguments
Add the sql queries that need to be partially or fully matched
Get the sql queries that need to be partially or fully matched
Get the autogenerated ID from the previous INSERT query.
Determines if we are connected to a server AND have a valid database selected.
Returns an escaped string. This string won't be quoted, so would be suitable for appending to other quoted strings.
Wrap a string into DB-specific quotes.
Escapes an identifier (table / database name). Typically the value is simply double quoted. Don't pass in already escaped identifiers in, as this will double escape the value!
Execute a complex manipulation on the database.
Enable supression of database messages.
Clear all data out of the database
Clear all data in a given table
Generates a WHERE clause for null comparison check
Generate a WHERE clause for text matching.
function to return an SQL datetime expression that can be used with the adapter in use used for querying a datetime in a certain format
function to return an SQL datetime expression that can be used with the adapter in use used for querying a datetime addition
function to return an SQL datetime expression that can be used with the adapter in use used for querying a datetime substraction
String operator for concatenation of strings
Returns true if this database supports collations
Can the database override timezone as a connection setting, or does it use the system timezone exclusively?
Query for the version of the currently connected database
Get the database server type (e.g. mysql, postgresql).
Return the number of rows affected by the previous operation.
The core search engine, used by this class and its subclasses to do fun stuff.
Determines if this database supports transactions
Does this database support savepoints in transactions By default it is assumed that they don't unless they are explicitly enabled.
Invoke $callback within a transaction
No description
Start a prepared transaction See http://developer.postgresql.org/pgdocs/postgres/sql-set-transaction.html for details on transaction isolation options
Create a savepoint that you can jump back to if you encounter problems
Rollback or revert to a savepoint if your queries encounter problems If you encounter a problem at any point during a transaction, you may need to rollback that particular query, or return to a savepoint
Commit everything inside this transaction so far
Return depth of current transaction
Determines if the used database supports application-level locks, which is different from table- or row-level locking.
Returns if the lock is available.
Sets an application-level lock so that no two processes can run at the same time, also called a "cooperative advisory lock".
Remove an application-level lock file to allow another process to run (if the execution aborts (e.g. due to an error) all locks are automatically released).
Instruct the database to generate a live connection
Determine if the database with the specified name exists
Retrieves the list of all databases the user has access to
Change the connection to the specified database, optionally creating the database if it doesn't exist in the current schema.
Drop the database that this object is currently connected to.
Returns the name of the currently selected database
Return SQL expression used to represent the current date/time
Returns the database-specific version of the random() function
Details
at line 67
DBConnector
getConnector()
Get the current connector
at line 77
setConnector(DBConnector $connector)
Injector injection point for connector dependency
at line 94
DBSchemaManager
getSchemaManager()
Returns the current schema manager
at line 104
setSchemaManager(DBSchemaManager $schemaManager)
Injector injection point for schema manager
at line 125
DBQueryBuilder
getQueryBuilder()
Returns the current query builder
at line 135
setQueryBuilder(DBQueryBuilder $queryBuilder)
Injector injection point for schema manager
at line 147
Query
query(string $sql, int $errorLevel = E_USER_ERROR)
Execute the given SQL query.
at line 173
Query
preparedQuery(string $sql, array $parameters, int $errorLevel = E_USER_ERROR)
Execute the given SQL parameterised query with the specified arguments
at line 279
static
setWhitelistQueryArray(array $whitelistArray)
Add the sql queries that need to be partially or fully matched
at line 289
static array
getWhitelistQueryArray()
Get the sql queries that need to be partially or fully matched
at line 300
integer
getGeneratedID(string $table)
Get the autogenerated ID from the previous INSERT query.
at line 311
boolean
isActive()
Determines if we are connected to a server AND have a valid database selected.
at line 323
string
escapeString(mixed $value)
Returns an escaped string. This string won't be quoted, so would be suitable for appending to other quoted strings.
at line 334
string
quoteString(mixed $value)
Wrap a string into DB-specific quotes.
at line 348
string
escapeIdentifier(string|array $value, string $separator = '.')
Escapes an identifier (table / database name). Typically the value is simply double quoted. Don't pass in already escaped identifiers in, as this will double escape the value!
at line 388
manipulate(array $manipulation)
Execute a complex manipulation on the database.
A manipulation is an array of insert / or update sequences. The keys of the array are table names, and the values are map containing 'command' and 'fields'. Command should be 'insert' or 'update', and fields should be a map of field names to field values, NOT including quotes.
The field values could also be in paramaterised format, such as array('MAX(?,?)' => array(42, 69)), allowing the use of raw SQL values such as array('NOW()' => array()).
at line 445
quiet()
Enable supression of database messages.
at line 453
clearAllData()
Clear all data out of the database
at line 466
clearTable(string $table)
Clear all data in a given table
at line 478
string
nullCheckClause(string $field, bool $isNull)
Generates a WHERE clause for null comparison check
at line 500
abstract String
comparisonClause(String $field, String $value, boolean $exact = false, boolean $negate = false, boolean $caseSensitive = null, boolean $parameterised = false)
Generate a WHERE clause for text matching.
at line 525
abstract string
formattedDatetimeClause(string $date, string $format)
function to return an SQL datetime expression that can be used with the adapter in use used for querying a datetime in a certain format
at line 546
abstract string
datetimeIntervalClause($date, $interval)
function to return an SQL datetime expression that can be used with the adapter in use used for querying a datetime addition
at line 559
abstract string
datetimeDifferenceClause($date1, $date2)
function to return an SQL datetime expression that can be used with the adapter in use used for querying a datetime substraction
at line 566
string
concatOperator()
String operator for concatenation of strings
at line 577
abstract boolean
supportsCollations()
Returns true if this database supports collations
at line 585
abstract Boolean
supportsTimezoneOverride()
Can the database override timezone as a connection setting, or does it use the system timezone exclusively?
at line 591
string
getVersion()
Query for the version of the currently connected database
at line 603
abstract string
getDatabaseServer()
Get the database server type (e.g. mysql, postgresql).
This value is passed to the connector as the 'driver' argument when initiating a database connection
at line 609
int
affectedRows()
Return the number of rows affected by the previous operation.
at line 629
abstract PaginatedList
searchEngine(array $classesToSearch, string $keywords, integer $start, integer $pageLength, string $sortBy = "Relevance DESC", string $extraFilter = "", boolean $booleanSearch = false, string $alternativeFileFilter = "", boolean $invertedMatch = false)
The core search engine, used by this class and its subclasses to do fun stuff.
Searches both SiteTree and File.
at line 646
abstract boolean
supportsTransactions()
Determines if this database supports transactions
at line 654
boolean
supportsSavepoints()
Does this database support savepoints in transactions By default it is assumed that they don't unless they are explicitly enabled.
at line 669
withTransaction(callable $callback, callable $errorCallback = null, bool|string $transactionMode = false, bool $errorIfTransactionsUnsupported = false)
Invoke $callback within a transaction
at line 707
supportsExtensions($extensions)
at line 720
abstract
transactionStart(string|boolean $transactionMode = false, string|boolean $sessionCharacteristics = false)
Start a prepared transaction See http://developer.postgresql.org/pgdocs/postgres/sql-set-transaction.html for details on transaction isolation options
at line 727
abstract
transactionSavepoint(string $savepoint)
Create a savepoint that you can jump back to if you encounter problems
at line 739
abstract bool|null
transactionRollback(string|boolean $savepoint = false)
Rollback or revert to a savepoint if your queries encounter problems If you encounter a problem at any point during a transaction, you may need to rollback that particular query, or return to a savepoint
at line 748
abstract bool|null
transactionEnd(bool $chain = false)
Commit everything inside this transaction so far
at line 755
int
transactionDepth()
Return depth of current transaction
at line 771
bool
supportsLocks()
Determines if the used database supports application-level locks, which is different from table- or row-level locking.
See {@link getLock()} for details.
at line 783
bool
canLock(string $name)
Returns if the lock is available.
See {@link supportsLocks()} to check if locking is generally supported.
at line 805
bool
getLock(string $name, integer $timeout = 5)
Sets an application-level lock so that no two processes can run at the same time, also called a "cooperative advisory lock".
Return FALSE if acquiring the lock fails; otherwise return TRUE, if lock was acquired successfully. Lock is automatically released if connection to the database is broken (either normally or abnormally), making it less prone to deadlocks than session- or file-based locks. Should be accompanied by a {@link releaseLock()} call after the logic requiring the lock has completed. Can be called multiple times, in which case locks "stack" (PostgreSQL, SQL Server), or auto-releases the previous lock (MySQL).
Note that this might trigger the database to wait for the lock to be released, delaying further execution.
at line 817
bool
releaseLock(string $name)
Remove an application-level lock file to allow another process to run (if the execution aborts (e.g. due to an error) all locks are automatically released).
at line 834
connect(array $parameters)
Instruct the database to generate a live connection
at line 858
bool
databaseExists(string $name)
Determine if the database with the specified name exists
at line 868
array
databaseList()
Retrieves the list of all databases the user has access to
at line 885
bool
selectDatabase(string $name, bool $create = false, int|bool $errorLevel = E_USER_ERROR)
Change the connection to the specified database, optionally creating the database if it doesn't exist in the current schema.
at line 911
dropSelectedDatabase()
Drop the database that this object is currently connected to.
Use with caution.
at line 925
string|null
getSelectedDatabase()
Returns the name of the currently selected database
at line 935
abstract string
now()
Return SQL expression used to represent the current date/time
at line 942
abstract string
random()
Returns the database-specific version of the random() function