Skip to content

Commit 1acb619

Browse files
committed
Implementing DboSource::insertMulti so it uses prepared statements, also changing internal uses of this method to reflect the new api, this brings as consequence a better abstracttion for datasources in model and fixtures, but breaks BC
1 parent 34813ab commit 1acb619

File tree

6 files changed

+17
-34
lines changed

6 files changed

+17
-34
lines changed

cake/libs/model/datasources/dbo/dbo_mysql.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -580,22 +580,6 @@ function _alterIndexes($table, $indexes) {
580580
return $alter;
581581
}
582582

583-
/**
584-
* Inserts multiple values into a table
585-
*
586-
* @param string $table
587-
* @param string $fields
588-
* @param array $values
589-
*/
590-
function insertMulti($table, $fields, $values) {
591-
$table = $this->fullTableName($table);
592-
if (is_array($fields)) {
593-
$fields = implode(', ', array_map(array(&$this, 'name'), $fields));
594-
}
595-
$values = implode(', ', $values);
596-
$this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}");
597-
}
598-
599583
/**
600584
* Returns an detailed array of sources (tables) in the database.
601585
*

cake/libs/model/datasources/dbo_source.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,7 @@ public function rawQuery($sql, $params = array()) {
291291
* @return mixed Resource or object representing the result set, or false on failure
292292
*/
293293
public function execute($sql, $options = array(), $params = array()) {
294-
$defaults = array('log' => $this->fullDebug);
295-
$options = array_merge($defaults, $options);
294+
$options = $options + array('log' => $this->fullDebug);
296295
$this->error = null;
297296

298297
$t = microtime(true);
@@ -2686,13 +2685,17 @@ public function boolean($data) {
26862685
*/
26872686
public function insertMulti($table, $fields, $values) {
26882687
$table = $this->fullTableName($table);
2689-
if (is_array($fields)) {
2690-
$fields = implode(', ', array_map(array(&$this, 'name'), $fields));
2691-
}
2688+
$holder = implode(',', array_fill(0, count($fields), '?'));
2689+
$fields = implode(', ', array_map(array(&$this, 'name'), $fields));
2690+
26922691
$count = count($values);
2692+
$sql = "INSERT INTO {$table} ({$fields}) VALUES ({$holder})";
2693+
$statement = $this->_connection->prepare($sql);
26932694
for ($x = 0; $x < $count; $x++) {
2694-
$this->query("INSERT INTO {$table} ({$fields}) VALUES {$values[$x]}");
2695+
$statement->execute($values[$x]);
2696+
$statement->closeCursor();
26952697
}
2698+
return true;
26962699
}
26972700

26982701
/**

cake/libs/model/model.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,15 +1459,11 @@ function __saveMulti($joined, $id, &$db) {
14591459

14601460
foreach ((array)$data as $row) {
14611461
if ((is_string($row) && (strlen($row) == 36 || strlen($row) == 16)) || is_numeric($row)) {
1462-
$values = array(
1463-
$db->value($id, $this->getColumnType($this->primaryKey)),
1464-
$db->value($row)
1465-
);
1462+
$values = array($id, $row);
14661463
if ($isUUID && $primaryAdded) {
1467-
$values[] = $db->value(String::uuid());
1464+
$values[] = String::uuid();
14681465
}
1469-
$values = implode(',', $values);
1470-
$newValues[] = "({$values})";
1466+
$newValues[] = $values;
14711467
unset($values);
14721468
} elseif (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
14731469
$newData[] = $row;
@@ -1506,7 +1502,6 @@ function __saveMulti($joined, $id, &$db) {
15061502
}
15071503

15081504
if (!empty($newValues)) {
1509-
$fields = implode(',', $fields);
15101505
$db->insertMulti($this->{$join}, $fields, $newValues);
15111506
}
15121507
}

cake/tests/cases/libs/model/behavior_collection.test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ function testInvalidBehaviorCausingCakeError() {
546546
*/
547547
function testBehaviorToggling() {
548548
$Apple = new Apple();
549+
$expected = $Apple->find('all');
549550
$this->assertIdentical($Apple->Behaviors->enabled(), array());
550551

551552
$Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value')));

cake/tests/cases/libs/model/model_read.test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5076,7 +5076,7 @@ function testMultipleBelongsToWithSameClass() {
50765076
'typ' => 2
50775077
)));
50785078

5079-
$this->assertEqual($result, $expected);
5079+
$this->assertEqual($expected, $result);
50805080
}
50815081

50825082
/**
@@ -5646,7 +5646,7 @@ public function testFindAllRecursiveWithHabtm() {
56465646
'name' => 'computer'
56475647
))))));
56485648

5649-
$this->assertIdentical($result, $expected);
5649+
$this->assertEquals($result, $expected);
56505650
}
56515651

56525652
/**

cake/tests/lib/cake_test_fixture.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ public function drop(&$db) {
163163
* @return boolean on success or if there are no records to insert, or false on failure
164164
*/
165165
public function insert(&$db) {
166+
$this->truncate($db);
166167
if (!isset($this->_insert)) {
167168
$values = array();
168-
169169
if (isset($this->records) && !empty($this->records)) {
170170
foreach ($this->records as $record) {
171171
$fields = array_keys($record);
172-
$values[] = '(' . implode(', ', array_map(array(&$db, 'value'), array_values($record))) . ')';
172+
$values[] = array_values($record);
173173
}
174174
return $db->insertMulti($this->table, $fields, $values);
175175
}

0 commit comments

Comments
 (0)