Properties

$_adapterMethods

$_adapterMethods : array

List of public methods supported by the attached adapter.

Method names are in the keys.

Type

array

Methods

__construct()

__construct(\Horde_Db_Adapter_Base  $adapter) 

Constructor.

Parameters

\Horde_Db_Adapter_Base $adapter

A Horde_Db_Adapter instance.

setAdapter()

setAdapter(\Horde_Db_Adapter  $adapter) 

Setter for a Horde_Db_Adapter instance.

Parameters

\Horde_Db_Adapter $adapter

A Horde_Db_Adapter instance.

makeColumn()

makeColumn(string  $name, string  $default, string  $sqlType = null, boolean  $null = true) : \Horde_Db_Adapter_Base_Column

Factory for Column objects.

Parameters

string $name

The column's name, such as "supplier_id" in "supplier_id int(11)".

string $default

The type-casted default value, such as "new" in "sales_stage varchar(20) default 'new'".

string $sqlType

Used to extract the column's type, length and signed status, if necessary. For example "varchar" and "60" in "company_name varchar(60)" or "unsigned => true" in "int(10) UNSIGNED".

boolean $null

Whether this column allows NULL values.

Returns

\Horde_Db_Adapter_Base_Column

A column object.

makeColumnDefinition()

makeColumnDefinition(  $base,   $name,   $type,   $limit = null,   $precision = null,   $scale = null,   $unsigned = null,   $default = null,   $null = null,   $autoincrement = null) : \Horde_Db_Adapter_Base_ColumnDefinition

Factory for ColumnDefinition objects.

Parameters

$base
$name
$type
$limit
$precision
$scale
$unsigned
$default
$null
$autoincrement

Returns

\Horde_Db_Adapter_Base_ColumnDefinition

A column definition object.

makeIndex()

makeIndex(string  $table, string  $name, boolean  $primary, boolean  $unique, array  $columns) : \Horde_Db_Adapter_Base_Index

Factory for Index objects.

Parameters

string $table

The table the index is on.

string $name

The index's name.

boolean $primary

Is this a primary key?

boolean $unique

Is this a unique index?

array $columns

The columns this index covers.

Returns

\Horde_Db_Adapter_Base_Index

An index object.

makeTable()

makeTable(  $name,   $primaryKey,   $columns,   $indexes) : \Horde_Db_Adapter_Base_Table

Factory for Table objects.

Parameters

$name
$primaryKey
$columns
$indexes

Returns

\Horde_Db_Adapter_Base_Table

A table object.

makeTableDefinition()

makeTableDefinition(  $name,   $base,   $options = array()) : \Horde_Db_Adapter_Base_TableDefinition

Factory for TableDefinition objects.

Parameters

$name
$base
$options

Returns

\Horde_Db_Adapter_Base_TableDefinition

A table definition object.

__call()

__call(string  $method, array  $args) : mixed

Delegates calls to the adapter object.

Parameters

string $method

A method name.

array $args

Method parameters.

Throws

\BadMethodCallException

if method doesn't exist in the adapter.

Returns

mixed —

The method call result.

__get()

__get(string  $key) : object

Delegates access to $_cache and $_logger to the adapter object.

Parameters

string $key

Property name. Only '_cache' and '_logger' are supported.

Returns

object —

The request property object.

quote()

quote(mixed  $value, object  $column = null) : string

Quotes the column value to help prevent SQL injection attacks.

This method makes educated guesses on the scalar type based on the passed value. Make sure to correctly cast the value and/or pass the $column parameter to get the best results.

Parameters

mixed $value

The scalar value to quote, a Horde_Db_Value, Horde_Date, or DateTime instance, or an object implementing quotedId().

object $column

An object implementing getType().

Returns

string —

The correctly quoted value.

quoteString()

quoteString(string  $string) : string

Quotes a string, escaping any ' (single quote) and \ (backslash) characters.

Parameters

string $string

A string to escape.

Returns

string —

The escaped and quoted string.

quoteColumnName()

quoteColumnName(string  $name) : string

Returns a quoted form of the column name.

Parameters

string $name

A column name.

Returns

string —

The quoted column name.

quoteTableName()

quoteTableName(string  $name) : string

Returns a quoted form of the table name.

Defaults to column name quoting.

Parameters

string $name

A table name.

Returns

string —

The quoted table name.

quoteTrue()

quoteTrue() : string

Returns a quoted boolean true.

Returns

string —

The quoted boolean true.

quoteFalse()

quoteFalse() : string

Returns a quoted boolean false.

Returns

string —

The quoted boolean false.

quoteDate()

quoteDate(  $value) : string

Returns a quoted date value.

Parameters

$value

Returns

string —

The quoted date value.

quoteBinary()

quoteBinary(  $value) : string

Returns a quoted binary value.

Parameters

$value

Returns

string —

The quoted binary value.

nativeDatabaseTypes()

nativeDatabaseTypes() : array

Returns a hash of mappings from the abstract data types to the native database types.

See TableDefinition::column() for details on the recognized abstract data types.

Returns

array —

A database type map.

tableAliasLength()

tableAliasLength() : integer

Returns the maximum length a table alias can have.

Returns

integer —

The maximum table alias length.

tableAliasFor()

tableAliasFor(string  $tableName) : string

Converts a table name into a suitable table alias.

Parameters

string $tableName

A table name.

Returns

string —

A possible alias name for the table.

tables()

tables() : array

Returns a list of all tables of the current database.

Returns

array —

A table list.

table()

table(string  $tableName, string  $name = null) : \Horde_Db_Adapter_Base_Table

Returns a Horde_Db_Adapter_Base_Table object for a table.

Parameters

string $tableName

A table name.

string $name

(can be removed?)

Returns

\Horde_Db_Adapter_Base_Table

A table object.

primaryKey()

primaryKey(string  $tableName, string  $name = null) : \Horde_Db_Adapter_Base_Index

Returns a table's primary key.

Parameters

string $tableName

A table name.

string $name

(can be removed?)

Returns

\Horde_Db_Adapter_Base_Index

The primary key index object.

indexes()

indexes(string  $tableName, string  $name = null) : array

Returns a list of tables indexes.

Parameters

string $tableName

A table name.

string $name

(can be removed?)

Returns

array —

A list of Horde_Db_Adapter_Base_Index objects.

columns()

columns(string  $tableName, string  $name = null) : array

Returns a list of table columns.

Parameters

string $tableName

A table name.

string $name

(can be removed?)

Returns

array —

A list of Horde_Db_Adapter_Base_Column objects.

column()

column(string  $tableName, string  $columnName) : \Horde_Db_Adapter_Base_Column

Returns a table column.

Parameters

string $tableName

A table name.

string $columnName

A column name.

Throws

\Horde_Db_Exception

if column not found.

Returns

\Horde_Db_Adapter_Base_Column

A column object.

createTable()

createTable(string  $name, array  $options = array()) : \Horde_Db_Adapter_Base_TableDefinition

Creates a new table.

The $options hash can include the following keys:

  • autoincrementKey (string|array): The name of the autoincrementing primary key, if one is to be added automatically. Defaults to "id".
  • options (array): Any extra options you want appended to the table definition.
  • temporary (boolean): Make a temporary table.
  • force (boolean): Set to true or false to drop the table before creating it. Defaults to false.

