mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Fixes #6135: Primary Key detection and load record fixtures on mssql. Tests added.
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8161 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
7eaf331213
commit
975ddaa4be
3 changed files with 114 additions and 34 deletions
|
@ -241,13 +241,14 @@ class DataSource extends Object {
|
|||
if ($this->cacheSources === false) {
|
||||
return null;
|
||||
}
|
||||
if (isset($this->__descriptions[$model->tablePrefix . $model->table])) {
|
||||
return $this->__descriptions[$model->tablePrefix . $model->table];
|
||||
$table = $this->fullTableName($model, false);
|
||||
if (isset($this->__descriptions[$table])) {
|
||||
return $this->__descriptions[$table];
|
||||
}
|
||||
$cache = $this->__cacheDescription($model->tablePrefix . $model->table);
|
||||
$cache = $this->__cacheDescription($table);
|
||||
|
||||
if ($cache !== null) {
|
||||
$this->__descriptions[$model->tablePrefix . $model->table] =& $cache;
|
||||
$this->__descriptions[$table] =& $cache;
|
||||
return $cache;
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -221,7 +221,7 @@ class DboMssql extends DboSource {
|
|||
'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')
|
||||
'key' => ($column[0]['Key'] == '1') ? 'primary' : false
|
||||
);
|
||||
if ($fields[$field]['default'] === 'null') {
|
||||
$fields[$field]['default'] = null;
|
||||
|
@ -341,17 +341,18 @@ class DboMssql extends DboSource {
|
|||
if (!empty($values)) {
|
||||
$fields = array_combine($fields, $values);
|
||||
}
|
||||
$primaryKey = $this->_getPrimaryKey($model);
|
||||
|
||||
if (array_key_exists($model->primaryKey, $fields)) {
|
||||
if (empty($fields[$model->primaryKey])) {
|
||||
unset($fields[$model->primaryKey]);
|
||||
if (array_key_exists($primaryKey, $fields)) {
|
||||
if (empty($fields[$primaryKey])) {
|
||||
unset($fields[$primaryKey]);
|
||||
} else {
|
||||
$this->_execute("SET IDENTITY_INSERT " . $this->fullTableName($model) . " ON");
|
||||
$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($model) . ' ON');
|
||||
}
|
||||
}
|
||||
$result = parent::create($model, array_keys($fields), array_values($fields));
|
||||
if (array_key_exists($model->primaryKey, $fields) && !empty($fields[$model->primaryKey])) {
|
||||
$this->_execute("SET IDENTITY_INSERT " . $this->fullTableName($model) . " OFF");
|
||||
if (array_key_exists($primaryKey, $fields) && !empty($fields[$primaryKey])) {
|
||||
$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($model) . ' OFF');
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
@ -631,6 +632,29 @@ class DboMssql extends DboSource {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Inserts multiple values into a table
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $fields
|
||||
* @param array $values
|
||||
* @access protected
|
||||
*/
|
||||
function insertMulti($table, $fields, $values) {
|
||||
$primaryKey = $this->_getPrimaryKey($table);
|
||||
$hasPrimaryKey = $primaryKey != null && (
|
||||
(is_array($fields) && in_array($primaryKey, $fields)
|
||||
|| (is_string($fields) && strpos($fields, $this->startQuote . $primaryKey . $this->endQuote) !== false))
|
||||
);
|
||||
|
||||
if ($hasPrimaryKey) {
|
||||
$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($table) . ' ON');
|
||||
}
|
||||
parent::insertMulti($table, $fields, $values);
|
||||
if ($hasPrimaryKey) {
|
||||
$this->_execute('SET IDENTITY_INSERT ' . $this->fullTableName($table) . ' OFF');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Generate a database-native column schema string
|
||||
*
|
||||
|
@ -680,5 +704,27 @@ class DboMssql extends DboSource {
|
|||
}
|
||||
return $join;
|
||||
}
|
||||
/**
|
||||
* Makes sure it will return the primary key
|
||||
*
|
||||
* @param mixed $model
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
function _getPrimaryKey($model) {
|
||||
if (is_object($model)) {
|
||||
$schema = $model->schema();
|
||||
} else {
|
||||
$schema = $this->describe($model);
|
||||
}
|
||||
|
||||
foreach ($schema as $field => $props) {
|
||||
if (isset($props['key']) && $props['key'] == 'primary') {
|
||||
return $field;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -94,6 +94,16 @@ class DboMssqlTestDb extends DboMssql {
|
|||
function getLastQuery() {
|
||||
return $this->simulated[count($this->simulated) - 1];
|
||||
}
|
||||
/**
|
||||
* getPrimaryKey method
|
||||
*
|
||||
* @param mixed $model
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function getPrimaryKey($model) {
|
||||
return parent::_getPrimaryKey($model);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* MssqlTestModel class
|
||||
|
@ -116,6 +126,32 @@ class MssqlTestModel extends Model {
|
|||
* @access public
|
||||
*/
|
||||
var $useTable = false;
|
||||
/**
|
||||
* _schema property
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
var $_schema = array(
|
||||
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
|
||||
'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
|
||||
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
||||
'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
||||
'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
|
||||
'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
|
||||
);
|
||||
/**
|
||||
* find method
|
||||
*
|
||||
|
@ -143,33 +179,14 @@ class MssqlTestModel extends Model {
|
|||
return $conditions;
|
||||
}
|
||||
/**
|
||||
* schema method
|
||||
* setSchema method
|
||||
*
|
||||
* @param array $schema
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function schema() {
|
||||
$this->_schema = array(
|
||||
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
|
||||
'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
|
||||
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
||||
'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
||||
'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
|
||||
'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
||||
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
||||
'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
|
||||
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
|
||||
);
|
||||
return $this->_schema;
|
||||
function setSchema($schema) {
|
||||
$this->_schema = $schema;
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
@ -329,5 +346,21 @@ class DboMssqlTest extends CakeTestCase {
|
|||
$this->assertPattern('/^UPDATE \[mssql_test_models\]/', $result);
|
||||
$this->assertPattern('/SET \[client_id\] = \[client_id\] \+ 1/', $result);
|
||||
}
|
||||
/**
|
||||
* testGetPrimaryKey method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testGetPrimaryKey() {
|
||||
$result = $this->db->getPrimaryKey($this->model);
|
||||
$this->assertEqual($result, 'id');
|
||||
|
||||
$schema = $this->model->schema();
|
||||
unset($schema['id']['key']);
|
||||
$this->model->setSchema($schema);
|
||||
$result = $this->db->getPrimaryKey($this->model);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue