diff options
author | Volker Thiel <riker09@gmx.de> | 2013-03-07 08:43:41 +0100 |
---|---|---|
committer | Volker Thiel <riker09@gmx.de> | 2013-03-07 08:43:41 +0100 |
commit | 763a79a04e9ee5efbc24c0c335e77a86cacb02be (patch) | |
tree | 0efb570d95c55ab34e5089a2497c56bc5abd05f6 /modules/database/classes/driver/pdo/db.php | |
parent | ee3133db1bcbab537a03db6ad498e6de689dc04a (diff) | |
parent | 2e8b8467e22f984b42a25b1d90c1a3a19ea4a99d (diff) | |
download | PHPixie-763a79a04e9ee5efbc24c0c335e77a86cacb02be.zip PHPixie-763a79a04e9ee5efbc24c0c335e77a86cacb02be.tar.gz PHPixie-763a79a04e9ee5efbc24c0c335e77a86cacb02be.tar.bz2 |
Merge branch 'master' into guidelines
Diffstat (limited to 'modules/database/classes/driver/pdo/db.php')
-rw-r--r-- | modules/database/classes/driver/pdo/db.php | 137 |
1 files changed, 78 insertions, 59 deletions
diff --git a/modules/database/classes/driver/pdo/db.php b/modules/database/classes/driver/pdo/db.php index 69a7457..5b81d66 100644 --- a/modules/database/classes/driver/pdo/db.php +++ b/modules/database/classes/driver/pdo/db.php @@ -4,23 +4,24 @@ * PDO Database implementation. * @package Database */ -class DB_PDO_Driver extends DB{ +class DB_PDO_Driver extends DB +{ /** * Connection object * @var PDO * @access public - * @link http://php.net/manual/en/class.pdo.php + * @link http://php.net/manual/en/class.pdo.php */ - public $conn; - - /** + public $conn; + + /** * Type of the database, e.g. mysql, pgsql etc. * @var string * @access public */ - public $db_type; - + public $db_type; + /** * Initializes database connection * @@ -28,17 +29,18 @@ class DB_PDO_Driver extends DB{ * @return void * @access public */ - public function __construct($config) { - $this->conn = new PDO( - Config::get("database.{$config}.connection"), - Config::get("database.{$config}.user",''), - Config::get("database.{$config}.password",'') - ); - $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $this->db_type = strtolower(str_replace('PDO_', '', $this->conn->getAttribute(PDO::ATTR_DRIVER_NAME))); - if($this->db_type!='sqlite') - $this->conn->exec("SET NAMES utf8"); - } + public function __construct($config) + { + $this->conn = new PDO( + Config::get("database.{$config}.connection"), Config::get("database.{$config}.user", ''), Config::get("database.{$config}.password", '') + ); + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->db_type = strtolower(str_replace('PDO_', '', $this->conn->getAttribute(PDO::ATTR_DRIVER_NAME))); + if ($this->db_type != 'sqlite') + { + $this->conn->exec("SET NAMES utf8"); + } + } /** * Builds a new Query implementation @@ -46,11 +48,12 @@ class DB_PDO_Driver extends DB{ * @param string $type Query type. Available types: select,update,insert,delete,count * @return Query_PDO_Driver Returns a PDO implementation of a Query. * @access public - * @see Query_Database + * @see Query_Database */ - public function build_query($type) { - return new Query_PDO_Driver($this,$type); - } + public function build_query($type) + { + return new Query_PDO_Driver($this, $type); + } /** * Gets the id of the last inserted row. @@ -58,39 +61,52 @@ class DB_PDO_Driver extends DB{ * @return mixed Row id * @access public */ - public function get_insert_id() { - if ($this->db_type == 'pgsql') - return $this->execute('SELECT lastval() as id')->current()->id; - return $this->conn->lastInsertId(); - } - - /** + public function get_insert_id() + { + if ($this->db_type == 'pgsql') + { + return $this->execute('SELECT lastval() as id')->current()->id; + } + return $this->conn->lastInsertId(); + } + + /** * 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 */ - public function list_columns($table) { - $columns=array(); - if ($this->db_type == 'mysql') { - $table_desc = $this->execute("DESCRIBE `$table`"); - foreach($table_desc as $column) - $columns[]=$column->Field; - } - if ($this->db_type == 'pgsql') { - $table_desc = $this->execute("select column_name from information_schema.columns where table_name = '{$table}' and table_catalog=current_database();"); - foreach($table_desc as $column) - $columns[]=$column->column_name; - } - if ($this->db_type == 'sqlite') { - $table_desc = $this->execute("PRAGMA table_info('$table')"); - foreach($table_desc as $column) - $columns[]=$column->name; - } - return $columns; - } - + public function list_columns($table) + { + $columns = array(); + if ($this->db_type == 'mysql') + { + $table_desc = $this->execute("DESCRIBE `$table`"); + foreach ($table_desc as $column) + { + $columns[] = $column->Field; + } + } + if ($this->db_type == 'pgsql') + { + $table_desc = $this->execute("select column_name from information_schema.columns where table_name = '{$table}' and table_catalog=current_database();"); + foreach ($table_desc as $column) + { + $columns[] = $column->column_name; + } + } + if ($this->db_type == 'sqlite') + { + $table_desc = $this->execute("PRAGMA table_info('$table')"); + foreach ($table_desc as $column) + { + $columns[] = $column->name; + } + } + return $columns; + } + /** * Executes a prepared statement query * @@ -99,14 +115,17 @@ class DB_PDO_Driver extends DB{ * @return Result_PDO_Driver PDO implementation of a database result * @access public * @throws Exception If the query resulted in an error - * @see Database_Result + * @see Database_Result */ - public function execute($query, $params = array()) { - $cursor = $this->conn->prepare($query); - if (!$cursor->execute($params)) { - $error = $cursor->errorInfo(); - throw new Exception("Database error:\n".$error[2]." \n in query:\n{$query}"); - } - return new Result_PDO_Driver($cursor); - } + public function execute($query, $params = array()) + { + $cursor = $this->conn->prepare($query); + if (!$cursor->execute($params)) + { + $error = $cursor->errorInfo(); + throw new Exception("Database error:\n".$error[2]." \n in query:\n{$query}"); + } + return new Result_PDO_Driver($cursor); + } + }
\ No newline at end of file |