diff options
Diffstat (limited to 'modules/database/classes/db.php')
-rw-r--r-- | modules/database/classes/db.php | 163 |
1 files changed, 86 insertions, 77 deletions
diff --git a/modules/database/classes/db.php b/modules/database/classes/db.php index dd24056..42e8534 100644 --- a/modules/database/classes/db.php +++ b/modules/database/classes/db.php @@ -6,133 +6,142 @@ * generic connection class used by drivers. * @package Database */ -abstract class DB { - +abstract class DB +{ /** * An associative array of connections to databases - * @var array - * @access private - * @static + * @var array + * @access private + * @static */ - private static $_instances=array(); + private static $_instances = array(); /** * Executes a prepared statement query - * + * * @param string $query A prepared statement query * @param array $params Parameters for the query - * @return Result_Database - * @access public - * @see Result_Database + * @return Result_Database + * @access public + * @see Result_Database */ - public abstract function execute($query, $params = array()); + public abstract function execute($query, $params = array()); /** * Builds a new Query to the database - * + * * @param string $type Query type. Available types: select, update, insert, delete, count - * @return Result_Database - * @access public - * @see Query_Database + * @return Result_Database + * @access public + * @see Query_Database */ - public abstract function build_query($type); + public abstract function build_query($type); /** * Gets the id of the last inserted row. - * - * @return mixed The id of the last inserted row - * @access public + * + * @return mixed The id of the last inserted row + * @access public */ - public abstract function get_insert_id(); - - /** + public abstract function get_insert_id(); + + /** * Gets column names for the specified table - * - * @param string $table Name of the table to get columns from + * + * @param string $table Name of the table to get columns from * @return array Array of column names - * @access public + * @access public */ - public abstract function list_columns($table); + public abstract function list_columns($table); /** * Executes a named query where parameters are passed as an associative array - * Example: - * <code> - * $result=$db->namedQuery("SELECT * FROM fairies where name = :name",array('name'=>'Tinkerbell')); - * </code> - * + * Example: + * <code> + * $result=$db->namedQuery("SELECT * FROM fairies where name = :name",array('name'=>'Tinkerbell')); + * </code> + * * @param string $query A named query * @param array $params Associative array of parameters * @return Result_Database Current drivers implementation of Result_Database - * @access public + * @access public */ - public function named_query($query, $params=array()) { - $bind = array(); - preg_match_all('#:(\w+)#is', $query, $matches,PREG_SET_ORDER); - foreach($matches as $match) - if(isset($params[$match[1]])){ - $query = preg_replace("#{$match[0]}#", '?', $query, 1); - $bind[] = $params[$match[1]]; - } - return $this->execute($query,$bind); - } + public function named_query($query, $params=array()) + { + $bind = array(); + preg_match_all('#:(\w+)#is', $query, $matches,PREG_SET_ORDER); + foreach($matches as $match) + { + if (isset($params[$match[1]])) + { + $query = preg_replace("#{$match[0]}#", '?', $query, 1); + $bind[] = $params[$match[1]]; + } + } + return $this->execute($query,$bind); + } /** * Returns an Expression_Database representation of the value. - * Values wrapped inside Expression_Database are not escaped in queries - * + * Values wrapped inside Expression_Database are not escaped in queries + * * @param mixed $value Value to be wrapped * @return Expression_Database Raw value that will not be escaped during query building - * @access public - * @static + * @access public + * @static */ - public static function expr($value){ - return new Expression_Database($value); - } - + public static function expr($value) + { + return new Expression_Database($value); + } + /** * Builds a query for specified connection. - * + * * @param string $type Query type. Available types: select,update,insert,delete,count * @param string $config Configuration name of the connection. - * Defaults to 'default'. + * Defaults to 'default'. * @return Query_Database Driver implementation of the Query_Database class. - * @access public - * @static + * @access public + * @static */ - public static function query($type,$config = 'default') { - return DB::instance($config)->build_query($type); - } + public static function query($type,$config = 'default') + { + return DB::instance($config)->build_query($type); + } /** * Gets the id of the last inserted row - * + * * @param string $config Configuration name of the connection. - * Defaults to 'default'. + * Defaults to 'default'. * @return mixed Id of the last inserted row - * @access public - * @static + * @access public + * @static */ - public static function insert_id($config = 'default') { - return DB::instance($config)->get_insert_id(); - } + public static function insert_id($config = 'default') + { + return DB::instance($config)->get_insert_id(); + } /** * Gets an instance of a connection to the database - * + * * @param string $config Configuration name of the connection. - * Defaults to 'default'. + * Defaults to 'default'. * @return DB Driver implementation of the DB class. - * @access public - * @static + * @access public + * @static */ - public static function instance($config='default'){ - if (!isset(DB::$_instances[$config])) { - $driver = Config::get("database.{$config}.driver"); - $driver="DB_{$driver}_Driver"; - DB::$_instances[$config] = new $driver($config); - } - return DB::$_instances[$config]; - } + public static function instance($config='default') + { + if (!isset(DB::$_instances[$config])) + { + $driver = Config::get("database.{$config}.driver"); + $driver = "DB_{$driver}_Driver"; + DB::$_instances[$config] = new $driver($config); + } + return DB::$_instances[$config]; + } -}
\ No newline at end of file +} |