Skip to content
Permalink
Browse files

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 1acb619e757f191d7d6f870643e968af044da8c3
@@ -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.
*
@@ -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);
@@ -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;
}
/**
@@ -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;
@@ -1506,7 +1502,6 @@ function __saveMulti($joined, $id, &$db) {
}
if (!empty($newValues)) {
$fields = implode(',', $fields);
$db->insertMulti($this->{$join}, $fields, $newValues);
}
}
@@ -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')));
@@ -5076,7 +5076,7 @@ function testMultipleBelongsToWithSameClass() {
'typ' => 2
)));
$this->assertEqual($result, $expected);
$this->assertEqual($expected, $result);
}
/**
@@ -5646,7 +5646,7 @@ public function testFindAllRecursiveWithHabtm() {
'name' => 'computer'
))))));
$this->assertIdentical($result, $expected);
$this->assertEquals($result, $expected);
}
/**
@@ -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);
}

0 comments on commit 1acb619

Please sign in to comment.
You can’t perform that action at this time.