From 42083e0a1bcd5da21b135acb937d3e9a047a2e7e Mon Sep 17 00:00:00 2001 From: nate Date: Mon, 21 Apr 2008 12:31:28 +0000 Subject: [PATCH] Refactoring insertMulti() for DboSource and drivers, updating Model test for non-transactional db's git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6707 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/datasources/dbo/dbo_adodb.php | 10 ---------- cake/libs/model/datasources/dbo/dbo_firebird.php | 10 ---------- cake/libs/model/datasources/dbo/dbo_mssql.php | 10 ---------- cake/libs/model/datasources/dbo/dbo_mysql.php | 15 +++++++++++++++ cake/libs/model/datasources/dbo/dbo_oracle.php | 11 ----------- cake/libs/model/datasources/dbo/dbo_sqlite.php | 10 ---------- cake/libs/model/datasources/dbo/dbo_sybase.php | 10 ---------- cake/tests/cases/libs/model/model.test.php | 16 ++++++++++++++++ 8 files changed, 31 insertions(+), 61 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_adodb.php b/cake/libs/model/datasources/dbo/dbo_adodb.php index b4e63875b..fe46cc718 100644 --- a/cake/libs/model/datasources/dbo/dbo_adodb.php +++ b/cake/libs/model/datasources/dbo/dbo_adodb.php @@ -418,15 +418,5 @@ class DboAdodb extends DboSource { return false; } } -/** - * Inserts multiple values into a join table - * - * @param string $table - * @param string $fields - * @param array $values - */ - function insertMulti($table, $fields, $values) { - parent::__insertMulti($table, $fields, $values); - } } ?> \ No newline at end of file diff --git a/cake/libs/model/datasources/dbo/dbo_firebird.php b/cake/libs/model/datasources/dbo/dbo_firebird.php index 405b943a1..b4082f30d 100644 --- a/cake/libs/model/datasources/dbo/dbo_firebird.php +++ b/cake/libs/model/datasources/dbo/dbo_firebird.php @@ -502,15 +502,5 @@ class DboFirebird extends DboSource { return false; } } -/** - * Inserts multiple values into a join table - * - * @param string $table - * @param string $fields - * @param array $values - */ - function insertMulti($table, $fields, $values) { - parent::__insertMulti($table, $fields, $values); - } } ?> \ No newline at end of file diff --git a/cake/libs/model/datasources/dbo/dbo_mssql.php b/cake/libs/model/datasources/dbo/dbo_mssql.php index 08590fb1d..81b7b77d9 100644 --- a/cake/libs/model/datasources/dbo/dbo_mssql.php +++ b/cake/libs/model/datasources/dbo/dbo_mssql.php @@ -565,16 +565,6 @@ class DboMssql extends DboSource { return false; } } -/** - * Inserts multiple values into a join table - * - * @param string $table - * @param string $fields - * @param array $values - */ - function insertMulti($table, $fields, $values) { - parent::__insertMulti($table, $fields, $values); - } /** * Generate a database-native column schema string * diff --git a/cake/libs/model/datasources/dbo/dbo_mysql.php b/cake/libs/model/datasources/dbo/dbo_mysql.php index 6d27c9d37..caca7fa5d 100644 --- a/cake/libs/model/datasources/dbo/dbo_mysql.php +++ b/cake/libs/model/datasources/dbo/dbo_mysql.php @@ -488,6 +488,21 @@ class DboMysql extends DboSource { function getEncoding() { return mysql_client_encoding($this->connection); } +/** + * Inserts multiple values into a table + * + * @param string $table + * @param string $fields + * @param array $values + */ + function insertMulti($table, $fields, $values) { + $table = $this->fullTableName($table); + if (is_array($fields)) { + $fields = join(', ', array_map(array(&$this, 'name'), $fields)); + } + $values = implode(', ', $values); + $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}"); + } /** * Returns an array of the indexes in given table name. * diff --git a/cake/libs/model/datasources/dbo/dbo_oracle.php b/cake/libs/model/datasources/dbo/dbo_oracle.php index 790cca0c1..2d6e3fee6 100644 --- a/cake/libs/model/datasources/dbo/dbo_oracle.php +++ b/cake/libs/model/datasources/dbo/dbo_oracle.php @@ -730,17 +730,6 @@ class DboOracle extends DboSource { function lastAffected() { return $this->_statementId ? ocirowcount($this->_statementId): false; } -/** - * Inserts multiple values into a join table - * - * @param string $table - * @param string $fields - * @param array $values - */ - function insertMulti($table, $fields, $values) { - parent::__insertMulti($table, $fields, $values); - } - /** * Renders a final SQL statement by putting together the component parts in the correct order * diff --git a/cake/libs/model/datasources/dbo/dbo_sqlite.php b/cake/libs/model/datasources/dbo/dbo_sqlite.php index 410f6c253..d7128f001 100644 --- a/cake/libs/model/datasources/dbo/dbo_sqlite.php +++ b/cake/libs/model/datasources/dbo/dbo_sqlite.php @@ -431,16 +431,6 @@ class DboSqlite extends DboSource { } return null; } -/** - * Inserts multiple values into a join table - * - * @param string $table - * @param string $fields - * @param array $values - */ - function insertMulti($table, $fields, $values) { - parent::__insertMulti($table, $fields, $values); - } /** * Generate a database-native column schema string * diff --git a/cake/libs/model/datasources/dbo/dbo_sybase.php b/cake/libs/model/datasources/dbo/dbo_sybase.php index 74a86d522..39b70e833 100644 --- a/cake/libs/model/datasources/dbo/dbo_sybase.php +++ b/cake/libs/model/datasources/dbo/dbo_sybase.php @@ -380,16 +380,6 @@ class DboSybase extends DboSource { return false; } } -/** - * Inserts multiple values into a join table - * - * @param string $table - * @param string $fields - * @param array $values - */ - function insertMulti($table, $fields, $values) { - parent::__insertMulti($table, $fields, $values); - } } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index a205072c3..77e20fb7e 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -2149,6 +2149,7 @@ class ModelTest extends CakeTestCase { array('author_id' => 1, 'title' => 'New Fifth Post'), array('author_id' => 1, 'title' => '') ); + $ts = date('Y-m-d H:i:s'); $this->assertFalse($this->model->saveAll($data)); $result = $this->model->find('all', array('recursive' => -1)); @@ -2157,6 +2158,15 @@ class ModelTest extends CakeTestCase { array('Post' => array('id' => '2', 'author_id' => 3, 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31')), array('Post' => array('id' => '3', 'author_id' => 1, 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')) ); + if (count($result) != 3) { + // Database doesn't support transactions + $expected[] = array('Post' => array('id' => '4', 'author_id' => 1, 'title' => 'New Fourth Post', 'body' => null, 'published' => 'N', 'created' => $ts, 'updated' => $ts)); + $expected[] = array('Post' => array('id' => '5', 'author_id' => 1, 'title' => 'New Fifth Post', 'body' => null, 'published' => 'N', 'created' => $ts, 'updated' => $ts)); + $this->assertEqual($result, $expected); + // Skip the rest of the transactional tests + return; + } + $this->assertEqual($result, $expected); $data = array( @@ -2164,6 +2174,7 @@ class ModelTest extends CakeTestCase { array('author_id' => 1, 'title' => ''), array('author_id' => 1, 'title' => 'New Sixth Post') ); + $ts = date('Y-m-d H:i:s'); $this->assertFalse($this->model->saveAll($data)); $result = $this->model->find('all', array('recursive' => -1)); @@ -2172,6 +2183,11 @@ class ModelTest extends CakeTestCase { array('Post' => array('id' => '2', 'author_id' => 3, 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31')), array('Post' => array('id' => '3', 'author_id' => 1, 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')) ); + if (count($result) != 3) { + // Database doesn't support transactions + $expected[] = array('Post' => array('id' => '4', 'author_id' => 1, 'title' => 'New Fourth Post', 'body' => 'Third Post Body', 'published' => 'N', 'created' => $ts, 'updated' => $ts)); + $expected[] = array('Post' => array('id' => '5', 'author_id' => 1, 'title' => 'Third Post', 'body' => 'Third Post Body', 'published' => 'N', 'created' => $ts, 'updated' => $ts)); + } $this->assertEqual($result, $expected); $this->model->validate = array('title' => VALID_NOT_EMPTY);