\Dewdrop\Db\AdapterMock

A mock adapter that uses a mock wpdb connection with no actual connection to the database. Can be used when testing to ensure certain methods are called when you expecte them to be without actually hitting the MySQL database directly.

However, it is altered to accommodate the limitations of wpdb and use the normal wpdb instance created on every WordPress request as its driver. This allows us to take advantage of the more expressive and powerful DB API from Zend_Db without needing to create a secondary MySQL connection on every request.

Summary

Methods
Properties
Constants
__construct()
setDriver()
getConnection()
getDriver()
getTableMetadata()
getTableMetadataPath()
setTableMetadataPath()
select()
fetchAll()
fetchAllGroupedByKey()
fetchAllWithGenerator()
fetchCol()
fetchRow()
fetchPairs()
fetchOne()
insert()
lastInsertId()
getLastInsertTableName()
setLastInsertTableName()
update()
delete()
query()
prepare()
quoteIdentifier()
quote()
quoteInto()
quoteTableAs()
quoteColumnAs()
getQuoteIdentifierSymbol()
limit()
listTables()
listForeignKeyReferences()
listMissingForeignKeyIndexes()
listUniqueConstraints()
describeTable()
beginTransaction()
commit()
rollback()
foldCase()
generateCreateIndexStatement()
generateAnalyzeTableStatement()
No public properties found
OBJECT
OBJECT_K
ARRAY_A
ARRAY_N
INT_TYPE
BIGINT_TYPE
FLOAT_TYPE
CASE_LOWER
CASE_NATURAL
CASE_UPPER
quoteInternal()
quoteIdentifierAs()
quoteIdentifierInternal()
whereExpr()
$caseFolding
$numericDataTypes
$autoQuoteIdentifiers
$driver
$lastInsertTableName
$tableMetadataPath
N/A
No private methods found
No private properties found
N/A

Constants

OBJECT

OBJECT

OBJECT_K

OBJECT_K

ARRAY_A

ARRAY_A

ARRAY_N

ARRAY_N

INT_TYPE

INT_TYPE

Use the INT_TYPE, BIGINT_TYPE, and FLOAT_TYPE with the quote() method.

BIGINT_TYPE

BIGINT_TYPE

FLOAT_TYPE

FLOAT_TYPE

CASE_LOWER

CASE_LOWER

PDO constant values used by some helper methods on the adapter class.

CASE_NATURAL

CASE_NATURAL

CASE_UPPER

CASE_UPPER

Properties

$caseFolding

$caseFolding : integer

How to handle case folding for keys in result sets.

Type

integer

$numericDataTypes

$numericDataTypes : array

Keys are UPPERCASE SQL datatypes or the constants Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.

Values are: 0 = 32-bit integer 1 = 64-bit integer 2 = float or decimal

Type

array — Associative array of datatypes to values 0, 1, or 2.

$autoQuoteIdentifiers

$autoQuoteIdentifiers : boolean

Whether to quote (i.e. add surrounding backticks to) identifiers like table and column names.

Type

boolean

$lastInsertTableName

$lastInsertTableName : mixed

The name of the table that was affected by the most recent call to insert() is stored here so that we can use it to retrieve the last insert ID when using PostgreSQL.

Type

mixed

$tableMetadataPath

$tableMetadataPath : string

Path to database table metadata.

Type

string

Methods

__construct()

__construct(\Dewdrop\Db\Driver\Mock  $driver = null) 

Override default adapter constructor because we no longer need to accept $wpdb when just using a mock adapter for testing.

Parameters

\Dewdrop\Db\Driver\Mock $driver

getConnection()

getConnection() : mixed

Returns the underlying database connection object or resource.

If not presently connected, this initiates the connection.

Returns

mixed

getTableMetadata()

getTableMetadata(string  $table) : array

Returns table metadata information.

Parameters

string $table

Throws

\Dewdrop\Exception

Returns

array

getTableMetadataPath()

getTableMetadataPath() : string

Returns table metadata filesystem path.

Returns

string

