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

This commit is contained in:
José Lorenzo Rodríguez 2010-10-24 19:59:54 -04:30
parent 34813ab35c
commit 1acb619e75
6 changed files with 17 additions and 34 deletions

View file

@ -580,22 +580,6 @@ class DboMysql extends DboSource {
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.
*

View file

@ -291,8 +291,7 @@ class DboSource extends DataSource {
* @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);
@ -2686,13 +2685,17 @@ class DboSource extends DataSource {
*/
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;
}
/**

View file

@ -1459,15 +1459,11 @@ class Model extends Object {
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;
@ -1506,7 +1502,6 @@ class Model extends Object {
}
if (!empty($newValues)) {
$fields = implode(',', $fields);
$db->insertMulti($this->{$join}, $fields, $newValues);
}
}

View file

@ -546,6 +546,7 @@ class BehaviorCollectionTest extends CakeTestCase {
*/
function testBehaviorToggling() {
$Apple = new Apple();
$expected = $Apple->find('all');
$this->assertIdentical($Apple->Behaviors->enabled(), array());
$Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value')));

View file

@ -5076,7 +5076,7 @@ class ModelReadTest extends BaseModelTest {
'typ' => 2
)));
$this->assertEqual($result, $expected);
$this->assertEqual($expected, $result);
}
/**
@ -5646,7 +5646,7 @@ class ModelReadTest extends BaseModelTest {
'name' => 'computer'
))))));
$this->assertIdentical($result, $expected);
$this->assertEquals($result, $expected);
}
/**

View file

@ -163,13 +163,13 @@ class CakeTestFixture {
* @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);
}