DB
class DB
Global database interface, complete with static methods.
Use this class for interacting with the database.
Constants
| USE_ANSI_SQL |
This constant was added in SilverStripe 2.4 to indicate that SQL-queries should now use ANSI-compatible syntax. The most notable affect of this change is that table and field names should be escaped with double quotes and not backticks |
| ALT_DB_KEY |
Session key for alternative database name |
Config options
| alternative_database_enabled | bool | Allow alternative DB to be disabled. |
Properties
| static string | $lastQuery | The last SQL query run. |
Methods
Retrieves the schema manager for the current database
Builds a sql query with the specified connection
Retrieves the connector object for the current database
Set an alternative database in a browser cookie, with the cookie lifetime set to the browser session.
Get the name of the database in use
Determines if the name is valid, as a security measure against setting arbitrary databases.
Specify connection to a database
Set config for a lazy-connected database
Get the named connection config
Returns true if a database connection has been attempted.
Helper function for generating a list of parameter placeholders for the given argument(s)
No description
Execute the given SQL parameterised query with the specified arguments
Execute a complex manipulation on the database.
Get the autogenerated ID from the previous INSERT query.
Check if the connection to the database is active.
Create the database and connect to it. This can be called if the initial database connection is not successful because the database does not exist.
Create a new table.
Create a new field on a table.
Generate the following table in the database, modifying whatever already exists as necessary.
Generate the given field on the table, modifying whatever already exists as necessary.
Generate the given index in the database, modifying whatever already exists as necessary.
If the given table exists, move it out of the way by renaming it to obsolete(tablename).
See {@link SS_Database->dontRequireField()}.
Checks a table's integrity and repairs it if necessary.
Return the number of rows affected by the previous operation.
Returns a list of all tables in the database.
Get a list of all the fields for the given table.
Enable supression of database messages.
Show a message about database alteration
Details
at line 87
static
set_conn(Database $connection, string $name = 'default')
Set the global database connection.
Pass an object that's a subclass of SS_Database. This object will be used when {@link DB::query()} is called.
at line 99
static Database
get_conn(string $name = 'default')
Get the global database connection.
at line 118
static
getConn($name = 'default')
deprecated
deprecated since version 4.0 Use DB::get_conn instead
at line 131
static DBSchemaManager
get_schema(string $name = 'default')
Retrieves the schema manager for the current database
at line 149
static string
build_sql(SQLExpression $expression, array $parameters, string $name = 'default')
Builds a sql query with the specified connection
at line 167
static DBConnector
get_connector(string $name = 'default')
Retrieves the connector object for the current database
at line 191
static
set_alternative_database_name(string $name = null)
Set an alternative database in a browser cookie, with the cookie lifetime set to the browser session.
This is useful for integration testing on temporary databases.
There is a strict naming convention for temporary databases to avoid abuse:
Note that the database will be set on the next request. Set it to null to revert to the main database.
at line 227
static string|false
get_alternative_database_name()
Get the name of the database in use
at line 263
static bool
valid_alternative_database_name(string $name)
Determines if the name is valid, as a security measure against setting arbitrary databases.
at line 285
static Database
connect(array $databaseConfig, string $label = 'default')
Specify connection to a database
Given the database configuration, this method will create the correct subclass of {@link SS_Database}.
at line 315
static
setConfig(array $databaseConfig, string $name = 'default')
Set config for a lazy-connected database
at line 326
static mixed
getConfig(string $name = 'default')
Get the named connection config
at line 338
static
connection_attempted()
Returns true if a database connection has been attempted.
In particular, it lets the caller know if we're still so early in the execution pipeline that we haven't even tried to connect to the database yet.
at line 349
static Query
query(string $sql, int $errorLevel = E_USER_ERROR)
Execute the given SQL query.
at line 365
static string|null
placeholders(array|integer $input, string $join = ', ')
Helper function for generating a list of parameter placeholders for the given argument(s)
at line 386
static string
inline_parameters(string $sql, array $parameters)
at line 441
static Query
prepared_query(string $sql, array $parameters, int $errorLevel = E_USER_ERROR)
Execute the given SQL parameterised query with the specified arguments
at line 490
static
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, including quotes. The field value can also be a SQL function or similar.
Example:
array(
// Command: insert
"table name" => array(
"command" => "insert",
"fields" => array(
"ClassName" => "'MyClass'", // if you're setting a literal, you need to escape and provide quotes
"Created" => "now()", // alternatively, you can call DB functions
"ID" => 234,
),
"id" => 234 // an alternative to providing ID in the fields list
),
// Command: update
"other table" => array(
"command" => "update",
"fields" => array(
"ClassName" => "'MyClass'",
"LastEdited" => "now()",
),
"where" => "ID = 234",
"id" => 234 // an alternative to providing a where clause
),
)
You'll note that only one command on a given table can be called. That's a limitation of the system that's due to it being written for {@link DataObject::write()}, which needs to do a single write on a number of different tables.
at line 502
static int
get_generated_id(string $table)
Get the autogenerated ID from the previous INSERT query.
at line 512
static boolean
is_active()
Check if the connection to the database is active.
at line 525
static boolean
create_database(string $database)
Create the database and connect to it. This can be called if the initial database connection is not successful because the database does not exist.
at line 543
static string
create_table(string $table, array$fields $fields = null, array $indexes = null, array $options = null, array $advancedOptions = null)
Create a new table.
at line 559
static
create_field(string $table, string $field, string $spec)
Create a new field on a table.
at line 579
static
require_table(string $table, string $fieldSchema = null, string $indexSchema = null, boolean $hasAutoIncPK = true, string $options = null, array $extensions = null)
Generate the following table in the database, modifying whatever already exists as necessary.
at line 597
static
require_field(string $table, string $field, string $spec)
Generate the given field on the table, modifying whatever already exists as necessary.
at line 609
static
require_index(string $table, string $index, string|boolean $spec)
Generate the given index in the database, modifying whatever already exists as necessary.
at line 619
static
dont_require_table(string $table)
If the given table exists, move it out of the way by renaming it to obsolete(tablename).
at line 630
static
dont_require_field(string $table, string $fieldName)
See {@link SS_Database->dontRequireField()}.
at line 641
static boolean
check_and_repair_table(string $table)
Checks a table's integrity and repairs it if necessary.
at line 651
static integer
affected_rows()
Return the number of rows affected by the previous operation.
at line 662
static array
table_list()
Returns a list of all tables in the database.
The table names will be in lower case.
at line 674
static array
field_list(string $table)
Get a list of all the fields for the given table.
Returns a map of field name => field spec.
at line 684
static
quiet(bool $quiet = true)
Enable supression of database messages.
at line 695
static
alteration_message(string $message, string $type = "")
Show a message about database alteration