AnewtDatabaseConnection Class Reference

Abstract database connection class. More...

List of all members.

Public Member Functions

 __construct ($settings)
 Create a new connection instance (internal use only).
Transaction Methods
 transaction_begin ()
 Start a transaction.
 transaction_commit ()
 Commit a transaction.
 transaction_rollback ()
 Roll back a transaction.
Escaping Methods
The implementations of these methods in AnewtDatabaseConnection are generic. Some of those methods are overridden in the database backend specific subclasses.

Note that it is often not the right approach to invoke these methods yourself when building complex SQL queries. Creating an AnewtDatabaseSQLTemplate with create_sql_template() and filling that instead results in code that is much cleaner and more easy to read.

 escape_boolean ($value)
 Escape a boolean for use in SQL queries.
 escape_string ($value)
 Escape a string for use in SQL queries.
 escape_table_name ($value)
 Escape a table name for use in SQL queries.
 escape_column_name ($value)
 Escape a column name for use in SQL queries.
 escape_date ($value)
 Escape a date for use in SQL queries.
 escape_time ($value)
 Escape a time for use in SQL queries.
 escape_datetime ($value)
 Escape a datetime for use in SQL queries.

Public Attributes

 $n_queries_executed
 The number of executed queries.
 $queries_executed
 List of executed queries.

Protected Attributes

 $settings
 Connection settings.
 $connection_handle
 The underlying database connection resource.

Connection methods

These methods can be used to connect and disconnect from the database. Note that, by default, database connections automatically connect when they are setup using AnewtDatabase::setup_connection(). If you do not want this, set the autoconnect property to false when setting up the connection.

 connect ()
 Establish a database connection.
 disconnect ()
 Disconnect a database connection.
 is_connected ()
 Check whether the connection is currently open.
 real_connect ()
 Establish a database connection.
 real_disconnect ()
 Disconnect the database connection.

Query methods

These methods provide a rich API to execute queries and retrieve resulting rows. The basic functionality to prepare a query that can later be executed is provided by prepare(), but several convenience methods are available that cover the most frequent use cases, e.g. fetching all rows from a result set.

To build complex SQL queries, while still using the extensive type checking this module offers, you can use create_sql_template() to obtain an AnewtDatabaseSQLTemplate instance that you can fill manually.

 prepare ($sql)
 Prepare a query for execution.
 prepare_execute ($sql, $values=null)
 Execute a query using the values passed as multiple parameters, without retrieving resulting rows.
 prepare_executev ($sql, $values=null)
 Execute a query using the values passed as a single parameter, without retrieving resulting rows.
 prepare_execute_fetch_one ($sql, $values=null)
 Execute a query using the values passed as multiple parameters, and fetch the first row.
 prepare_executev_fetch_one ($sql, $values=null)
 Execute a query using the values passed as a single parameter, and fetch the first row.
 prepare_execute_fetch_all ($sql, $values=null)
 Execute a query using the values passed as multiple parameters, and fetch all rows.
 prepare_executev_fetch_all ($sql, $values=null)
 Execute a query using the values passed as a single parameter, and fetch all rows.
 create_sql_template ($sql)
 Return an AnewtDatabaseSQLTemplate for this connection.
 last_insert_id ($options=null)
 Get the last insert id for this connection.
 real_execute_sql ($sql)
 Execute SQL and create an AnewtDatabaseResultSet for a query.
 execute_sql ($sql)
 Execute SQL and create an AnewtDatabaseResultSet.


Detailed Description

Abstract database connection class.

This class is the base class for database connections, and is subclassed by specific database backends. AnewtDatabaseConnection instances cannot be created directly; see AnewtDatabase on how to setup and obtain AnewtDatabaseConnection instances.

The settings accepted by all backends are:

See the documentation for the backend-specific AnewtDatabaseConnection subclasses for more information:

See also:
AnewtDatabase

Definition at line 34 of file connection.lib.php.


Constructor & Destructor Documentation

AnewtDatabaseConnection::__construct ( settings  ) 

Create a new connection instance (internal use only).

Do not call this method directly; it is for internal use only. See the AnewtDatabase documentation on how to setup and obtain AnewtDatabaseConnection instances.

Parameters:
$settings Associative array with connection settings.

Reimplemented in AnewtDatabaseConnectionMemcache, AnewtDatabaseConnectionMySQLOld, AnewtDatabaseConnectionMySQL, AnewtDatabaseConnectionPostgreSQL, and AnewtDatabaseConnectionSQLite.

Definition at line 74 of file connection.lib.php.


Member Function Documentation

AnewtDatabaseConnection::connect (  ) 

Establish a database connection.

This function does nothing if the connection has been established already.

Definition at line 104 of file connection.lib.php.

AnewtDatabaseConnection::disconnect (  ) 

