Permalink
Browse files

MSSQL2005 driver: autoincrement determination fixed

  • Loading branch information...
1 parent 16b254f commit 9001afe7ca6783d986019b5d0d7414ebd14c5af4 @whipsterCZ whipsterCZ committed with Dec 21, 2012
Showing with 21 additions and 9 deletions.
  1. +21 −9 dibi/drivers/mssql2005.reflector.php
@@ -57,32 +57,44 @@ public function getTables()
*/
public function getColumns($table)
{
- $res = $this->driver->query("SELECT
- C.COLUMN_NAME, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH, C.COLUMN_DEFAULT, C.NUMERIC_PRECISION, C.NUMERIC_SCALE, C.IS_NULLABLE, Case When Z.CONSTRAINT_NAME Is Null Then 0 Else 1 End As IsPartOfPrimaryKey
+ $res = $this->driver->query("
+ SELECT c.name as COLUMN_NAME, c.is_identity AS AUTO_INCREMENT
+ FROM sys.columns c
+ INNER JOIN sys.tables t ON c.object_id = t.object_id
+ WHERE t.name = {$this->driver->escape($table, dibi::TEXT)}
+ ");
+
+ $autoIncrements = array();
+ while ($row = $res->fetch(TRUE)) {
+ $autoIncrements[$row['COLUMN_NAME']] = (bool) $row['AUTO_INCREMENT'];
+ }
+
+ $res = $this->driver->query("
+ SELECT C.COLUMN_NAME, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH , C.COLUMN_DEFAULT , C.NUMERIC_PRECISION, C.NUMERIC_SCALE , C.IS_NULLABLE, Case When Z.CONSTRAINT_NAME Is Null Then 0 Else 1 End As IsPartOfPrimaryKey
FROM INFORMATION_SCHEMA.COLUMNS As C
Outer Apply (
SELECT CCU.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS As TC
- Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As CCU
- On CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
+ Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As CCU
+ On CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
WHERE TC.TABLE_SCHEMA = C.TABLE_SCHEMA
And TC.TABLE_NAME = C.TABLE_NAME
And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
And CCU.COLUMN_NAME = C.COLUMN_NAME
- ) As Z
- WHERE C.TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}"
- );
+ ) As Z
+ WHERE C.TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}
+ ");
$columns = array();
while ($row = $res->fetch(TRUE)) {
$columns[] = array(
'name' => $row['COLUMN_NAME'],
'table' => $table,
'nativetype' => strtoupper($row['DATA_TYPE']),
- 'size' => $row['CHARACTER_MAXIMUM_LENGTH'] ? $row['CHARACTER_MAXIMUM_LENGTH'] : NULL,
+ 'size' => $row['CHARACTER_MAXIMUM_LENGTH'],
'unsigned' => TRUE,
'nullable' => $row['IS_NULLABLE'] === 'YES',
'default' => $row['COLUMN_DEFAULT'],
- 'autoincrement' => (bool) $row['IsPartOfPrimaryKey'] && strtoupper($row['DATA_TYPE']) === 'INT',
+ 'autoincrement' => $autoIncrements[$row['COLUMN_NAME']],
'vendor' => $row,
);
}

0 comments on commit 9001afe

Please sign in to comment.