From 37d26f29234978c59efcd11189aa6e1c1462e502 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Wed, 27 Apr 2011 21:27:53 -0400 Subject: [PATCH] Update the describe. --- lib/Cake/Model/Datasource/Database/Mssql.php | 29 +++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Mssql.php b/lib/Cake/Model/Datasource/Database/Mssql.php index 173ca89940d..0f2fa9d053d 100644 --- a/lib/Cake/Model/Datasource/Database/Mssql.php +++ b/lib/Cake/Model/Datasource/Database/Mssql.php @@ -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; }