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 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 query ()
Execute a query and return the PDO statement object so you can minimize memory usage.
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 pairs ()
Fetch an associative array of two fields, the first being the keys and the second being the values.
DB is a database abstraction layer and connection manager. It provides two things:
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:
Values inserted use proper prepared statements and bound parameters to prevent SQL injection.