Skip to content

Commit

Permalink
Fixes #6135: Primary Key detection and load record fixtures on mssql.…
Browse files Browse the repository at this point in the history
… Tests added.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8161 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
renan committed Apr 29, 2009
1 parent 7eaf331 commit 975ddaa
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 34 deletions.
9 changes: 5 additions & 4 deletions cake/libs/model/datasources/datasource.php
Expand Up @@ -241,13 +241,14 @@ function describe($model) {
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;
Expand Down
60 changes: 53 additions & 7 deletions cake/libs/model/datasources/dbo/dbo_mssql.php
Expand Up @@ -221,7 +221,7 @@ function describe(&$model) {
'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;
Expand Down Expand Up @@ -341,17 +341,18 @@ function create(&$model, $fields = null, $values = null) {
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;
}
Expand Down Expand Up @@ -631,6 +632,29 @@ function fetchResult() {
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
*
Expand Down Expand Up @@ -680,5 +704,27 @@ function buildIndex($indexes, $table = null) {
}
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;
}
}
?>
79 changes: 56 additions & 23 deletions cake/tests/cases/libs/model/datasources/dbo/dbo_mssql.test.php
Expand Up @@ -94,6 +94,16 @@ function fetchAll($sql, $cache = true, $modelName = null) {
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
Expand All @@ -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
*
Expand Down Expand Up @@ -143,33 +179,14 @@ function findAll($conditions = null, $fields = null, $order = null, $recursive =
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;
}
}
/**
Expand Down Expand Up @@ -329,5 +346,21 @@ function testUpdateAllSyntax() {
$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);
}
}
?>

0 comments on commit 975ddaa

Please sign in to comment.