Permalink
Browse files

NEW - # 73: ORM

https://trac.seemlux.com/seemlux/ticket/73

Implemented _very_ rudimentary ORM
Implemented prepared statement support in DB drivers

git-svn-id: https://openpasl.googlecode.com/svn@80 38472cee-844d-11dd-8d42-05603b9c63e9
  • Loading branch information...
1 parent 62badcd commit 8003e4efe96fea50bf9dd5f1d158897f1e2c8cf5 good.midget committed Jan 24, 2009
@@ -55,7 +55,7 @@
* @param String $query
* @return mixed
*/
- abstract public function query($query);
+ abstract public function query($query, array $bind=null);
abstract public function free($result);
abstract public function fetchOne($result,$colnum);
@@ -58,7 +58,9 @@ class PASL_DB_Driver_mysql extends PASL_DB_Driver_Common
*/
private static $instance = null;
- private $Link = null;
+ public $db = null;
+
+ private $lastbind;
/**
* Connects to a MySQL database
@@ -70,9 +72,45 @@ class PASL_DB_Driver_mysql extends PASL_DB_Driver_Common
*/
public function __construct($Host, $Username, $Password, $Database)
{
-
- $this->Link = mysql_connect($Host, $Username, $Password);
- mysql_select_db($Database, $this->Link);
+ $this->db = mysqli_connect($Host, $Username, $Password, $Database);
+ }
+
+ private function statementFetchRow($statement)
+ {
+ if (!$statement->num_rows()) return null;
+
+ $statement->fetch();
+ $row = Array();
+ foreach($statement->bind as $key=>$val) $row[$key] = $val;
+ return $row;
+ }
+
+ private function getDataTypes(array $data)
+ {
+ $types = Array();
+ foreach($data as $val)
+ {
+ switch(gettype($val))
+ {
+ case 'string':
+ $type = 's';
+ break;
+ case 'integer':
+ $type = 'i';
+ break;
+ case 'double':
+ $type = 'd';
+ break;
+ case 'blob':
+ $type = 'b';
+ break;
+ default:
+ $type = 's';
+ }
+
+ $types[] = $type;
+ }
+ return join('',$types);
}
/**
@@ -81,9 +119,39 @@ public function __construct($Host, $Username, $Password, $Database)
* @param string Query
* @return MySQLResult
*/
- public function query($query)
+ public function query($query, array $bind=null)
{
- return mysql_query($query, $this->Link);
+ if ($bind) // We'll use a prepared statement
+ {
+ //TODO: Add token replacement for associative keys in query string
+ // query string should support 'select * from table where `c_key` = :c_key'
+ // should be replaced as 'select * from table where `c_key` = ?'
+ $statement = $this->db->prepare($query);
+ array_unshift($bind, $this->getDataTypes($bind));
+
+ call_user_func_array(array($statement,'bind_param'), $bind);
+
+ @$statement->execute();
+ $statement->store_result();
+
+ $bind = Array();
+
+ if ($statement->num_rows())
+ {
+ $fields = $statement->result_metadata()->fetch_fields();
+ foreach($fields as $field)
+ {
+ $bind[] = &$row[$field->name];
+ }
+
+ @$statement->bind = $row;
+ call_user_func_array(array($statement,'bind_result'),$bind);
+ }
+
+ return $statement;
+ }
+
+ return $this->db->query($query);
}
/**
@@ -93,7 +161,7 @@ public function query($query)
*/
public function free($result)
{
- mysql_free_result($result);
+ $result->close();
}
@@ -108,7 +176,7 @@ public function fetchOne($result, $column)
{
// TODO: Type checking on colnum
$fetchMode = (!is_int($column)) ? MYSQL_ASSOC : MYSQL_NUM;
- $One = mysql_fetch_array($result, $fetchMode);
+ $One = mysqli_fetch_array($result, $fetchMode);
$One = $One[$column];
return $One;
}
@@ -121,7 +189,8 @@ public function fetchOne($result, $column)
*/
public function fetchRow($result)
{
- return mysql_fetch_assoc($result);
+ if (get_class($result) == 'mysqli_stmt') return $this->statementFetchRow($result);
+ return mysqli_fetch_assoc($result);
}
/**
@@ -137,7 +206,7 @@ public function fetchCol($result, $column)
$AssocNew = Array();
$fetchMode = (!is_int($column)) ? MYSQL_ASSOC : MYSQL_NUM;
- while($AssocArray = mysql_fetch_array($result, $fetchMode))
+ while($AssocArray = mysqli_fetch_array($result, $fetchMode))
{
$AssocNew[] = $AssocArray[$column];
}
@@ -154,7 +223,7 @@ public function fetchCol($result, $column)
public function fetchAll($result)
{
$AssocNew = Array();
- while($AssocArray = mysql_fetch_array($result, MYSQL_ASSOC))
+ while($AssocArray = (get_class($result) == 'mysqli_stmt') ? $this->statementFetchRow($result) : mysqli_fetch_array($result, MYSQL_ASSOC))
{
$AssocNew[] = $AssocArray;
}
View
@@ -0,0 +1,49 @@
+<?php
+/**
+ * OpenPASL
+ *
+ * Copyright (c) 2008, Danny Graham, Scott Thundercloud
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the Danny Graham, Scott Thundercloud, nor the names of
+ * their contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @copyright Copyright (c) 2008, Danny Graham, Scott Thundercloud
+ */
+
+class PASL_ORM
+{
+ private static $dbList = Array();
+
+ public static function registerDB($name, $instance)
+ {
+ if (!array_key_exists($name, self::$dbList)) self::$dbList[$name] = $instance;
+ }
+
+ public static function getDB($name)
+ {
+ if (array_key_exists($name, self::$dbList)) return self::$dbList[$name];
+ }
+}
+?>
@@ -0,0 +1,92 @@
+<?php
+/**
+ * OpenPASL
+ *
+ * Copyright (c) 2008, Danny Graham, Scott Thundercloud
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the Danny Graham, Scott Thundercloud, nor the names of
+ * their contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @copyright Copyright (c) 2008, Danny Graham, Scott Thundercloud
+ */
+
+class PASL_ORM_SimpleObject
+{
+ protected $table;
+
+ public function __construct(PASL_ORM_SimpleTable $table)
+ {
+ $this->table = $table;
+ }
+
+ public function __set($name, $value)
+ {
+ $this->table->$name = $value;
+ }
+
+ public function __get($name)
+ {
+ return $this->table->$name;
+ }
+
+ public function __call($name, $args)
+ {
+ if (method_exists($this->table, $name)) return call_user_func_array(array($this->table,$name),$args);
+ return false;
+ }
+
+ public function save()
+ {
+ //TODO: Add support to get clustered db handle
+ $db = Slx_ORM::$db;
+
+ $where = $set = $bindwhere = $bindval = Array();
+ foreach($this->schema['pkeys'] as $key)
+ {
+ $set[] = $where[] = "`$key` = ?";
+ $bindval[$key] = (isset($this->newValues[$key])) ? $this->newValues[$key] : $this->rowValues[$key] ;
+ $bindwhere[] = $this->rowValues[$key];
+ }
+ $bind = array_merge($bindval,$bindwhere);
+
+ //TODO: Map Exception for non AutoInc
+
+ $query = "update `{$this->schema['table']}` set ". join(',',$set) ." where ". join(',',$where);
+ $db->query($query, $bind);
+ }
+
+ public function delete()
+ {
+ //TODO: Add support to get clustered db handle
+ $db = Slx_ORM::$db;
+
+ $pkey = $this->schema['pkeys'][0];
+ $query = "delete from {$this->schema['table']} where `$pkey` = '{$this->$pkey}'";
+
+ $db->query($query);
+ }
+}
+
+?>
Oops, something went wrong.

0 comments on commit 8003e4e

Please sign in to comment.