Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implementing DboSource::insertMulti so it uses prepared statements, a…
…lso 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
  • Loading branch information
lorenzo committed Oct 25, 2010
1 parent 34813ab commit 1acb619
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 34 deletions.
16 changes: 0 additions & 16 deletions cake/libs/model/datasources/dbo/dbo_mysql.php
Expand Up @@ -580,22 +580,6 @@ function _alterIndexes($table, $indexes) {
return $alter;
}

/**
* 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 = implode(', ', array_map(array(&$this, 'name'), $fields));
}
$values = implode(', ', $values);
$this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}");
}

/**
* Returns an detailed array of sources (tables) in the database.
*
Expand Down
15 changes: 9 additions & 6 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -291,8 +291,7 @@ public function rawQuery($sql, $params = array()) {
* @return mixed Resource or object representing the result set, or false on failure
*/
public function execute($sql, $options = array(), $params = array()) {
$defaults = array('log' => $this->fullDebug);
$options = array_merge($defaults, $options);
$options = $options + array('log' => $this->fullDebug);
$this->error = null;

$t = microtime(true);
Expand Down Expand Up @@ -2686,13 +2685,17 @@ public function boolean($data) {
*/
public function insertMulti($table, $fields, $values) {
$table = $this->fullTableName($table);
if (is_array($fields)) {
$fields = implode(', ', array_map(array(&$this, 'name'), $fields));
}
$holder = implode(',', array_fill(0, count($fields), '?'));
$fields = implode(', ', array_map(array(&$this, 'name'), $fields));

$count = count($values);
$sql = "INSERT INTO {$table} ({$fields}) VALUES ({$holder})";
$statement = $this->_connection->prepare($sql);
for ($x = 0; $x < $count; $x++) {
$this->query("INSERT INTO {$table} ({$fields}) VALUES {$values[$x]}");
$statement->execute($values[$x]);
$statement->closeCursor();
}
return true;
}

/**
Expand Down
11 changes: 3 additions & 8 deletions cake/libs/model/model.php
Expand Up @@ -1459,15 +1459,11 @@ function __saveMulti($joined, $id, &$db) {

foreach ((array)$data as $row) {
if ((is_string($row) && (strlen($row) == 36 || strlen($row) == 16)) || is_numeric($row)) {
$values = array(
$db->value($id, $this->getColumnType($this->primaryKey)),
$db->value($row)
);
$values = array($id, $row);
if ($isUUID && $primaryAdded) {
$values[] = $db->value(String::uuid());
$values[] = String::uuid();
}
$values = implode(',', $values);
$newValues[] = "({$values})";
$newValues[] = $values;
unset($values);
} elseif (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
$newData[] = $row;
Expand Down Expand Up @@ -1506,7 +1502,6 @@ function __saveMulti($joined, $id, &$db) {
}

if (!empty($newValues)) {
$fields = implode(',', $fields);
$db->insertMulti($this->{$join}, $fields, $newValues);
}
}
Expand Down
1 change: 1 addition & 0 deletions cake/tests/cases/libs/model/behavior_collection.test.php
Expand Up @@ -546,6 +546,7 @@ function testInvalidBehaviorCausingCakeError() {
*/
function testBehaviorToggling() {
$Apple = new Apple();
$expected = $Apple->find('all');
$this->assertIdentical($Apple->Behaviors->enabled(), array());

$Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value')));
Expand Down
4 changes: 2 additions & 2 deletions cake/tests/cases/libs/model/model_read.test.php
Expand Up @@ -5076,7 +5076,7 @@ function testMultipleBelongsToWithSameClass() {
'typ' => 2
)));

$this->assertEqual($result, $expected);
$this->assertEqual($expected, $result);
}

/**
Expand Down Expand Up @@ -5646,7 +5646,7 @@ public function testFindAllRecursiveWithHabtm() {
'name' => 'computer'
))))));

$this->assertIdentical($result, $expected);
$this->assertEquals($result, $expected);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions cake/tests/lib/cake_test_fixture.php
Expand Up @@ -163,13 +163,13 @@ public function drop(&$db) {
* @return boolean on success or if there are no records to insert, or false on failure
*/
public function insert(&$db) {
$this->truncate($db);
if (!isset($this->_insert)) {
$values = array();

if (isset($this->records) && !empty($this->records)) {
foreach ($this->records as $record) {
$fields = array_keys($record);
$values[] = '(' . implode(', ', array_map(array(&$db, 'value'), array_values($record))) . ')';
$values[] = array_values($record);
}
return $db->insertMulti($this->table, $fields, $values);
}
Expand Down

0 comments on commit 1acb619

Please sign in to comment.