Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes

DatabaseConnection Class Reference

Inheritance diagram for DatabaseConnection:

List of all members.

Public Member Functions

 connect ($connect_string, $db_user, $db_pass)
 disconnect ()
 is_connected ()
 in_transaction ()
 table ($name)
 register_table ($name)
 set_fetch_mode ($mode)
 set_fetch_class ($class_name)
 exec ($query)
 query ($query, $args=array())
 execute_procedure ($procedure, $args=array())
 begin_transaction ()
 rollback ()
 commit ()
 get_profiles ()
 add_error ($error)
 get_errors ()
 has_errors ()
 clear_errors ()
 get_last_error ()
 get_results ($query, $args=array(), $class_name= '\Habari\QueryRecord')
 get_row ($query, $args=array(), $class_name= '\Habari\QueryRecord')
 get_column ($query, $args=array())
 get_value ($query, $args=array())
 get_keyvalue ($query, $args=array())
 insert ($table, $fieldvalues)
 exists ($table, $keyfieldvalues)
 update ($table, $fieldvalues, $keyfields)
 delete ($table, $keyfields)
 last_insert_id ()
 dbdelta ($queries, $execute=true, $silent=true)
 upgrade_pre ($old_version)
 upgrade_post ($old_version)
 query_file ($file)
 sql_t ($query)
 filter_tables ($query)
 get_driver_name ()
 get_driver_version ()
 row_count ()
 list_tables ()
 quote ($string)

Static Public Member Functions

static ConnectionFactory ($connect_string)

Protected Member Functions

 load_tables ()

Protected Attributes

 $pdo = null
 $prefix = ''

Detailed Description

Habari DatabaseConnection Class

Actual database connection.

Definition at line 14 of file databaseconnection.php.


Member Function Documentation

DatabaseConnection::add_error ( error  ) 

Adds an error to the internal collection

Parameters:
array $error An array with error components

Definition at line 418 of file databaseconnection.php.

Referenced by execute_procedure(), and query().

DatabaseConnection::begin_transaction (  ) 

Start a transaction against the RDBMS in order to wrap multiple statements in a safe ACID-compliant container

Definition at line 368 of file databaseconnection.php.

DatabaseConnection::clear_errors (  ) 

Updates the last error pointer to simulate resetting the error array

Definition at line 449 of file databaseconnection.php.

DatabaseConnection::commit (  ) 

Commit a currently running transaction

Definition at line 394 of file databaseconnection.php.

DatabaseConnection::connect ( connect_string,
db_user,
db_pass 
)

Connect to a database server

Parameters:
string $connect_string a PDO connection string
string $db_user the database user name
string $db_pass the database user password
Returns:
boolean true on success, false on error

Reimplemented in MySQLConnection, PGSQLConnection, and SQLiteConnection.

Definition at line 124 of file databaseconnection.php.

static DatabaseConnection::ConnectionFactory ( connect_string  )  [static]

Returns the appropriate type of Connection class for the connect string passed or null on failure

Parameters:
string $connect_string a PDO connection string
Returns:
mixed returns appropriate DatabaseConnection child class instance or errors out if requiring the db class fails

Definition at line 80 of file databaseconnection.php.

Referenced by DB::connect().

DatabaseConnection::dbdelta ( queries,
execute = true,
silent = true 
)

Automatic diffing function, used for determining required database upgrades. Implemented in child classes.

Definition at line 694 of file databaseconnection.php.

DatabaseConnection::delete ( table,
keyfields 
)

Deletes any record that matches the specific criteria

Parameters:
string $table Table to delete from
array $keyfields Associative array of field values to match
Returns:
boolean True on success, false if not DB::delete( 'mytable', array( 'fieldname' => 'value' ) );

Definition at line 660 of file databaseconnection.php.

DatabaseConnection::disconnect (  ) 

Disconnect from the database server.

Returns:
boolean true

Definition at line 138 of file databaseconnection.php.

DatabaseConnection::exec ( query  ) 

Execute the given query on the database. Encapsulates PDO::exec. WARNING: Make sure you don't call this with a SELECT statement. PDO will buffer the results and leave your cursor dangling.

Parameters:
string $query the query to run
Returns:
boolean true on success, false on error

Definition at line 222 of file databaseconnection.php.

Referenced by SQLiteConnection::connect(), MySQLConnection::connect(), SQLiteConnection::dbdelta(), PGSQLConnection::dbdelta(), and MySQLConnection::dbdelta().

DatabaseConnection::execute_procedure ( procedure,
args = array() 
)

Execute a stored procedure