setTableMetadataPath()

setTableMetadataPath(string  $tableMetadataPath) : \Dewdrop\Db\Adapter

Sets table metadata filesystem path.

Parameters

string $tableMetadataPath

Returns

\Dewdrop\Db\Adapter

select()

select() : \Dewdrop\Db\Select

Create a \Dewdrop\Db\Select object.

Returns

\Dewdrop\Db\Select

fetchAll()

fetchAll(string|\Dewdrop\Db\Select  $sql, array  $bind = array(), string  $fetchMode = null) : array

Fetch all results for the supplied SQL statement.

Parameters

string|\Dewdrop\Db\Select $sql
array $bind
string $fetchMode

Returns

array

fetchAllGroupedByKey()

fetchAllGroupedByKey(string|\Dewdrop\Db\Select  $sql, string  $groupKey, array  $bind = array(), string  $fetchMode = null) : array

Fetch all results for the supplied SQL statement and group them into a nested array using the supplied $groupKey.

For exapmle, if you had a resultset containing these three rows:

name | family_id
----------------
Bob  | 1
Tim  | 1
Ken  | 2

And you called fetchAllGroupedByKey() with a $groupKey of 'family_id', you'd get the following array in return:

[
    1 => [
        ['name' => 'Bob', 'family_id' => 1],
        ['name' => 'Tim', 'family_id' => 1],
    ],
    2 => [
        ['name' => 'Ken', 'family_id' => 2],
    ]
]

Parameters

string|\Dewdrop\Db\Select $sql
string $groupKey
array $bind
string $fetchMode

Returns

array

fetchAllWithGenerator()

fetchAllWithGenerator(string|\Dewdrop\Db\Select  $sql, array  $bind = array(), string  $fetchMode = null) : \Generator

Fetch all results for the supplied SQL statement using a PHP generator.

This approach uses less memory, but the result set has a forward-only cursor.

Parameters

string|\Dewdrop\Db\Select $sql
array $bind
string $fetchMode

Returns

\Generator

fetchCol()

fetchCol(string|\Dewdrop\Db\Select  $sql, array  $bind = array()) : array

Fetch a single column of the results from the supplied SQL statement.

Parameters

string|\Dewdrop\Db\Select $sql
array $bind

Returns

array

fetchRow()

fetchRow(string|\Dewdrop\Db\Select  $sql, array  $bind = array(), string  $fetchMode = null) : array

Fetch a single row from the results from the supplied SQL statement.

This method still uses a fetchAll internally, so you should either by selecting on a primary key or other unique key or LIMITing your results explicitly.

Parameters

string|\Dewdrop\Db\Select $sql
array $bind
string $fetchMode

Returns

array

fetchPairs()

fetchPairs(string|\Dewdrop\Db\Select  $sql, array  $bind = array()) : array

Returns the first two columns from the SQL results as key-value pairs useful, for examples, for lists of options in a drop-down.

Parameters

string|\Dewdrop\Db\Select $sql

An SQL SELECT statement.

array $bind

Returns

array

fetchOne()

fetchOne(string|\Dewdrop\Db\Select  $sql, array  $bind = array()) : mixed

Fetch a single scalar value from the results of the supplied SQL statement.

Parameters

string|\Dewdrop\Db\Select $sql
array $bind

Returns

mixed

insert()

insert(mixed  $table, array  $bind) : integer

Inserts a table row with specified data.

Parameters

mixed $table

The table to insert data into.

array $bind

Column-value pairs.

Returns

integer —

The number of affected rows.

lastInsertId()

lastInsertId() : integer

Get the last insert ID from after performing an insert on a table with an auto-incrementing primary key.

Returns

integer

getLastInsertTableName()

getLastInsertTableName() : string

Retrieve the name of the table affected by the last call to insert().

Allows us to get last insert ID when using drivers that don't have a function for that built-in (i.e. pgsql).

Returns

string

setLastInsertTableName()

setLastInsertTableName(string  $lastInsertTableName) : $this

