[limb-svn] r7096 - in 3.x/trunk/limb/dbal: src tests/cases/non-driver

svn at limb-project.com svn at limb-project.com
Mon Jul 7 15:13:09 MSD 2008


Author: pachanga
Date: 2008-07-07 15:13:09 +0400 (Mon, 07 Jul 2008)
New Revision: 7096
URL: http://fisheye.limb-project.com/changelog/limb/?cs=7096

Modified:
   3.x/trunk/limb/dbal/src/lmbSimpleDb.class.php
   3.x/trunk/limb/dbal/tests/cases/non-driver/lmbSimpleDbTest.class.php
Log:
-- adding convenience lmbSimpleDb::query(sql) method

Modified: 3.x/trunk/limb/dbal/src/lmbSimpleDb.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/lmbSimpleDb.class.php	2008-07-07 05:38:40 UTC (rev 7095)
+++ 3.x/trunk/limb/dbal/src/lmbSimpleDb.class.php	2008-07-07 11:13:09 UTC (rev 7096)
@@ -1,179 +1,184 @@
-<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- */
-lmb_require('limb/dbal/src/query/lmbInsertQuery.class.php');
-lmb_require('limb/dbal/src/query/lmbSelectQuery.class.php');
-lmb_require('limb/dbal/src/query/lmbUpdateQuery.class.php');
-lmb_require('limb/dbal/src/query/lmbDeleteQuery.class.php');
-lmb_require('limb/dbal/src/criteria/lmbSQLCriteria.class.php');
-
-/**
- * class lmbSimpleDb.
- *
- * @package dbal
- * @version $Id$
- */
-class lmbSimpleDb
-{
-  protected $conn;
-  protected $stmt;
-
-  function __construct($conn)
-  {
-    $this->conn = $conn;
-  }
-
-  function getConnection()
-  {
-    return $this->conn;
-  }
-
-  function getType()
-  {
-    return $this->conn->getType();
-  }
-
-  function execute($sql)
-  {
-    $this->conn->execute($sql);
-  }
-
-  function select($table, $criteria = null, $order = array())
-  {
-    $query = new lmbSelectQuery($table, $this->conn);
-
-    if($criteria)
-      $query->addCriteria(lmbSQLCriteria :: objectify($criteria));
-
-    $rs = $query->getRecordSet();
-
-    if(is_array($order) && sizeof($order))
-      $rs->sort($order);
-
-    return $rs;
-  }
-
-  function selectRecord($table, $criteria = null, $order = array())
-  {
-    $rs = $this->select($table, $criteria, $order)->paginate(0, 1);
-    $rs->rewind();
-    if($rs->valid())
-      return $rs->current();
-  }
-
-  /**
-   * @deprecated
-   */
-  function getFirstRecordFrom($table_name, $criteria = null, $order = array())
-  {
-    return $this->selectRecord($table_name, $criteria, $order);
-  }
-
-  function count($table_name, $criteria = null)
-  {
-    $rs = $this->select($table_name, $criteria);
-    return $rs->count();
-  }
-
-  function countAffected()
-  {
-    if($this->stmt)
-      return $this->stmt->getAffectedRowCount();
-    else
-      return 0;
-  }
-
-  function insert($table, $values, $primary_key = 'id')
-  {
-    $query = new lmbInsertQuery($table, $this->conn);
-
-    foreach($values as $key => $value)
-      $query->addField($key , $value);
-
-    $stmt = $query->getStatement($this->conn);
-
-    if($primary_key)
-    {
-      if(isset($values[$primary_key]))
-      {
-        $stmt->execute();
-        return $values[$primary_key];
-      }
-      else
-        return $stmt->insertId($primary_key);
-    }
-    else
-      $stmt->execute();
-  }
-
-  function update($table, $values, $criteria = null)
-  {
-    $query = new lmbUpdateQuery($table, $this->conn);
-
-    if($criteria)
-      $query->addCriteria(lmbSQLCriteria :: objectify($criteria));
-
-    foreach($values as $key => $value)
-      $query->addField($key, $value);
-
-    $this->stmt = $query->getStatement($this->conn);
-    $this->stmt->execute();
-    return $this;
-  }
-
-  function delete($table, $criteria = null)
-  {
-    $query = new lmbDeleteQuery($table, $this->conn);
-
-    if($criteria)
-      $query->addCriteria(lmbSQLCriteria :: objectify($criteria));
-
-    $this->stmt = $query->getStatement($this->conn);
-    $this->stmt->execute();
-    return $this;
-  }
-
-  function truncateDb()
-  {
-    $info = $this->conn->getDatabaseInfo();
-    foreach($info->getTableList() as $table)
-      $this->conn->newStatement("DELETE FROM $table")->execute();
-    return $this;
-  }
-
-  function disconnect()
-  {
-    $this->conn->disconnect();
-    return $this;
-  }
-
-  function begin()
-  {
-    $this->conn->beginTransaction();
-    return $this;
-  }
-
-  function commit()
-  {
-    $this->conn->commitTransaction();
-    return $this;
-  }
-
-  function rollback()
-  {
-    $this->conn->rollbackTransaction();
-    return $this;
-  }
-
-  function quote($id)
-  {
-    return $this->conn->quoteIdentifier($id);
-  }
-}
-
-
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+lmb_require('limb/dbal/src/query/lmbInsertQuery.class.php');
+lmb_require('limb/dbal/src/query/lmbSelectQuery.class.php');
+lmb_require('limb/dbal/src/query/lmbUpdateQuery.class.php');
+lmb_require('limb/dbal/src/query/lmbDeleteQuery.class.php');
+lmb_require('limb/dbal/src/criteria/lmbSQLCriteria.class.php');
+
+/**
+ * class lmbSimpleDb.
+ *
+ * @package dbal
+ * @version $Id$
+ */
+class lmbSimpleDb
+{
+  protected $conn;
+  protected $stmt;
+
+  function __construct($conn)
+  {
+    $this->conn = $conn;
+  }
+
+  function getConnection()
+  {
+    return $this->conn;
+  }
+
+  function getType()
+  {
+    return $this->conn->getType();
+  }
+
+  function execute($sql)
+  {
+    $this->conn->execute($sql);
+  }
+
+  function query($sql)
+  {
+    return $this->conn->newStatement($sql)->getRecordSet();
+  }
+
+  function select($table, $criteria = null, $order = array())
+  {
+    $query = new lmbSelectQuery($table, $this->conn);
+
+    if($criteria)
+      $query->addCriteria(lmbSQLCriteria :: objectify($criteria));
+
+    $rs = $query->getRecordSet();
+
+    if(is_array($order) && sizeof($order))
+      $rs->sort($order);
+
+    return $rs;
+  }
+
+  function selectRecord($table, $criteria = null, $order = array())
+  {
+    $rs = $this->select($table, $criteria, $order)->paginate(0, 1);
+    $rs->rewind();
+    if($rs->valid())
+      return $rs->current();
+  }
+
+  /**
+   * @deprecated
+   */
+  function getFirstRecordFrom($table_name, $criteria = null, $order = array())
+  {
+    return $this->selectRecord($table_name, $criteria, $order);
+  }
+
+  function count($table_name, $criteria = null)
+  {
+    $rs = $this->select($table_name, $criteria);
+    return $rs->count();
+  }
+
+  function countAffected()
+  {
+    if($this->stmt)
+      return $this->stmt->getAffectedRowCount();
+    else
+      return 0;
+  }
+
+  function insert($table, $values, $primary_key = 'id')
+  {
+    $query = new lmbInsertQuery($table, $this->conn);
+
+    foreach($values as $key => $value)
+      $query->addField($key , $value);
+
+    $stmt = $query->getStatement($this->conn);
+
+    if($primary_key)
+    {
+      if(isset($values[$primary_key]))
+      {
+        $stmt->execute();
+        return $values[$primary_key];
+      }
+      else
+        return $stmt->insertId($primary_key);
+    }
+    else
+      $stmt->execute();
+  }
+
+  function update($table, $values, $criteria = null)
+  {
+    $query = new lmbUpdateQuery($table, $this->conn);
+
+    if($criteria)
+      $query->addCriteria(lmbSQLCriteria :: objectify($criteria));
+
+    foreach($values as $key => $value)
+      $query->addField($key, $value);
+
+    $this->stmt = $query->getStatement($this->conn);
+    $this->stmt->execute();
+    return $this;
+  }
+
+  function delete($table, $criteria = null)
+  {
+    $query = new lmbDeleteQuery($table, $this->conn);
+
+    if($criteria)
+      $query->addCriteria(lmbSQLCriteria :: objectify($criteria));
+
+    $this->stmt = $query->getStatement($this->conn);
+    $this->stmt->execute();
+    return $this;
+  }
+
+  function truncateDb()
+  {
+    $info = $this->conn->getDatabaseInfo();
+    foreach($info->getTableList() as $table)
+      $this->conn->newStatement("DELETE FROM $table")->execute();
+    return $this;
+  }
+
+  function disconnect()
+  {
+    $this->conn->disconnect();
+    return $this;
+  }
+
+  function begin()
+  {
+    $this->conn->beginTransaction();
+    return $this;
+  }
+
+  function commit()
+  {
+    $this->conn->commitTransaction();
+    return $this;
+  }
+
+  function rollback()
+  {
+    $this->conn->rollbackTransaction();
+    return $this;
+  }
+
+  function quote($id)
+  {
+    return $this->conn->quoteIdentifier($id);
+  }
+}
+
+

Modified: 3.x/trunk/limb/dbal/tests/cases/non-driver/lmbSimpleDbTest.class.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/non-driver/lmbSimpleDbTest.class.php	2008-07-07 05:38:40 UTC (rev 7095)
+++ 3.x/trunk/limb/dbal/tests/cases/non-driver/lmbSimpleDbTest.class.php	2008-07-07 11:13:09 UTC (rev 7096)
@@ -185,6 +185,15 @@
     $this->assertEqual($this->db->select('test_db_table')->count(), 0);
   }
 
+  function testQuery()
+  {
+    $this->db->insert('test_db_table', array('title' =>  'wow', 'description' => 'descr'));
+    $arr = $this->db->query("SELECT * from test_db_table")->getArray();
+    $this->assertEqual(sizeof($arr), 1);
+    $this->assertEqual($arr[0]["title"], 'wow');
+    $this->assertEqual($arr[0]["description"], 'descr');
+  }
+
   function testQuote()
   {
      $this->assertEqual($this->db->quote('foo'), $this->conn->quoteIdentifier('foo'));



More information about the limb-svn mailing list