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

svn at limb-project.com svn at limb-project.com
Thu Jul 10 10:58:15 MSD 2008


Author: serega
Date: 2008-07-10 10:58:15 +0400 (Thu, 10 Jul 2008)
New Revision: 7108
URL: http://fisheye.limb-project.com/changelog/limb/?cs=7108

Added:
   3.x/trunk/limb/dbal/src/query/lmbBulkInsertQuery.class.php
   3.x/trunk/limb/dbal/tests/cases/non-driver/query/.setup.php
   3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbBulkInsertQueryTest.class.php
   3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbQueryBaseTestCase.class.php
Modified:
   3.x/trunk/limb/dbal/src/lmbTableGateway.class.php
   3.x/trunk/limb/dbal/tests/cases/non-driver/lmbTableGatewayTest.class.php
   3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbDeleteQueryTest.class.php
   3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbInsertQueryTest.class.php
   3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbUpdateQueryTest.class.php
Log:
-- new lmbBulkInsertQuery added. 
-- minor refactorings in tests

Modified: 3.x/trunk/limb/dbal/src/lmbTableGateway.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/lmbTableGateway.class.php	2008-07-09 20:05:52 UTC (rev 7107)
+++ 3.x/trunk/limb/dbal/src/lmbTableGateway.class.php	2008-07-10 06:58:15 UTC (rev 7108)
@@ -168,7 +168,7 @@
 
     return (int)$this->_stmt->insertId($this->_primary_key_name);
   }
-
+  
   function update($set, $criteria = null)
   {
     if(is_array($set))

Added: 3.x/trunk/limb/dbal/src/query/lmbBulkInsertQuery.class.php
===================================================================
--- 3.x/trunk/limb/dbal/src/query/lmbBulkInsertQuery.class.php	                        (rev 0)
+++ 3.x/trunk/limb/dbal/src/query/lmbBulkInsertQuery.class.php	2008-07-10 06:58:15 UTC (rev 7108)
@@ -0,0 +1,82 @@
+<?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/lmbTemplateQuery.class.php');
+
+/**
+ * class lmbInsertQuery.
+ *
+ * @package dbal
+ * @version $Id: lmbInsertQuery.class.php 6221 2007-08-07 07:24:35Z pachanga $
+ */
+class lmbBulkInsertQuery extends lmbTemplateQuery
+{
+  protected $_table;
+  protected $_fields = array();
+  protected $_sets = array();
+
+  function __construct($table, $conn)
+  {
+    $this->_table = $table;
+    parent :: __construct("INSERT INTO %table% (%fields%) VALUES %values%", $conn);
+  }
+
+  function addSet($set)
+  {
+    foreach($set as $field => $value)
+      $this->_fields[$field] = $field;
+    
+    $this->_sets[] = $set;
+  }
+
+  protected function _getTableHint()
+  {
+    return $this->_conn->quoteIdentifier($this->_table);
+  }
+
+  protected function _getFieldsHint()
+  {
+    return implode(',', array_map(array($this->_conn, 'quoteIdentifier'), array_keys($this->_fields)));
+  }
+
+  protected function _getValuesHint()
+  {
+    $set_strings = array();
+    foreach($this->_sets as $index => $set)
+    {
+      $values = array();
+      foreach($this->_fields as $field)
+      {
+        if(!isset($set[$field]))
+          throw new lmbException('Field "' . '" not found in set ' . print_r($set, true));
+        
+        $values[] = ":{$index}_{$field}:";
+      }
+      
+      $set_strings[] = '(' . implode(',', $values) . ')';
+    }
+
+    return implode(',', $set_strings);
+  }
+  
+  function getStatement()
+  {
+    $stmt = parent :: getStatement();
+    
+    foreach($this->_sets as $index => $set)
+    {
+      foreach($this->_fields as $field)
+      {
+        $stmt->set("{$index}_{$field}", $set[$field]);
+      }
+    }
+
+    return $stmt;
+  }  
+}
+

Modified: 3.x/trunk/limb/dbal/tests/cases/non-driver/lmbTableGatewayTest.class.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/non-driver/lmbTableGatewayTest.class.php	2008-07-09 20:05:52 UTC (rev 7107)
+++ 3.x/trunk/limb/dbal/tests/cases/non-driver/lmbTableGatewayTest.class.php	2008-07-10 06:58:15 UTC (rev 7108)
@@ -62,7 +62,7 @@
     $this->assertEqual($record->get('description'), 'wow!');
     $this->assertEqual($record->get('id'), $id);
   }
-
+  
 //  function testInsertUpdatesSequenceIfAutoIncrementFieldWasSet()
 //  {
 //    $id = $this->db_table_test->insert(array('id' => 4, 'title' =>  'wow', 'description' => 'wow!'));

Added: 3.x/trunk/limb/dbal/tests/cases/non-driver/query/.setup.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/non-driver/query/.setup.php	                        (rev 0)
+++ 3.x/trunk/limb/dbal/tests/cases/non-driver/query/.setup.php	2008-07-10 06:58:15 UTC (rev 7108)
@@ -0,0 +1,5 @@
+<?php
+require_once(dirname(__FILE__) . '/lmbQueryBaseTestCase.class.php');
+lmb_require('limb/dbal/src/criteria/lmbSQLFieldCriteria.class.php');
+
+

Added: 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbBulkInsertQueryTest.class.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbBulkInsertQueryTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbBulkInsertQueryTest.class.php	2008-07-10 06:58:15 UTC (rev 7108)
@@ -0,0 +1,33 @@
+<?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/lmbBulkInsertQuery.class.php');
+
+class lmbBulkInsertQueryTest extends lmbQueryBaseTestCase
+{
+  function testInsert()
+  { 
+    $query = new lmbBulkInsertQuery('test_db_table', $this->conn);
+    $query->addSet(array('id' => 2, 'title' => 'some title', 'description' => 'some description'));
+    $query->addSet(array('id' => 4, 'title' => 'some other title', 'description' => 'some other description'));
+    $stmt = $query->getStatement();
+    $stmt->execute();
+
+    $rs = $this->db->select('test_db_table')->sort(array('id' => 'ASC'));
+    $arr = $rs->getArray();
+
+    $this->assertEqual(sizeof($arr), 2);
+    $this->assertEqual($arr[0]['id'], 2);
+    $this->assertEqual($arr[0]['title'], 'some title');
+    $this->assertEqual($arr[0]['description'], 'some description');
+    $this->assertEqual($arr[1]['id'], 4);
+    $this->assertEqual($arr[1]['description'], 'some other description');
+    $this->assertEqual($arr[1]['title'], 'some other title');
+  }
+}
+

Modified: 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbDeleteQueryTest.class.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbDeleteQueryTest.class.php	2008-07-09 20:05:52 UTC (rev 7107)
+++ 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbDeleteQueryTest.class.php	2008-07-10 06:58:15 UTC (rev 7108)
@@ -6,34 +6,10 @@
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html
  */
-lmb_require('limb/dbal/src/criteria/lmbSQLFieldCriteria.class.php');
-lmb_require('limb/dbal/src/lmbSimpleDb.class.php');
 lmb_require('limb/dbal/src/query/lmbDeleteQuery.class.php');
 
-class lmbDeleteQueryTest extends UnitTestCase
+class lmbDeleteQueryTest extends lmbQueryBaseTestCase
 {
-  protected $query;
-  protected $db;
-
-  function setUp()
-  {
-    $toolkit = lmbToolkit :: instance();
-    $this->conn = $toolkit->getDefaultDbConnection();
-    $this->db = new lmbSimpleDb($this->conn);
-
-    $this->_dbCleanUp();
-  }
-
-  function tearDown()
-  {
-    $this->_dbCleanUp();
-  }
-
-  function _dbCleanUp()
-  {
-    $this->db->delete('test_db_table');
-  }
-
   function testDelete()
   {
     $startId = $this->db->insert('test_db_table', array('description' => 'text1'));

Modified: 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbInsertQueryTest.class.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbInsertQueryTest.class.php	2008-07-09 20:05:52 UTC (rev 7107)
+++ 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbInsertQueryTest.class.php	2008-07-10 06:58:15 UTC (rev 7108)
@@ -7,32 +7,9 @@
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html
  */
 lmb_require('limb/dbal/src/query/lmbInsertQuery.class.php');
-lmb_require('limb/dbal/src/lmbSimpleDb.class.php');
 
-class lmbInsertQueryTest extends UnitTestCase
+class lmbInsertQueryTest extends lmbQueryBaseTestCase
 {
-  var $query;
-  var $db;
-
-  function setUp()
-  {
-    $toolkit = lmbToolkit :: instance();
-    $this->conn = $toolkit->getDefaultDbConnection();
-    $this->db = new lmbSimpleDb($this->conn);
-
-    $this->_dbCleanUp();
-  }
-
-  function tearDown()
-  {
-    $this->_dbCleanUp();
-  }
-
-  function _dbCleanUp()
-  {
-    $this->db->delete('test_db_table');
-  }
-
   function testInsert()
   {
     $startId = $this->db->insert('test_db_table', array('description' => 'text1'));

Added: 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbQueryBaseTestCase.class.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbQueryBaseTestCase.class.php	                        (rev 0)
+++ 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbQueryBaseTestCase.class.php	2008-07-10 06:58:15 UTC (rev 7108)
@@ -0,0 +1,35 @@
+<?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/lmbSimpleDb.class.php');
+
+class lmbQueryBaseTestCase extends UnitTestCase
+{
+  protected $db;
+  protected $conn;
+
+  function setUp()
+  {
+    $toolkit = lmbToolkit :: instance();
+    $this->conn = $toolkit->getDefaultDbConnection();
+    $this->db = new lmbSimpleDb($this->conn);
+
+    $this->_dbCleanUp();
+  }
+
+  function tearDown()
+  {
+    $this->_dbCleanUp();
+  }
+
+  function _dbCleanUp()
+  {
+    $this->db->delete('test_db_table');
+  }
+}
+

Modified: 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbUpdateQueryTest.class.php
===================================================================
--- 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbUpdateQueryTest.class.php	2008-07-09 20:05:52 UTC (rev 7107)
+++ 3.x/trunk/limb/dbal/tests/cases/non-driver/query/lmbUpdateQueryTest.class.php	2008-07-10 06:58:15 UTC (rev 7108)
@@ -6,34 +6,10 @@
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html
  */
-lmb_require('limb/dbal/src/criteria/lmbSQLFieldCriteria.class.php');
-lmb_require('limb/dbal/src/lmbSimpleDb.class.php');
 lmb_require('limb/dbal/src/query/lmbUpdateQuery.class.php');
 
-class lmbUpdateQueryTest extends UnitTestCase
+class lmbUpdateQueryTest extends lmbQueryBaseTestCase
 {
-  var $query;
-  var $db;
-
-  function setUp()
-  {
-    $toolkit = lmbToolkit :: instance();
-    $this->conn = $toolkit->getDefaultDbConnection();
-    $this->db = new lmbSimpleDb($this->conn);
-
-    $this->_dbCleanUp();
-  }
-
-  function tearDown()
-  {
-    $this->_dbCleanUp();
-  }
-
-  function _dbCleanUp()
-  {
-    $this->db->delete('test_db_table');
-  }
-
   function testUpdate()
   {
     $startId = $this->db->insert('test_db_table', array('description' => 'text1'));



More information about the limb-svn mailing list