Set the name of the table into which data were last INSERTed. Useful when an INSERT occurs outside the context of the insert() method.

Parameters

string $lastInsertTableName

Returns

$this

update()

update(mixed  $table, array  $bind, mixed  $where = '') : integer

Updates table rows with specified data based on a WHERE clause.

Parameters

mixed $table

The table to update.

array $bind

Column-value pairs.

mixed $where

UPDATE WHERE clause(s).

Returns

integer —

The number of affected rows.

delete()

delete(mixed  $table, mixed  $where = '') : integer

Deletes table rows based on a WHERE clause.

Parameters

mixed $table

The table to update.

mixed $where

DELETE WHERE clause(s).

Returns

integer —

The number of affected rows.

query()

query(string|\Dewdrop\Db\Select  $sql, array  $bind = array()) : mixed

Run the supplied query, binding the supplied data to the statement prior to execution.

Parameters

string|\Dewdrop\Db\Select $sql
array $bind

Returns

mixed

prepare()

prepare(string|\Dewdrop\Db\Select  $sql, array  $bind = array()) : string

Quote the bound values into the provided SQL query.

Because the mysql extension used by wpdb does not support prepared statements, we emulate that support here by interpolating the bound values into "?" placeholders in the SQL string. We have to break the overall SQL command into segments containing a single "?" placeholder each so that we don't mistake "?" characters in previously bound values as new placeholders.

Parameters

string|\Dewdrop\Db\Select $sql
array $bind

Returns

string

quoteIdentifier()

quoteIdentifier(string  $identifier, boolean  $auto = false) : string

Quote a database identifier such as a column name or table name to make it safe to include in a SQL statement.

Parameters

string $identifier
boolean $auto

Returns

string

quote()

quote(mixed  $value, mixed  $type = null) : mixed

Safely quotes a value for an SQL statement.

If an array is passed as the value, the array values are quoted and then returned as a comma-separated string.

Parameters

mixed $value

The value to quote.

mixed $type

OPTIONAL the SQL datatype name, or constant, or null.

Returns

mixed —

An SQL-safe quoted value (or string of separated values).

quoteInto()

quoteInto(string  $text, mixed  $value, string  $type = null, integer  $count = null) : string

Quotes a value and places into a piece of text at a placeholder.

The placeholder is a question-mark; all placeholders will be replaced with the quoted value. For example:

$text = "WHERE date < ?";
$date = "2005-01-02";
$safe = $sql->quoteInto($text, $date);
// $safe = "WHERE date < '2005-01-02'"

Parameters

string $text

The text with a placeholder.

mixed $value

The value to quote.

string $type

OPTIONAL SQL datatype

integer $count

OPTIONAL count of placeholders to replace

Returns

string —

An SQL-safe quoted value placed into the original text.

quoteTableAs()

quoteTableAs(string|array|\Dewdrop\Db\Expr  $ident, string  $alias = null, boolean  $auto = false) : string

Quote a table identifier and alias.

Parameters

string|array|\Dewdrop\Db\Expr $ident

The identifier or expression.

string $alias

An alias for the table.

boolean $auto

If true, heed the AUTO_QUOTE_IDENTIFIERS config option.

Returns

string —

The quoted identifier and alias.

quoteColumnAs()

quoteColumnAs(string|array|\Dewdrop\Db\Expr  $ident, string  $alias, boolean  $auto = false) : string

Quote a column identifier and alias.

Parameters

string|array|\Dewdrop\Db\Expr $ident

The identifier or expression.

string $alias

An alias for the column.

boolean $auto

If true, heed the AUTO_QUOTE_IDENTIFIERS config option.

Returns

string —

The quoted identifier and alias.

getQuoteIdentifierSymbol()

getQuoteIdentifierSymbol() : string

Returns the symbol the adapter uses for delimited identifiers.

Returns

string

limit()

limit(string  $sql, integer  $count, integer  $offset) : string

Adds an adapter-specific LIMIT clause to the SELECT statement.

Parameters

string $sql
integer $count
integer $offset