Disconnect a database connection.

This function does nothing if the connection has been disconnected already.

Definition at line 118 of file connection.lib.php.

AnewtDatabaseConnection::is_connected (  )  [abstract]

Check whether the connection is currently open.

Returns:
True if the connection is open, false otherwise.

Reimplemented in AnewtDatabaseConnectionMemcache, AnewtDatabaseConnectionMySQLOld, AnewtDatabaseConnectionMySQL, AnewtDatabaseConnectionPostgreSQL, and AnewtDatabaseConnectionSQLite.

AnewtDatabaseConnection::prepare ( sql  ) 

Prepare a query for execution.

This method can be used to execute the same query more than once, e.g. each with different values for the placeholders.

To obtain a result row (or all result rows) returned by a query, it is easier to use AnewtDatabaseConnection::prepare_execute_fetch or one of the other variants.

Parameters:
$sql The SQL query to be prepared (optionally with placeholders)
Returns:
A new AnewtDatabasePreparedQuery instance.
See also:
AnewtDatabasePreparedQuery

AnewtDatabaseResultSet

Definition at line 183 of file connection.lib.php.

AnewtDatabaseConnection::prepare_execute ( sql,
values = null 
)

Execute a query using the values passed as multiple parameters, without retrieving resulting rows.

See AnewtDatabaseConnection::prepare_executev for a detailed description.

Parameters:
$sql The SQL query to be prepared (optionally with placeholders)
$values Zero or more values to be substituted for the placeholders
See also:
AnewtDatabaseConnection::prepare_executev

AnewtDatabaseConnection::prepare_execute_fetch_one

AnewtDatabaseConnection::prepare_execute_fetch_all

Definition at line 202 of file connection.lib.php.

AnewtDatabaseConnection::prepare_executev ( sql,
values = null 
)

Execute a query using the values passed as a single parameter, without retrieving resulting rows.

For some query types the number of affected rows is returned. This only works for queries that operate on a number of rows, i.e. INSERT, UPDATE, REPLACE, and DELETE queries. For other query types null is returned.

Note that this method is mostly useless for SELECT queries since it will not return any results; use AnewtDatabaseConnection::prepare_execute_fetch or AnewtDatabaseConnection::prepare_execute_fetch_all if you want to retrieve result rows.

Parameters:
$sql The SQL query to be prepared (optionally with placeholders)
$values Zero or more values to be substituted for the placeholders
Returns:
The number of rows affected by the query.
See also:
AnewtDatabaseConnection::prepare_execute

AnewtDatabaseConnection::prepare_executev_fetch_one

AnewtDatabaseConnection::prepare_executev_fetch_all

Definition at line 235 of file connection.lib.php.

AnewtDatabaseConnection::prepare_execute_fetch_one ( sql,
values = null 
)

Execute a query using the values passed as multiple parameters, and fetch the first row.

Parameters:
$sql The SQL query to be prepared (optionally with placeholders)
$values Zero or more values to be substituted for the placeholders
Returns:
A single row, or NULL
See also:
AnewtDatabaseConnection::prepare_executev_fetch_one

AnewtDatabaseConnection::prepare_execute

AnewtDatabaseConnection::prepare_execute_fetch_all

Definition at line 274 of file connection.lib.php.

AnewtDatabaseConnection::prepare_executev_fetch_one ( sql,
values = null 
)

Execute a query using the values passed as a single parameter, and fetch the first row.

Parameters:
$sql The SQL query to be prepared (optionally with placeholders)
$values Zero or more values to be substituted for the placeholders
Returns:
A single row, or NULL
See also:
AnewtDatabaseConnection::prepare_execute_fetch_one

AnewtDatabaseConnection::prepare_executev

AnewtDatabaseConnection::prepare_executev_fetch_all

Reimplemented in AnewtDatabaseConnectionMemcache.

Definition at line 294 of file connection.lib.php.

AnewtDatabaseConnection::prepare_execute_fetch_all ( sql,
values = null 
)

Execute a query using the values passed as multiple parameters, and fetch all rows.

Parameters:
$sql The SQL query to be prepared (optionally with placeholders)
$values Zero or more values to be substituted for the placeholders
Returns:
Array of all rows (may be empty)
See also:
AnewtDatabaseConnection::prepare_executev_fetch_all

AnewtDatabaseConnection::prepare_execute

AnewtDatabaseConnection::prepare_execute_fetch_one

Definition at line 315 of file connection.lib.php.

AnewtDatabaseConnection::prepare_executev_fetch_all ( sql,
values = null 
)

Execute a query using the values passed as a single parameter, and fetch all rows.

Parameters:
$sql The SQL query to be prepared (optionally with placeholders)
$values Zero or more values to be substituted for the placeholders
Returns:
Array of all rows (may be empty)
See also:
AnewtDatabaseConnection::prepare_execute_fetch_all