Examples: // Add a backend specific option to the generated SQL (MySQL) $schema->createTable('suppliers', array('options' => 'ENGINE=InnoDB DEFAULT CHARSET=utf8'))); generates:

 CREATE TABLE suppliers (
   id int(10) UNSIGNED  NOT NULL AUTO_INCREMENT PRIMARY KEY
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8

// Rename the primary key column $table = $schema->createTable('objects', array('autoincrementKey' => 'guid')); $table->column('name', 'string', array('limit' => 80)); $table->end(); generates:

 CREATE TABLE objects (
   guid int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
   name varchar(80)
 )

// Do not add a primary key column, use fluent interface, use type // method. $schema->createTable('categories_suppliers', array('autoincrementKey' => false)) ->column('category_id', 'integer') ->integer('supplier_id') ->end(); generates:

 CREATE TABLE categories_suppliers (
   category_id int(11),
   supplier_id int(11)
 )

See also Horde_Db_Adapter_Base_TableDefinition::column() for details on how to create columns.

Parameters

string $name

A table name.

array $options

A list of table options, see the method description.

Returns

\Horde_Db_Adapter_Base_TableDefinition

The definition of the created table.

endTable()

endTable(string|\Horde_Db_Adapter_Base_TableDefinition  $name, array  $options = array()) 

Finishes and executes table creation.

Parameters

string|\Horde_Db_Adapter_Base_TableDefinition $name

A table name or object.

array $options

A list of options. See createTable().

renameTable()

renameTable(string  $name, string  $newName) 

Renames a table.

Parameters

string $name

A table name.

string $newName

The new table name.

dropTable()

dropTable(string  $name) 

Drops a table from the database.

Parameters

string $name

A table name.

addColumn()

addColumn(string  $tableName, string  $columnName, string  $type, array  $options = array()) 

Adds a new column to a table.

Parameters

string $tableName

A table name.

string $columnName

A column name.

string $type

A data type.

array $options

Column options. See Horde_Db_Adapter_Base_TableDefinition#column() for details.

removeColumn()

removeColumn(string  $tableName, string  $columnName) 

Removes a column from a table.

Parameters

string $tableName

A table name.

string $columnName

A column name.

changeColumn()

changeColumn(string  $tableName, string  $columnName, string  $type, array  $options = array()) 

Changes an existing column's definition.

Parameters

string $tableName

A table name.

string $columnName

A column name.

string $type

A data type.

array $options

Column options. See Horde_Db_Adapter_Base_TableDefinition#column() for details.

changeColumnDefault()

changeColumnDefault(string  $tableName, string  $columnName, mixed  $default) 

Sets a new default value for a column.

If you want to set the default value to NULL, you are out of luck. You need to execute the apppropriate SQL statement yourself.

Parameters

string $tableName

A table name.

string $columnName

A column name.

mixed $default

The new default value.

renameColumn()

renameColumn(string  $tableName, string  $columnName, string  $newColumnName) 

Renames a column.

Parameters

string $tableName

A table name.

string $columnName

A column name.

string $newColumnName

The new column name.

addPrimaryKey()

addPrimaryKey(string  $tableName,   $columns) 

Adds a primary key to a table.

Parameters

string $tableName

A table name.

$columns

Throws

\Horde_Db_Exception

removePrimaryKey()

removePrimaryKey(string  $tableName) 

Removes a primary key from a table.

Parameters

string $tableName

A table name.

Throws

\Horde_Db_Exception

addIndex()

addIndex(string  $tableName, string|array  $columnName, array  $options = array()) : string

Adds a new index to a table.

The index will be named after the table and the first column names, unless you pass 'name' as an option.

When creating an index on multiple columns, the first column is used as a name for the index. For example, when you specify an index on two columns 'first' and 'last', the DBMS creates an index for both columns as well as an index for the first colum 'first'. Using just the first name for this index makes sense, because you will never have to create a singular index with this name.

Examples:

Creating a simple index $schema->addIndex('suppliers', 'name'); generates CREATE INDEX suppliers_name_index ON suppliers(name)

Creating a unique index $schema->addIndex('accounts', array('branch_id', 'party_id'), array('unique' => true)); generates CREATE UNIQUE INDEX accounts_branch_id_index ON accounts(branch_id, party_id)

Creating a named index $schema->addIndex('accounts', array('branch_id', 'party_id'), array('unique' => true, 'name' => 'by_branch_party')); generates CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)

Parameters

string $tableName

A table name.

string|array $columnName

One or more column names.

array $options

Index options:

  • name: (string) the index name.
  • unique: (boolean) create a unique index?

Returns

string —

The index name. @since Horde_Db 2.1.0

removeIndex()

removeIndex(string  $tableName, string|array  $options = array()) 

Removes an index from a table.

Examples:

Remove the suppliers_name_index in the suppliers table: $schema->removeIndex('suppliers', 'name');

Remove the index named accounts_branch_id in the accounts table: $schema->removeIndex('accounts', array('column' => 'branch_id'));

Remove the index named by_branch_party in the accounts table: $schema->removeIndex('accounts', array('name' => 'by_branch_party'));

You can remove an index on multiple columns by specifying the first column: $schema->addIndex('accounts', array('username', 'password')) $schema->removeIndex('accounts', 'username');

