|
|
@@ -1,6 +1,6 @@
|
|
|
<?php
|
|
|
/**
|
|
|
- * MS SQL layer for DBO
|
|
|
+ * MS SQL Server layer for DBO
|
|
|
*
|
|
|
* PHP 5
|
|
|
*
|
|
|
@@ -17,6 +17,8 @@
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
*/
|
|
|
|
|
|
+App::uses('DboSource', 'Model/Datasource');
|
|
|
+
|
|
|
/**
|
|
|
* MS SQL layer for DBO
|
|
|
*
|
|
|
@@ -24,14 +26,14 @@
|
|
|
*
|
|
|
* @package cake.libs.model.datasources.dbo
|
|
|
*/
|
|
|
-class DboMssql extends DboSource {
|
|
|
+class Sqlserver extends DboSource {
|
|
|
|
|
|
/**
|
|
|
* Driver description
|
|
|
*
|
|
|
* @var string
|
|
|
*/
|
|
|
- public $description = "MS SQL DBO Driver";
|
|
|
+ public $description = "SQL Server DBO Driver";
|
|
|
|
|
|
/**
|
|
|
* Starting quote character for quoted identifiers
|
|
|
@@ -53,7 +55,14 @@ class DboMssql extends DboSource {
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
- private $__fieldMappings = array();
|
|
|
+ protected $_fieldMappings = array();
|
|
|
+
|
|
|
+/**
|
|
|
+ * Storing the last affected value
|
|
|
+ *
|
|
|
+ * @var mixed
|
|
|
+ */
|
|
|
+ protected $_lastAffected = false;
|
|
|
|
|
|
/**
|
|
|
* Base configuration settings for MS SQL driver
|
|
|
@@ -62,11 +71,10 @@ class DboMssql extends DboSource {
|
|
|
*/
|
|
|
protected $_baseConfig = array(
|
|
|
'persistent' => true,
|
|
|
- 'host' => 'localhost',
|
|
|
- 'login' => 'root',
|
|
|
+ 'host' => '(local)\sqlexpress',
|
|
|
+ 'login' => '',
|
|
|
'password' => '',
|
|
|
- 'database' => 'cake',
|
|
|
- 'port' => '1433',
|
|
|
+ 'database' => 'cake'
|
|
|
);
|
|
|
|
|
|
/**
|
|
|
@@ -92,7 +100,6 @@ class DboMssql extends DboSource {
|
|
|
* Index of basic SQL commands
|
|
|
*
|
|
|
* @var array
|
|
|
- * @access protected
|
|
|
*/
|
|
|
protected $_commands = array(
|
|
|
'begin' => 'BEGIN TRANSACTION',
|
|
|
@@ -104,92 +111,44 @@ class DboMssql extends DboSource {
|
|
|
* Define if the last query had error
|
|
|
*
|
|
|
* @var string
|
|
|
- * @access private
|
|
|
*/
|
|
|
private $__lastQueryHadError = false;
|
|
|
-/**
|
|
|
- * MS SQL DBO driver constructor; sets SQL Server error reporting defaults
|
|
|
- *
|
|
|
- * @param array $config Configuration data from app/config/databases.php
|
|
|
- * @return boolean True if connected successfully, false on error
|
|
|
- */
|
|
|
- function __construct($config, $autoConnect = true) {
|
|
|
- if ($autoConnect) {
|
|
|
- if (!function_exists('mssql_min_message_severity')) {
|
|
|
- trigger_error(__d('cake_dev', "PHP SQL Server interface is not installed, cannot continue. For troubleshooting information, see http://php.net/mssql/"), E_USER_WARNING);
|
|
|
- }
|
|
|
- mssql_min_message_severity(15);
|
|
|
- mssql_min_error_severity(2);
|
|
|
- }
|
|
|
- return parent::__construct($config, $autoConnect);
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* Connects to the database using options in the given configuration array.
|
|
|
*
|
|
|
* @return boolean True if the database could be connected, else false
|
|
|
*/
|
|
|
- function connect() {
|
|
|
+ public function connect() {
|
|
|
$config = $this->config;
|
|
|
-
|
|
|
- $os = env('OS');
|
|
|
- if (!empty($os) && strpos($os, 'Windows') !== false) {
|
|
|
- $sep = ',';
|
|
|
- } else {
|
|
|
- $sep = ':';
|
|
|
- }
|
|
|
$this->connected = false;
|
|
|
-
|
|
|
- if (is_numeric($config['port'])) {
|
|
|
- $port = $sep . $config['port']; // Port number
|
|
|
- } elseif ($config['port'] === null) {
|
|
|
- $port = ''; // No port - SQL Server 2005
|
|
|
- } else {
|
|
|
- $port = '\\' . $config['port']; // Named pipe
|
|
|
- }
|
|
|
-
|
|
|
- if (!$config['persistent']) {
|
|
|
- $this->connection = mssql_connect($config['host'] . $port, $config['login'], $config['password'], true);
|
|
|
- } else {
|
|
|
- $this->connection = mssql_pconnect($config['host'] . $port, $config['login'], $config['password']);
|
|
|
- }
|
|
|
-
|
|
|
- if (mssql_select_db($config['database'], $this->connection)) {
|
|
|
- $this->_execute("SET DATEFORMAT ymd");
|
|
|
+ try {
|
|
|
+ $flags = array(PDO::ATTR_PERSISTENT => $config['persistent']);
|
|
|
+ if (!empty($config['encoding'])) {
|
|
|
+ $flags[PDO::SQLSRV_ATTR_ENCODING] = $config['encoding'];
|
|
|
+ }
|
|
|
+ $this->_connection = new PDO(
|
|
|
+ "sqlsrv:server={$config['host']};Database={$config['database']}",
|
|
|
+ $config['login'],
|
|
|
+ $config['password'],
|
|
|
+ $flags
|
|
|
+ );
|
|
|
$this->connected = true;
|
|
|
+ } catch (PDOException $e) {
|
|
|
+ throw new MissingConnectionException(array('class' => $e->getMessage()));
|
|
|
}
|
|
|
+
|
|
|
+// $this->_execute("SET DATEFORMAT ymd");
|
|
|
return $this->connected;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Check that MsSQL is installed/loaded
|
|
|
+ * Check that PDO SQL Server is installed/loaded
|
|
|
*
|
|
|
* @return boolean
|
|
|
*/
|
|
|
- function enabled() {
|
|
|
- return extension_loaded('mssql');
|
|
|
- }
|
|
|
-/**
|
|
|
- * Disconnects from database.
|
|
|
- *
|
|
|
- * @return boolean True if the database could be disconnected, else false
|
|
|
- */
|
|
|
- function disconnect() {
|
|
|
- @mssql_free_result($this->results);
|
|
|
- $this->connected = !@mssql_close($this->connection);
|
|
|
- return !$this->connected;
|
|
|
- }
|
|
|
-
|
|
|
-/**
|
|
|
- * Executes given SQL statement.
|
|
|
- *
|
|
|
- * @param string $sql SQL statement
|
|
|
- * @return resource Result resource identifier
|
|
|
- */
|
|
|
- protected function _execute($sql) {
|
|
|
- $result = @mssql_query($sql, $this->connection);
|
|
|
- $this->__lastQueryHadError = ($result === false);
|
|
|
- return $result;
|
|
|
+ public function enabled() {
|
|
|
+ return in_array('sqlsrv', PDO::getAvailableDrivers());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -197,23 +156,24 @@ class DboMssql extends DboSource {
|
|
|
*
|
|
|
* @return array Array of tablenames in the database
|
|
|
*/
|
|
|
- function listSources() {
|
|
|
+ public function listSources() {
|
|
|
$cache = parent::listSources();
|
|
|
-
|
|
|
- if ($cache != null) {
|
|
|
+ if ($cache !== null) {
|
|
|
return $cache;
|
|
|
}
|
|
|
- $result = $this->fetchAll('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES', false);
|
|
|
+ $result = $this->_execute("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'");
|
|
|
|
|
|
- if (!$result || empty($result)) {
|
|
|
+ if (!$result) {
|
|
|
+ $result->closeCursor();
|
|
|
return array();
|
|
|
} else {
|
|
|
$tables = array();
|
|
|
|
|
|
- foreach ($result as $table) {
|
|
|
- $tables[] = $table[0]['TABLE_NAME'];
|
|
|
+ while ($line = $result->fetch()) {
|
|
|
+ $tables[] = $line[0];
|
|
|
}
|
|
|
|
|
|
+ $result->closeCursor();
|
|
|
parent::listSources($tables);
|
|
|
return $tables;
|
|
|
}
|
|
|
@@ -225,42 +185,45 @@ class DboMssql extends DboSource {
|
|
|
* @param Model $model Model object to describe
|
|
|
* @return array Fields in table. Keys are name and type
|
|
|
*/
|
|
|
- function describe($model) {
|
|
|
+ public function describe($model) {
|
|
|
$cache = parent::describe($model);
|
|
|
-
|
|
|
if ($cache != null) {
|
|
|
return $cache;
|
|
|
}
|
|
|
-
|
|
|
+ $fields = false;
|
|
|
$table = $this->fullTableName($model, false);
|
|
|
- $cols = $this->fetchAll("SELECT COLUMN_NAME as Field, DATA_TYPE as Type, COL_LENGTH('" . $table . "', COLUMN_NAME) as Length, IS_NULLABLE As [Null], COLUMN_DEFAULT as [Default], COLUMNPROPERTY(OBJECT_ID('" . $table . "'), COLUMN_NAME, 'IsIdentity') as [Key], NUMERIC_SCALE as Size FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $table . "'", false);
|
|
|
+ $cols = $this->_execute("SELECT COLUMN_NAME as Field, DATA_TYPE as Type, COL_LENGTH('" . $table . "', COLUMN_NAME) as Length, IS_NULLABLE As [Null], COLUMN_DEFAULT as [Default], COLUMNPROPERTY(OBJECT_ID('" . $table . "'), COLUMN_NAME, 'IsIdentity') as [Key], NUMERIC_SCALE as Size FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $table . "'");
|
|
|
+ if (!$cols) {
|
|
|
+ throw new CakeException(__d('cake_dev', 'Could not describe table for %s', $model->name));
|
|
|
+ }
|
|
|
|
|
|
- $fields = false;
|
|
|
foreach ($cols as $column) {
|
|
|
- $field = $column[0]['Field'];
|
|
|
+ $field = $column->Field;
|
|
|
$fields[$field] = array(
|
|
|
- 'type' => $this->column($column[0]['Type']),
|
|
|
- 'null' => (strtoupper($column[0]['Null']) == 'YES'),
|
|
|
- 'default' => preg_replace("/^[(]{1,2}'?([^')]*)?'?[)]{1,2}$/", "$1", $column[0]['Default']),
|
|
|
- 'length' => intval($column[0]['Length']),
|
|
|
- 'key' => ($column[0]['Key'] == '1') ? 'primary' : false
|
|
|
+ 'type' => $this->column($column->Type),
|
|
|
+ 'null' => ($column->Null === 'YES' ? true : false),
|
|
|
+ 'default' => preg_replace("/^[(]{1,2}'?([^')]*)?'?[)]{1,2}$/", "$1", $column->Default),
|
|
|
+ 'length' => intval($column->Length),
|
|
|
+ 'key' => ($column->Key == '1') ? 'primary' : false
|
|
|
);
|
|
|
+
|
|
|
if ($fields[$field]['default'] === 'null') {
|
|
|
$fields[$field]['default'] = null;
|
|
|
} else {
|
|
|
$this->value($fields[$field]['default'], $fields[$field]['type']);
|
|
|
}
|
|
|
|
|
|
- if ($fields[$field]['key'] && $fields[$field]['type'] == 'integer') {
|
|
|
+ if ($fields[$field]['key'] !== false && $fields[$field]['type'] == 'integer') {
|
|
|
$fields[$field]['length'] = 11;
|
|
|
- } elseif (!$fields[$field]['key']) {
|
|
|
+ } elseif ($fields[$field]['key'] === false) {
|
|
|
unset($fields[$field]['key']);
|
|
|
}
|
|
|
if (in_array($fields[$field]['type'], array('date', 'time', 'datetime', 'timestamp'))) {
|
|
|
$fields[$field]['length'] = null;
|
|
|
}
|
|
|
}
|
|
|
- $this->__cacheDescription($this->fullTableName($model, false), $fields);
|
|
|
+ $this->__cacheDescription($table, $fields);
|
|
|
+ $cols->closeCursor();
|
|
|
return $fields;
|
|
|
}
|
|
|
|
|
|
@@ -272,9 +235,15 @@ class DboMssql extends DboSource {
|
|
|
* @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
|
|
|
* @return string Quoted and escaped data
|
|
|
*/
|
|
|
- function value($data, $column = null, $safe = false) {
|
|
|
+ public function value($data, $column = null, $safe = false) {
|
|
|
$parent = parent::value($data, $column, $safe);
|
|
|
|
|
|
+ if ($column === 'float' && strpos($data, '.') !== false) {
|
|
|
+ return rtrim($data, '0');
|
|
|
+ }
|
|
|
+ if ($parent === "''" && ($column === null || $column !== 'string')) {
|
|
|
+ return 'NULL';
|
|
|
+ }
|
|
|
if ($parent != null) {
|
|
|
return $parent;
|
|
|
}
|
|
|
@@ -315,7 +284,7 @@ class DboMssql extends DboSource {
|
|
|
* @param mixed $fields
|
|
|
* @return array
|
|
|
*/
|
|
|
- function fields($model, $alias = null, $fields = array(), $quote = true) {
|
|
|
+ public function fields($model, $alias = null, $fields = array(), $quote = true) {
|
|
|
if (empty($alias)) {
|
|
|
$alias = $model->alias;
|
|
|
}
|
|
|
@@ -331,7 +300,7 @@ class DboMssql extends DboSource {
|
|
|
$prepend = 'DISTINCT ';
|
|
|
$fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
|
|
|
}
|
|
|
- $fieldAlias = count($this->__fieldMappings);
|
|
|
+ $fieldAlias = count($this->_fieldMappings);
|
|
|
|
|
|
if (!preg_match('/\s+AS\s+/i', $fields[$i])) {
|
|
|
if (substr($fields[$i], -1) == '*') {
|
|
|
@@ -348,12 +317,12 @@ class DboMssql extends DboSource {
|
|
|
}
|
|
|
|
|
|
if (strpos($fields[$i], '.') === false) {
|
|
|
- $this->__fieldMappings[$alias . '__' . $fieldAlias] = $alias . '.' . $fields[$i];
|
|
|
+ $this->_fieldMappings[$alias . '__' . $fieldAlias] = $alias . '.' . $fields[$i];
|
|
|
$fieldName = $this->name($alias . '.' . $fields[$i]);
|
|
|
$fieldAlias = $this->name($alias . '__' . $fieldAlias);
|
|
|
} else {
|
|
|
$build = explode('.', $fields[$i]);
|
|
|
- $this->__fieldMappings[$build[0] . '__' . $fieldAlias] = $fields[$i];
|
|
|
+ $this->_fieldMappings[$build[0] . '__' . $fieldAlias] = $fields[$i];
|
|
|
$fieldName = $this->name($build[0] . '.' . $build[1]);
|
|
|
$fieldAlias = $this->name(preg_replace("/^\[(.+)\]$/", "$1", $build[0]) . '__' . $fieldAlias);
|
|
|
}
|
|
|
@@ -381,7 +350,7 @@ class DboMssql extends DboSource {
|
|
|
* @param mixed $conditions
|
|
|
* @return array
|
|
|
*/
|
|
|
- function create($model, $fields = null, $values = null) {
|
|
|
+ public function create($model, $fields = null, $values = null) {
|
|
|
if (!empty($values)) {
|
|
|
$fields = array_combine($fields, $values);
|
|
|
}
|
|
|
@@ -411,7 +380,7 @@ class DboMssql extends DboSource {
|
|
|
* @param mixed $conditions
|
|
|
* @return array
|
|
|
*/
|
|
|
- function update($model, $fields = array(), $values = null, $conditions = null) {
|
|
|
+ public function update($model, $fields = array(), $values = null, $conditions = null) {
|
|
|
if (!empty($values)) {
|
|
|
$fields = array_combine($fields, $values);
|
|
|
}
|
|
|
@@ -425,65 +394,13 @@ class DboMssql extends DboSource {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns a formatted error message from previous database operation.
|
|
|
- *
|
|
|
- * @return string Error message with error number
|
|
|
- */
|
|
|
- function lastError() {
|
|
|
- if ($this->__lastQueryHadError) {
|
|
|
- $error = mssql_get_last_message();
|
|
|
- if ($error && !preg_match('/contexto de la base de datos a|contesto di database|changed database|contexte de la base de don|datenbankkontext/i', $error)) {
|
|
|
- return $error;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-/**
|
|
|
- * Returns number of affected rows in previous database operation. If no previous operation exists,
|
|
|
- * this returns false.
|
|
|
- *
|
|
|
- * @return integer Number of affected rows
|
|
|
- */
|
|
|
- function lastAffected() {
|
|
|
- if ($this->_result) {
|
|
|
- return mssql_rows_affected($this->connection);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-/**
|
|
|
- * Returns number of rows in previous resultset. If no previous resultset exists,
|
|
|
- * this returns false.
|
|
|
- *
|
|
|
- * @return integer Number of rows in resultset
|
|
|
- */
|
|
|
- function lastNumRows() {
|
|
|
- if ($this->_result) {
|
|
|
- return @mssql_num_rows($this->_result);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-/**
|
|
|
- * Returns the ID generated from the previous INSERT operation.
|
|
|
- *
|
|
|
- * @param unknown_type $source
|
|
|
- * @return in
|
|
|
- */
|
|
|
- function lastInsertId($source = null) {
|
|
|
- $id = $this->fetchRow('SELECT SCOPE_IDENTITY() AS insertID', false);
|
|
|
- return $id[0]['insertID'];
|
|
|
- }
|
|
|
-
|
|
|
-/**
|
|
|
* Returns a limit statement in the correct format for the particular database.
|
|
|
*
|
|
|
* @param integer $limit Limit of results returned
|
|
|
* @param integer $offset Offset from which to start results
|
|
|
* @return string SQL limit/offset statement
|
|
|
*/
|
|
|
- function limit($limit, $offset = null) {
|
|
|
+ public function limit($limit, $offset = null) {
|
|
|
if ($limit) {
|
|
|
$rt = '';
|
|
|
if (!strpos(strtolower($limit), 'top') || strpos(strtolower($limit), 'top') === 0) {
|
|
|
@@ -504,7 +421,7 @@ class DboMssql extends DboSource {
|
|
|
* @param string $real Real database-layer column type (i.e. "varchar(255)")
|
|
|
* @return string Abstract column type (i.e. "string")
|
|
|
*/
|
|
|
- function column($real) {
|
|
|
+ public function column($real) {
|
|
|
if (is_array($real)) {
|
|
|
$col = $real['name'];
|
|
|
|
|
|
@@ -544,33 +461,32 @@ class DboMssql extends DboSource {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Enter description here...
|
|
|
+ * Builds a map of the columns contained in a result
|
|
|
*
|
|
|
- * @param unknown_type $results
|
|
|
+ * @param PDOStatement $results
|
|
|
*/
|
|
|
- function resultSet(&$results) {
|
|
|
- $this->results =& $results;
|
|
|
+ public function resultSet($results) {
|
|
|
$this->map = array();
|
|
|
- $numFields = mssql_num_fields($results);
|
|
|
+ $numFields = $results->columnCount();
|
|
|
$index = 0;
|
|
|
- $j = 0;
|
|
|
|
|
|
- while ($j < $numFields) {
|
|
|
- $column = mssql_field_name($results, $j);
|
|
|
+ while ($numFields-- > 0) {
|
|
|
+ $column = $results->getColumnMeta($index);
|
|
|
+ $name = $column['name'];
|
|
|
|
|
|
- if (strpos($column, '__')) {
|
|
|
- if (isset($this->__fieldMappings[$column]) && strpos($this->__fieldMappings[$column], '.')) {
|
|
|
- $map = explode('.', $this->__fieldMappings[$column]);
|
|
|
- } elseif (isset($this->__fieldMappings[$column])) {
|
|
|
- $map = array(0, $this->__fieldMappings[$column]);
|
|
|
+ if (strpos($name, '__')) {
|
|
|
+ if (isset($this->_fieldMappings[$name]) && strpos($this->_fieldMappings[$name], '.')) {
|
|
|
+ $map = explode('.', $this->_fieldMappings[$name]);
|
|
|
+ } elseif (isset($this->_fieldMappings[$name])) {
|
|
|
+ $map = array(0, $this->_fieldMappings[$name]);
|
|
|
} else {
|
|
|
- $map = array(0, $column);
|
|
|
+ $map = array(0, $name);
|
|
|
}
|
|
|
- $this->map[$index++] = $map;
|
|
|
} else {
|
|
|
- $this->map[$index++] = array(0, $column);
|
|
|
+ $map = array(0, $name);
|
|
|
}
|
|
|
- $j++;
|
|
|
+ $map[] = ($column['sqlsrv:decl_type'] == 'bit') ? 'boolean' : $column['native_type'];
|
|
|
+ $this->map[$index++] = $map;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -581,7 +497,7 @@ class DboMssql extends DboSource {
|
|
|
* @param array $data Query data
|
|
|
* @return string
|
|
|
*/
|
|
|
- function renderStatement($type, $data) {
|
|
|
+ public function renderStatement($type, $data) {
|
|
|
switch (strtolower($type)) {
|
|
|
case 'select':
|
|
|
extract($data);
|
|
|
@@ -631,9 +547,8 @@ class DboMssql extends DboSource {
|
|
|
*
|
|
|
* @param string $order
|
|
|
* @return string
|
|
|
- * @access private
|
|
|
*/
|
|
|
- function __switchSort($order) {
|
|
|
+ private function __switchSort($order) {
|
|
|
$order = preg_replace('/\s+ASC/i', '__tmp_asc__', $order);
|
|
|
$order = preg_replace('/\s+DESC/i', ' ASC', $order);
|
|
|
return preg_replace('/__tmp_asc__/', ' DESC', $order);
|
|
|
@@ -644,13 +559,12 @@ class DboMssql extends DboSource {
|
|
|
*
|
|
|
* @param string $sql A snippet of SQL representing an ORDER or WHERE statement
|
|
|
* @return string The value of $sql with field names replaced
|
|
|
- * @access private
|
|
|
*/
|
|
|
- function __mapFields($sql) {
|
|
|
- if (empty($sql) || empty($this->__fieldMappings)) {
|
|
|
+ private function __mapFields($sql) {
|
|
|
+ if (empty($sql) || empty($this->_fieldMappings)) {
|
|
|
return $sql;
|
|
|
}
|
|
|
- foreach ($this->__fieldMappings as $key => $val) {
|
|
|
+ foreach ($this->_fieldMappings as $key => $val) {
|
|
|
$sql = preg_replace('/' . preg_quote($val) . '/', $this->name($key), $sql);
|
|
|
$sql = preg_replace('/' . preg_quote($this->name($val)) . '/', $this->name($key), $sql);
|
|
|
}
|
|
|
@@ -665,31 +579,31 @@ class DboMssql extends DboSource {
|
|
|
* @param boolean $cache Enables returning/storing cached query results
|
|
|
* @return array Array of resultset rows, or false if no rows matched
|
|
|
*/
|
|
|
- function read($model, $queryData = array(), $recursive = null) {
|
|
|
+ public function read($model, $queryData = array(), $recursive = null) {
|
|
|
$results = parent::read($model, $queryData, $recursive);
|
|
|
- $this->__fieldMappings = array();
|
|
|
+ $this->_fieldMappings = array();
|
|
|
return $results;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Fetches the next row from the current result set
|
|
|
*
|
|
|
- * @return unknown
|
|
|
+ * @return mixed
|
|
|
*/
|
|
|
- function fetchResult() {
|
|
|
- if ($row = mssql_fetch_row($this->results)) {
|
|
|
+ public function fetchResult() {
|
|
|
+ if ($row = $this->_result->fetch()) {
|
|
|
$resultRow = array();
|
|
|
- $i = 0;
|
|
|
-
|
|
|
- foreach ($row as $index => $field) {
|
|
|
- list($table, $column) = $this->map[$index];
|
|
|
- $resultRow[$table][$column] = $row[$index];
|
|
|
- $i++;
|
|
|
+ foreach ($this->map as $col => $meta) {
|
|
|
+ list($table, $column, $type) = $meta;
|
|
|
+ $resultRow[$table][$column] = $row[$col];
|
|
|
+ if ($type === 'boolean' && !is_null($row[$col])) {
|
|
|
+ $resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]);
|
|
|
+ }
|
|
|
}
|
|
|
return $resultRow;
|
|
|
- } else {
|
|
|
- return false;
|
|
|
}
|
|
|
+ $this->_result->closeCursor();
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -709,7 +623,16 @@ class DboMssql extends DboSource {
|
|
|
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->begin();
|
|
|
+ foreach ($values as $value) {
|
|
|
+ $holder = implode(', ', array_map(array(&$this, 'value'), $value));
|
|
|
+ $this->_execute("INSERT INTO {$table} ({$fields}) VALUES ({$holder})");
|
|
|
+ }
|
|
|
+ $this->commit();
|
|
|
+
|
|
|
if ($hasPrimaryKey) {
|
|
|
$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($table) . ' OFF');
|
|
|
}
|
|
|
@@ -722,10 +645,14 @@ class DboMssql extends DboSource {
|
|
|
* where options can be 'default', 'length', or 'key'.
|
|
|
* @return string
|
|
|
*/
|
|
|
- function buildColumn($column) {
|
|
|
+ public function buildColumn($column) {
|
|
|
$result = preg_replace('/(int|integer)\([0-9]+\)/i', '$1', parent::buildColumn($column));
|
|
|
if (strpos($result, 'DEFAULT NULL') !== false) {
|
|
|
- $result = str_replace('DEFAULT NULL', 'NULL', $result);
|
|
|
+ if (isset($column['default']) && $column['default'] === '') {
|
|
|
+ $result = str_replace('DEFAULT NULL', "DEFAULT ''", $result);
|
|
|
+ } else {
|
|
|
+ $result = str_replace('DEFAULT NULL', 'NULL', $result);
|
|
|
+ }
|
|
|
} else if (array_keys($column) == array('type', 'name')) {
|
|
|
$result .= ' NULL';
|
|
|
}
|
|
|
@@ -739,7 +666,7 @@ class DboMssql extends DboSource {
|
|
|
* @param string $table
|
|
|
* @return string
|
|
|
*/
|
|
|
- function buildIndex($indexes, $table = null) {
|
|
|
+ public function buildIndex($indexes, $table = null) {
|
|
|
$join = array();
|
|
|
|
|
|
foreach ($indexes as $name => $value) {
|
|
|
@@ -763,17 +690,14 @@ class DboMssql extends DboSource {
|
|
|
/**
|
|
|
* Makes sure it will return the primary key
|
|
|
*
|
|
|
- * @param mixed $model
|
|
|
- * @access protected
|
|
|
+ * @param mixed $model Model instance of table name
|
|
|
* @return string
|
|
|
*/
|
|
|
- function _getPrimaryKey($model) {
|
|
|
- if (is_object($model)) {
|
|
|
- $schema = $model->schema();
|
|
|
- } else {
|
|
|
- $schema = $this->describe($model);
|
|
|
+ protected function _getPrimaryKey($model) {
|
|
|
+ if (!is_object($model)) {
|
|
|
+ $model = new Model(false, $model);
|
|
|
}
|
|
|
-
|
|
|
+ $schema = $this->describe($model);
|
|
|
foreach ($schema as $field => $props) {
|
|
|
if (isset($props['key']) && $props['key'] == 'primary') {
|
|
|
return $field;
|
|
|
@@ -781,4 +705,49 @@ class DboMssql extends DboSource {
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Returns number of affected rows in previous database operation. If no previous operation exists,
|
|
|
+ * this returns false.
|
|
|
+ *
|
|
|
+ * @return integer Number of affected rows
|
|
|
+ */
|
|
|
+ public function lastAffected() {
|
|
|
+ $affected = parent::lastAffected();
|
|
|
+ if ($affected === null && $this->_lastAffected !== false) {
|
|
|
+ return $this->_lastAffected;
|
|
|
+ }
|
|
|
+ return $affected;
|
|
|
+ }
|
|
|
+/**
|
|
|
+ * Executes given SQL statement.
|
|
|
+ *
|
|
|
+ * @param string $sql SQL statement
|
|
|
+ * @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 mixed PDOStatement if query executes with no problem, true as the result of a succesfull, false on error
|
|
|
+ * query returning no rows, suchs as a CREATE statement, false otherwise
|
|
|
+ */
|
|
|
+ protected function _execute($sql, $params = array(), $prepareOptions = array()) {
|
|
|
+ $this->_lastAffected = false;
|
|
|
+ if (strncasecmp($sql, 'SELECT', 6) == 0) {
|
|
|
+ $prepareOptions += array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL);
|
|
|
+ return parent::_execute($sql, $params, $prepareOptions);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ $this->_lastAffected = $this->_connection->exec($sql);
|
|
|
+ if ($this->_lastAffected === false) {
|
|
|
+ $this->_results = null;
|
|
|
+ $error = $this->_connection->errorInfo();
|
|
|
+ $this->error = $error[2];
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ } catch (PDOException $e) {
|
|
|
+ $this->_results = null;
|
|
|
+ $this->error = $e->getMessage();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|