diff --git a/src/xPDO/Om/sqlsrv/xPDOGenerator.php b/src/xPDO/Om/sqlsrv/xPDOGenerator.php index 25b3b2f1..0b361e67 100644 --- a/src/xPDO/Om/sqlsrv/xPDOGenerator.php +++ b/src/xPDO/Om/sqlsrv/xPDOGenerator.php @@ -56,41 +56,66 @@ public function writeSchema($schemaFile, $package= '', $baseClass= '', $tablePre $baseClass= 'xPDO\Om\xPDOObject'; if (empty ($tablePrefix)) $tablePrefix= $this->manager->xpdo->config[xPDO::OPT_TABLE_PREFIX]; + + $schemaVersion = xPDO::SCHEMA_VERSION; $xmlContent = array(); $xmlContent[] = ""; - $xmlContent[] = ""; + $xmlContent[] = ""; //read list of tables $tableLike= ($tablePrefix && $restrictPrefix); if ($tableLike) { - $tablesStmt= $this->manager->xpdo->query("SELECT * FROM sys.Tables WHERE name LIKE '{$tablePrefix}%' ORDER BY name"); + $tablesStmt= $this->manager->xpdo->query(" + SELECT SCHEMA_NAME(schema_id) + As schema_name, * + from sys.tables + WHERE SCHEMA_NAME(schema_id) = '{$tablePrefix}' + ORDER BY name + "); } else { $tablesStmt= $this->manager->xpdo->query("SELECT * FROM sys.Tables ORDER BY name"); } - $tables= $tablesStmt->fetchAll(PDO::FETCH_NUM); + $tables= $tablesStmt->fetchAll(PDO::FETCH_ASSOC); if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, print_r($tables, true)); foreach ($tables as $table) { $xmlObject= array(); $xmlFields= array(); $xmlIndices= array(); - if (!$tableName= $this->getTableName($table[0], $tablePrefix, $restrictPrefix)) { - continue; - } + $tableName= $table['name']; + $tableId= $table['object_id']; $class= $this->getClassName($tableName); $extends= $baseClass; - $fieldsStmt= $this->manager->xpdo->query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{$tableName}'"); + //COLUMNS + $fieldsStmt= $this->manager->xpdo->query(" + SELECT + col.*, + ic.*, + object_definition(col.default_object_id) AS dflt_value, + TYPE_NAME(col.user_type_id) AS type, + i.is_primary_key AS pk, + i.is_unique AS is_unique + FROM sys.columns col + LEFT JOIN sys.index_columns ic ON col.object_id = ic.object_id AND col.column_id = ic.column_id + LEFT JOIN sys.indexes i ON col.object_id = i.object_id AND ic.index_id = i.index_id + WHERE col.object_id = '{$tableId}' + "); + $fields= $fieldsStmt->fetchAll(PDO::FETCH_ASSOC); - if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Fields for table {$table[0]}: " . print_r($fields, true)); + if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Fields for table {$tableName}: " . print_r($fields, true)); $cid = 0; foreach ($fields as $field) { $name = ''; $type = ''; - $notnull = 0; + $max_length = ''; + $is_nullable = 0; $dflt_value = null; + $Key = ''; $pk = 0; + $is_unique = 0; + $index_id = ''; extract($field, EXTR_OVERWRITE); $Field= $name; - $PhpType= $this->manager->getPhpType($type); - $Null= ' null="' . ($notnull ? 'false' : 'true') . '"'; + $PhpType= $this->manager->xpdo->driver->getPhpType($type); + $Null= ' null="' . ($is_nullable == 1 ? 'true' : 'false') . '"'; $Default= $this->getDefault($dflt_value); $Extra= ''; if (!empty($pk)) { @@ -104,23 +129,44 @@ public function writeSchema($schemaFile, $package= '', $baseClass= '', $tablePre } $Key = ' index="pk"'; } else { - $Key = $this->getIndex($field); + if ($is_unique == 1) { + $Key = ' index="unique"'; + } elseif (!empty($index_id)) { + $Key = ' index="index"'; + } } - $xmlFields[]= "\t\t"; + $xmlFields[]= "\t\t"; $cid++; } - $indicesStmt= $this->manager->xpdo->query("PRAGMA index_list({$table[0]})"); + //INDEXES + $indicesStmt= $this->manager->xpdo->query(" + SELECT + i.* + FROM sys.indexes i + WHERE i.object_id = '{$tableId}' + "); + $indices= $indicesStmt->fetchAll(PDO::FETCH_ASSOC); - if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Indices for table {$table[0]}: " . print_r($indices, true)); + + if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Indices for table {$tableName}: " . print_r($indices, true)); foreach ($indices as $index) { - $primary = preg_match('/^sqlite_autoindex/', $index['name']) ? 'true' : 'false'; - $unique = !empty($index['unique']) ? 'true' : 'false'; + $primary = ($index['is_primary_key'] == 1) ? 'true' : 'false'; + $unique = ($index['is_unique'] == 1) ? 'true' : 'false'; $xmlIndices[]= "\t\t"; - $columnsStmt = $this->manager->xpdo->query("PRAGMA index_info({$index['name']})"); + + //INDEX COLUMNS + $columnsStmt = $this->manager->xpdo->query(" + SELECT + ic.*,col.* + FROM sys.index_columns ic + LEFT JOIN sys.columns col ON ic.column_id = col.column_id + WHERE ic.object_id = '{$tableId}' AND col.object_id = '{$tableId}' AND ic.index_id = '{$index['index_id']}' + "); $columns = $columnsStmt->fetchAll(PDO::FETCH_ASSOC); if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Columns of index {$index['name']}: " . print_r($columns, true)); foreach ($columns as $column) { - $xmlIndices[]= "\t\t\t"; + $Null= ' null="' . ($column['is_nullable'] == 1 ? 'true' : 'false') . '"'; + $xmlIndices[]= "\t\t\t"; } $xmlIndices[]= "\t\t"; } @@ -135,7 +181,7 @@ public function writeSchema($schemaFile, $package= '', $baseClass= '', $tablePre } $xmlContent[] = ""; if ($this->manager->xpdo->getDebug() === true) { - $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, implode("\n", $xmlContent)); + $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, implode("\n", $xmlContent)); } $file= fopen($schemaFile, 'wb'); $written= fwrite($file, implode("\n", $xmlContent)); diff --git a/src/xPDO/Om/sqlsrv/xPDOQuery.php b/src/xPDO/Om/sqlsrv/xPDOQuery.php index f8adac8e..6c651d28 100644 --- a/src/xPDO/Om/sqlsrv/xPDOQuery.php +++ b/src/xPDO/Om/sqlsrv/xPDOQuery.php @@ -227,15 +227,15 @@ public function construct() { $tables= array (); foreach ($this->query['from']['tables'] as $table) { if ($command != 'SELECT') { - $tables[]= $this->xpdo->escape($table['table']); + $tables[]= $this->xpdo->escape($table['table_prefix']).'.'.$this->xpdo->escape(str_replace(array('[',']',$table['table_prefix']),'',$table['table'])); } else { - $tables[]= $this->xpdo->escape($table['table']) . ' AS ' . $this->xpdo->escape($table['alias']); + $tables[]= $this->xpdo->escape($table['table_prefix']).'.'.$this->xpdo->escape(str_replace(array('[',']',$table['table_prefix']),'',$table['table'])) . ' AS ' . $this->xpdo->escape($table['alias']); } } $sql.= $this->query['from']['tables'] ? implode(', ', $tables) . ' ' : ''; if (!empty ($this->query['from']['joins'])) { foreach ($this->query['from']['joins'] as $join) { - $sql.= $join['type'] . ' ' . $this->xpdo->escape($join['table']) . ' AS ' . $this->xpdo->escape($join['alias']) . ' '; + $sql.= $join['type'] . ' ' . $this->xpdo->escape($join['table_prefix']).'.'.$this->xpdo->escape(str_replace(array('[',']',$join['table_prefix']),'',$join['table'])) . ' AS ' . $this->xpdo->escape($join['alias']) . ' '; if (!empty ($join['conditions'])) { $sql.= 'ON '; $sql.= $this->buildConditionalClause($join['conditions']); diff --git a/src/xPDO/Om/xPDOQuery.php b/src/xPDO/Om/xPDOQuery.php index 10118684..ea3a5095 100644 --- a/src/xPDO/Om/xPDOQuery.php +++ b/src/xPDO/Om/xPDOQuery.php @@ -123,7 +123,8 @@ public function __construct(& $xpdo, $class, $criteria= null) { $this->_tableClass = $this->xpdo->getTableClass($this->_class); $this->query['from']['tables'][0]= array ( 'table' => $this->xpdo->getTableName($this->_class), - 'alias' => & $this->_alias + 'alias' => & $this->_alias, + 'table_prefix' => $this->xpdo->getOption('table_prefix') ); if ($criteria !== null) { if (is_object($criteria)) { @@ -296,6 +297,7 @@ public function join($class, $alias= '', $type= xPDOQuery::SQL_JOIN_CROSS, $cond $targetIdx= count($target); $target[$targetIdx]= array ( 'table' => $this->xpdo->getTableName($class), + 'table_prefix' => $this->xpdo->getOption('table_prefix'), 'class' => $class, 'alias' => $alias, 'type' => $type,