Parameters

string $tableName

A table name.

string|array $options

Either a column name or index options:

  • name: (string) the index name.
  • column: (string|array) column name(s).

indexName()

indexName(string  $tableName, string|array  $options = array()) 

Builds the name for an index.

Parameters

string $tableName

A table name.

string|array $options

Either a column name or index options:

  • column: (string|array) column name(s).
  • name: (string) the index name to fall back to if no column names specified.

recreateDatabase()

recreateDatabase(string  $name) 

Recreates, i.e. drops then creates a database.

Parameters

string $name

A database name.

createDatabase()

createDatabase(string  $name, array  $options = array()) 

Creates a database.

Parameters

string $name

A database name.

array $options

Database options.

dropDatabase()

dropDatabase(string  $name) 

Drops a database.

Parameters

string $name

A database name.

currentDatabase()

currentDatabase() : string

Returns the name of the currently selected database.

Returns

string —

The database name.

typeToSql()

typeToSql(string  $type, integer  $limit = null, integer  $precision = null, integer  $scale = null, boolean  $unsigned = null) : string

Generates the SQL definition for a column type.

Parameters

string $type

A column type.

integer $limit

Maximum column length (non decimal type only)

integer $precision

The number precision (decimal type only).

integer $scale

The number scaling (decimal columns only).

boolean $unsigned

Whether the column is an unsigned number (non decimal columns only).

Returns

string —

The SQL definition. If $type is not one of the internally supported types, $type is returned unchanged.

addColumnOptions()

addColumnOptions(string  $sql, array  $options) : string

Adds default/null options to column SQL definitions.

Parameters

string $sql

Existing SQL definition for a column.

array $options

Column options:

  • column: (Horde_Db_Adapter_Base_ColumnDefinition The column definition class.
  • null: (boolean) Whether to allow NULL values.
  • default: (mixed) Default column value.
  • autoincrement: (boolean) Whether the column is an autoincrement column. Driver depedendent.

Returns

string —

The manipulated SQL definition.

distinct()

distinct(string  $columns, string  $orderBy = null) : string

Generates a DISTINCT clause for SELECT queries.

$connection->distinct('posts.id', 'posts.created_at DESC')

Parameters

string $columns

A column list.

string $orderBy

An ORDER clause.

Returns

string —

The generated DISTINCT clause.

addOrderByForAssocLimiting()

addOrderByForAssocLimiting(string  $sql, array  $options) : string

Adds an ORDER BY clause to an existing query.

Parameters

string $sql

An SQL query to manipulate.

array $options

Options:

  • order: Order column an direction.

Returns

string —

The manipulated SQL query.

interval()

interval(string  $interval, string  $precision) : string

Generates an INTERVAL clause for SELECT queries.

Parameters

string $interval

The interval.

string $precision

The precision.

Returns

string —

The generated INTERVAL clause.

modifyDate()

modifyDate(string  $reference, string  $operator, integer  $amount, string  $interval) : string

Generates a modified date for SELECT queries.

Parameters

string $reference

The reference date - this is a column referenced in the SELECT.

string $operator

Add or subtract time? (+/-)

integer $amount

The shift amount (number of days if $interval is DAY, etc).

string $interval

The interval (SECOND, MINUTE, HOUR, DAY, MONTH, YEAR).

Returns

string —

The generated INTERVAL clause.

buildClause()

buildClause(string  $lhs, string  $op, string  $rhs, boolean  $bind = false, array  $params = array()) : string|array

Returns an expression using the specified operator.

Parameters

string $lhs

The column or expression to test.

string $op

The operator.

string $rhs

The comparison value.

boolean $bind

If true, the method returns the query and a list of values suitable for binding as an array.

array $params

Any additional parameters for the operator.

Returns

string|array —

The SQL test fragment, or an array containing the query and a list of values if $bind is true.

_escapePrepare()

_escapePrepare(string  $query) : string

Escapes all characters in a string that are placeholders for prepare/execute methods.

Parameters

string $query

A string to escape.

Returns

string —

The correctly escaped string.

_clearTableCache()

_clearTableCache(string  $tableName) 

Clears the cache for tables when altering them.

Parameters

string $tableName

A table name.