OPTIONAL

Returns

string

listTables()

listTables() : array

Returns a list of the tables in the database.

Returns

array

listForeignKeyReferences()

listForeignKeyReferences(string  $tableName) : array

Returns an associative array containing all the foreign key relationships associated with the supplied table.

The array has the following format:

array(
    'column_name' => array(
        'table'  => 'foreign_table',
        'column' => 'foreign_column'
    )
)

Parameters

string $tableName

Returns

array

listMissingForeignKeyIndexes()

listMissingForeignKeyIndexes(  $tableName) 

Parameters

$tableName

listUniqueConstraints()

listUniqueConstraints(string  $tableName) : array

Returns an associative array containing all the unique constraints on a table.

The array has the following format:

array(
    'key_name' => array(
        sequence_in_index => 'column_name'
    )
)

Parameters

string $tableName

Returns

array

describeTable()

describeTable(string  $tableName) : array

Returns the column descriptions for a table.

The return value is an associative array keyed by the column name, as returned by the RDBMS.

The value of each array element is an associative array with the following keys:

SCHEMA_NAME => string; name of database or schema TABLE_NAME => string; COLUMN_NAME => string; column name COLUMN_POSITION => number; ordinal position of column in table DATA_TYPE => string; SQL datatype name of column DEFAULT => string; default expression of column, null if none NULLABLE => boolean; true if column can have nulls LENGTH => number; length of CHAR/VARCHAR SCALE => number; scale of NUMERIC/DECIMAL PRECISION => number; precision of NUMERIC/DECIMAL UNSIGNED => boolean; unsigned property of an integer type PRIMARY => boolean; true if column is part of the primary key PRIMARY_POSITION => integer; position of column in primary key IDENTITY => integer; true if column is auto-generated with unique values

Parameters

string $tableName

Returns

array

beginTransaction()

beginTransaction() : void

Begin a new transaction.

commit()

commit() : void

Commit the current transaction.

rollback()

rollback() : void

Rollback the current transaction.

foldCase()

foldCase(string  $key) : string

Helper method to change the case of the strings used when returning result sets in FETCH_ASSOC and FETCH_BOTH modes.

This is not intended to be used by application code, but the method must be public so the Statement class can invoke it.

Parameters

string $key

Returns

string

generateCreateIndexStatement()

generateCreateIndexStatement(  $tableName, array  $columnNames) 

Parameters

$tableName
array $columnNames

generateAnalyzeTableStatement()

generateAnalyzeTableStatement(  $tableName) 

Parameters

$tableName

quoteInternal()

quoteInternal(mixed  $value) : mixed

An internal method used by other quote methods. This was origianlly _quote() in Zend_Db, but in PSR-2 code style, it had to be renamed to avoid conflicting with the public method of the same name. Because this adapter only needs to work with MySQL (because that is the only RDBMS supported by WP), this could likely be integrated directly with the quote methods.

Parameters

mixed $value

Returns

mixed

quoteIdentifierAs()

quoteIdentifierAs(string|array|\Dewdrop\Db\Expr  $ident, string  $alias = null, boolean  $auto = false, string  $as = ' AS ') : string

Quote an identifier and an optional alias.

Parameters

string|array|\Dewdrop\Db\Expr $ident

The identifier or expression.

string $alias

An optional alias.

boolean $auto

If true, heed the AUTO_QUOTE_IDENTIFIERS config option.

string $as

The string to add between the identifier/expression and the alias.

Returns

string —

The quoted identifier and alias.

quoteIdentifierInternal()

quoteIdentifierInternal(string  $value, boolean  $auto = false) : string

Quote an identifier.

Parameters

string $value

The identifier or expression.

boolean $auto

If true, heed the AUTO_QUOTE_IDENTIFIERS config option.

Returns

string —

The quoted identifier and alias.

whereExpr()

whereExpr(mixed  $where) : string

Convert an array, string, or Zend_Db_Expr object into a string to put in a WHERE clause.

Parameters

mixed $where

Returns

string