summaryrefslogtreecommitdiffstats
path: root/modules/database
diff options
context:
space:
mode:
authorDracony <draconyster@gmail.com>2013-01-10 16:25:50 +0200
committerDracony <draconyster@gmail.com>2013-01-10 16:25:50 +0200
commit4161d1d639dee2df73a30f4872e5280205e5da2d (patch)
tree0e791ba6b4a7853fb66bbf947ab49880e47c07cf /modules/database
parenta5d20f2082ac7de829996d8df23af6bbc27a91d5 (diff)
downloadPHPixie-4161d1d639dee2df73a30f4872e5280205e5da2d.zip
PHPixie-4161d1d639dee2df73a30f4872e5280205e5da2d.tar.gz
PHPixie-4161d1d639dee2df73a30f4872e5280205e5da2d.tar.bz2
New format for config files.
Writing of config files now supported
Diffstat (limited to 'modules/database')
-rw-r--r--modules/database/classes/database/expression.php61
-rw-r--r--modules/database/classes/database/query.php1
-rw-r--r--modules/database/classes/database/result.php147
-rw-r--r--modules/database/classes/db.php255
-rw-r--r--modules/database/classes/driver/mysql/db.php175
-rw-r--r--modules/database/classes/driver/mysql/query.php1
-rw-r--r--modules/database/classes/driver/mysql/result.php93
-rw-r--r--modules/database/classes/driver/pdo/db.php1
-rw-r--r--modules/database/classes/driver/pdo/query.php5
-rw-r--r--modules/database/classes/driver/pdo/result.php93
10 files changed, 421 insertions, 411 deletions
diff --git a/modules/database/classes/database/expression.php b/modules/database/classes/database/expression.php
index bc8cea5..e987d52 100644
--- a/modules/database/classes/database/expression.php
+++ b/modules/database/classes/database/expression.php
@@ -1,31 +1,32 @@
-<?php
-
-/**
- * This class allows you to wrap fields or values that you don't want to be escaped
- * inside the query
- */
-class Expression_Database{
-
- /**
- * Part of query that should not be escaped
- * @var mixed
- * @access public
- */
- public $value;
-
- /**
- * Marks a part of query as a database specific expression,
- * e.g. calls to SQL functions like MAX(), SUBSTR() etc.
- * Example
- * <code>
- * $q->fields(DB::expr('COUNT(*)'));
- * </code>
- *
- * @param mixed $value Part of query that should not be escaped
- * @return Expression_Database
- * @access public
- */
- public function __construct($value){
- $this->value=$value;
- }
+<?php
+
+/**
+ * This class allows you to wrap fields or values that you don't want to be escaped
+ * inside the query
+ * @package Database
+ */
+class Expression_Database{
+
+ /**
+ * Part of query that should not be escaped
+ * @var mixed
+ * @access public
+ */
+ public $value;
+
+ /**
+ * Marks a part of query as a database specific expression,
+ * e.g. calls to SQL functions like MAX(), SUBSTR() etc.
+ * Example
+ * <code>
+ * $q->fields(DB::expr('COUNT(*)'));
+ * </code>
+ *
+ * @param mixed $value Part of query that should not be escaped
+ * @return Expression_Database
+ * @access public
+ */
+ public function __construct($value){
+ $this->value=$value;
+ }
} \ No newline at end of file
diff --git a/modules/database/classes/database/query.php b/modules/database/classes/database/query.php
index 5728514..8558499 100644
--- a/modules/database/classes/database/query.php
+++ b/modules/database/classes/database/query.php
@@ -25,6 +25,7 @@
*
* @method mixed type(string $type = null) Set query type. Available types: select, update, insert, delete, count.
* Without arguments returns current type argument, returns self otherwise.
+ * @package Database
*/
abstract class Query_Database {
diff --git a/modules/database/classes/database/result.php b/modules/database/classes/database/result.php
index ba83f25..8b80ac2 100644
--- a/modules/database/classes/database/result.php
+++ b/modules/database/classes/database/result.php
@@ -1,74 +1,75 @@
-<?php
-
-/**
- * Allows to access database results in a unified way and
- * provides iterator support, so it can be used inside loops like 'foreach'
- */
-abstract class Result_Database implements Iterator {
-
- /**
- * Current row number
- * @var integer
- * @access protected
- */
- protected $_position = 0;
-
- /**
- * Database result object
- * @var mixed
- * @access protected
- */
- protected $_result;
-
- /**
- * Current row
- * @var object
- * @access protected
- */
- protected $_row;
-
-
- /**
- * Returns current row
- *
- * @return object Current row in result set
- * @access public
- */
- public function current() {
- return $this->_row;
- }
-
- /**
- * Gets the number of the current row
- *
- * @return integer Row number
- * @access public
- */
- public function key() {
- return $this->_position;
- }
-
- /**
- * Check if current row exists.
- *
- * @return bool True if row exists
- * @access public
- */
- public function valid() {
- return $this->_row!=null;
- }
-
- /**
- * Returns all rows as array
- *
- * @return array Array of rows
- * @access public
- */
- public function as_array() {
- $arr = array();
- foreach($this as $row)
- $arr[] = $row;
- return $arr;
- }
-
+<?php
+
+/**
+ * Allows to access database results in a unified way and
+ * provides iterator support, so it can be used inside loops like 'foreach'
+ * @package Database
+ */
+abstract class Result_Database implements Iterator {
+
+ /**
+ * Current row number
+ * @var integer
+ * @access protected
+ */
+ protected $_position = 0;
+
+ /**
+ * Database result object
+ * @var mixed
+ * @access protected
+ */
+ protected $_result;
+
+ /**
+ * Current row
+ * @var object
+ * @access protected
+ */
+ protected $_row;
+
+
+ /**
+ * Returns current row
+ *
+ * @return object Current row in result set
+ * @access public
+ */
+ public function current() {
+ return $this->_row;
+ }
+
+ /**
+ * Gets the number of the current row
+ *
+ * @return integer Row number
+ * @access public
+ */
+ public function key() {
+ return $this->_position;
+ }
+
+ /**
+ * Check if current row exists.
+ *
+ * @return bool True if row exists
+ * @access public
+ */
+ public function valid() {
+ return $this->_row!=null;
+ }
+
+ /**
+ * Returns all rows as array
+ *
+ * @return array Array of rows
+ * @access public
+ */
+ public function as_array() {
+ $arr = array();
+ foreach($this as $row)
+ $arr[] = $row;
+ return $arr;
+ }
+
} \ No newline at end of file
diff --git a/modules/database/classes/db.php b/modules/database/classes/db.php
index 6e8c145..854b06f 100644
--- a/modules/database/classes/db.php
+++ b/modules/database/classes/db.php
@@ -1,128 +1,129 @@
-<?php
-
-/**
- * Database related functions. Creates connections,
- * executes queries and returns results. It is also the
- * generic connection class used by drivers.
- */
-abstract class DB {
-
- /**
- * An associative array of connections to databases
- * @var array
- * @access private
- * @static
- */
- 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
- */
- 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
- */
- 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
- */
- public abstract function get_insert_id();
-
- /**
- * 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>
- *
- * @param string $query A named query
- * @param array $params Associative array of parameters
- * @return Result_Database Current drivers implementation of Result_Database
- * @access public
- */
- public function namedQuery($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
- *
- * @param mixed $value Value to be wrapped
- * @return Expression_Database Raw value that will not be escaped during query building
- * @access public
- * @static
- */
- 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'.
- * @return Query_Database Driver implementation of the Query_Database class.
- * @access public
- * @static
- */
- 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'.
- * @return mixed Id of the last inserted row
- * @access public
- * @static
- */
- 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'.
- * @return DB Driver implementation of the DB class.
- * @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];
- }
-
+<?php
+
+/**
+ * Database related functions. Creates connections,
+ * executes queries and returns results. It is also the
+ * generic connection class used by drivers.
+ * @package Database
+ */
+abstract class DB {
+
+ /**
+ * An associative array of connections to databases
+ * @var array
+ * @access private
+ * @static
+ */
+ 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
+ */
+ 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
+ */
+ 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
+ */
+ public abstract function get_insert_id();
+
+ /**
+ * 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>
+ *
+ * @param string $query A named query
+ * @param array $params Associative array of parameters
+ * @return Result_Database Current drivers implementation of Result_Database
+ * @access public
+ */
+ public function namedQuery($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
+ *
+ * @param mixed $value Value to be wrapped
+ * @return Expression_Database Raw value that will not be escaped during query building
+ * @access public
+ * @static
+ */
+ 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'.
+ * @return Query_Database Driver implementation of the Query_Database class.
+ * @access public
+ * @static
+ */
+ 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'.
+ * @return mixed Id of the last inserted row
+ * @access public
+ * @static
+ */
+ 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'.
+ * @return DB Driver implementation of the DB class.
+ * @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];
+ }
+
} \ No newline at end of file
diff --git a/modules/database/classes/driver/mysql/db.php b/modules/database/classes/driver/mysql/db.php
index 553cfb9..879d1b8 100644
--- a/modules/database/classes/driver/mysql/db.php
+++ b/modules/database/classes/driver/mysql/db.php
@@ -1,88 +1,89 @@
-<?php
-
-/**
- * Mysqli Database Implementation
- */
-class DB_Mysql_Driver extends DB{
-
- /**
- * Mysqli database connection object
- * @var mysqli
- * @access public
- * @link http://php.net/manual/en/class.mysqli.php
- */
- public $conn;
-
- /**
- * Initializes database connection
- *
- * @param string $config Name of the connection to initialize
- * @return void
- * @access public
- */
- public function __construct($config) {
- $this->conn = mysqli_connect(
- Config::get("database.{$config}.host",'localhost'),
- Config::get("database.{$config}.user",''),
- Config::get("database.{$config}.password",''),
- Config::get("database.{$config}.db")
- );
- }
-
- /**
- * Builds a new Query implementation
- *
- * @param string $type Query type. Available types: select,update,insert,delete,count
- * @return Query_Mysql_Driver Returns a Mysqli implementation of a Query.
- * @access public
- * @see Query_Database
- */
- public function build_query($type) {
- return new Query_Mysql_Driver($this,$type);
- }
-
- /**
- * Gets the id of the last inserted row.
- *
- * @return mixed Row id
- * @access public
- */
- public function get_insert_id() {
- return $this->conn->insert_id;
- }
-
- /**
- * Executes a prepared statement query
- *
- * @param string $query A prepared statement query
- * @param array $params Parameters for the query
- * @return Result_Mysql_Driver Mysqli implementation of a database result
- * @access public
- * @throws Exception If the query resulted in an error
- * @see Database_Result
- */
- public function execute($query, $params = array()) {
- $cursor = $this->conn->prepare($query);
- if (!$cursor)
- throw new Exception("Database error: {$this->conn->error} \n in query:\n{$query}");
- $types = '';
- $bind = array();
- $refs = array();
- if(!empty($params)){
- foreach($params as $key=>$param) {
- $refs[$key]=is_array($param)?$param[0]:$param;
- $bind[]=&$refs[$key];
- $types.=is_array($param)?$param[1]:'s';
- }
- array_unshift($bind, $types);
-
- call_user_func_array(array($cursor, 'bind_param'), $bind);
- }
- $cursor->execute();
- $res = $cursor->get_result();
- if (is_object($res)){
- $res=new Result_Mysql_Driver($res);
- }
- return $res;
- }
+<?php
+
+/**
+ * Mysqli Database Implementation
+ * @package Database
+ */
+class DB_Mysql_Driver extends DB{
+
+ /**
+ * Mysqli database connection object
+ * @var mysqli
+ * @access public
+ * @link http://php.net/manual/en/class.mysqli.php
+ */
+ public $conn;
+
+ /**
+ * Initializes database connection
+ *
+ * @param string $config Name of the connection to initialize
+ * @return void
+ * @access public
+ */
+ public function __construct($config) {
+ $this->conn = mysqli_connect(
+ Config::get("database.{$config}.host",'localhost'),
+ Config::get("database.{$config}.user",''),
+ Config::get("database.{$config}.password",''),
+ Config::get("database.{$config}.db")
+ );
+ }
+
+ /**
+ * Builds a new Query implementation
+ *
+ * @param string $type Query type. Available types: select,update,insert,delete,count
+ * @return Query_Mysql_Driver Returns a Mysqli implementation of a Query.
+ * @access public
+ * @see Query_Database
+ */
+ public function build_query($type) {
+ return new Query_Mysql_Driver($this,$type);
+ }
+
+ /**
+ * Gets the id of the last inserted row.
+ *
+ * @return mixed Row id
+ * @access public
+ */
+ public function get_insert_id() {
+ return $this->conn->insert_id;
+ }
+
+ /**
+ * Executes a prepared statement query
+ *
+ * @param string $query A prepared statement query
+ * @param array $params Parameters for the query
+ * @return Result_Mysql_Driver Mysqli implementation of a database result
+ * @access public
+ * @throws Exception If the query resulted in an error
+ * @see Database_Result
+ */
+ public function execute($query, $params = array()) {
+ $cursor = $this->conn->prepare($query);
+ if (!$cursor)
+ throw new Exception("Database error: {$this->conn->error} \n in query:\n{$query}");
+ $types = '';
+ $bind = array();
+ $refs = array();
+ if(!empty($params)){
+ foreach($params as $key=>$param) {
+ $refs[$key]=is_array($param)?$param[0]:$param;
+ $bind[]=&$refs[$key];
+ $types.=is_array($param)?$param[1]:'s';
+ }
+ array_unshift($bind, $types);
+
+ call_user_func_array(array($cursor, 'bind_param'), $bind);
+ }
+ $cursor->execute();
+ $res = $cursor->get_result();
+ if (is_object($res)){
+ $res=new Result_Mysql_Driver($res);
+ }
+ return $res;
+ }
} \ No newline at end of file
diff --git a/modules/database/classes/driver/mysql/query.php b/modules/database/classes/driver/mysql/query.php
index e0ceacd..48b140e 100644
--- a/modules/database/classes/driver/mysql/query.php
+++ b/modules/database/classes/driver/mysql/query.php
@@ -2,6 +2,7 @@
/**
* Mysqli implementation of the database Query
+ * @package Database
*/
class Query_Mysql_Driver extends Query_PDO_Driver {
diff --git a/modules/database/classes/driver/mysql/result.php b/modules/database/classes/driver/mysql/result.php
index 0fbb645..a461c81 100644
--- a/modules/database/classes/driver/mysql/result.php
+++ b/modules/database/classes/driver/mysql/result.php
@@ -1,47 +1,48 @@
-<?php
-
-/**
- * Database result implementation for Mysqli
- */
-class Result_Mysql_Driver extends Result_Database {
-
- /**
- * Initializes new result object
- *
- * @param mysqli_result $result Mysqli Result
- * @return void
- * @access public
- * @link http://php.net/manual/en/class.mysqli-result.php
- */
- public function __construct($result) {
- $this->_result = $result;
- $this->_row=$this->_result->fetch_object();
- }
-
- /**
- * Throws exception if rewind is attempted.
- *
- * @return void
- * @access public
- * @throws Exception If rewind is attempted
- */
- public function rewind() {
- if($this->_position!=0)
- throw new Exception('Mysqli result cannot be rewound for unbuffered queries.');
- }
-
- /**
- * Iterates to the next row in the result set
- *
- * @return void
- * @access public
- */
- public function next() {
-
- $this->_position++;
- $this->_row=$this->_result->fetch_object();
- if ($this->_row == null)
- $this->_result->free();
- }
-
+<?php
+
+/**
+ * Database result implementation for Mysqli
+ * @package Database
+ */
+class Result_Mysql_Driver extends Result_Database {
+
+ /**
+ * Initializes new result object
+ *
+ * @param mysqli_result $result Mysqli Result
+ * @return void
+ * @access public
+ * @link http://php.net/manual/en/class.mysqli-result.php
+ */
+ public function __construct($result) {
+ $this->_result = $result;
+ $this->_row=$this->_result->fetch_object();
+ }
+
+ /**
+ * Throws exception if rewind is attempted.
+ *
+ * @return void
+ * @access public
+ * @throws Exception If rewind is attempted
+ */
+ public function rewind() {
+ if($this->_position!=0)
+ throw new Exception('Mysqli result cannot be rewound for unbuffered queries.');
+ }
+
+ /**
+ * Iterates to the next row in the result set
+ *
+ * @return void
+ * @access public
+ */
+ public function next() {
+
+ $this->_position++;
+ $this->_row=$this->_result->fetch_object();
+ if ($this->_row == null)
+ $this->_result->free();
+ }
+
} \ No newline at end of file
diff --git a/modules/database/classes/driver/pdo/db.php b/modules/database/classes/driver/pdo/db.php
index 18f278d..59705f7 100644
--- a/modules/database/classes/driver/pdo/db.php
+++ b/modules/database/classes/driver/pdo/db.php
@@ -2,6 +2,7 @@
/**
* PDO Database implementation.
+ * @package Database
*/
class DB_PDO_Driver extends DB{
diff --git a/modules/database/classes/driver/pdo/query.php b/modules/database/classes/driver/pdo/query.php
index 1102b44..662ce71 100644
--- a/modules/database/classes/driver/pdo/query.php
+++ b/modules/database/classes/driver/pdo/query.php
@@ -2,6 +2,7 @@
/**
* PDO implementation of the database Query
+ * @package Database
*/
class Query_PDO_Driver extends Query_Database {
@@ -218,11 +219,11 @@ class Query_PDO_Driver extends Query_Database {
}else {
$param = $this->escape_value($p['value'],$params);
}
- return $this->escape_field($p['field']).' '.$p['operator'].' '.$param;
+ return $this->escape_field($p['field']).' '.$p['operator'].' '.$param.' ';
}
if (isset($p['logic'])) {
return ($skip_first_operator?'':strtoupper($p['logic'])).' '
- .$this->get_condition_query($p['conditions'],$params,false,$value_is_field);
+ .$this->get_condition_query($p['conditions'],$params,false,$value_is_field).' ';
}
$conds = '';
diff --git a/modules/database/classes/driver/pdo/result.php b/modules/database/classes/driver/pdo/result.php
index 3270231..fec0652 100644
--- a/modules/database/classes/driver/pdo/result.php
+++ b/modules/database/classes/driver/pdo/result.php
@@ -1,47 +1,48 @@
-<?php
-
-/**
- * Database result implementation for PDO
- */
-class Result_PDO_Driver extends Result_Database {
-
- /**
- * Initializes new result object
- *
- * @param PDOStatement $stmt PDO Statement
- * @return void
- * @access public
- * @link http://php.net/manual/en/class.pdostatement.php
- */
- public function __construct($stmt) {
- $this->_result = $stmt;
- $this->_row=$this->_result->fetchObject();
- }
-
- /**
- * Throws exception if rewind is attempted.
- *
- * @return void
- * @access public
- * @throws Exception If rewind is attempted
- */
- public function rewind() {
- if($this->_position!=0)
- throw new Exception('PDO statement cannot be rewound for unbuffered queries');
- }
-
- /**
- * Iterates to the next row in the result set
- *
- * @return void
- * @access public
- */
- public function next() {
-
- $this->_position++;
- $this->_row=$this->_result->fetchObject();
- if ($this->_row == false)
- $this->_result->closeCursor();
- }
-
+<?php
+
+/**
+ * Database result implementation for PDO
+ * @package Database
+ */
+class Result_PDO_Driver extends Result_Database {
+
+ /**
+ * Initializes new result object
+ *
+ * @param PDOStatement $stmt PDO Statement
+ * @return void
+ * @access public
+ * @link http://php.net/manual/en/class.pdostatement.php
+ */
+ public function __construct($stmt) {
+ $this->_result = $stmt;
+ $this->_row=$this->_result->fetchObject();
+ }
+
+ /**
+ * Throws exception if rewind is attempted.
+ *
+ * @return void
+ * @access public
+ * @throws Exception If rewind is attempted
+ */
+ public function rewind() {
+ if($this->_position!=0)
+ throw new Exception('PDO statement cannot be rewound for unbuffered queries');
+ }
+
+ /**
+ * Iterates to the next row in the result set
+ *
+ * @return void
+ * @access public
+ */
+ public function next() {
+
+ $this->_position++;
+ $this->_row=$this->_result->fetchObject();
+ if ($this->_row == false)
+ $this->_result->closeCursor();
+ }
+
} \ No newline at end of file