Permalink
Browse files

SQL Server PDO support prepare only to SELECT statements, others comm…

…ands need to use exec method.
  • Loading branch information...
1 parent 968fa1d commit ad67f3e1bc98f33eb0d2de8ed0f90d6d9630692c @jrbasso jrbasso committed May 22, 2011
Showing with 16 additions and 4 deletions.
  1. +16 −4 lib/Cake/Model/Datasource/Database/Mssql.php
View
20 lib/Cake/Model/Datasource/Database/Mssql.php
@@ -616,7 +616,16 @@ public function insertMulti($table, $fields, $values) {
if ($hasPrimaryKey) {
$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($table) . ' ON');
}
- parent::insertMulti($table, $fields, $values);
+
+ $table = $this->fullTableName($table);
+ $fields = implode(', ', array_map(array(&$this, 'name'), $fields));
+ $this->_connection->beginTransaction();
+ foreach ($values as $value) {
+ $holder = implode(', ', array_map(array(&$this, 'value'), $value));
+ $this->_execute("INSERT INTO {$table} ({$fields}) VALUES ({$holder})");
+ }
+ $this->_connection->commit();
+
if ($hasPrimaryKey) {
$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($table) . ' OFF');
}
@@ -694,14 +703,17 @@ protected function _getPrimaryKey($model) {
* Executes given SQL statement.
*
* @param string $sql SQL statement
- * @param array $params list of params to be bound to query
+ * @param array $params list of params to be bound to query (supported only in select)
* @param array $prepareOptions Options to be used in the prepare statement
* @return PDOStatement if query executes with no problem, true as the result of a succesfull
* query returning no rows, suchs as a CREATE statement, false otherwise
*/
protected function _execute($sql, $params = array(), $prepareOptions = array()) {
- $prepareOptions += array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL);
- return parent::_execute($sql, $params, $prepareOptions);
+ if (strncasecmp($sql, 'SELECT', 6) == 0) {
+ $prepareOptions += array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL);
+ return parent::_execute($sql, $params, $prepareOptions);
+ }
+ return $this->_connection->exec($sql);
}
}

2 comments on commit ad67f3e

@lorenzo
CakePHP member

If not calling the parent you also need to take in account errors, cursor closing, etc. Read DboSource::_execute()

@jrbasso
CakePHP member

The select is calling the parent, it is not calling to the others commands. Using exec it returns an integer and not PDOStatement, so I don't need to close the cursor (BTW, I need to update the PHPDoc in this class and in the DboSource). About the error, yes, I forget, you update the code.
Thanks!

Please sign in to comment.