Parameters:
string $procedure name of the stored procedure
array $args arguments for the procedure
Returns:
mixed whatever the procedure returns...
Todo:

EVERYTHING... :) Implemented in child classes. Most RDBMS use ANSI-92 syntax,

Make sure it's MultiByte safe ( CALL procname ( param1, param2, ... ), so they return the call to here. Those that don't, handle the call individually

Reimplemented in SQLiteConnection.

Definition at line 324 of file databaseconnection.php.

DatabaseConnection::exists ( table,
keyfieldvalues 
)

Checks for a record that matches the specific criteria

Parameters:
string $table Table to check
array $keyfieldvalues Associative array of field values to match
Returns:
boolean True if any matching record exists, false if not DB::exists( 'mytable', array( 'fieldname' => 'value' ) );

Definition at line 589 of file databaseconnection.php.

Referenced by update().

DatabaseConnection::filter_tables ( query  ) 

Replace braced table names with their prefixed counterparts

Parameters:
string $query The query with {braced} table names
Returns:
string The translated query

Definition at line 795 of file databaseconnection.php.

Referenced by MySQLConnection::dbdelta(), exec(), and query().

DatabaseConnection::get_column ( query,
args = array() 
)

Returns all values for a column for a query

Parameters:
string $query The query to execute
array $args Arguments to pass for prepared statements
Returns:
array An array containing the column data $ary= DB::get_column( 'SELECT col1 FROM tablename WHERE foo= ?', array( 'fieldvalue' ) );

Definition at line 513 of file databaseconnection.php.

Referenced by SQLiteConnection::dbdelta(), PGSQLConnection::dbdelta(), and MySQLConnection::dbdelta().

DatabaseConnection::get_errors (  ) 

Returns error data gathered from database connection

Returns:
array An array of error data

Definition at line 432 of file databaseconnection.php.

Referenced by SQLiteConnection::dbdelta(), PGSQLConnection::dbdelta(), and MySQLConnection::dbdelta().

DatabaseConnection::get_keyvalue ( query,
args = array() 
)

Returns an associative array using the first returned column as the array key and the second as the array value

Parameters:
string $query The query to execute
array $args Arguments to pass for prepared statements
Returns:
array An array containing the associative data $ary= $dbconnection->get_keyvalue( 'SELECT keyfield, valuefield FROM tablename');

Definition at line 549 of file databaseconnection.php.

DatabaseConnection::get_last_error (  ) 

Returns only the last error info

Returns:
array Data for the last error

Definition at line 458 of file databaseconnection.php.

DatabaseConnection::get_profiles (  ) 

Returns query profiles

Returns:
array an array of query profiles

Definition at line 408 of file databaseconnection.php.

DatabaseConnection::get_results ( query,
args = array(),
class_name = '\Habari\QueryRecord' 
)

Execute a query and return the results as an array of objects

Parameters:
string $query The query to execute
array $args An array of arguments to pass for prepared statements
string $class_name class name for row result objects
Returns:
array An array of QueryRecord or the named class each containing the row data $ary= DB::get_results( 'SELECT * FROM tablename WHERE foo= ?', array( 'fieldvalue' ), 'extendedQueryRecord' );

Definition at line 472 of file databaseconnection.php.

Referenced by SQLiteConnection::dbdelta(), PGSQLConnection::dbdelta(), and MySQLConnection::dbdelta().

DatabaseConnection::get_row ( query,
args = array(),
class_name = '\Habari\QueryRecord' 
)

Returns a single row (the first in a multi-result set) object for a query

Parameters:
string $query The query to execute
array $args Arguments to pass for prepared statements
string $class_name Optional class name for row result object
Returns:
object A QueryRecord or an instance of the named class containing the row data $obj= DB::get_row( 'SELECT * FROM tablename WHERE foo= ?', array( 'fieldvalue' ), 'extendedQueryRecord' );

Definition at line 492 of file databaseconnection.php.

Referenced by exists().

DatabaseConnection::get_value ( query,
args = array() 
)

Return a single value from the database

Parameters:
string $query the query to execute
array $args Arguments to pass for prepared statements
Returns:
mixed a single value ( int, string )

Definition at line 530 of file databaseconnection.php.

Referenced by SQLiteConnection::dbdelta().

DatabaseConnection::has_errors (  ) 

Determines if there have been errors since the last clear_errors() call

Returns:
boolean True if there were errors, false if not

Definition at line 441 of file databaseconnection.php.

DatabaseConnection::in_transaction (  ) 

Check whether there is a transaction underway.

Returns:
boolean

Definition at line 160 of file databaseconnection.php.

