Skip to content

Commit

Permalink
Update the describe.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed May 6, 2011
1 parent 3f984b6 commit 37d26f2
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/Cake/Model/Datasource/Database/Mssql.php
Expand Up @@ -181,40 +181,43 @@ public function listSources() {
*/
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->Type),
'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;
}

Expand Down

0 comments on commit 37d26f2

Please sign in to comment.