AnewtDatabaseConnection::prepare_executev

AnewtDatabaseConnection::prepare_executev_fetch_one

Reimplemented in AnewtDatabaseConnectionMemcache.

Definition at line 335 of file connection.lib.php.

AnewtDatabaseConnection::create_sql_template ( sql  ) 

Return an AnewtDatabaseSQLTemplate for this connection.

Parameters:
$sql The SQL query to be prepared (optionally with placeholders)
Returns:
A new AnewtDatabaseSQLTemplate instance.

Definition at line 350 of file connection.lib.php.

AnewtDatabaseConnection::last_insert_id ( options = null  )  [abstract]

Get the last insert id for this connection.

The $options parameter is database backend specific. For most backends this value is not needed. At least PostgreSQL needs a sequence name. See the backend documentation for more information.

Parameters:
$options Backend-specific value.

Reimplemented in AnewtDatabaseConnectionMemcache, AnewtDatabaseConnectionMySQLOld, AnewtDatabaseConnectionMySQL, AnewtDatabaseConnectionPostgreSQL, and AnewtDatabaseConnectionSQLite.

AnewtDatabaseConnection::execute_sql ( sql  )  [private]

Execute SQL and create an AnewtDatabaseResultSet.

This method is for internal use only. Use one of the prepare() methods to prepare and execute queries.

Parameters:
$sql The SQL query to execute.
Returns:
A new AnewtDatabaseResultSet instance.

Definition at line 381 of file connection.lib.php.

AnewtDatabaseConnection::real_execute_sql ( sql  )  [abstract, protected]

Execute SQL and create an AnewtDatabaseResultSet for a query.

This method is for internal use only and is backend-specific.

Parameters:
$sql The SQL query to execute.

Reimplemented in AnewtDatabaseConnectionMemcache, AnewtDatabaseConnectionMySQLOld, AnewtDatabaseConnectionMySQL, AnewtDatabaseConnectionPostgreSQL, and AnewtDatabaseConnectionSQLite.

AnewtDatabaseConnection::escape_boolean ( value  ) 

Escape a boolean for use in SQL queries.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented in AnewtDatabaseConnectionMemcache, and AnewtDatabaseConnectionPostgreSQL.

Definition at line 459 of file connection.lib.php.

AnewtDatabaseConnection::escape_string ( value  ) 

Escape a string for use in SQL queries.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented in AnewtDatabaseConnectionMemcache, AnewtDatabaseConnectionMySQLOld, AnewtDatabaseConnectionMySQL, AnewtDatabaseConnectionPostgreSQL, and AnewtDatabaseConnectionSQLite.

Definition at line 471 of file connection.lib.php.

AnewtDatabaseConnection::escape_table_name ( value  ) 

Escape a table name for use in SQL queries.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented in AnewtDatabaseConnectionMemcache, AnewtDatabaseConnectionMySQLOld, AnewtDatabaseConnectionMySQL, and AnewtDatabaseConnectionPostgreSQL.

Definition at line 483 of file connection.lib.php.

AnewtDatabaseConnection::escape_column_name ( value  ) 

Escape a column name for use in SQL queries.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented in AnewtDatabaseConnectionMemcache, AnewtDatabaseConnectionMySQLOld, AnewtDatabaseConnectionMySQL, and AnewtDatabaseConnectionPostgreSQL.

Definition at line 495 of file connection.lib.php.

AnewtDatabaseConnection::escape_date ( value  ) 

Escape a date for use in SQL queries.

This method merely adds quotes.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented in AnewtDatabaseConnectionMemcache.

Definition at line 509 of file connection.lib.php.

AnewtDatabaseConnection::escape_time ( value  ) 

Escape a time for use in SQL queries.

This method merely adds quotes.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented in AnewtDatabaseConnectionMemcache.

Definition at line 523 of file connection.lib.php.

AnewtDatabaseConnection::escape_datetime ( value  ) 

Escape a datetime for use in SQL queries.

This method merely adds quotes.

Parameters:
$value The value to escape
Returns:
The escaped value

Reimplemented in AnewtDatabaseConnectionMemcache.

Definition at line 537 of file connection.lib.php.


Member Data Documentation

AnewtDatabaseConnection::$n_queries_executed

The number of executed queries.

This is mostly useful for debugging.

Definition at line 51 of file connection.lib.php.

AnewtDatabaseConnection::$queries_executed

List of executed queries.

This is mostly useful for debugging. Note that only a certain number of queries are kept here before the first query is no longer kept around (ring buffer), hence the list may not be complete.

Definition at line 60 of file connection.lib.php.


The documentation for this class was generated from the following file:

Generated on Sun Aug 2 22:54:37 2009 for Anewt by  doxygen 1.5.9