Class: DB

DB is a database abstraction layer and connection manager. It provides two things:

  1. A flexible connection manager with lazy loading and master/slave awareness.
  2. A set of convenience methods that operate transparently on the PDO connection(s).

The connection manager lazy loads the connections on the first call to DB::get_connection(), so requests that don't need a database connection don't suffer the extra overhead. It is also master/slave aware, with write requests going to the master and reads being directed to a random connection.

Note: This is a simple database abstraction layer. For more advanced modelling see Model which provides a more complete abstraction and a way of organizing your application logic.

Usage:

<?php

if (! DB::open (array (
    'driver' => 'sqlite', 'file' => 'conf/site.db'
))) {
    die (DB::error ());
}

DB::execute (
    'insert into sometable values (?, ?)', 'one', 'two'
);

$id = DB::last_id ();

$res = DB::fetch ('select * from sometable');
foreach ($res as $row) {
    echo $row->fieldname;
}

$row = DB::single ('select * from sometable where id = ?', $id);

$fieldname = DB::shift (
    'select fieldname from sometable where id = ?', $id
);

?>

Values inserted use proper prepared statements and bound parameters to prevent SQL injection.

Свойства

public static $connections = array ()

List of PDO connection objects.

public static $error = false

Error message or false if no error.

public static $last_conn = false

The array key of the last connection.

public static $last_sql = false

The last SQL statement.

public static $last_args = array ()

The arguments for the last SQL statement.

public static $prefix = ''

Table name prefix to replace #prefix# occurrences with.

Methods

public static open ($conf)

Open a database connection and add it to the pool. Accepts an array of connection info taken from the global conf().

public static load_connections ()

Connect to the databases. Will die if the master connect fails, or if all connections fail, but will continue as long as the master connection succeeds since that is require to issue write commands.

public static get_connection ($master = 0)

Get a database connection. If $master is 1, it will return the master connection, -1 and it will return a random connection from only the slaves, 0 and it will return a random connection which could be any of the slaves or the master.

public static disconnect ()

Discard existing connections. Note: Will auto-reconnect on the next request, so no need for an equivalent connect() method.

public static count ()

Returns a count of active database connections.

public static args ($args)

Normalizes arguments passed as an array, object, or as multiple extra parameters.

public static normalize_sql ($db, $sql)

Normalize use of escape characters (`) to the database that's currently in use.

public static prepare ($args, $master = 0)

Prepares a statement from a list of arguments, the first being the SQL query and the rest being the parameters, and a $master flag to determine which connection to use. Also replaces #query# with a database table name prefix set in the global configuration.

public static error ()

Get the last error message.

public static fetch ()

Fetch an array of all result objects.

public static execute ()

Execute a statement and return true/false.

public static query ()

Execute a query and return the PDO statement object so you can minimize memory usage.

public static single ()

Fetch a single object.

public static shift ()

Fetch the a single value from the first result returned. Useful for count() and other such calculations, and when you only need a single piece of information.

public static shift_array ()

Fetch an array of a single field.

public static pairs ()

Fetch an associative array of two fields, the first being the keys and the second being the values.

public static last_id ($name)

Get the last inserted id value.

public static last_error ()

Get the last error message directly from PDO. Useful for queries that were done with the global $db object directly.

public static last_sql ()

Fetch the last SQL statement.

public static last_args ()

Fetch the arguments for the last SQL statement.

public static beginTransaction ()

Begin a database transaction.

public static commit ()

Commit a database transaction.

public static rollback ()

Rollback a database transaction.