DatabaseConnection::insert ( table,
fieldvalues 
)

Inserts into the specified table values associated to the key fields

Parameters:
string $table The table name
array $fieldvalues An associative array of fields and values to insert
Returns:
boolean True on success, false if not DB::insert( 'mytable', array( 'fieldname' => 'value' ) );

Definition at line 567 of file databaseconnection.php.

Referenced by update().

DatabaseConnection::is_connected (  ) 

Check whether there is an existing connection to a database.

Returns:
boolean

Definition at line 150 of file databaseconnection.php.

DatabaseConnection::last_insert_id (  ) 

Helper function to return the last inserted sequence or auto_increment field. Useful when doing multiple inserts within a single transaction -- for example, adding dependent related rows.

Returns:
mixed The last sequence value ( RDBMS-dependent! )
See also:
http://us2.php.net/manual/en/function.pdo-lastinsertid.php

Definition at line 680 of file databaseconnection.php.

DatabaseConnection::list_tables (  ) 

Returns a list of tables the DB currently knows about.

Returns:
array The list of tables.

Definition at line 828 of file databaseconnection.php.

DatabaseConnection::load_tables (  )  [protected]

Populate the table mapping.

Returns:
void

Definition at line 99 of file databaseconnection.php.

Referenced by connect(), and register_table().

DatabaseConnection::query ( query,
args = array() 
)

Execute a SQL statement.

Parameters:
string $query the SQL statement
array $args values for the bound parameters
Returns:
boolean true on success, false on failure

Definition at line 243 of file databaseconnection.php.

Referenced by SQLiteConnection::dbdelta(), delete(), get_column(), get_keyvalue(), get_results(), get_row(), get_value(), insert(), query_file(), and update().

DatabaseConnection::query_file ( file  ) 

Load a file containing queries, replace the prefix, execute all queries present

Parameters:
string $file The filename containing the queries
Returns:
boolean True on successful execution of all queries

Definition at line 758 of file databaseconnection.php.

DatabaseConnection::quote ( string  ) 

Return a PDO-quoted string appropriate for the DB backend we're using.

If you're using this then there's 99+% probability you're building your queries the wrong way!

Parameters:
string $string The string to quote.
Returns:
string A DB-safe quoted string.

Definition at line 841 of file databaseconnection.php.

DatabaseConnection::register_table ( name  ) 

Adds a table to the list of tables known to Habari. Used by Theme and Plugin classes to inform the DB class about custom tables used by the plugin

Parameters:
string $name the table name

Definition at line 188 of file databaseconnection.php.

DatabaseConnection::rollback (  ) 

Rolls a currently running transaction back to the prexisting state, or, if the RDBMS supports it, whenever a savepoint was committed.

Definition at line 382 of file databaseconnection.php.

DatabaseConnection::row_count (  ) 

Returns number of rows affected by the last DELETE, INSERT, or UPDATE

Returns:
int The number of rows affected.

Definition at line 818 of file databaseconnection.php.

DatabaseConnection::set_fetch_class ( class_name  ) 

Sets the class to fetch, if fetch mode is PDO::FETCH_CLASS

Parameters:
string $class_name Name of class to create during fetch

Definition at line 209 of file databaseconnection.php.

Referenced by get_results(), and get_row().

DatabaseConnection::set_fetch_mode ( mode  ) 

Sets the fetch mode for return calls from PDOStatement

Parameters:
integer $mode One of the PDO::FETCH_MODE integers

Definition at line 199 of file databaseconnection.php.

Referenced by get_results(), and get_row().

DatabaseConnection::sql_t ( query  ) 

Translates the query for the current database engine

Parameters:
string $query The query to translate for the current database engine
Returns:
string The translated query

Reimplemented in MySQLConnection, PGSQLConnection, and SQLiteConnection.

Definition at line 784 of file databaseconnection.php.

Referenced by exec(), execute_procedure(), and query().

DatabaseConnection::table ( name  ) 

Get the full table name for the given table.

Parameters:
string $name name of the table
Returns:
string the full table name, or the original value if the table was not found

Definition at line 171 of file databaseconnection.php.

DatabaseConnection::update ( table,
fieldvalues,
keyfields 
)

function update Updates any record that matches the specific criteria A new row is inserted if no existing record matches the criteria

Parameters:
string $table Table to update
array $fieldvalues Associative array of field values to set
array $keyfields Associative array of field values to match
Returns:
boolean True on success, false if not DB::update( 'mytable', array( 'fieldname' => 'newvalue' ), array( 'fieldname' => 'value' ) );

Definition at line 612 of file databaseconnection.php.


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