From 758fe810a58616da9517913ec3778ab831e54b9c Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Wed, 3 Apr 2013 17:19:26 +1300 Subject: [PATCH] API Upgraded module to use new database ORM --- _config/connectors.yml | 25 + _config/mssql.yml | 2 + _register_database.php | 42 + code/MSSQLAzureDatabase.php | 84 +- code/MSSQLDatabase.php | 1610 ++------------------- code/MSSQLDatabaseConfigurationHelper.php | 267 ++-- code/MSSQLQueryBuilder.php | 112 ++ code/MSSQLSchemaManager.php | 848 +++++++++++ code/SQLServerConnector.php | 195 +++ code/SQLServerQuery.php | 76 + 10 files changed, 1602 insertions(+), 1659 deletions(-) create mode 100644 _config/connectors.yml create mode 100644 _config/mssql.yml create mode 100644 _register_database.php create mode 100644 code/MSSQLQueryBuilder.php create mode 100644 code/MSSQLSchemaManager.php create mode 100644 code/SQLServerConnector.php create mode 100644 code/SQLServerQuery.php diff --git a/_config/connectors.yml b/_config/connectors.yml new file mode 100644 index 0000000..bdd9117 --- /dev/null +++ b/_config/connectors.yml @@ -0,0 +1,25 @@ +--- +name: mssqlconnectors +--- +Injector: +# Connect using PDO + MSSQLPDODatabase: + class: 'MSSQLDatabase' + properties: + connector: %$PDOConnector + schemaManager: %$MSSQLSchemaManager + queryBuilder: %$MSSQLQueryBuilder +# Uses sqlsrv_connect + MSSQLDatabase: + class: 'MSSQLDatabase' + properties: + connector: %$SQLServerConnector + schemaManager: %$MSSQLSchemaManager + queryBuilder: %$MSSQLQueryBuilder +# Uses sqlsrv_connect to connect to a MS Azure Database + MSSQLAzureDatabase: + class: 'MSSQLAzureDatabase' + properties: + connector: %$SQLServerConnector + schemaManager: %$MSSQLSchemaManager + queryBuilder: %$MSSQLQueryBuilder \ No newline at end of file diff --git a/_config/mssql.yml b/_config/mssql.yml new file mode 100644 index 0000000..7969743 --- /dev/null +++ b/_config/mssql.yml @@ -0,0 +1,2 @@ +MSSQLDatabase: + collation: null diff --git a/_register_database.php b/_register_database.php new file mode 100644 index 0000000..1cb9006 --- /dev/null +++ b/_register_database.php @@ -0,0 +1,42 @@ + 'MSSQLPDODatabase', + 'title' => 'SQL Server 2008 (using PDO)', + 'helperPath' => dirname(__FILE__).'/code/MSSQLDatabaseConfigurationHelper.php', + 'supported' => (class_exists('PDO') && in_array('sqlsrv', PDO::getAvailableDrivers())), + 'missingExtensionText' => + 'Either the PDO Extension or + the SQL Server PDO Driver + are unavailable. Please install or enable these and refresh this page.' +)); + +// Basic driver using sqlsrv connector +DatabaseAdapterRegistry::register(array( + 'class' => 'MSSQLDatabase', + 'title' => 'SQL Server 2008 (using sqlsrv)', + 'helperPath' => dirname(__FILE__).'/code/MSSQLDatabaseConfigurationHelper.php', + 'supported' => function_exists('sqlsrv_connect'), + 'missingExtensionText' => + 'The sqlsrv + PHP extensions is not available. Please install or enable it and refresh this page.', + 'fields' => array_merge(DatabaseAdapterRegistry::get_default_fields(), array( + // @todo - do we care about windows authentication for PDO/SQL Server? + 'windowsauthentication' => array( + 'title' => 'Use Windows authentication? (leave blank for false)', + 'default' => '' + ) + )) +)); + +// MS Azure uses an online database +DatabaseAdapterRegistry::register(array( + 'class' => 'MSSQLAzureDatabase', + 'title' => 'MS Azure Database (using sqlsrv)', + 'helperPath' => dirname(__FILE__).'/code/MSSQLDatabaseConfigurationHelper.php', + 'supported' => function_exists('sqlsrv_connect'), + 'missingExtensionText' => + 'The sqlsrv + PHP extension is not available. Please install or enable it and refresh this page.' +)); diff --git a/code/MSSQLAzureDatabase.php b/code/MSSQLAzureDatabase.php index 82b2e57..f47b566 100644 --- a/code/MSSQLAzureDatabase.php +++ b/code/MSSQLAzureDatabase.php @@ -1,4 +1,5 @@ connectDatabase($parameters); @@ -28,42 +38,68 @@ class MSSQLAzureDatabase extends MSSQLDatabase { /** * Connect to a SQL Azure database with the given parameters. * @param array $parameters Connection parameters set by environment - * @return resource SQL Azure database connection link + * - server: The server, eg, localhost + * - username: The username to log on with + * - password: The password to log on with + * - database: The database to connect to + * - windowsauthentication: Not supported for Azure */ - protected function connectDatabase($parameters) { - $this->dbConn = sqlsrv_connect($parameters['server'], array( - 'Database' => $parameters['database'], - 'UID' => $parameters['username'], - 'PWD' => $parameters['password'], - 'MultipleActiveResultSets' => '0' - )); + protected function connect($parameters) { + $this->parameters = $parameters; + $this->connectDatabase($parameters['database']); + } + + /** + * Connect to a database using the provided parameters + * + * @param string $database + */ + protected function connectDatabase($database) { + $parameters = $this->parameters; + $parameters['database'] = $database; + $parameters['multipleactiveresultsets'] = 0; - $this->tableList = $this->fieldList = $this->indexList = null; - $this->database = $parameters['database']; - $this->active = true; - $this->fullTextEnabled = false; + // Ensure that driver is available (required by PDO) + if(empty($parameters['driver'])) { + $parameters['driver'] = $this->getDatabaseServer(); + } + // Notify connector of parameters, instructing the connector + // to connect immediately to the Azure database + $this->connector->connect($parameters, true); + + // Configure the connection $this->query('SET QUOTED_IDENTIFIER ON'); $this->query('SET TEXTSIZE 2147483647'); } /** * Switches to the given database. - * - * If the database doesn't exist, you should call - * createDatabase() after calling selectDatabase() * * IMPORTANT: SQL Azure doesn't support "USE", so we need * to reinitialize the database connection with the requested * database name. + * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee336288.aspx * - * @param string $dbname The database name to switch to + * @param type $name The database name to switch to + * @param type $create + * @param type $errorLevel */ - public function selectDatabase($dbname) { - global $databaseConfig; - $parameters = $databaseConfig; - $parameters['database'] = $dbname; - $this->connectDatabase($parameters); + public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR) { + $this->fullTextEnabled = null; + if (!$this->schemaManager->databaseExists($name)) { + // Check DB creation permisson + if (!$create) { + if ($errorLevel !== false) { + user_error("Attempted to connect to non-existing database \"$name\"", $errorLevel); + } + // Unselect database + $this->connector->unloadDatabase(); + return false; + } + $this->schemaManager->createDatabase($name); + } + $this->connectDatabase($name); + return true; } - -} \ No newline at end of file +} diff --git a/code/MSSQLDatabase.php b/code/MSSQLDatabase.php index 6163987..e68c1b3 100644 --- a/code/MSSQLDatabase.php +++ b/code/MSSQLDatabase.php @@ -40,37 +40,6 @@ */ class MSSQLDatabase extends SS_Database { - /** - * Connection to the DBMS. - * @var resource - */ - protected $dbConn; - - /** - * True if we are connected to a database. - * @var boolean - */ - protected $active; - - /** - * The name of the database. - * @var string - */ - protected $database; - - /** - * If true, use the mssql_... functions. - * If false use the sqlsrv_... functions - */ - protected $mssql = null; - - /** - * Stores the affected rows of the last query. - * Used by sqlsrv functions only, as sqlsrv_rows_affected - * accepts a result instead of a database handle. - */ - protected $lastAffectedRows; - /** * Words that will trigger an error if passed to a SQL Server fulltext search */ @@ -94,23 +63,25 @@ class MSSQLDatabase extends SS_Database { */ protected $fullTextEnabled = null; - /** - * Stores per-request cached constraint checks that come from the database. - * @var array - */ - protected static $cached_checks = array(); - - /** - * @ignore - */ - protected static $collation = null; - /** * Set the default collation of the MSSQL nvarchar fields that we create. * We don't apply this to the database as a whole, so that we can use unicode collations. + * + * @param string $collation */ public static function set_collation($collation) { - self::$collation = $collation; + Config::inst()->update('MSSQLDatabase', 'collation', $collation); + } + + /** + * The default collation of the MSSQL nvarchar fields that we create. + * We don't apply this to the database as a whole, so that we can use + * unicode collations. + * + * @return string + */ + public static function get_collation() { + return Config::inst()->get('MSSQLDatabase', 'collation'); } /** @@ -120,75 +91,15 @@ class MSSQLDatabase extends SS_Database { * - username: The username to log on with * - password: The password to log on with * - database: The database to connect to + * - windowsauthentication: Set to true to use windows authentication + * instead of username/password */ - public function __construct($parameters) { - if(function_exists('mssql_connect')) { - $this->mssql = true; - } else if(function_exists('sqlsrv_connect')) { - $this->mssql = false; - } else { - user_error("Neither the mssql_connect() nor the sqlsrv_connect() functions are available. Please install the PHP native mssql module, or the Microsoft-provided sqlsrv module.", E_USER_ERROR); - } + public function connect($parameters) { + parent::connect($parameters); - if($this->mssql) { - // Switch to utf8 connection charset - ini_set('mssql.charset', 'utf8'); - $this->dbConn = mssql_connect($parameters['server'], $parameters['username'], $parameters['password'], true); - } else { - // Disable default warnings as errors behaviour for sqlsrv to keep it in line with mssql functions - if(ini_get('sqlsrv.WarningsReturnAsErrors')) { - ini_set('sqlsrv.WarningsReturnAsErrors', 'Off'); - } - - $options = array( - 'CharacterSet' => 'UTF-8', - 'MultipleActiveResultSets' => true - ); - if(!(defined('MSSQL_USE_WINDOWS_AUTHENTICATION') && MSSQL_USE_WINDOWS_AUTHENTICATION == true)) { - $options['UID'] = $parameters['username']; - $options['PWD'] = $parameters['password']; - } - - $this->dbConn = sqlsrv_connect($parameters['server'], $options); - } - - if(!$this->dbConn) { - $this->databaseError('Couldn\'t connect to SQL Server database'); - } else { - $this->database = $parameters['database']; - $this->selectDatabase($this->database); - - // Configure the connection - $this->query('SET QUOTED_IDENTIFIER ON'); - $this->query('SET TEXTSIZE 2147483647'); - } - } - - public function __destruct() { - if(is_resource($this->dbConn)) { - if($this->mssql) { - mssql_close($this->dbConn); - } else { - sqlsrv_close($this->dbConn); - } - } - } - - /** - * Return the last database error. - * @return string - */ - public function getLastError() { - $error = ''; - if($this->mssql) { - $error = mssql_get_last_message(); - } else { - $errors = sqlsrv_errors(); - if($errors) foreach($errors as $info) { - $error .= implode(', ', array($info['SQLSTATE'], $info['code'], $info['message'])); - } - } - return $error; + // Configure the connection + $this->query('SET QUOTED_IDENTIFIER ON'); + $this->query('SET TEXTSIZE 2147483647'); } /** @@ -199,80 +110,31 @@ class MSSQLDatabase extends SS_Database { */ public function fullTextEnabled() { if($this->fullTextEnabled === null) { - $isInstalled = (boolean) DB::query("SELECT fulltextserviceproperty('isfulltextinstalled')")->value(); - $enabledForDb = (boolean) DB::query(" - SELECT is_fulltext_enabled - FROM sys.databases - WHERE name = '$this->database' - ")->value(); - $this->fullTextEnabled = (boolean) ($isInstalled && $enabledForDb); + $this->fullTextEnabled = $this->updateFullTextEnabled(); } return $this->fullTextEnabled; } - + /** - * Throw a database error - */ - function databaseError($message, $errorLevel = E_USER_ERROR) { - $lastError = $this->getLastError(); - if($lastError) $message .= "\nLast error: " . $lastError; - return parent::databaseError($message, $errorLevel); - } - - /** - * This will set up the full text search capabilities. - */ - function createFullTextCatalog() { - $result = $this->fullTextCatalogExists(); - if(!$result) $this->query('CREATE FULLTEXT CATALOG "ftCatalog" AS DEFAULT;'); - } - - /** - * Check that a fulltext catalog has been created yet. + * Checks whether the current SQL Server version has full-text + * support installed and full-text is enabled for this database. + * * @return boolean */ - public function fullTextCatalogExists() { - return (bool) $this->query("SELECT name FROM sys.fulltext_catalogs WHERE name = 'ftCatalog';")->value(); + protected function updateFullTextEnabled() { + // Check if installed + $isInstalled = $this->query("SELECT fulltextserviceproperty('isfulltextinstalled')")->value(); + if(!$isInstalled) return false; + + // Check if current database is enabled + $database = $this->getSelectedDatabase(); + $enabledForDb = $this->preparedQuery( + "SELECT is_fulltext_enabled FROM sys.databases WHERE name = ?", + array($database) + )->value(); + return $enabledForDb; } - /** - * Sleep until the catalog has been fully rebuilt. This is a busy wait designed for situations - * when you need to be sure the index is up to date - for example in unit tests. - * - * TODO: move this to Database class? Can we assume this will be useful for all databases? - * Also see the wrapper functions "waitUntilIndexingFinished" in SearchFormTest and TranslatableSearchFormTest - * - * @param int $maxWaitingTime Time in seconds to wait for the database. - */ - function waitUntilIndexingFinished($maxWaitingTime = 15) { - if($this->fullTextEnabled()) { - $this->query("EXEC sp_fulltext_catalog 'ftCatalog', 'Rebuild';"); - - // Busy wait until it's done updating, but no longer than 15 seconds. - $start = time(); - while(time()-$start<$maxWaitingTime) { - $status = $this->query("EXEC sp_help_fulltext_catalogs 'ftCatalog';")->first(); - - if (isset($status['STATUS']) && $status['STATUS']==0) { - // Idle! - break; - } - sleep(1); - } - } - } - - /** - * Not implemented, needed for PDO - */ - public function getConnect($parameters) { - return null; - } - - /** - * Returns true if this database supports collations - * @return boolean - */ public function supportsCollations() { return true; } @@ -281,985 +143,20 @@ class MSSQLDatabase extends SS_Database { return true; } - /** - * Get the version of MSSQL. - * @return string - */ - public function getVersion() { - return trim($this->query("SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))")->value()); - } - - /** - * Get the database server, namely mssql. - * @return string - */ public function getDatabaseServer() { - return "mssql"; - } - - public function query($sql, $errorLevel = E_USER_ERROR) { - if(isset($_REQUEST['previewwrite']) && in_array(strtolower(substr($sql,0,strpos($sql,' '))), array('insert','update','delete','replace'))) { - Debug::message("Will execute: $sql"); - return; - } - - if(isset($_REQUEST['showqueries'])) { - $starttime = microtime(true); - } - - $error = ''; - if($this->mssql) { - $handle = mssql_query($sql, $this->dbConn); - } else { - $handle = sqlsrv_query($this->dbConn, $sql); - if($handle) $this->lastAffectedRows = sqlsrv_rows_affected($handle); - } - - if(isset($_REQUEST['showqueries'])) { - $endtime = round(microtime(true) - $starttime,4); - Debug::message("\n$sql\n{$endtime}ms\n", false); - } - - if(!$handle && $errorLevel) $this->databaseError("Couldn't run query: $sql", $errorLevel); - return new MSSQLQuery($this, $handle, $this->mssql); - } - - public function getGeneratedID($table) { - return $this->query("SELECT IDENT_CURRENT('$table')")->value(); - } - - /** - * MSSQL stores the primary key column with an internal identifier, - * so a lookup needs to be done to determine it. - * - * @param string $tableName Name of table with primary key column "ID" - * @return string Internal identifier for primary key - */ - function getPrimaryKey($tableName){ - $indexes=DB::query("EXEC sp_helpindex '$tableName';"); - $indexName = ''; - foreach($indexes as $index) { - if($index['index_keys'] == 'ID') { - $indexName = $index['index_name']; - break; - } - } - - return $indexName; - } - - function getIdentityColumn($tableName) { - return $this->query(" - SELECT - TABLE_NAME + '.' + COLUMN_NAME, - TABLE_NAME - FROM - INFORMATION_SCHEMA.COLUMNS - WHERE - TABLE_SCHEMA = 'dbo' AND - COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1 AND - TABLE_NAME = '$tableName' - ")->value(); - } - - public function isActive() { - return $this->active ? true : false; - } - - /** - * Create the database that is currently selected. - */ - public function createDatabase() { - $this->query("CREATE DATABASE \"$this->database\""); - $this->selectDatabase($this->database); - } - - /** - * Drop the database that this object is currently connected to. - * Use with caution. - */ - public function dropDatabase() { - $db = $this->database; - $this->selectDatabase('master'); - $this->query("DROP DATABASE \"$db\""); - $this->active = false; - } - - /** - * Drop the given database name. - * Use with caution. - * @param string $name Database name to drop - */ - public function dropDatabaseByName($name) { - $this->query("DROP DATABASE \"$name\""); - } - - /** - * Returns the name of the currently selected database - */ - public function currentDatabase() { - return $this->database; - } - - /** - * Switches to the given database. - * - * If the database doesn't exist, you should call - * createDatabase() after calling selectDatabase() - * - * @param string $dbname The database name to switch to - */ - public function selectDatabase($dbname) { - $this->database = $dbname; - - if($this->databaseExists($this->database)) { - if($this->mssql) { - if(mssql_select_db($this->database, $this->dbConn)) { - $this->active = true; - } - } else { - $this->query("USE \"$this->database\""); - $this->active = true; - } - } - - $this->tableList = $this->fieldList = $this->indexList = $this->fullTextEnabled = null; - } - - /** - * Check if the given database exists from {@link allDatabaseNames()}. - * @param string $name Name of database to check exists - * @return boolean - */ - public function databaseExists($name) { - $databases = $this->allDatabaseNames(); - foreach($databases as $dbname) { - if($dbname == $name) return true; - } - return false; - } - - /** - * Return all databases names from the server. - * @return array - */ - public function allDatabaseNames() { - return $this->query('SELECT NAME FROM sys.sysdatabases')->column(); - } - - /** - * Create a new table. - * @param $tableName The name of the table - * @param $fields A map of field names to field types - * @param $indexes A map of indexes - * @param $options An map of additional options. The available keys are as follows: - * - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL. - * - 'temporary' - If true, then a temporary table will be created - * @return The table name generated. This may be different from the table name, for example with temporary tables. - */ - public function createTable($tableName, $fields = null, $indexes = null, $options = null, $advancedOptions = null) { - $fieldSchemas = $indexSchemas = ""; - if($fields) foreach($fields as $k => $v) $fieldSchemas .= "\"$k\" $v,\n"; - - // Temporary tables start with "#" in MSSQL-land - if(!empty($options['temporary'])) { - // Randomize the temp table name to avoid conflicts in the tempdb table which derived databases share - $tableName = "#$tableName" . '-' . rand(1000000, 9999999); - } - - $this->query("CREATE TABLE \"$tableName\" ( - $fieldSchemas - primary key (\"ID\") - );"); - - //we need to generate indexes like this: CREATE INDEX IX_vault_to_export ON vault (to_export); - //This needs to be done AFTER the table creation, so we can set up the fulltext indexes correctly - if($indexes) foreach($indexes as $k => $v) { - $indexSchemas .= $this->getIndexSqlDefinition($tableName, $k, $v) . "\n"; - } - - if($indexSchemas) $this->query($indexSchemas); - - return $tableName; - } - - /** - * Alter a table's schema. - * @param $table The name of the table to alter - * @param $newFields New fields, a map of field name => field schema - * @param $newIndexes New indexes, a map of index name => index type - * @param $alteredFields Updated fields, a map of field name => field schema - * @param $alteredIndexes Updated indexes, a map of index name => index type - */ - public function alterTable($tableName, $newFields = null, $newIndexes = null, $alteredFields = null, $alteredIndexes = null, $alteredOptions=null, $advancedOptions=null) { - $alterList = array(); - $indexList = $this->indexList($tableName); - - // drop any fulltext indexes that exist on the table before altering the structure - if($this->fullTextIndexExists($tableName)) { - $alterList[] = "\nDROP FULLTEXT INDEX ON \"$tableName\";"; - } - - if($newFields) foreach($newFields as $k => $v) $alterList[] = "ALTER TABLE \"$tableName\" ADD \"$k\" $v"; - - if($alteredFields) foreach($alteredFields as $k => $v) $alterList[] = $this->alterTableAlterColumn($tableName, $k, $v, $indexList); - if($alteredIndexes) foreach($alteredIndexes as $k => $v) $alterList[] = $this->getIndexSqlDefinition($tableName, $k, $v); - if($newIndexes) foreach($newIndexes as $k => $v) $alterList[] = $this->getIndexSqlDefinition($tableName, $k, $v); - - if($alterList) { - foreach($alterList as $alteration) { - if($alteration != '') { - $this->query($alteration); - } - } - } - } - - /** - * Given the table and column name, retrieve the constraint name for that column - * in the table. - */ - public function getConstraintName($tableName, $columnName) { - return $this->query(" - SELECT CONSTRAINT_NAME - FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE - WHERE TABLE_NAME = '$tableName' AND COLUMN_NAME = '$columnName' - ")->value(); - } - - /** - * Given a table and column name, return a check constraint clause for that column in - * the table. - * - * This is an expensive query, so it is cached per-request and stored by table. The initial - * call for a table that has not been cached will query all columns and store that - * so subsequent calls are fast. - * - * @param string $tableName Table name column resides in - * @param string $columnName Column name the constraint is for - * @return string The check string - */ - public function getConstraintCheckClause($tableName, $columnName) { - if(isset(self::$cached_checks[$tableName])) { - if(!isset(self::$cached_checks[$tableName][$columnName])) self::$cached_checks[$tableName][$columnName] = null; - return self::$cached_checks[$tableName][$columnName]; - } - - $checks = array(); - foreach($this->query(" - SELECT CAST(CHECK_CLAUSE AS TEXT) AS CHECK_CLAUSE, COLUMN_NAME - FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS AS CC - INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CCU ON CCU.CONSTRAINT_NAME = CC.CONSTRAINT_NAME - WHERE TABLE_NAME = '$tableName' - ") as $record) { - $checks[$record['COLUMN_NAME']] = $record['CHECK_CLAUSE']; - } - - self::$cached_checks[$tableName] = $checks; - if(!isset(self::$cached_checks[$tableName][$columnName])) self::$cached_checks[$tableName][$columnName] = null; - - return self::$cached_checks[$tableName][$columnName]; - } - - /** - * Return the name of the default constraint applied to $tableName.$colName. - * Will return null if no such constraint exists - */ - protected function defaultConstraintName($tableName, $colName) { - return $this->query("SELECT s.name --default name - FROM sys.sysobjects s - join sys.syscolumns c ON s.parent_obj = c.id - WHERE s.xtype = 'd' - and c.cdefault = s.id - and parent_obj= OBJECT_ID('$tableName') - and c.name = '$colName'")->value(); - } - - - /** - * Get enum values from a constraint check clause. - * @param string $clause Check clause to parse values from - * @return array Enum values - */ - protected function enumValuesFromCheckClause($clause) { - $segments = preg_split('/ +OR *\[/i', $clause); - $constraints = array(); - foreach($segments as $segment) { - $bits = preg_split('/ *= */', $segment); - for($i = 1; $i < sizeof($bits); $i += 2) { - array_unshift($constraints, substr(rtrim($bits[$i], ')'), 1, -1)); - } - } - return $constraints; - } - - /* - * Creates an ALTER expression for a column in MS SQL - * - * @param $tableName Name of the table to be altered - * @param $colName Name of the column to be altered - * @param $colSpec String which contains conditions for a column - * @return string - */ - protected function alterTableAlterColumn($tableName, $colName, $colSpec, $indexList){ - - // First, we split the column specifications into parts - // TODO: this returns an empty array for the following string: int(11) not null auto_increment - // on second thoughts, why is an auto_increment field being passed through? - $pattern = '/^([\w()]+)\s?((?:not\s)?null)?\s?(default\s[\w\']+)?\s?(check\s?[\w()\'",\s]+)?$/i'; - $matches=Array(); - preg_match($pattern, $colSpec, $matches); - - // drop the index if it exists - $alterCol=''; - - // drop *ALL* indexes on a table before proceeding - // this won't drop primary keys, though - $indexes = $this->indexNames($tableName); - foreach($indexes as $indexName) { - $alterCol = "\nDROP INDEX \"$indexName\" ON \"$tableName\";"; - } - - $prefix="ALTER TABLE \"" . $tableName . "\" "; - - // Remove the old default prior to adjusting the column. - if($defaultConstraintName = $this->defaultConstraintName($tableName, $colName)) { - $alterCol .= ";\n$prefix DROP CONSTRAINT \"$defaultConstraintName\""; - } - - if(isset($matches[1])) { - //We will prevent any changes being made to the ID column. Primary key indexes will have a fit if we do anything here. - if($colName!='ID'){ - - // SET null / not null - if(!empty($matches[2])) $alterCol .= ";\n$prefix ALTER COLUMN \"$colName\" $matches[1] $matches[2]"; - else $alterCol .= ";\n$prefix ALTER COLUMN \"$colName\" $matches[1]"; - - // Add a default back - if(!empty($matches[3])) $alterCol .= ";\n$prefix ADD $matches[3] FOR \"$colName\""; - - // SET check constraint (The constraint HAS to be dropped) - if(!empty($matches[4])) { - $constraint = $this->getConstraintName($tableName, $colName); - if($constraint) { - $alterCol .= ";\n$prefix DROP CONSTRAINT {$constraint}"; - } - - //NOTE: 'with nocheck' seems to solve a few problems I've been having for modifying existing tables. - $alterCol .= ";\n$prefix WITH NOCHECK ADD CONSTRAINT \"{$tableName}_{$colName}_check\" $matches[4]"; - } - } - } - - return isset($alterCol) ? $alterCol : ''; - } - - public function renameTable($oldTableName, $newTableName) { - $this->query("EXEC sp_rename \"$oldTableName\", \"$newTableName\""); - } - - /** - * Checks a table's integrity and repairs it if necessary. - * NOTE: MSSQL does not appear to support any vacuum or optimise commands - * - * @var string $tableName The name of the table. - * @return boolean Return true if the table has integrity after the method is complete. - */ - public function checkAndRepairTable($tableName) { - return true; - } - - public function createField($tableName, $fieldName, $fieldSpec) { - $this->query("ALTER TABLE \"$tableName\" ADD \"$fieldName\" $fieldSpec"); - } - - /** - * Change the database type of the given field. - * @param string $tableName The name of the tbale the field is in. - * @param string $fieldName The name of the field to change. - * @param string $fieldSpec The new field specification - */ - public function alterField($tableName, $fieldName, $fieldSpec) { - $this->query("ALTER TABLE \"$tableName\" CHANGE \"$fieldName\" \"$fieldName\" $fieldSpec"); - } - - /** - * Change the database column name of the given field. - * - * @param string $tableName The name of the tbale the field is in. - * @param string $oldName The name of the field to change. - * @param string $newName The new name of the field - */ - public function renameField($tableName, $oldName, $newName) { - $this->query("EXEC sp_rename @objname = '$tableName.$oldName', @newname = '$newName', @objtype = 'COLUMN'"); - } - - public function fieldList($table) { - //This gets us more information than we need, but I've included it all for the moment.... - $fieldRecords = $this->query("SELECT ordinal_position, column_name, data_type, column_default, - is_nullable, character_maximum_length, numeric_precision, numeric_scale, collation_name - FROM information_schema.columns WHERE table_name = '$table' - ORDER BY ordinal_position;"); - - // Cache the records from the query - otherwise a lack of multiple active result sets - // will cause subsequent queries to fail in this method - $fields = array(); - $output = array(); - foreach($fieldRecords as $record) { - $fields[] = $record; - } - - foreach($fields as $field) { - // Update the data_type field to be a complete column definition string for use by - // SS_Database::requireField() - switch($field['data_type']){ - case 'bigint': - case 'numeric': - case 'float': - case 'bit': - if($field['data_type'] != 'bigint' && $sizeSuffix = $field['numeric_precision']) { - $field['data_type'] .= "($sizeSuffix)"; - } - - if($field['is_nullable'] == 'YES') { - $field['data_type'] .= ' null'; - } else { - $field['data_type'] .= ' not null'; - } - if($field['column_default']) { - $default=substr($field['column_default'], 2, -2); - $field['data_type'] .= " default $default"; - } - break; - - case 'decimal': - if($field['numeric_precision']) { - $sizeSuffix = $field['numeric_precision'] . ',' . $field['numeric_scale']; - $field['data_type'] .= "($sizeSuffix)"; - } - - if($field['is_nullable'] == 'YES') { - $field['data_type'] .= ' null'; - } else { - $field['data_type'] .= ' not null'; - } - if($field['column_default']) { - $default=substr($field['column_default'], 2, -2); - $field['data_type'] .= " default $default"; - } - break; - - case 'nvarchar': - case 'varchar': - //Check to see if there's a constraint attached to this column: - $clause = $this->getConstraintCheckClause($table, $field['column_name']); - if($clause) { - $constraints = $this->enumValuesFromCheckClause($clause); - $default=substr($field['column_default'], 2, -2); - $field['data_type'] = $this->enum(array( - 'default' => $default, - 'name' => $field['column_name'], - 'enums' => $constraints, - 'table' => $table - )); - break; - } - - default: - $sizeSuffix = $field['character_maximum_length']; - if($sizeSuffix == '-1') $sizeSuffix = 'max'; - if($sizeSuffix) { - $field['data_type'] .= "($sizeSuffix)"; - } - - if($field['is_nullable'] == 'YES') { - $field['data_type'] .= ' null'; - } else { - $field['data_type'] .= ' not null'; - } - if($field['column_default']) { - $default=substr($field['column_default'], 2, -2); - $field['data_type'] .= " default '$default'"; - } - } - $output[$field['column_name']]=$field; - - } - - return $output; - } - - /** - * - * This is a stub function. Postgres caches the fieldlist results. - * - * @param string $tableName - * - * @return boolean - */ - function clear_cached_fieldlist($tableName=false){ - return true; - } - - /** - * Create an index on a table. - * @param string $tableName The name of the table. - * @param string $indexName The name of the index. - * @param string $indexSpec The specification of the index, see SS_Database::requireIndex() for more details. - */ - public function createIndex($tableName, $indexName, $indexSpec) { - $this->query($this->getIndexSqlDefinition($tableName, $indexName, $indexSpec)); - } - - /** - * This takes the index spec which has been provided by a class (ie static $indexes = blah blah) - * and turns it into a proper string. - * Some indexes may be arrays, such as fulltext and unique indexes, and this allows database-specific - * arrays to be created. - */ - public function convertIndexSpec($indexSpec){ - $indexSpec = $this->parseIndexSpec(null, $indexSpec); - return "{$indexSpec['type']} ({$indexSpec['value']})"; + return "sqlsrv"; } - /** - * Splits a spec string safely, considering quoted columns, whitespace, - * and cleaning brackets - * @param string $spec The input index specification - * @return array List of columns in the spec - */ - function explodeColumnString($spec) { - // Remove any leading/trailing brackets and outlying modifiers - // E.g. 'unique (Title, "QuotedColumn");' => 'Title, "QuotedColumn"' - $containedSpec = preg_replace('/(.*\(\s*)|(\s*\).*)/', '', $spec); + public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR) { + $this->fullTextEnabled = null; - // Split potentially quoted modifiers - // E.g. 'Title, "QuotedColumn"' => array('Title', 'QuotedColumn') - return preg_split('/"?\s*,\s*"?/', trim($containedSpec, '(") ')); - } - - /** - * Builds a properly quoted column list from an array - * @param array $columns List of columns to implode - * @return string A properly quoted list of column names - */ - function implodeColumnList($columns) { - if(empty($columns)) return ''; - return '"' . implode('","', $columns) . '"'; - } - - /** - * Given an index specification in the form of a string ensure that each - * column name is property quoted, stripping brackets and modifiers. - * This index may also be in the form of a "CREATE INDEX..." sql fragment - * @param string $spec The input specification or query. E.g. 'unique (Column1, Column2)' - * @return string The properly quoted column list. E.g. '"Column1", "Column2"' - */ - function quoteColumnSpecString($spec) { - $bits = $this->explodeColumnString($spec); - return $this->implodeColumnList($bits); - } - - /** - * Given an index spec determines the index type - * @param type $spec - * @return string - */ - function determineIndexType($spec) { - // check array spec - if(is_array($spec) && isset($spec['type'])) { - return $spec['type']; - } elseif (!is_array($spec) && preg_match('/(?\w+)\s*\(/', $spec, $matchType)) { - return strtolower($matchType['type']); - } else { - return 'index'; - } - } - - /** - * Converts an array or string index spec into a universally useful array - * @see convertIndexSpec() for approximate inverse - * @param string|array $spec An index specification in either one of two formats: - * - * Index types may be one of unique, index, fulltext, or any adaptor specific - * format. Column names may be double quoted. - * @return array The resulting spec array with the required fields 'name', - * 'type', and 'value'. The index value will have cleanly quoted column names. - * E.g. array( - * 'type' => 'unique', - * 'name' => 'MyIndex', - * 'value' => '"Title", "Content"' - * ) - */ - function parseIndexSpec($name, $spec) { - - // Do minimal cleanup on any already parsed spec - if(is_array($spec)) { - $spec['value'] = $this->quoteColumnSpecString($spec['value']); - return $spec; - } - - // Nicely formatted spec! - return array( - 'name' => $name, - 'value' => $this->quoteColumnSpecString($spec), - 'type' => $this->determineIndexType($spec) - ); + return parent::selectDatabase($name, $create, $errorLevel); } - /** - * Builds the internal MS SQL Server index name given the silverstripe table and index name - * @param string $tableName - * @param string $indexName - * @param string $prefix The optional prefix for the index. Defaults to "ix" for indexes. - * @return string The postgres name of the index - */ - function buildMSSQLIndexName($tableName, $indexName, $prefix = 'ix') { - - // Cleanup names of namespaced tables - $tableName = str_replace('\\', '_', $tableName); - $indexName = str_replace('\\', '_', $indexName); - - return "{$prefix}_{$tableName}_{$indexName}"; - } - - /** - * Return SQL for dropping and recreating an index - */ - protected function getIndexSqlDefinition($tableName, $indexName, $indexSpec) { - - // Determine index name - $index = $this->buildMSSQLIndexName($tableName, $indexName); - - // Consolidate/Cleanup spec into array format - $indexSpec = $this->parseIndexSpec($indexName, $indexSpec); - - $drop = "IF EXISTS (SELECT name FROM sys.indexes WHERE name = '$index') DROP INDEX $index ON \"" . $tableName . "\";"; - - // create a type-specific index - if($indexSpec['type'] == 'fulltext' && $this->fullTextEnabled()) { - // enable fulltext on this table - $this->createFullTextCatalog(); - $primary_key = $this->getPrimaryKey($tableName); - - if($primary_key) { - return "$drop CREATE FULLTEXT INDEX ON \"$tableName\" ({$indexSpec['value']})" - . "KEY INDEX $primary_key WITH CHANGE_TRACKING AUTO;"; - } - } - - if($indexSpec['type'] == 'unique') { - return "$drop CREATE UNIQUE INDEX $index ON \"$tableName\" ({$indexSpec['value']});"; - } - - return "$drop CREATE INDEX $index ON \"$tableName\" ({$indexSpec['value']});"; - } - - function getDbSqlDefinition($tableName, $indexName, $indexSpec){ - return $indexName; - } - - /** - * Alter an index on a table. - * @param string $tableName The name of the table. - * @param string $indexName The name of the index. - * @param string $indexSpec The specification of the index, see SS_Database::requireIndex() for more details. - */ - public function alterIndex($tableName, $indexName, $indexSpec) { - $this->createIndex($tableName, $indexName, $indexSpec); - } - - /** - * Return the list of indexes in a table. - * @param string $table The table name. - * @return array - */ - public function indexList($table) { - $indexes = DB::query("EXEC sp_helpindex '$table';"); - $indexList = array(); - - // Enumerate all basic indexes - foreach($indexes as $index) { - if(strpos($index['index_description'], 'unique') !== false) { - $prefix = 'unique '; - } else { - $prefix= 'index '; - } - - // Extract name from index - $baseIndexName = $this->buildMSSQLIndexName($table, ''); - $indexName = substr($index['index_name'], strlen($baseIndexName)); - - // Extract columns - $columns = $this->quoteColumnSpecString($index['index_keys']); - - $indexList[$indexName]['indexname'] = $indexName; - $indexList[$indexName]['spec'] = "$prefix($columns)"; - } - - // separately build up a list of the fulltext indexes for this table - // as MSSQL doesn't return fulltext indexes in sp_helpindex - if($this->fullTextEnabled()) { - $result = DB::query('EXEC sp_help_fulltext_columns;'); - - // Extract columns from this fulltext definition - $columns = array(); - foreach($result as $row) { - if($row['TABLE_NAME'] == $table) { - $columns[] = $row['FULLTEXT_COLUMN_NAME']; - } - } - - if(!empty($columns)) { - $columns = $this->implodeColumnList($columns); - $indexList['SearchFields']['indexname'] = 'SearchFields'; - $indexList['SearchFields']['spec'] = "fulltext ($columns)"; - } - } - - return $indexList; - } - - /** - * For a given table name, get all the internal index names, - * except for those that are primary keys and fulltext indexes. - * - * @return array - */ - public function indexNames($tableName) { - return $this->query(sprintf('SELECT ind.name FROM sys.indexes ind - INNER JOIN sys.tables t ON ind.object_id = t.object_id - WHERE is_primary_key = 0 AND t.name = \'%s\'', $tableName))->column(); - } - - /** - * Returns a list of all the tables in the database. - * Table names will all be in lowercase. - * @return array - */ - public function tableList() { - $tables = array(); - foreach($this->query("EXEC sp_tables @table_owner = 'dbo';") as $record) { - $tables[strtolower($record['TABLE_NAME'])] = $record['TABLE_NAME']; - } - return $tables; - } - - /** - * Empty the given table of all contents. - */ public function clearTable($table) { $this->query("TRUNCATE TABLE \"$table\""); } - /** - * Return the number of rows affected by the previous operation. - * @return int - */ - public function affectedRows() { - if($this->mssql) { - return mssql_rows_affected($this->dbConn); - } else { - return $this->lastAffectedRows; - } - } - - /** - * Return a boolean type-formatted string - * We use 'bit' so that we can do numeric-based comparisons - * - * @params array $values Contains a tokenised list of info about this data type - * @return string - */ - public function boolean($values) { - $default = ($values['default']) ? '1' : '0'; - return 'bit not null default ' . $default; - } - - /** - * Return a date type-formatted string. - * - * @params array $values Contains a tokenised list of info about this data type - * @return string - */ - public function date($values) { - return 'date null'; - } - - /** - * Return a decimal type-formatted string - * - * @params array $values Contains a tokenised list of info about this data type - * @return string - */ - public function decimal($values) { - // Avoid empty strings being put in the db - if($values['precision'] == '') { - $precision = 1; - } else { - $precision = $values['precision']; - } - - $defaultValue = '0'; - if(isset($values['default']) && is_numeric($values['default'])) { - $defaultValue = $values['default']; - } - - return 'decimal(' . $precision . ') not null default ' . $defaultValue; - } - - /** - * Return a enum type-formatted string - * - * @params array $values Contains a tokenised list of info about this data type - * @return string - */ - public function enum($values) { - // Enums are a bit different. We'll be creating a varchar(255) with a constraint of all the - // usual enum options. - // NOTE: In this one instance, we are including the table name in the values array - - $maxLength = max(array_map('strlen', $values['enums'])); - - return "varchar($maxLength) not null default '" . $values['default'] - . "' check(\"" . $values['name'] . "\" in ('" . implode("','", $values['enums']) - . "'))"; - } - - /** - * @todo Make this work like {@link MySQLDatabase::set()} - */ - public function set($values) { - return $this->enum($values); - } - - /** - * Return a float type-formatted string. - * - * @params array $values Contains a tokenised list of info about this data type - * @return string - */ - public function float($values) { - return 'float(53) not null default ' . $values['default']; - } - - /** - * Return a int type-formatted string - * - * @params array $values Contains a tokenised list of info about this data type - * @return string - */ - public function int($values) { - //We'll be using an 8 digit precision to keep it in line with the serial8 datatype for ID columns - return 'numeric(8) not null default ' . (int) $values['default']; - } - - /** - * Return a datetime type-formatted string - * For MS SQL, we simply return the word 'timestamp', no other parameters are necessary - * - * @params array $values Contains a tokenised list of info about this data type - * @return string - */ - public function ss_datetime($values) { - return 'datetime null'; - } - - /** - * Return a text type-formatted string - * - * @params array $values Contains a tokenised list of info about this data type - * @return string - */ - public function text($values) { - $collation = self::$collation ? " COLLATE " . self::$collation : ""; - return "nvarchar(max)$collation null"; - } - - /** - * Return a time type-formatted string. - * - * @params array $values Contains a tokenised list of info about this data type - * @return string - */ - public function time($values){ - return 'time null'; - } - - /** - * Return a varchar type-formatted string - * - * @params array $values Contains a tokenised list of info about this data type - * @return string - */ - public function varchar($values) { - $collation = self::$collation ? " COLLATE " . self::$collation : ""; - return "nvarchar(" . $values['precision'] . ")$collation null"; - } - - /** - * Return a 4 digit numeric type. - * @return string - */ - public function year($values) { - return 'numeric(4)'; - } - - /** - * This returns the column which is the primary key for each table - * In Postgres, it is a SERIAL8, which is the equivalent of an auto_increment - * - * @return string - */ - function IdColumn($asDbValue=false, $hasAutoIncPK=true){ - if($asDbValue) - return 'bigint not null'; - else { - if($hasAutoIncPK) - return 'bigint identity(1,1)'; - else return 'bigint not null'; - } - } - - /** - * Returns the SQL command to get all the tables in this database - */ - function allTablesSQL(){ - return "SELECT \"name\" FROM \"sys\".\"tables\";"; - } - - /** - * Returns true if this table exists - * @todo Make a proper implementation - */ - function hasTable($tableName) { - $SQL_tableName = Convert::raw2sql($tableName); - $value = DB::query("SELECT table_name FROM information_schema.tables WHERE table_name = '$SQL_tableName'")->value(); - return (bool)$value; - } - - /** - * Returns the values of the given enum field - * NOTE: Experimental; introduced for db-abstraction and may changed before 2.4 is released. - */ - public function enumValuesForField($tableName, $fieldName) { - $classes = array(); - - // Get the enum of all page types from the SiteTree table - $clause = $this->getConstraintCheckClause($tableName, $fieldName); - if($clause) { - $classes = $this->enumValuesFromCheckClause($clause); - } - - return $classes; - } - /** * SQL Server uses CURRENT_TIMESTAMP for the current date/time. */ @@ -1274,136 +171,6 @@ class MSSQLDatabase extends SS_Database { return 'RAND()'; } - /** - * This is a lookup table for data types. - * - * For instance, MSSQL uses 'BIGINT', while MySQL uses 'UNSIGNED' - * and PostgreSQL uses 'INT'. - */ - function dbDataType($type){ - $values = array( - 'unsigned integer'=>'BIGINT' - ); - if(isset($values[$type])) return $values[$type]; - else return ''; - } - - /** - * Convert a SQLQuery object into a SQL statement. - * - * Needs to be overloaded from {@link Database} because MSSQL has - * a very specific way of limiting results from a query. - * - * @param SQLQuery - * @return string SQL text - */ - public function sqlQueryToString(SQLQuery $query) { - // get the limit and offset - $limit = ''; - $offset = '0'; - $text = ''; - $suffixText = ''; - $nestedQuery = false; - - if(is_array($query->getLimit())) { - $limitArr = $query->getLimit(); - if(isset($limitArr['limit'])) $limit = $limitArr['limit']; - if(isset($limitArr['start'])) $offset = $limitArr['start']; - } else if(preg_match('/^([0-9]+) offset ([0-9]+)$/i', trim($query->getLimit()), $matches)) { - $limit = $matches[1]; - $offset = $matches[2]; - } else { - //could be a comma delimited string - $bits = explode(',', $query->getLimit()); - if(sizeof($bits) > 1) { - list($offset, $limit) = $bits; - } else { - $limit = $bits[0]; - } - } - - // DELETE queries - if($query->getDelete()) { - $text = 'DELETE '; - } else { - $distinct = $query->getDistinct() ? 'DISTINCT ' : ''; - // If there's a limit but no offset, just use 'TOP X' - // rather than the more complex sub-select method - if ($limit != 0 && $offset == 0) { - $text = "SELECT $distinct TOP $limit"; - - // If there's a limit and an offset, then we need to do a subselect - } else if($limit && $offset) { - $orderby = $query->getOrderBy(); - - // workaround for subselect not working with alias functions - // just use the function directly in the order by instead of the alias - $selects = $query->getSelect(); - foreach($orderby as $field => $dir) { - if(preg_match('/_SortColumn/', $field)) { - unset($orderby[$field]); - $orderby[$selects[str_replace('"', '', $field)]] = $dir; - } - } - - if($orderby) { - $orderByClause = $this->sqlOrderByToString($orderby); - $rowNumber = "ROW_NUMBER() OVER ($orderByClause) AS Number"; - } else { - $selects = $query->getSelect(); - $firstCol = reset($selects); - $rowNumber = "ROW_NUMBER() OVER (ORDER BY $firstCol) AS Number"; - } - $text = "SELECT * FROM (SELECT $distinct$rowNumber, "; - $suffixText .= ") AS Numbered WHERE Number BETWEEN " . ($offset+1) ." AND " . ($offset+$limit) - . " ORDER BY Number"; - $nestedQuery = true; - - // Otherwise a simple query - } else { - $text = "SELECT $distinct"; - } - - // Now add the columns to be selected - // strip off the SELECT text as it gets done above instead - $text .= trim(str_replace('SELECT' , '', $this->sqlSelectToString($query->getSelect()))); - } - - if($query->getFrom()) $text .= $this->sqlFromToString($query->getFrom()); - if($query->getWhere()) $text .= $this->sqlWhereToString($query->getWhere(), $query->getConnective()); - - // these clauses only make sense in SELECT queries, not DELETE - if(!$query->getDelete()) { - if($query->getGroupBy()) $text .= $this->sqlGroupByToString($query->getGroupBy()); - if($query->getHaving()) $text .= $this->sqlHavingToString($query->getHaving()); - if($query->getOrderBy() && !$nestedQuery) $text .= $this->sqlOrderByToString($query->getOrderBy()); - } - - // $suffixText is used by the nested queries to create an offset limit - if($suffixText) $text .= $suffixText; - - return $text; - } - - /** - * Escapes a value with specific escape characters specific to the MSSQL. - * @param string $value String to escape - * @return string Escaped string - */ - function addslashes($value){ - $value=str_replace("'","''",$value); - $value=str_replace("\0","[NULL]",$value); - - return $value; - } - - /** - * This changes the index name depending on database requirements. - */ - function modifyIndex($index) { - return $index; - } - /** * The core search engine configuration. * Picks up the fulltext-indexed tables from the database and executes search on all of them. @@ -1422,28 +189,31 @@ class MSSQLDatabase extends SS_Database { $allClassesToSearch = array(); foreach ($classesToSearch as $class) { - $allClassesToSearch = array_merge($allClassesToSearch, ClassInfo::dataClassesFor($class)); + $allClassesToSearch = array_merge($allClassesToSearch, array_values(ClassInfo::dataClassesFor($class))); } $allClassesToSearch = array_unique($allClassesToSearch); //Get a list of all the tables and columns we'll be searching on: - $fulltextColumns = DB::query('EXEC sp_help_fulltext_columns'); + $fulltextColumns = $this->query('EXEC sp_help_fulltext_columns'); $queries = array(); // Sort the columns back into tables. $tables = array(); foreach($fulltextColumns as $column) { // Skip extension tables. - if(substr($column['TABLE_NAME'], -5)=='_Live' || substr($column['TABLE_NAME'], -9)=='_versions') continue; + if(substr($column['TABLE_NAME'], -5) == '_Live' || substr($column['TABLE_NAME'], -9) == '_versions') continue; // Add the column to table. $table = &$tables[$column['TABLE_NAME']]; - if (!$table) $table = array($column['FULLTEXT_COLUMN_NAME']); - else array_push($table, $column['FULLTEXT_COLUMN_NAME']); + if (!$table) { + $table = array($column['FULLTEXT_COLUMN_NAME']); + } else { + array_push($table, $column['FULLTEXT_COLUMN_NAME']); + } } // Create one query per each table, $columns not used. We want just the ID and the ClassName of the object from this query. - foreach($tables as $tableName=>$columns){ + foreach($tables as $tableName => $columns){ $baseClass = ClassInfo::baseDataClass($tableName); $join = $this->fullTextSearchMSSQL($tableName, $keywords); @@ -1479,23 +249,28 @@ class MSSQLDatabase extends SS_Database { $queries[$tableName]->addWhere($extraFilter); } if (count($allClassesToSearch)) { - $queries[$tableName]->addWhere("\"$baseClass\".\"ClassName\" IN ('".implode($allClassesToSearch, "', '")."')"); + $classesPlaceholder = DB::placeholders($allClassesToSearch); + $queries[$tableName]->addWhere(array( + "\"$baseClass\".\"ClassName\" IN ($classesPlaceholder)" => + $allClassesToSearch + )); } // Reset the parameters that would get in the way - } // Generate SQL $querySQLs = array(); + $queryParameters = array(); foreach($queries as $query) { - $querySQLs[] = $query->sql(); + $querySQLs[] = $query->sql($parameters); + $queryParameters = array_merge($queryParameters, $parameters); } // Unite the SQL $fullQuery = implode(" UNION ", $querySQLs) . " ORDER BY $sortBy"; // Perform the search - $result = DB::query($fullQuery); + $result = $this->preparedQuery($fullQuery, $queryParameters); // Regenerate DataObjectSet - watch out, numRecords doesn't work on sqlsrv driver on Windows. $current = -1; @@ -1515,16 +290,13 @@ class MSSQLDatabase extends SS_Database { $list->setPageStart($start); $list->setPageLength($pageLength); $list->setTotalItems($current+1); - - // The list has already been limited by the query above - $list->setLimitItems(false); - return $list; } /** * Allow auto-increment primary key editing on the given table. * Some databases need to enable this specially. + * * @param $table The name of the table to have PK editing allowed on * @param $allow True to start, false to finish */ @@ -1532,21 +304,6 @@ class MSSQLDatabase extends SS_Database { $this->query("SET IDENTITY_INSERT \"$table\" " . ($allow ? "ON" : "OFF")); } - /** - * Check if a fulltext index exists on a particular table name. - * @return boolean TRUE index exists | FALSE index does not exist | NULL no support - */ - function fulltextIndexExists($tableName) { - // Special case for no full text index support - if(!$this->fullTextEnabled()) return null; - - return (bool) $this->query(" - SELECT 1 FROM sys.fulltext_indexes i - JOIN sys.objects o ON i.object_id = o.object_id - WHERE o.name = '$tableName' - ")->value(); - } - /** * Returns a SQL fragment for querying a fulltext search index * @@ -1556,30 +313,26 @@ class MSSQLDatabase extends SS_Database { * * @returns null if keyword set is empty or the string with JOIN clause to be added to SQL query */ - function fullTextSearchMSSQL($tableName, $keywords, $fields = null) { + public function fullTextSearchMSSQL($tableName, $keywords, $fields = null) { // Make sure we are getting an array of fields if (isset($fields) && !is_array($fields)) $fields = array($fields); // Strip unfriendly characters, SQLServer "CONTAINS" predicate will crash on & and | and ignore others anyway. if (function_exists('mb_ereg_replace')) { $keywords = mb_ereg_replace('[^\w\s]', '', trim($keywords)); - } - else { - $keywords = Convert::raw2sql(str_replace(array('&','|','!','"','\''), '', trim($keywords))); + } else { + $keywords = $this->escapeString(str_replace(array('&','|','!','"','\''), '', trim($keywords))); } // Remove stopwords, concat with ANDs - $keywords = explode(' ', $keywords); - $keywords = self::removeStopwords($keywords); - + $keywordList = explode(' ', $keywords); + $keywordList = $this->removeStopwords($keywordList); + // remove any empty values from the array - foreach($keywords as $i => $word) { - if(!strlen($word)) unset($keywords[$i]); - } - - $keywords = implode(' AND ', $keywords); - if (!$keywords || trim($keywords)=='') return null; + $keywordList = array_filter($keywordList); + if(empty($keywordList)) return null; + $keywords = implode(' AND ', $keywordList); if ($fields) $fieldNames = '"' . implode('", "', $fields) . '"'; else $fieldNames = "*"; @@ -1593,7 +346,7 @@ class MSSQLDatabase extends SS_Database { * * @return array $keywords with stopwords removed */ - static public function removeStopwords($keywords) { + public function removeStopwords($keywords) { $goodKeywords = array(); foreach($keywords as $keyword) { if (in_array($keyword, self::$noiseWords)) continue; @@ -1612,8 +365,13 @@ class MSSQLDatabase extends SS_Database { /** * This is a quick lookup to discover if the database supports particular extensions * Currently, MSSQL supports no extensions + * + * @param array $extensions List of extensions to check for support of. The key of this array + * will be an extension name, and the value the configuration for that extension. This + * could be one of partitions, tablespaces, or clustering + * @return boolean Flag indicating support for all of the above */ - public function supportsExtensions($extensions=Array('partitions', 'tablespaces', 'clustering')){ + public function supportsExtensions($extensions = array('partitions', 'tablespaces', 'clustering')){ if(isset($extensions['partitions'])) return false; elseif(isset($extensions['tablespaces'])) @@ -1627,75 +385,37 @@ class MSSQLDatabase extends SS_Database { /** * Start transaction. READ ONLY not supported. */ - public function transactionStart($transaction_mode=false, $session_characteristics=false){ - if($this->mssql) { - DB::query('BEGIN TRANSACTION'); + public function transactionStart($transactionMode = false, $sessionCharacteristics = false){ + if($this->connector instanceof SQLServerConnector) { + $this->connector->transactionStart(); } else { - $result = sqlsrv_begin_transaction($this->dbConn); - if (!$result) $this->databaseError("Couldn't start the transaction.", E_USER_ERROR); + $this->query('BEGIN TRANSACTION'); } } - /** - * Create a savepoint that you can jump back to if you encounter problems - */ public function transactionSavepoint($savepoint){ - DB::query("SAVE TRANSACTION \"$savepoint\""); + $this->query("SAVE TRANSACTION \"$savepoint\""); } - /** - * Rollback or revert to a savepoint if your queries encounter problems - * If you encounter a problem at any point during a transaction, you may - * need to rollback that particular query, or return to a savepoint - */ - public function transactionRollback($savepoint=false){ + public function transactionRollback($savepoint = false) { if($savepoint) { - DB::query("ROLLBACK TRANSACTION \"$savepoint\""); + $this->query("ROLLBACK TRANSACTION \"$savepoint\""); + } elseif($this->connector instanceof SQLServerConnector) { + $this->connector->transactionRollback(); } else { - if($this->mssql) { - DB::query('ROLLBACK TRANSACTION'); - } else { - $result = sqlsrv_rollback($this->dbConn); - if (!$result) $this->databaseError("Couldn't rollback the transaction.", E_USER_ERROR); - } + $this->query('ROLLBACK TRANSACTION'); } } - /** - * Commit everything inside this transaction so far - */ - public function transactionEnd(){ - if($this->mssql) { - DB::query('COMMIT TRANSACTION'); + public function transactionEnd($chain = false) { + if($this->connector instanceof SQLServerConnector) { + $this->connector->transactionEnd(); } else { - $result = sqlsrv_commit($this->dbConn); - if (!$result) $this->databaseError("Couldn't commit the transaction.", E_USER_ERROR); + $this->query('COMMIT TRANSACTION'); } } - - /** - * Overload the Database::prepStringForDB() method and include "N" prefix so unicode - * strings are saved to the database correctly. - * - * @param string $string String to be encoded - * @return string Processed string ready for DB - */ - public function prepStringForDB($string) { - return "N'" . Convert::raw2sql($string) . "'"; - } - - /** - * Generate a WHERE clause for text matching. - * - * @param String $field Quoted field name - * @param String $value Escaped search. Can include percentage wildcards. - * @param boolean $exact Exact matches or wildcard support. - * @param boolean $negate Negate the clause. - * @param boolean $caseSensitive Enforce case sensitivity if TRUE or FALSE. - * Stick with default collation if set to NULL. - * @return String SQL - */ - public function comparisonClause($field, $value, $exact = false, $negate = false, $caseSensitive = null) { + + public function comparisonClause($field, $value, $exact = false, $negate = false, $caseSensitive = null, $parameterised = false) { if($exact) { $comp = ($negate) ? '!=' : '='; } else { @@ -1707,22 +427,22 @@ class MSSQLDatabase extends SS_Database { // change used collation for case sensitive searches. $collateClause = ''; if($caseSensitive === true) { - if(self::$collation) { - $collation = preg_replace('/_CI_/', '_CS_', self::$collation); + if(self::get_collation()) { + $collation = preg_replace('/_CI_/', '_CS_', self::get_collation()); } else { $collation = 'Latin1_General_CS_AS'; } $collateClause = ' COLLATE ' . $collation; } elseif($caseSensitive === false) { - if(self::$collation) { - $collation = preg_replace('/_CS_/', '_CI_', self::$collation); + if(self::get_collation()) { + $collation = preg_replace('/_CS_/', '_CI_', self::get_collation()); } else { $collation = 'Latin1_General_CI_AS'; } $collateClause = ' COLLATE ' . $collation; } - $clause = sprintf("%s %s '%s'", $field, $comp, $value); + $clause = sprintf("%s %s %s", $field, $comp, $parameterised ? '?' : "'$value'"); if($collateClause) $clause .= $collateClause; return $clause; @@ -1731,6 +451,7 @@ class MSSQLDatabase extends SS_Database { /** * Function to return an SQL datetime expression for MSSQL * used for querying a datetime in a certain format + * * @param string $date to be formated, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"' * @param string $format to be used, supported specifiers: * %Y = Year (four digits) @@ -1792,6 +513,7 @@ class MSSQLDatabase extends SS_Database { /** * Function to return an SQL datetime expression for MSSQL. * used for querying a datetime addition + * * @param string $date, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"' * @param string $interval to be added, use the format [sign][integer] [qualifier], e.g. -1 Day, +15 minutes, +1 YEAR * supported qualifiers: @@ -1833,6 +555,7 @@ class MSSQLDatabase extends SS_Database { /** * Function to return an SQL datetime expression for MSSQL. * used for querying a datetime substraction + * * @param string $date1, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"' * @param string $date2 to be substracted of $date1, can be either 'now', literal datetime like '1973-10-14 10:30:00' or field name, e.g. '"SiteTree"."Created"' * @return string SQL datetime expression to query for the interval between $date1 and $date2 in seconds which is the result of the substraction @@ -1854,134 +577,3 @@ class MSSQLDatabase extends SS_Database { return "DATEDIFF(s, $date2, $date1)"; } } - -/** - * A result-set from a MSSQL database. - * @package sapphire - * @subpackage model - */ -class MSSQLQuery extends SS_Query { - - /** - * The MSSQLDatabase object that created this result set. - * @var MSSQLDatabase - */ - private $database; - - /** - * The internal MSSQL handle that points to the result set. - * @var resource - */ - private $handle; - - /** - * If true, use the mssql_... functions. - * If false use the sqlsrv_... functions - */ - private $mssql = null; - - /** - * A list of field meta-data, such as column name and data type. - * @var array - */ - private $fields = array(); - - /** - * Hook the result-set given into a Query class, suitable for use by sapphire. - * @param database The database object that created this query. - * @param handle the internal mssql handle that is points to the resultset. - */ - public function __construct(MSSQLDatabase $database, $handle, $mssql) { - $this->database = $database; - $this->handle = $handle; - $this->mssql = $mssql; - - // build a list of field meta-data for this query we'll use in nextRecord() - // doing it here once saves us from calling mssql_fetch_field() in nextRecord() - // potentially hundreds of times, which is unnecessary. - if($this->mssql && is_resource($this->handle)) { - for($i = 0; $i < mssql_num_fields($handle); $i++) { - $this->fields[$i] = mssql_fetch_field($handle, $i); - } - } - } - - public function __destruct() { - if(is_resource($this->handle)) { - if($this->mssql) { - mssql_free_result($this->handle); - } else { - sqlsrv_free_stmt($this->handle); - } - } - } - - public function seek($row) { - if(!is_resource($this->handle)) return false; - - if($this->mssql) { - return mssql_data_seek($this->handle, $row); - } else { - user_error('MSSQLQuery::seek() not supported in sqlsrv', E_USER_WARNING); - } - } - - public function numRecords() { - if(!is_resource($this->handle)) return false; - - if($this->mssql) { - return mssql_num_rows($this->handle); - } else { - // WARNING: This will only work if the cursor type is scrollable! - if(function_exists('sqlsrv_num_rows')) { - return sqlsrv_num_rows($this->handle); - } else { - user_error('MSSQLQuery::numRecords() not supported in this version of sqlsrv', E_USER_WARNING); - } - } - } - - public function nextRecord() { - if(!is_resource($this->handle)) return false; - - if($this->mssql) { - if($row = mssql_fetch_row($this->handle)) { - foreach($row as $i => $value) { - $field = $this->fields[$i]; - - // fix datetime formatting from format "Jan 1 2012 12:00:00:000AM" to "2012-01-01 12:00:00" - // strtotime doesn't understand this format, so we need to do some modification of the value first - if($field->type == 'datetime' && $value) { - $value = date('Y-m-d H:i:s', strtotime(preg_replace('/:[0-9][0-9][0-9]([ap]m)$/i', ' \\1', $value))); - } - - if(isset($value) || !isset($data[$field->name])) { - $data[$field->name] = $value; - } - } - return $data; - } else { - // free the handle, no more results - mssql_free_result($this->handle); - $this->handle = null; - } - } else { - if($data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC)) { - - // special case for sqlsrv - date values are DateTime coming out of the sqlsrv drivers, - // so we convert to the usual Y-m-d H:i:s value! - foreach($data as $name => $value) { - if($value instanceof DateTime) $data[$name] = $value->format('Y-m-d H:i:s'); - } - return $data; - } else { - // Free the handle if there are no more results - sqlsrv crashes if there are too many handles - sqlsrv_free_stmt($this->handle); - $this->handle = null; - } - } - - return false; - } - -} diff --git a/code/MSSQLDatabaseConfigurationHelper.php b/code/MSSQLDatabaseConfigurationHelper.php index 7812874..ec5f0fb 100644 --- a/code/MSSQLDatabaseConfigurationHelper.php +++ b/code/MSSQLDatabaseConfigurationHelper.php @@ -1,4 +1,5 @@ true, 'error' => 'details of error') + * Create a connection of the appropriate type + * + * @param array $databaseConfig + * @param string $error Error message passed by value + * @return mixed|null Either the connection object, or null if error */ - public function requireDatabaseServer($databaseConfig) { - $success = false; - $error = ''; - - if(function_exists('mssql_connect')) { - $conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true); + protected function createConnection($databaseConfig, &$error) { + $error = null; + try { + switch($databaseConfig['type']) { + case 'MSSQLDatabase': + case 'MSSQLAzureDatabase': + $parameters = array( + 'UID' => $databaseConfig['username'], + 'PWD' => $databaseConfig['password'] + ); + + // Azure has additional parameter requirements + if($this->isAzure($databaseConfig)) { + $parameters['database'] = $databaseConfig['database']; + $parameters['multipleactiveresultsets'] = 0; + } + $conn = @sqlsrv_connect($databaseConfig['server'], $parameters); + if($conn) return $conn; + + // Get error + if($errors = sqlsrv_errors()) { + $error = ''; + foreach($errors as $detail) { + $error .= "{$detail['message']}\n"; + } + } else { + $error = 'Unknown connection error'; + } + return null; + case 'MSSQLPDODatabase': + // May throw a PDOException if fails + $conn = @new PDO('sqlsrv:Server='.$databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']); + if($conn) { + return $conn; + } else { + $error = 'Unknown connection error'; + return null; + } + default: + $error = 'Invalid connection type'; + return null; + } + } catch(Exception $ex) { + $error = $ex->getMessage(); + return null; + } + } + + /** + * Helper function to quote a string value + * + * @param mixed $conn Connection object/resource + * @param string $value Value to quote + * @return string Quoted strieng + */ + protected function quote($conn, $value) { + if($conn instanceof PDO) { + return $conn->quote($value); + } elseif(is_resource($conn)) { + $value = str_replace("'", "''", $value); + $value = str_replace("\0", "[NULL]", $value); + return "N'$value'"; } else { - $conn = @sqlsrv_connect($databaseConfig['server'], array( - 'UID' => $databaseConfig['username'], - 'PWD' => $databaseConfig['password'] - )); - - $errors = sqlsrv_errors(); - if($errors) { - $error .= "\n"; - foreach($errors as $detail) { - $error .= "\n" . @$detail['message'] . "\n"; - } + user_error('Invalid database connection', E_USER_ERROR); + } + } + + /** + * Helper function to execute a query + * + * @param mixed $conn Connection object/resource + * @param string $sql SQL string to execute + * @return array List of first value from each resulting row + */ + protected function query($conn, $sql) { + $items = array(); + if($conn instanceof PDO) { + $result = $conn->query($sql); + if($result) foreach($result as $row) { + $items[] = $row[0]; + } + } elseif(is_resource($conn)) { + $result = sqlsrv_query($conn, $sql); + if($result) while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC)) { + $items[] = $row[0]; } } - - if($conn) { - $success = true; - } else { - $success = false; - if(!$error) $error = 'SQL Server requires a valid username and password to determine if the server exists.'; - } + return $items; + } + + public function requireDatabaseFunctions($databaseConfig) { + $data = DatabaseAdapterRegistry::get_adapter($databaseConfig['type']); + return !empty($data['supported']); + } + + public function requireDatabaseServer($databaseConfig) { + $conn = $this->createConnection($databaseConfig, $error); + $success = !empty($conn); return array( 'success' => $success, @@ -59,37 +126,9 @@ class MSSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper { ); } - /** - * Ensure a database connection is possible using credentials provided. - * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc - * @return array Result - e.g. array('success' => true, 'error' => 'details of error') - */ public function requireDatabaseConnection($databaseConfig) { - $success = false; - $error = ''; - - if(function_exists('mssql_connect')) { - $conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true); - } else { - $conn = @sqlsrv_connect($databaseConfig['server'], array( - 'UID' => $databaseConfig['username'], - 'PWD' => $databaseConfig['password'] - )); - - $errors = sqlsrv_errors(); - if($errors) { - $error .= "\n"; - foreach($errors as $detail) { - $error .= "\n" . @$detail['message'] . "\n"; - } - } - } - - if($conn) { - $success = true; - } else { - $success = false; - } + $conn = $this->createConnection($databaseConfig, $error); + $success = !empty($conn); return array( 'success' => $success, @@ -99,31 +138,14 @@ class MSSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper { } public function getDatabaseVersion($databaseConfig) { - $version = 0; - - // Get the version using SERVERPROPERTY() function - if(function_exists('mssql_connect')) { - $conn = @mssql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password'], true); - $result = @mssql_query("SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))", $conn); - $row = @mssql_fetch_array($result); - } else { - $conn = @sqlsrv_connect($databaseConfig['server'], array( - 'UID' => $databaseConfig['username'], - 'PWD' => $databaseConfig['password'] - )); - $result = @sqlsrv_query($conn, "SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))"); - $row = @sqlsrv_fetch_array($result); - } - - if($row && isset($row[0])) { - $version = trim($row[0]); - } - - return $version; + $conn = $this->createConnection($databaseConfig, $error); + $result = $this->query($conn, "SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))"); + return empty($result) ? 0 : reset($result); } /** * Ensure that the SQL Server version is at least 10.00.2531 (SQL Server 2008 SP1). + * * @see http://www.sqlteam.com/article/sql-server-versions * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc * @return array Result - e.g. array('success' => true, 'error' => 'details of error') @@ -148,37 +170,24 @@ class MSSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper { ); } - /** - * Ensure that the database connection is able to use an existing database, - * or be able to create one if it doesn't exist. - * - * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc - * @return array Result - e.g. array('success' => true, 'alreadyExists' => 'true') - */ public function requireDatabaseOrCreatePermissions($databaseConfig) { - $success = false; - $alreadyExists = false; - - $check = $this->requireDatabaseConnection($databaseConfig); - $conn = $check['connection']; - - if(function_exists('mssql_select_db')) { - $exists = @mssql_select_db($databaseConfig['database'], $conn); - } else { - $exists = @sqlsrv_query($conn, "USE \"$databaseConfig[database]\""); - } - - if($exists) { + $conn = $this->createConnection($databaseConfig, $error); + if(empty($conn)) { + $success = false; + $alreadyExists = false; + } elseif($databaseConfig['type'] == 'MSSQLAzureDatabase') { + // Don't bother with DB selection for azure, as it's not supported $success = true; $alreadyExists = true; } else { - if(function_exists('mssql_query') && mssql_query("CREATE DATABASE testing123", $conn)) { - mssql_query("DROP DATABASE testing123", $conn); - $success = true; - $alreadyExists = false; - } elseif(function_exists('sqlsrv_query') && @sqlsrv_query($conn, "CREATE DATABASE testing123")) { - sqlsrv_query($conn, "DROP DATABASE testing123"); + // does this database exist already? + $list = $this->query($conn, 'SELECT NAME FROM sys.sysdatabases'); + if(in_array($databaseConfig['database'], $list)) { $success = true; + $alreadyExists = true; + } else { + $permissions = $this->query($conn, "select COUNT(*) from sys.fn_my_permissions('','') where permission_name like 'CREATE ANY DATABASE' or permission_name like 'CREATE DATABASE';"); + $success = $permissions[0] > 0; $alreadyExists = false; } } @@ -188,16 +197,22 @@ class MSSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper { 'alreadyExists' => $alreadyExists ); } - - /** - * Ensure we have permissions to alter tables. - * - * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc - * @return array Result - e.g. array('okay' => true, 'applies' => true), where applies is whether - * the test is relevant for the database - */ - public function requireDatabaseAlterPermissions($databaseConfig) { - return array('success' => true, 'applies' => false); - } + public function requireDatabaseAlterPermissions($databaseConfig) { + $success = false; + $conn = $this->createConnection($databaseConfig, $error); + if(!empty($conn)) { + if(!$this->isAzure($databaseConfig)) { + // Make sure to select the current database when checking permission against this database + $this->query($conn, "USE \"{$databaseConfig['database']}\""); + } + $permissions = $this->query($conn, "select COUNT(*) from sys.fn_my_permissions(NULL,'DATABASE') WHERE permission_name like 'create table';"); + $success = $permissions[0] > 0; + } + + return array( + 'success' => $success, + 'applies' => true + ); + } } diff --git a/code/MSSQLQueryBuilder.php b/code/MSSQLQueryBuilder.php new file mode 100644 index 0000000..d42b944 --- /dev/null +++ b/code/MSSQLQueryBuilder.php @@ -0,0 +1,112 @@ +parseLimit($query); + + // If not using ofset then query generation is quite straightforward + if(empty($offset)) { + $sql = parent::buildSelectQuery($query, $parameters); + // Inject limit into SELECT fragment + if(!empty($limit)) { + $sql = preg_replace('/^(SELECT (DISTINCT)?)/i', '${1} TOP '.$limit, $sql); + } + return $sql; + } + + // When using offset we must use a subselect + // @see http://stackoverflow.com/questions/2135418/equivalent-of-limit-and-offset-for-sql-server + $orderby = $query->getOrderBy(); + + // workaround for subselect not working with alias functions + // just use the function directly in the order by instead of the alias + $selects = $query->getSelect(); + foreach($orderby as $field => $dir) { + if(preg_match('/_SortColumn/', $field)) { + unset($orderby[$field]); + $orderby[$selects[str_replace('"', '', $field)]] = $dir; + } + } + + // Create order expression, using the first column if none explicitly specified + if($orderby) { + // Simple implementation of buildOrderByFragment + $statements = array(); + foreach ($orderby as $clause => $dir) { + $statements[] = trim("$clause $dir"); + } + $orderByClause = "ORDER BY " . implode(', ', $statements); + } else { + $selects = $query->getSelect(); + $firstCol = reset($selects); + $orderByClause = "ORDER BY $firstCol"; + } + + // Build main query SQL + $sql = parent::buildSelectQuery($query, $parameters); + + // Inject row number into selection + $sql = preg_replace('/^(SELECT (DISTINCT)?)/i', '${1} ROW_NUMBER() OVER ('.$orderByClause.') AS Number, ', $sql); + + // Sub-query this SQL + if(empty($limit)) { + $limitCondition = "Number > ?"; + $parameters[] = $offset; + } else { + $limitCondition = "Number BETWEEN ? AND ?"; + $parameters[] = $offset + 1; + $parameters[] = $offset + $limit; + } + return "SELECT * FROM ($sql) AS Numbered WHERE $limitCondition ORDER BY Number"; + } + + public function buildLimitFragment(SQLSelect $query, array &$parameters) { + // Limit is handled at the buildSelectQuery level + return ''; + } + + public function buildOrderByFragment(SQLSelect $query, array &$parameters) { + // If doing a limit/offset at the same time then don't build the orde by fragment here + list($offset, $limit) = $this->parseLimit($query); + if(empty($offset) || empty($limit)) { + return parent::buildOrderByFragment($query, $parameters); + } + return ''; + } + + /** + * Extracts the limit and offset from the limit clause + * + * @param SQLSelect $query + * @return array Two item array with $limit and $offset as values + * @throws InvalidArgumentException + */ + protected function parseLimit(SQLSelect $query) { + + $limit = ''; + $offset = '0'; + if(is_array($query->getLimit())) { + $limitArr = $query->getLimit(); + if(isset($limitArr['limit'])) $limit = $limitArr['limit']; + if(isset($limitArr['start'])) $offset = $limitArr['start']; + } else if(preg_match('/^([0-9]+) offset ([0-9]+)$/i', trim($query->getLimit()), $matches)) { + $limit = $matches[1]; + $offset = $matches[2]; + } else { + //could be a comma delimited string + $bits = explode(',', $query->getLimit()); + if(sizeof($bits) > 1) { + list($offset, $limit) = $bits; + } else { + $limit = $bits[0]; + } + } + return array($limit, $offset); + } +} diff --git a/code/MSSQLSchemaManager.php b/code/MSSQLSchemaManager.php new file mode 100644 index 0000000..d1c5496 --- /dev/null +++ b/code/MSSQLSchemaManager.php @@ -0,0 +1,848 @@ +fullTextCatalogExists(); + if(!$result) $this->query("CREATE FULLTEXT CATALOG \"$name\" AS DEFAULT;"); + } + + /** + * Check that a fulltext catalog has been created yet. + * + * @param string $name Name of full text catalog to use + * @return boolean + */ + public function fullTextCatalogExists($name = 'ftCatalog') { + return (bool) $this->preparedQuery( + "SELECT name FROM sys.fulltext_catalogs WHERE name = ?;", + array($name) + )->value(); + } + + /** + * Sleep until the catalog has been fully rebuilt. This is a busy wait designed for situations + * when you need to be sure the index is up to date - for example in unit tests. + * + * TODO: move this to Database class? Can we assume this will be useful for all databases? + * Also see the wrapper functions "waitUntilIndexingFinished" in SearchFormTest and TranslatableSearchFormTest + * + * @param int $maxWaitingTime Time in seconds to wait for the database. + */ + function waitUntilIndexingFinished($maxWaitingTime = 15) { + if(!$this->database->fullTextEnabled()) return; + + $this->query("EXEC sp_fulltext_catalog 'ftCatalog', 'Rebuild';"); + + // Busy wait until it's done updating, but no longer than 15 seconds. + $start = time(); + while(time() - $start < $maxWaitingTime) { + $status = $this->query("EXEC sp_help_fulltext_catalogs 'ftCatalog';")->first(); + + if (isset($status['STATUS']) && $status['STATUS'] == 0) { + // Idle! + break; + } + sleep(1); + } + } + + /** + * Check if a fulltext index exists on a particular table name. + * + * @param string $tableName + * @return boolean TRUE index exists | FALSE index does not exist | NULL no support + */ + function fulltextIndexExists($tableName) { + // Special case for no full text index support + if(!$this->database->fullTextEnabled()) return null; + + return (bool) $this->preparedQuery(" + SELECT 1 FROM sys.fulltext_indexes i + JOIN sys.objects o ON i.object_id = o.object_id + WHERE o.name = ?", + array($tableName) + )->value(); + } + + /** + * MSSQL stores the primary key column with an internal identifier, + * so a lookup needs to be done to determine it. + * + * @param string $tableName Name of table with primary key column "ID" + * @return string Internal identifier for primary key + */ + function getPrimaryKey($tableName){ + $indexes = $this->query("EXEC sp_helpindex '$tableName';"); + $indexName = ''; + foreach($indexes as $index) { + if($index['index_keys'] == 'ID') { + $indexName = $index['index_name']; + break; + } + } + + return $indexName; + } + + /** + * Gets the identity column of a table + * + * @param string $tableName + * @return string|null + */ + function getIdentityColumn($tableName) { + return $this->preparedQuery(" + SELECT + TABLE_NAME + '.' + COLUMN_NAME, + TABLE_NAME + FROM + INFORMATION_SCHEMA.COLUMNS + WHERE + TABLE_SCHEMA = ? AND + COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1 AND + TABLE_NAME = ? + ", array('dbo', $tableName))->value(); + } + + public function createDatabase($name) { + $this->query("CREATE DATABASE \"$name\""); + } + + public function dropDatabase($name) { + $this->query("DROP DATABASE \"$name\""); + } + + public function databaseExists($name) { + $databases = $this->databaseList(); + foreach($databases as $dbname) { + if($dbname == $name) return true; + } + return false; + } + + public function databaseList() { + return $this->query('SELECT NAME FROM sys.sysdatabases')->column(); + } + + /** + * Create a new table. + * @param $tableName The name of the table + * @param $fields A map of field names to field types + * @param $indexes A map of indexes + * @param $options An map of additional options. The available keys are as follows: + * - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL. + * - 'temporary' - If true, then a temporary table will be created + * @return The table name generated. This may be different from the table name, for example with temporary tables. + */ + public function createTable($tableName, $fields = null, $indexes = null, $options = null, $advancedOptions = null) { + $fieldSchemas = $indexSchemas = ""; + if($fields) foreach($fields as $k => $v) $fieldSchemas .= "\"$k\" $v,\n"; + + // Temporary tables start with "#" in MSSQL-land + if(!empty($options['temporary'])) { + // Randomize the temp table name to avoid conflicts in the tempdb table which derived databases share + $tableName = "#$tableName" . '-' . rand(1000000, 9999999); + } + + $this->query("CREATE TABLE \"$tableName\" ( + $fieldSchemas + primary key (\"ID\") + );"); + + //we need to generate indexes like this: CREATE INDEX IX_vault_to_export ON vault (to_export); + //This needs to be done AFTER the table creation, so we can set up the fulltext indexes correctly + if($indexes) foreach($indexes as $k => $v) { + $indexSchemas .= $this->getIndexSqlDefinition($tableName, $k, $v) . "\n"; + } + + if($indexSchemas) $this->query($indexSchemas); + + return $tableName; + } + + /** + * Alter a table's schema. + * @param $table The name of the table to alter + * @param $newFields New fields, a map of field name => field schema + * @param $newIndexes New indexes, a map of index name => index type + * @param $alteredFields Updated fields, a map of field name => field schema + * @param $alteredIndexes Updated indexes, a map of index name => index type + */ + public function alterTable($tableName, $newFields = null, $newIndexes = null, $alteredFields = null, $alteredIndexes = null, $alteredOptions=null, $advancedOptions=null) { + $alterList = array(); + + // drop any fulltext indexes that exist on the table before altering the structure + if($this->fullTextIndexExists($tableName)) { + $alterList[] = "\nDROP FULLTEXT INDEX ON \"$tableName\";"; + } + + if($newFields) foreach($newFields as $k => $v) $alterList[] = "ALTER TABLE \"$tableName\" ADD \"$k\" $v"; + + if($alteredFields) foreach($alteredFields as $k => $v) $alterList[] = $this->alterTableAlterColumn($tableName, $k, $v); + if($alteredIndexes) foreach($alteredIndexes as $k => $v) $alterList[] = $this->getIndexSqlDefinition($tableName, $k, $v); + if($newIndexes) foreach($newIndexes as $k => $v) $alterList[] = $this->getIndexSqlDefinition($tableName, $k, $v); + + if($alterList) { + foreach($alterList as $alteration) { + if($alteration != '') { + $this->query($alteration); + } + } + } + } + + /** + * Given the table and column name, retrieve the constraint name for that column + * in the table. + * + * @param string $tableName Table name column resides in + * @param string $columnName Column name the constraint is for + * @return string|null + */ + public function getConstraintName($tableName, $columnName) { + return $this->preparedQuery(" + SELECT CONSTRAINT_NAME + FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE + WHERE TABLE_NAME = ? AND COLUMN_NAME = ?", + array($tableName, $columnName) + )->value(); + } + + /** + * Given a table and column name, return a check constraint clause for that column in + * the table. + * + * This is an expensive query, so it is cached per-request and stored by table. The initial + * call for a table that has not been cached will query all columns and store that + * so subsequent calls are fast. + * + * @param string $tableName Table name column resides in + * @param string $columnName Column name the constraint is for + * @return string The check string + */ + public function getConstraintCheckClause($tableName, $columnName) { + // Check already processed table columns + if(isset(self::$cached_checks[$tableName])) { + if(!isset(self::$cached_checks[$tableName][$columnName])) { + return null; + } + return self::$cached_checks[$tableName][$columnName]; + } + + // Regenerate cehcks for this table + $checks = array(); + foreach($this->preparedQuery(" + SELECT CAST(CHECK_CLAUSE AS TEXT) AS CHECK_CLAUSE, COLUMN_NAME + FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS AS CC + INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CCU ON CCU.CONSTRAINT_NAME = CC.CONSTRAINT_NAME + WHERE TABLE_NAME = ?", + array($tableName) + ) as $record) { + $checks[$record['COLUMN_NAME']] = $record['CHECK_CLAUSE']; + } + self::$cached_checks[$tableName] = $checks; + + // Return via cached records + return $this->getConstraintCheckClause($tableName, $columnName); + } + + /** + * Return the name of the default constraint applied to $tableName.$colName. + * Will return null if no such constraint exists + * + * @param string $tableName Name of the table + * @param string $colName Name of the column + * @return string|null + */ + protected function defaultConstraintName($tableName, $colName) { + return $this->preparedQuery(" + SELECT s.name --default name + FROM sys.sysobjects s + join sys.syscolumns c ON s.parent_obj = c.id + WHERE s.xtype = 'd' + and c.cdefault = s.id + and parent_obj = OBJECT_ID(?) + and c.name = ?", + array($tableName, $colName) + )->value(); + } + + /** + * Get enum values from a constraint check clause. + * + * @param string $clause Check clause to parse values from + * @return array Enum values + */ + protected function enumValuesFromCheckClause($clause) { + $segments = preg_split('/ +OR *\[/i', $clause); + $constraints = array(); + foreach($segments as $segment) { + $bits = preg_split('/ *= */', $segment); + for($i = 1; $i < sizeof($bits); $i += 2) { + array_unshift($constraints, substr(rtrim($bits[$i], ')'), 1, -1)); + } + } + return $constraints; + } + + /* + * Creates an ALTER expression for a column in MS SQL + * + * @param string $tableName Name of the table to be altered + * @param string $colName Name of the column to be altered + * @param string $colSpec String which contains conditions for a column + * @return string + */ + protected function alterTableAlterColumn($tableName, $colName, $colSpec){ + + // First, we split the column specifications into parts + // TODO: this returns an empty array for the following string: int(11) not null auto_increment + // on second thoughts, why is an auto_increment field being passed through? + $pattern = '/^(?[\w()]+)\s?(?(?:not\s)?null)?\s?(?default\s[\w\']+)?\s?(?check\s?[\w()\'",\s]+)?$/i'; + $matches = array(); + preg_match($pattern, $colSpec, $matches); + + // drop the index if it exists + $alterQueries = array(); + + // drop *ALL* indexes on a table before proceeding + // this won't drop primary keys, though + $indexes = $this->indexNames($tableName); + foreach($indexes as $indexName) { + $alterQueries[] = "DROP INDEX \"$indexName\" ON \"$tableName\";"; + } + + $prefix = "ALTER TABLE \"$tableName\" "; + + // Remove the old default prior to adjusting the column. + if($defaultConstraintName = $this->defaultConstraintName($tableName, $colName)) { + $alterQueries[] = "$prefix DROP CONSTRAINT \"$defaultConstraintName\";"; + } + + if(isset($matches['definition'])) { + //We will prevent any changes being made to the ID column. Primary key indexes will have a fit if we do anything here. + if($colName != 'ID'){ + + // SET null / not null + $nullFragment = empty($matches['null']) ? '' : " {$matches['null']}"; + $alterQueries[] = "$prefix ALTER COLUMN \"$colName\" {$matches['definition']}$nullFragment;"; + + // Add a default back + if(!empty($matches['default'])) { + $alterQueries[] = "$prefix ADD {$matches['default']} FOR \"$colName\";"; + } + + // SET check constraint (The constraint HAS to be dropped) + if(!empty($matches['check'])) { + $constraint = $this->getConstraintName($tableName, $colName); + if($constraint) { + $alterQueries[] = "$prefix DROP CONSTRAINT {$constraint};"; + } + + //NOTE: 'with nocheck' seems to solve a few problems I've been having for modifying existing tables. + $alterQueries[] = "$prefix WITH NOCHECK ADD CONSTRAINT \"{$tableName}_{$colName}_check\" {$matches['check']};"; + } + } + } + + return implode("\n", $alterQueries); + } + + public function renameTable($oldTableName, $newTableName) { + $this->query("EXEC sp_rename \"$oldTableName\", \"$newTableName\""); + } + + /** + * Checks a table's integrity and repairs it if necessary. + * NOTE: MSSQL does not appear to support any vacuum or optimise commands + * + * @var string $tableName The name of the table. + * @return boolean Return true if the table has integrity after the method is complete. + */ + public function checkAndRepairTable($tableName) { + return true; + } + + public function createField($tableName, $fieldName, $fieldSpec) { + $this->query("ALTER TABLE \"$tableName\" ADD \"$fieldName\" $fieldSpec"); + } + + /** + * Change the database type of the given field. + * @param string $tableName The name of the tbale the field is in. + * @param string $fieldName The name of the field to change. + * @param string $fieldSpec The new field specification + */ + public function alterField($tableName, $fieldName, $fieldSpec) { + $this->query("ALTER TABLE \"$tableName\" CHANGE \"$fieldName\" \"$fieldName\" $fieldSpec"); + } + + public function renameField($tableName, $oldName, $newName) { + $this->query("EXEC sp_rename @objname = '$tableName.$oldName', @newname = '$newName', @objtype = 'COLUMN'"); + } + + public function fieldList($table) { + //This gets us more information than we need, but I've included it all for the moment.... + $fieldRecords = $this->preparedQuery("SELECT ordinal_position, column_name, data_type, column_default, + is_nullable, character_maximum_length, numeric_precision, numeric_scale, collation_name + FROM information_schema.columns WHERE table_name = ? + ORDER BY ordinal_position;", + array($table) + ); + + // Cache the records from the query - otherwise a lack of multiple active result sets + // will cause subsequent queries to fail in this method + $fields = array(); + $output = array(); + foreach($fieldRecords as $record) { + $fields[] = $record; + } + + foreach($fields as $field) { + // Update the data_type field to be a complete column definition string for use by + // SS_Database::requireField() + switch($field['data_type']){ + case 'bigint': + case 'numeric': + case 'float': + case 'bit': + if($field['data_type'] != 'bigint' && $sizeSuffix = $field['numeric_precision']) { + $field['data_type'] .= "($sizeSuffix)"; + } + + if($field['is_nullable'] == 'YES') { + $field['data_type'] .= ' null'; + } else { + $field['data_type'] .= ' not null'; + } + if($field['column_default']) { + $default=substr($field['column_default'], 2, -2); + $field['data_type'] .= " default $default"; + } + break; + + case 'decimal': + if($field['numeric_precision']) { + $sizeSuffix = $field['numeric_precision'] . ',' . $field['numeric_scale']; + $field['data_type'] .= "($sizeSuffix)"; + } + + if($field['is_nullable'] == 'YES') { + $field['data_type'] .= ' null'; + } else { + $field['data_type'] .= ' not null'; + } + if($field['column_default']) { + $default=substr($field['column_default'], 2, -2); + $field['data_type'] .= " default $default"; + } + break; + + case 'nvarchar': + case 'varchar': + //Check to see if there's a constraint attached to this column: + $clause = $this->getConstraintCheckClause($table, $field['column_name']); + if($clause) { + $constraints = $this->enumValuesFromCheckClause($clause); + $default=substr($field['column_default'], 2, -2); + $field['data_type'] = $this->enum(array( + 'default' => $default, + 'name' => $field['column_name'], + 'enums' => $constraints, + 'table' => $table + )); + break; + } + + default: + $sizeSuffix = $field['character_maximum_length']; + if($sizeSuffix == '-1') $sizeSuffix = 'max'; + if($sizeSuffix) { + $field['data_type'] .= "($sizeSuffix)"; + } + + if($field['is_nullable'] == 'YES') { + $field['data_type'] .= ' null'; + } else { + $field['data_type'] .= ' not null'; + } + if($field['column_default']) { + $default=substr($field['column_default'], 2, -2); + $field['data_type'] .= " default '$default'"; + } + } + $output[$field['column_name']] = $field; + + } + + return $output; + } + + /** + * Create an index on a table. + * @param string $tableName The name of the table. + * @param string $indexName The name of the index. + * @param string $indexSpec The specification of the index, see SS_Database::requireIndex() for more details. + */ + public function createIndex($tableName, $indexName, $indexSpec) { + $this->query($this->getIndexSqlDefinition($tableName, $indexName, $indexSpec)); + } + + /** + * Return SQL for dropping and recreating an index + * + * @param string $tableName Name of table to create this index against + * @param string $indexName Name of this index + * @param array|string $indexSpec Index specification, either as a raw string + * or parsed array form + * @return string The SQL required to generate this index + */ + protected function getIndexSqlDefinition($tableName, $indexName, $indexSpec) { + + // Determine index name + $index = $this->buildMSSQLIndexName($tableName, $indexName); + + // Consolidate/Cleanup spec into array format + $indexSpec = $this->parseIndexSpec($indexName, $indexSpec); + + $drop = "IF EXISTS (SELECT name FROM sys.indexes WHERE name = '$index') DROP INDEX $index ON \"$tableName\";"; + + // create a type-specific index + if($indexSpec['type'] == 'fulltext' && $this->database->fullTextEnabled()) { + // enable fulltext on this table + $this->createFullTextCatalog(); + $primary_key = $this->getPrimaryKey($tableName); + + if($primary_key) { + return "$drop CREATE FULLTEXT INDEX ON \"$tableName\" ({$indexSpec['value']})" + . "KEY INDEX $primary_key WITH CHANGE_TRACKING AUTO;"; + } + } + + if($indexSpec['type'] == 'unique') { + return "$drop CREATE UNIQUE INDEX $index ON \"$tableName\" ({$indexSpec['value']});"; + } + + return "$drop CREATE INDEX $index ON \"$tableName\" ({$indexSpec['value']});"; + } + + public function alterIndex($tableName, $indexName, $indexSpec) { + $this->createIndex($tableName, $indexName, $indexSpec); + } + + /** + * Return the list of indexes in a table. + * @param string $table The table name. + * @return array + */ + public function indexList($table) { + $indexes = $this->query("EXEC sp_helpindex '$table';"); + $indexList = array(); + + // Enumerate all basic indexes + foreach($indexes as $index) { + if(strpos($index['index_description'], 'unique') !== false) { + $indexType = 'unique '; + } else { + $indexType = 'index '; + } + + // Extract name from index + $baseIndexName = $this->buildMSSQLIndexName($table, ''); + $indexName = substr($index['index_name'], strlen($baseIndexName)); + + // Extract columns + $columns = $this->quoteColumnSpecString($index['index_keys']); + $indexList[$indexName] = $this->parseIndexSpec($indexName, array( + 'name' => $indexName, + 'value' => $columns, + 'type' => $indexType + )); + } + + // Now we need to check to see if we have any fulltext indexes attached to this table: + if($this->database->fullTextEnabled()) { + $result = $this->query('EXEC sp_help_fulltext_columns;'); + + // Extract columns from this fulltext definition + $columns = array(); + foreach($result as $row) { + if($row['TABLE_NAME'] == $table) { + $columns[] = $row['FULLTEXT_COLUMN_NAME']; + } + } + + if(!empty($columns)) { + $indexList['SearchFields'] = $this->parseIndexSpec('SearchFields', array( + 'name' => 'SearchFields', + 'value' => $this->implodeColumnList($columns), + 'type' => 'fulltext' + )); + } + } + + return $indexList; + } + + /** + * For a given table name, get all the internal index names, + * except for those that are primary keys and fulltext indexes. + * + * @return array + */ + public function indexNames($tableName) { + return $this->preparedQuery(' + SELECT ind.name FROM sys.indexes ind + INNER JOIN sys.tables t ON ind.object_id = t.object_id + WHERE is_primary_key = 0 AND t.name = ?', + array($tableName) + )->column(); + } + + public function tableList() { + $tables = array(); + foreach($this->query("EXEC sp_tables @table_owner = 'dbo';") as $record) { + $tables[strtolower($record['TABLE_NAME'])] = $record['TABLE_NAME']; + } + return $tables; + } + + /** + * Return a boolean type-formatted string + * We use 'bit' so that we can do numeric-based comparisons + * + * @params array $values Contains a tokenised list of info about this data type + * @return string + */ + public function boolean($values) { + $default = ($values['default']) ? '1' : '0'; + return 'bit not null default ' . $default; + } + + /** + * Return a date type-formatted string. + * + * @params array $values Contains a tokenised list of info about this data type + * @return string + */ + public function date($values) { + return 'date null'; + } + + /** + * Return a decimal type-formatted string + * + * @params array $values Contains a tokenised list of info about this data type + * @return string + */ + public function decimal($values) { + // Avoid empty strings being put in the db + if($values['precision'] == '') { + $precision = 1; + } else { + $precision = $values['precision']; + } + + $defaultValue = '0'; + if(isset($values['default']) && is_numeric($values['default'])) { + $defaultValue = $values['default']; + } + + return "decimal($precision) not null default $defaultValue"; + } + + /** + * Return a enum type-formatted string + * + * @params array $values Contains a tokenised list of info about this data type + * @return string + */ + public function enum($values) { + // Enums are a bit different. We'll be creating a varchar(255) with a constraint of all the + // usual enum options. + // NOTE: In this one instance, we are including the table name in the values array + + $maxLength = max(array_map('strlen', $values['enums'])); + + return "varchar($maxLength) not null default '" . $values['default'] + . "' check(\"" . $values['name'] . "\" in ('" . implode("','", $values['enums']) + . "'))"; + } + + /** + * @todo Make this work like {@link MySQLDatabase::set()} + */ + public function set($values) { + return $this->enum($values); + } + + /** + * Return a float type-formatted string. + * + * @params array $values Contains a tokenised list of info about this data type + * @return string + */ + public function float($values) { + return 'float(53) not null default ' . $values['default']; + } + + /** + * Return a int type-formatted string + * + * @params array $values Contains a tokenised list of info about this data type + * @return string + */ + public function int($values) { + //We'll be using an 8 digit precision to keep it in line with the serial8 datatype for ID columns + return 'numeric(8) not null default ' . (int) $values['default']; + } + + /** + * Return a datetime type-formatted string + * For MS SQL, we simply return the word 'timestamp', no other parameters are necessary + * + * @params array $values Contains a tokenised list of info about this data type + * @return string + */ + public function ss_datetime($values) { + return 'datetime null'; + } + + /** + * Return a text type-formatted string + * + * @params array $values Contains a tokenised list of info about this data type + * @return string + */ + public function text($values) { + $collation = MSSQLDatabase::get_collation(); + $collationSQL = $collation ? " COLLATE $collation" : ""; + return "nvarchar(max)$collationSQL null"; + } + + /** + * Return a time type-formatted string. + * + * @params array $values Contains a tokenised list of info about this data type + * @return string + */ + public function time($values){ + return 'time null'; + } + + /** + * Return a varchar type-formatted string + * + * @params array $values Contains a tokenised list of info about this data type + * @return string + */ + public function varchar($values) { + $collation = MSSQLDatabase::get_collation(); + $collationSQL = $collation ? " COLLATE $collation" : ""; + return "nvarchar(" . $values['precision'] . ")$collationSQL null"; + } + + /** + * Return a 4 digit numeric type. + * @return string + */ + public function year($values) { + return 'numeric(4)'; + } + + /** + * This returns the column which is the primary key for each table + * In Postgres, it is a SERIAL8, which is the equivalent of an auto_increment + * + * @return string + */ + function IdColumn($asDbValue = false, $hasAutoIncPK = true){ + if($asDbValue) { + return 'bigint not null'; + } else if($hasAutoIncPK) { + return 'bigint identity(1,1)'; + } else { + return 'bigint not null'; + } + } + + function hasTable($tableName) { + return (bool)$this->preparedQuery( + "SELECT table_name FROM information_schema.tables WHERE table_name = ?", + array($tableName) + )->value(); + } + + /** + * Returns the values of the given enum field + * NOTE: Experimental; introduced for db-abstraction and may changed before 2.4 is released. + */ + public function enumValuesForField($tableName, $fieldName) { + $classes = array(); + + // Get the enum of all page types from the SiteTree table + $clause = $this->getConstraintCheckClause($tableName, $fieldName); + if($clause) { + $classes = $this->enumValuesFromCheckClause($clause); + } + + return $classes; + } + + /** + * This is a lookup table for data types. + * + * For instance, MSSQL uses 'BIGINT', while MySQL uses 'UNSIGNED' + * and PostgreSQL uses 'INT'. + */ + function dbDataType($type){ + $values = array( + 'unsigned integer'=>'BIGINT' + ); + if(isset($values[$type])) { + return $values[$type]; + } else { + return ''; + } + } + + protected function indexKey($table, $index, $spec) { + return $index; + } +} diff --git a/code/SQLServerConnector.php b/code/SQLServerConnector.php new file mode 100644 index 0000000..08dfda8 --- /dev/null +++ b/code/SQLServerConnector.php @@ -0,0 +1,195 @@ + $charset, + 'MultipleActiveResultSets' => $multiResultSets + ); + + if( !(defined('MSSQL_USE_WINDOWS_AUTHENTICATION') && MSSQL_USE_WINDOWS_AUTHENTICATION == true) + && empty($parameters['windowsauthentication']) + ) { + $options['UID'] = $parameters['username']; + $options['PWD'] = $parameters['password']; + } + + // Required by MS Azure database + if($selectDB && !empty($parameters['database'])) { + $options['Database'] = $parameters['database']; + } + + $this->dbConn = sqlsrv_connect($parameters['server'], $options); + + if(empty($this->dbConn)) { + $this->databaseError("Couldn't connect to SQL Server database"); + } elseif($selectDB && !empty($parameters['database'])) { + // Check selected database (Azure) + $this->selectedDatabase = $parameters['database']; + } + } + + /** + * Start transaction. READ ONLY not supported. + */ + public function transactionStart(){ + $result = sqlsrv_begin_transaction($this->dbConn); + if (!$result) { + $this->databaseError("Couldn't start the transaction."); + } + } + + /** + * Commit everything inside this transaction so far + */ + public function transactionEnd() { + $result = sqlsrv_commit($this->dbConn); + if (!$result) { + $this->databaseError("Couldn't commit the transaction."); + } + } + + /** + * Rollback or revert to a savepoint if your queries encounter problems + * If you encounter a problem at any point during a transaction, you may + * need to rollback that particular query, or return to a savepoint + */ + public function transactionRollback(){ + $result = sqlsrv_rollback($this->dbConn); + if (!$result) { + $this->databaseError("Couldn't rollback the transaction."); + } + } + + public function affectedRows() { + return $this->lastAffectedRows; + } + + public function getLastError() { + $errorMessages = array(); + $errors = sqlsrv_errors(); + if($errors) foreach($errors as $info) { + $errorMessages[] = implode(', ', array($info['SQLSTATE'], $info['code'], $info['message'])); + } + return implode('; ', $errorMessages); + } + + public function isActive() { + return $this->dbConn && $this->selectedDatabase; + } + + public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR) { + // Check if we should only preview this query + if ($this->previewWrite($sql)) return; + + $parsedParameters = $this->parameterValues($parameters); + + // Benchmark query + $dbConn = $this->dbConn; + $handle = $this->benchmarkQuery($sql, function($sql) use($dbConn, $parsedParameters) { + if(empty($parsedParameters)) { + return sqlsrv_query($dbConn, $sql); + } else { + return sqlsrv_query($dbConn, $sql, $parsedParameters); + } + }); + + if($handle) { + $this->lastAffectedRows = sqlsrv_rows_affected($handle); + } else { + $this->databaseError($this->getLastError(), $errorLevel, $sql, $parsedParameters); + return null; + } + + return new SQLServerQuery($this, $handle); + } + + public function query($sql, $errorLevel = E_USER_ERROR) { + return $this->preparedQuery($sql, array(), $errorLevel); + } + + public function selectDatabase($name) { + $this->query("USE \"$name\""); + $this->selectedDatabase = $name; + return true; + } + + public function __destruct() { + if(is_resource($this->dbConn)) { + sqlsrv_close($this->dbConn); + } + } + + public function getVersion() { + // @todo - use sqlsrv_server_info? + return trim($this->query("SELECT CONVERT(char(15), SERVERPROPERTY('ProductVersion'))")->value()); + } + + public function getGeneratedID($table) { + return $this->query("SELECT IDENT_CURRENT('$table')")->value(); + } + + public function getSelectedDatabase() { + return $this->selectedDatabase; + } + + public function unloadDatabase() { + $this->selectDatabase('Master'); + $this->selectedDatabase = null; + } + + /** + * Quotes a string, including the "N" prefix so unicode + * strings are saved to the database correctly. + * + * @param string $string String to be encoded + * @return string Processed string ready for DB + */ + public function quoteString($value) { + return "N'" . $this->escapeString($value) . "'"; + } + + public function escapeString($value) { + $value = str_replace("'", "''", $value); + $value = str_replace("\0", "[NULL]", $value); + return $value; + } +} diff --git a/code/SQLServerQuery.php b/code/SQLServerQuery.php new file mode 100644 index 0000000..10cebda --- /dev/null +++ b/code/SQLServerQuery.php @@ -0,0 +1,76 @@ +connector = $connector; + $this->handle = $handle; + } + + public function __destruct() { + if(is_resource($this->handle)) { + sqlsrv_free_stmt($this->handle); + } + } + + public function seek($row) { + if(!is_resource($this->handle)) return false; + + user_error('MSSQLQuery::seek() not supported in sqlsrv', E_USER_WARNING); + } + + public function numRecords() { + if(!is_resource($this->handle)) return false; + + // WARNING: This will only work if the cursor type is scrollable! + if(function_exists('sqlsrv_num_rows')) { + return sqlsrv_num_rows($this->handle); + } else { + user_error('MSSQLQuery::numRecords() not supported in this version of sqlsrv', E_USER_WARNING); + } + } + + public function nextRecord() { + if(!is_resource($this->handle)) return false; + + if($data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC)) { + // special case for sqlsrv - date values are DateTime coming out of the sqlsrv drivers, + // so we convert to the usual Y-m-d H:i:s value! + foreach($data as $name => $value) { + if($value instanceof DateTime) $data[$name] = $value->format('Y-m-d H:i:s'); + } + return $data; + } else { + // Free the handle if there are no more results - sqlsrv crashes if there are too many handles + sqlsrv_free_stmt($this->handle); + $this->handle = null; + } + + return false; + } + +}