mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-02 09:32:43 +00:00
Fixing transaction statement and Italian-language error checking in MSSQL database driver, fixes #4074, fixes #4890
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7266 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
65281500b3
commit
f52706ea9a
1 changed files with 82 additions and 35 deletions
|
@ -92,6 +92,17 @@ class DboMssql extends DboSource {
|
|||
'binary' => array('name' => 'image'),
|
||||
'boolean' => array('name' => 'bit')
|
||||
);
|
||||
/**
|
||||
* Index of basic SQL commands
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
var $_commands = array(
|
||||
'begin' => 'BEGIN TRANSACTION',
|
||||
'commit' => 'COMMIT',
|
||||
'rollback' => 'ROLLBACK'
|
||||
);
|
||||
/**
|
||||
* MS SQL DBO driver constructor; sets SQL Server error reporting defaults
|
||||
*
|
||||
|
@ -170,7 +181,7 @@ class DboMssql extends DboSource {
|
|||
* @return array Array of tablenames in the database
|
||||
*/
|
||||
function listSources() {
|
||||
$cache = parent::listSources();
|
||||
//$cache = parent::listSources();
|
||||
|
||||
if ($cache != null) {
|
||||
return $cache;
|
||||
|
@ -215,9 +226,12 @@ class DboMssql extends DboSource {
|
|||
'length' => intval($column[0]['Length']),
|
||||
'key' => ($column[0]['Key'] == '1')
|
||||
);
|
||||
if (in_array($fields[$field]['default'], array('null', '(null)'))) {
|
||||
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') {
|
||||
$fields[$field]['length'] = 11;
|
||||
} elseif (!$fields[$field]['key']) {
|
||||
|
@ -315,20 +329,6 @@ class DboMssql extends DboSource {
|
|||
}
|
||||
return $fields;
|
||||
}
|
||||
/**
|
||||
* Begin a transaction
|
||||
*
|
||||
* @param unknown_type $model
|
||||
* @return boolean True on success, false on fail
|
||||
* (i.e. if the database/model does not support transactions).
|
||||
*/
|
||||
function begin(&$model) {
|
||||
if (parent::begin($model) && $this->execute('BEGIN TRANSACTION')) {
|
||||
$this->_transactionStarted = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Generates and executes an SQL INSERT statement for given model, fields, and values.
|
||||
* Removes Identity (primary key) column from update data before returning to parent, if
|
||||
|
@ -530,27 +530,47 @@ class DboMssql extends DboSource {
|
|||
* @return string
|
||||
*/
|
||||
function renderStatement($type, $data) {
|
||||
if (strtolower($type) == 'select') {
|
||||
extract($data);
|
||||
$fields = trim($fields);
|
||||
switch (strtolower($type)) {
|
||||
case 'select':
|
||||
extract($data);
|
||||
$fields = trim($fields);
|
||||
|
||||
if (strpos($limit, 'TOP') !== false && strpos($fields, 'DISTINCT ') === 0) {
|
||||
$limit = 'DISTINCT ' . trim($limit);
|
||||
$fields = substr($fields, 9);
|
||||
}
|
||||
if (strpos($limit, 'TOP') !== false && strpos($fields, 'DISTINCT ') === 0) {
|
||||
$limit = 'DISTINCT ' . trim($limit);
|
||||
$fields = substr($fields, 9);
|
||||
}
|
||||
|
||||
if (preg_match('/offset\s+([0-9]+)/i', $limit, $offset)) {
|
||||
$limit = preg_replace('/\s*offset.*$/i', '', $limit);
|
||||
preg_match('/top\s+([0-9]+)/i', $limit, $limitVal);
|
||||
$offset = intval($offset[1]) + intval($limitVal[1]);
|
||||
$rOrder = $this->__switchSort($order);
|
||||
list($order2, $rOrder) = array($this->__mapFields($order), $this->__mapFields($rOrder));
|
||||
return "SELECT * FROM (SELECT {$limit} * FROM (SELECT TOP {$offset} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$order}) AS Set1 {$rOrder}) AS Set2 {$order2}";
|
||||
} else {
|
||||
return "SELECT {$limit} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$order}";
|
||||
}
|
||||
} else {
|
||||
return parent::renderStatement($type, $data);
|
||||
if (preg_match('/offset\s+([0-9]+)/i', $limit, $offset)) {
|
||||
$limit = preg_replace('/\s*offset.*$/i', '', $limit);
|
||||
preg_match('/top\s+([0-9]+)/i', $limit, $limitVal);
|
||||
$offset = intval($offset[1]) + intval($limitVal[1]);
|
||||
$rOrder = $this->__switchSort($order);
|
||||
list($order2, $rOrder) = array($this->__mapFields($order), $this->__mapFields($rOrder));
|
||||
return "SELECT * FROM (SELECT {$limit} * FROM (SELECT TOP {$offset} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$order}) AS Set1 {$rOrder}) AS Set2 {$order2}";
|
||||
} else {
|
||||
return "SELECT {$limit} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$order}";
|
||||
}
|
||||
break;
|
||||
case "schema":
|
||||
extract($data);
|
||||
|
||||
foreach ($indexes as $i => $index) {
|
||||
if (preg_match('/PRIMARY KEY/', $index)) {
|
||||
unset($indexes[$i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (array('columns', 'indexes') as $var) {
|
||||
if (is_array(${$var})) {
|
||||
${$var} = "\t" . join(",\n\t", array_filter(${$var}));
|
||||
}
|
||||
}
|
||||
return "CREATE TABLE {$table} (\n{$columns});\n{$indexes}";
|
||||
break;
|
||||
default:
|
||||
return parent::renderStatement($type, $data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
@ -636,6 +656,33 @@ class DboMssql extends DboSource {
|
|||
}
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* Format indexes for create table
|
||||
*
|
||||
* @param array $indexes
|
||||
* @param string $table
|
||||
* @return string
|
||||
*/
|
||||
function buildIndex($indexes, $table = null) {
|
||||
$join = array();
|
||||
|
||||
foreach ($indexes as $name => $value) {
|
||||
if ($name == 'PRIMARY') {
|
||||
$out = 'PRIMARY KEY (' . $this->name($value['column']) . ')';
|
||||
} else {
|
||||
$out = "ALTER TABLE {$table} ADD CONSTRAINT {$name} UNIQUE";
|
||||
|
||||
if (is_array($value['column'])) {
|
||||
$value['column'] = join(', ', array_map(array(&$this, 'name'), $value['column']));
|
||||
} else {
|
||||
$value['column'] = $this->name($value['column']);
|
||||
}
|
||||
$out .= "({$value['column']});";
|
||||
}
|
||||
$join[] = $out;
|
||||
}
|
||||
return $join;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue