2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * MSSQL specific SQL code generator.
21 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
22 * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir.'/ddl/sql_generator.php');
31 * This class generate SQL code to be used against MSSQL
32 * It extends XMLDBgenerator so everything can be
33 * overridden as needed to generate correct SQL.
36 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
37 * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class mssql_sql_generator extends sql_generator {
42 // Only set values that are different from the defaults present in XMLDBgenerator
44 /** @var string To be automatically added at the end of each statement. */
45 public $statement_end = "\ngo";
47 /** @var string Proper type for NUMBER(x) in this DB. */
48 public $number_type = 'DECIMAL';
50 /** @var string To define the default to set for NOT NULLs CHARs without default (null=do nothing).*/
51 public $default_for_char = '';
54 * @var bool To force the generator if NULL clauses must be specified. It shouldn't be necessary.
55 * note: some mssql drivers require them or everything is created as NOT NULL :-(
57 public $specify_nulls = true;
59 /** @var bool True if the generator needs to add extra code to generate the sequence fields.*/
60 public $sequence_extra_code = false;
62 /** @var string The particular name for inline sequences in this generator.*/
63 public $sequence_name = 'IDENTITY(1,1)';
65 /** @var bool To avoid outputting the rest of the field specs, leaving only the name and the sequence_name returned.*/
66 public $sequence_only = false;
68 /** @var bool True if the generator needs to add code for table comments.*/
69 public $add_table_comments = false;
71 /** @var string Characters to be used as concatenation operator.*/
72 public $concat_character = '+';
74 /** @var string SQL sentence to rename one table, both 'OLDNAME' and 'NEWNAME' keywords are dynamically replaced.*/
75 public $rename_table_sql = "sp_rename 'OLDNAME', 'NEWNAME'";
77 /** @var string SQL sentence to rename one column where 'TABLENAME', 'OLDFIELDNAME' and 'NEWFIELDNAME' keywords are dynamically replaced.*/
78 public $rename_column_sql = "sp_rename 'TABLENAME.OLDFIELDNAME', 'NEWFIELDNAME', 'COLUMN'";
80 /** @var string SQL sentence to drop one index where 'TABLENAME', 'INDEXNAME' keywords are dynamically replaced.*/
81 public $drop_index_sql = 'DROP INDEX TABLENAME.INDEXNAME';
83 /** @var string SQL sentence to rename one index where 'TABLENAME', 'OLDINDEXNAME' and 'NEWINDEXNAME' are dynamically replaced.*/
84 public $rename_index_sql = "sp_rename 'TABLENAME.OLDINDEXNAME', 'NEWINDEXNAME', 'INDEX'";
86 /** @var string SQL sentence to rename one key 'TABLENAME', 'OLDKEYNAME' and 'NEWKEYNAME' are dynamically replaced.*/
87 public $rename_key_sql = null;
90 * Reset a sequence to the id field of a table.
92 * @param xmldb_table|string $table name of table or the table object.
93 * @return array of sql statements
95 public function getResetSequenceSQL($table) {
97 if (is_string($table)) {
98 $table = new xmldb_table($table);
101 $value = (int)$this->mdb->get_field_sql('SELECT MAX(id) FROM {'. $table->getName() . '}');
104 // MSSQL has one non-consistent behavior to create the first identity value, depending
105 // if the table has been truncated or no. If you are really interested, you can find the
106 // whole description of the problem at:
107 // http://www.justinneff.com/archive/tag/dbcc-checkident
109 // truncate to get consistent result from reseed
110 $sqls[] = "TRUNCATE TABLE " . $this->getTableName($table);
114 // From http://msdn.microsoft.com/en-us/library/ms176057.aspx
115 $sqls[] = "DBCC CHECKIDENT ('" . $this->getTableName($table) . "', RESEED, $value)";
120 * Given one xmldb_table, returns it's correct name, depending of all the parametrization
121 * Overridden to allow change of names in temp tables
123 * @param xmldb_table table whose name we want
124 * @param boolean to specify if the name must be quoted (if reserved word, only!)
125 * @return string the correct name of the table
127 public function getTableName(xmldb_table $xmldb_table, $quoted=true) {
128 // Get the name, supporting special mssql names for temp tables
129 if ($this->temptables->is_temptable($xmldb_table->getName())) {
130 $tablename = $this->temptables->get_correct_name($xmldb_table->getName());
132 $tablename = $this->prefix . $xmldb_table->getName();
135 // Apply quotes optionally
137 $tablename = $this->getEncQuoted($tablename);
144 * Given one correct xmldb_table, returns the SQL statements
145 * to create temporary table (inside one array).
147 * @param xmldb_table $xmldb_table The xmldb_table object instance.
148 * @return array of sql statements
150 public function getCreateTempTableSQL($xmldb_table) {
151 $this->temptables->add_temptable($xmldb_table->getName());
152 $sqlarr = $this->getCreateTableSQL($xmldb_table);
157 * Given one correct xmldb_table, returns the SQL statements
158 * to drop it (inside one array).
160 * @param xmldb_table $xmldb_table The table to drop.
161 * @return array SQL statement(s) for dropping the specified table.
163 public function getDropTableSQL($xmldb_table) {
164 $sqlarr = parent::getDropTableSQL($xmldb_table);
165 if ($this->temptables->is_temptable($xmldb_table->getName())) {
166 $this->temptables->delete_temptable($xmldb_table->getName());
172 * Given one XMLDB Type, length and decimals, returns the DB proper SQL type.
174 * @param int $xmldb_type The xmldb_type defined constant. XMLDB_TYPE_INTEGER and other XMLDB_TYPE_* constants.
175 * @param int $xmldb_length The length of that data type.
176 * @param int $xmldb_decimals The decimal places of precision of the data type.
177 * @return string The DB defined data type.
179 public function getTypeSQL($xmldb_type, $xmldb_length=null, $xmldb_decimals=null) {
181 switch ($xmldb_type) {
182 case XMLDB_TYPE_INTEGER: // From http://msdn.microsoft.com/library/en-us/tsqlref/ts_da-db_7msw.asp?frame=true
183 if (empty($xmldb_length)) {
186 if ($xmldb_length > 9) {
188 } else if ($xmldb_length > 4) {
191 $dbtype = 'SMALLINT';
194 case XMLDB_TYPE_NUMBER:
195 $dbtype = $this->number_type;
196 if (!empty($xmldb_length)) {
197 $dbtype .= '(' . $xmldb_length;
198 if (!empty($xmldb_decimals)) {
199 $dbtype .= ',' . $xmldb_decimals;
204 case XMLDB_TYPE_FLOAT:
206 if (!empty($xmldb_decimals)) {
207 if ($xmldb_decimals < 6) {
212 case XMLDB_TYPE_CHAR:
213 $dbtype = 'NVARCHAR';
214 if (empty($xmldb_length)) {
217 $dbtype .= '(' . $xmldb_length . ') COLLATE database_default';
219 case XMLDB_TYPE_TEXT:
220 $dbtype = 'NVARCHAR(MAX) COLLATE database_default';
222 case XMLDB_TYPE_BINARY:
223 $dbtype = 'VARBINARY(MAX)';
225 case XMLDB_TYPE_DATETIME:
226 $dbtype = 'DATETIME';
233 * Given one xmldb_table and one xmldb_field, return the SQL statements needed to drop the field from the table.
234 * MSSQL overwrites the standard sentence because it needs to do some extra work dropping the default and
237 * @param xmldb_table $xmldb_table The table related to $xmldb_field.
238 * @param xmldb_field $xmldb_field The instance of xmldb_field to create the SQL from.
239 * @return array The SQL statement for dropping a field from the table.
241 public function getDropFieldSQL($xmldb_table, $xmldb_field) {
244 // Get the quoted name of the table and field
245 $tablename = $this->getTableName($xmldb_table);
246 $fieldname = $this->getEncQuoted($xmldb_field->getName());
248 // Look for any default constraint in this field and drop it
249 if ($defaultname = $this->getDefaultConstraintName($xmldb_table, $xmldb_field)) {
250 $results[] = 'ALTER TABLE ' . $tablename . ' DROP CONSTRAINT ' . $defaultname;
253 // Build the standard alter table drop column
254 $results[] = 'ALTER TABLE ' . $tablename . ' DROP COLUMN ' . $fieldname;
260 * Given one correct xmldb_field and the new name, returns the SQL statements
261 * to rename it (inside one array).
263 * MSSQL is special, so we overload the function here. It needs to
264 * drop the constraints BEFORE renaming the field
266 * @param xmldb_table $xmldb_table The table related to $xmldb_field.
267 * @param xmldb_field $xmldb_field The instance of xmldb_field to get the renamed field from.
268 * @param string $newname The new name to rename the field to.
269 * @return array The SQL statements for renaming the field.
271 public function getRenameFieldSQL($xmldb_table, $xmldb_field, $newname) {
273 $results = array(); //Array where all the sentences will be stored
275 // Although this is checked in database_manager::rename_field() - double check
276 // that we aren't trying to rename one "id" field. Although it could be
277 // implemented (if adding the necessary code to rename sequences, defaults,
278 // triggers... and so on under each getRenameFieldExtraSQL() function, it's
279 // better to forbid it, mainly because this field is the default PK and
280 // in the future, a lot of FKs can be pointing here. So, this field, more
281 // or less, must be considered immutable!
282 if ($xmldb_field->getName() == 'id') {
286 // Call to standard (parent) getRenameFieldSQL() function
287 $results = array_merge($results, parent::getRenameFieldSQL($xmldb_table, $xmldb_field, $newname));
293 * Returns the code (array of statements) needed to execute extra statements on table rename.
295 * @param xmldb_table $xmldb_table The xmldb_table object instance.
296 * @param string $newname The new name for the table.
297 * @return array Array of extra SQL statements to rename a table.
299 public function getRenameTableExtraSQL($xmldb_table, $newname) {
307 * Given one xmldb_table and one xmldb_field, return the SQL statements needed to alter the field in the table.
309 * @param xmldb_table $xmldb_table The table related to $xmldb_field.
310 * @param xmldb_field $xmldb_field The instance of xmldb_field to create the SQL from.
311 * @param string $skip_type_clause The type clause on alter columns, NULL by default.
312 * @param string $skip_default_clause The default clause on alter columns, NULL by default.
313 * @param string $skip_notnull_clause The null/notnull clause on alter columns, NULL by default.
314 * @return string The field altering SQL statement.
316 public function getAlterFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL) {
318 $results = array(); // To store all the needed SQL commands
320 // Get the quoted name of the table and field
321 $tablename = $xmldb_table->getName();
322 $fieldname = $xmldb_field->getName();
324 // Take a look to field metadata
325 $meta = $this->mdb->get_columns($tablename);
326 $metac = $meta[$fieldname];
327 $oldmetatype = $metac->meta_type;
329 $oldlength = $metac->max_length;
330 $olddecimals = empty($metac->scale) ? null : $metac->scale;
331 $oldnotnull = empty($metac->not_null) ? false : $metac->not_null;
332 //$olddefault = empty($metac->has_default) ? null : strtok($metac->default_value, ':');
334 $typechanged = true; //By default, assume that the column type has changed
335 $lengthchanged = true; //By default, assume that the column length has changed
337 // Detect if we are changing the type of the column
338 if (($xmldb_field->getType() == XMLDB_TYPE_INTEGER && $oldmetatype == 'I') ||
339 ($xmldb_field->getType() == XMLDB_TYPE_NUMBER && $oldmetatype == 'N') ||
340 ($xmldb_field->getType() == XMLDB_TYPE_FLOAT && $oldmetatype == 'F') ||
341 ($xmldb_field->getType() == XMLDB_TYPE_CHAR && $oldmetatype == 'C') ||
342 ($xmldb_field->getType() == XMLDB_TYPE_TEXT && $oldmetatype == 'X') ||
343 ($xmldb_field->getType() == XMLDB_TYPE_BINARY && $oldmetatype == 'B')) {
344 $typechanged = false;
347 // If the new field (and old) specs are for integer, let's be a bit more specific differentiating
348 // types of integers. Else, some combinations can cause things like MDL-21868
349 if ($xmldb_field->getType() == XMLDB_TYPE_INTEGER && $oldmetatype == 'I') {
350 if ($xmldb_field->getLength() > 9) { // Convert our new lenghts to detailed meta types
351 $newmssqlinttype = 'I8';
352 } else if ($xmldb_field->getLength() > 4) {
353 $newmssqlinttype = 'I';
355 $newmssqlinttype = 'I2';
357 if ($metac->type == 'bigint') { // Convert current DB type to detailed meta type (our metatype is not enough!)
358 $oldmssqlinttype = 'I8';
359 } else if ($metac->type == 'smallint') {
360 $oldmssqlinttype = 'I2';
362 $oldmssqlinttype = 'I';
364 if ($newmssqlinttype != $oldmssqlinttype) { // Compare new and old meta types
365 $typechanged = true; // Change in meta type means change in type at all effects
369 // Detect if we are changing the length of the column, not always necessary to drop defaults
370 // if only the length changes, but it's safe to do it always
371 if ($xmldb_field->getLength() == $oldlength) {
372 $lengthchanged = false;
375 // If type or length have changed drop the default if exists
376 if ($typechanged || $lengthchanged) {
377 $results = $this->getDropDefaultSQL($xmldb_table, $xmldb_field);
380 // Some changes of type require multiple alter statements, because mssql lacks direct implicit cast between such types
381 // Here it is the matrix: http://msdn.microsoft.com/en-us/library/ms187928(SQL.90).aspx
382 // Going to store such intermediate alters in array of objects, storing all the info needed
383 $multiple_alter_stmt = array();
384 $targettype = $xmldb_field->getType();
386 if ($targettype == XMLDB_TYPE_TEXT && $oldmetatype == 'I') { // integer to text
387 $multiple_alter_stmt[0] = new stdClass; // needs conversion to varchar
388 $multiple_alter_stmt[0]->type = XMLDB_TYPE_CHAR;
389 $multiple_alter_stmt[0]->length = 255;
391 } else if ($targettype == XMLDB_TYPE_TEXT && $oldmetatype == 'N') { // decimal to text
392 $multiple_alter_stmt[0] = new stdClass; // needs conversion to varchar
393 $multiple_alter_stmt[0]->type = XMLDB_TYPE_CHAR;
394 $multiple_alter_stmt[0]->length = 255;
396 } else if ($targettype == XMLDB_TYPE_TEXT && $oldmetatype == 'F') { // float to text
397 $multiple_alter_stmt[0] = new stdClass; // needs conversion to varchar
398 $multiple_alter_stmt[0]->type = XMLDB_TYPE_CHAR;
399 $multiple_alter_stmt[0]->length = 255;
401 } else if ($targettype == XMLDB_TYPE_INTEGER && $oldmetatype == 'X') { // text to integer
402 $multiple_alter_stmt[0] = new stdClass; // needs conversion to varchar
403 $multiple_alter_stmt[0]->type = XMLDB_TYPE_CHAR;
404 $multiple_alter_stmt[0]->length = 255;
405 $multiple_alter_stmt[1] = new stdClass; // and also needs conversion to decimal
406 $multiple_alter_stmt[1]->type = XMLDB_TYPE_NUMBER; // without decimal positions
407 $multiple_alter_stmt[1]->length = 10;
409 } else if ($targettype == XMLDB_TYPE_NUMBER && $oldmetatype == 'X') { // text to decimal
410 $multiple_alter_stmt[0] = new stdClass; // needs conversion to varchar
411 $multiple_alter_stmt[0]->type = XMLDB_TYPE_CHAR;
412 $multiple_alter_stmt[0]->length = 255;
414 } else if ($targettype == XMLDB_TYPE_FLOAT && $oldmetatype == 'X') { // text to float
415 $multiple_alter_stmt[0] = new stdClass; // needs conversion to varchar
416 $multiple_alter_stmt[0]->type = XMLDB_TYPE_CHAR;
417 $multiple_alter_stmt[0]->length = 255;
420 // Just prevent default clauses in this type of sentences for mssql and launch the parent one
421 if (empty($multiple_alter_stmt)) { // Direct implicit conversion allowed, launch it
422 $results = array_merge($results, parent::getAlterFieldSQL($xmldb_table, $xmldb_field, NULL, true, NULL));
424 } else { // Direct implicit conversion forbidden, use the intermediate ones
425 $final_type = $xmldb_field->getType(); // Save final type and length
426 $final_length = $xmldb_field->getLength();
427 foreach ($multiple_alter_stmt as $alter) {
428 $xmldb_field->setType($alter->type); // Put our intermediate type and length and alter to it
429 $xmldb_field->setLength($alter->length);
430 $results = array_merge($results, parent::getAlterFieldSQL($xmldb_table, $xmldb_field, NULL, true, NULL));
432 $xmldb_field->setType($final_type); // Set the final type and length and alter to it
433 $xmldb_field->setLength($final_length);
434 $results = array_merge($results, parent::getAlterFieldSQL($xmldb_table, $xmldb_field, NULL, true, NULL));
437 // Finally, process the default clause to add it back if necessary
438 if ($typechanged || $lengthchanged) {
439 $results = array_merge($results, $this->getCreateDefaultSQL($xmldb_table, $xmldb_field));
447 * Given one xmldb_table and one xmldb_field, return the SQL statements needed to modify the default of the field in the table.
449 * @param xmldb_table $xmldb_table The table related to $xmldb_field.
450 * @param xmldb_field $xmldb_field The instance of xmldb_field to get the modified default value from.
451 * @return array The SQL statement for modifying the default value.
453 public function getModifyDefaultSQL($xmldb_table, $xmldb_field) {
454 // MSSQL is a bit special with default constraints because it implements them as external constraints so
455 // normal ALTER TABLE ALTER COLUMN don't work to change defaults. Because this, we have this method overloaded here
459 // Decide if we are going to create/modify or to drop the default
460 if ($xmldb_field->getDefault() === null) {
461 $results = $this->getDropDefaultSQL($xmldb_table, $xmldb_field); //Drop but, under some circumstances, re-enable
462 $default_clause = $this->getDefaultClause($xmldb_field);
463 if ($default_clause) { //If getDefaultClause() it must have one default, create it
464 $results = array_merge($results, $this->getCreateDefaultSQL($xmldb_table, $xmldb_field)); //Create/modify
467 $results = $this->getDropDefaultSQL($xmldb_table, $xmldb_field); //Drop (only if exists)
468 $results = array_merge($results, $this->getCreateDefaultSQL($xmldb_table, $xmldb_field)); //Create/modify
475 * Given one xmldb_table and one xmldb_field, return the SQL statements needed to add its default
476 * (usually invoked from getModifyDefaultSQL()
478 * @param xmldb_table $xmldb_table The xmldb_table object instance.
479 * @param xmldb_field $xmldb_field The xmldb_field object instance.
480 * @return array Array of SQL statements to create a field's default.
482 public function getCreateDefaultSQL($xmldb_table, $xmldb_field) {
483 // MSSQL is a bit special and it requires the corresponding DEFAULT CONSTRAINT to be dropped
487 // Get the quoted name of the table and field
488 $tablename = $this->getTableName($xmldb_table);
489 $fieldname = $this->getEncQuoted($xmldb_field->getName());
491 // Now, check if, with the current field attributes, we have to build one default
492 $default_clause = $this->getDefaultClause($xmldb_field);
493 if ($default_clause) {
494 // We need to build the default (Moodle) default, so do it
495 $sql = 'ALTER TABLE ' . $tablename . ' ADD' . $default_clause . ' FOR ' . $fieldname;
503 * Given one xmldb_table and one xmldb_field, return the SQL statements needed to drop its default
504 * (usually invoked from getModifyDefaultSQL()
506 * Note that this method may be dropped in future.
508 * @param xmldb_table $xmldb_table The xmldb_table object instance.
509 * @param xmldb_field $xmldb_field The xmldb_field object instance.
510 * @return array Array of SQL statements to create a field's default.
512 * @todo MDL-31147 Moodle 2.1 - Drop getDropDefaultSQL()
514 public function getDropDefaultSQL($xmldb_table, $xmldb_field) {
515 // MSSQL is a bit special and it requires the corresponding DEFAULT CONSTRAINT to be dropped
519 // Get the quoted name of the table and field
520 $tablename = $this->getTableName($xmldb_table);
521 $fieldname = $this->getEncQuoted($xmldb_field->getName());
523 // Look for the default contraint and, if found, drop it
524 if ($defaultname = $this->getDefaultConstraintName($xmldb_table, $xmldb_field)) {
525 $results[] = 'ALTER TABLE ' . $tablename . ' DROP CONSTRAINT ' . $defaultname;
532 * Given one xmldb_table and one xmldb_field, returns the name of its default constraint in DB
533 * or false if not found
534 * This function should be considered internal and never used outside from generator
536 * @param xmldb_table $xmldb_table The xmldb_table object instance.
537 * @param xmldb_field $xmldb_field The xmldb_field object instance.
540 protected function getDefaultConstraintName($xmldb_table, $xmldb_field) {
542 // Get the quoted name of the table and field
543 $tablename = $this->getTableName($xmldb_table);
544 $fieldname = $xmldb_field->getName();
546 // Look for any default constraint in this field and drop it
547 if ($default = $this->mdb->get_record_sql("SELECT id, object_name(cdefault) AS defaultconstraint
549 WHERE id = object_id(?)
550 AND name = ?", array($tablename, $fieldname))) {
551 return $default->defaultconstraint;
558 * Given three strings (table name, list of fields (comma separated) and suffix),
559 * create the proper object name quoting it if necessary.
561 * IMPORTANT: This function must be used to CALCULATE NAMES of objects TO BE CREATED,
562 * NEVER TO GUESS NAMES of EXISTING objects!!!
564 * IMPORTANT: We are overriding this function for the MSSQL generator because objects
565 * belonging to temporary tables aren't searchable in the catalog neither in information
566 * schema tables. So, for temporary tables, we are going to add 4 randomly named "virtual"
567 * fields, so the generated names won't cause concurrency problems. Really nasty hack,
568 * but the alternative involves modifying all the creation table code to avoid naming
569 * constraints for temp objects and that will dupe a lot of code.
571 * @param string $tablename The table name.
572 * @param string $fields A list of comma separated fields.
573 * @param string $suffix A suffix for the object name.
574 * @return string Object's name.
576 public function getNameForObject($tablename, $fields, $suffix='') {
577 if ($this->temptables->is_temptable($tablename)) { // Is temp table, inject random field names
578 $random = strtolower(random_string(12)); // 12cc to be split in 4 parts
579 $fields = $fields . ', ' . implode(', ', str_split($random, 3));
581 return parent::getNameForObject($tablename, $fields, $suffix); // Delegate to parent (common) algorithm
585 * Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg).
587 * (MySQL requires the whole xmldb_table object to be specified, so we add it always)
589 * This is invoked from getNameForObject().
590 * Only some DB have this implemented.
592 * @param string $object_name The object's name to check for.
593 * @param string $type The object's type (pk, uk, fk, ck, ix, uix, seq, trg).
594 * @param string $table_name The table's name to check in
595 * @return bool If such name is currently in use (true) or no (false)
597 public function isNameInUse($object_name, $type, $table_name) {
605 if ($check = $this->mdb->get_records_sql("SELECT name
607 WHERE lower(name) = ?", array(strtolower($object_name)))) {
613 if ($check = $this->mdb->get_records_sql("SELECT name
615 WHERE lower(name) = ?", array(strtolower($object_name)))) {
620 return false; //No name in use found
624 * Returns the code (array of statements) needed to add one comment to the table.
626 * @param xmldb_table $xmldb_table The xmldb_table object instance.
627 * @return array Array of SQL statements to add one comment to the table.
629 public function getCommentSQL($xmldb_table) {
634 * Adds slashes to string.
636 * @return string The escaped string.
638 public function addslashes($s) {
639 // do not use php addslashes() because it depends on PHP quote settings!
640 $s = str_replace("'", "''", $s);
645 * Returns an array of reserved words (lowercase) for this DB
646 * @return array An array of database specific reserved words
648 public static function getReservedWords() {
649 // This file contains the reserved words for MSSQL databases
650 // from http://msdn2.microsoft.com/en-us/library/ms189822.aspx
651 // Should be identical to sqlsrv_native_moodle_database::$reservewords.
652 $reserved_words = array (
653 "add", "all", "alter", "and", "any", "as", "asc", "authorization", "avg", "backup", "begin", "between", "break",
654 "browse", "bulk", "by", "cascade", "case", "check", "checkpoint", "close", "clustered", "coalesce", "collate", "column",
655 "commit", "committed", "compute", "confirm", "constraint", "contains", "containstable", "continue", "controlrow",
656 "convert", "count", "create", "cross", "current", "current_date", "current_time", "current_timestamp", "current_user",
657 "cursor", "database", "dbcc", "deallocate", "declare", "default", "delete", "deny", "desc", "disk", "distinct",
658 "distributed", "double", "drop", "dummy", "dump", "else", "end", "errlvl", "errorexit", "escape", "except", "exec",
659 "execute", "exists", "exit", "external", "fetch", "file", "fillfactor", "floppy", "for", "foreign", "freetext",
660 "freetexttable", "from", "full", "function", "goto", "grant", "group", "having", "holdlock", "identity",
661 "identity_insert", "identitycol", "if", "in", "index", "inner", "insert", "intersect", "into", "is", "isolation",
662 "join", "key", "kill", "left", "level", "like", "lineno", "load", "max", "merge", "min", "mirrorexit", "national",
663 "nocheck", "nonclustered", "not", "null", "nullif", "of", "off", "offsets", "on", "once", "only", "open",
664 "opendatasource", "openquery", "openrowset", "openxml", "option", "or", "order", "outer", "over", "percent", "perm",
665 "permanent", "pipe", "pivot", "plan", "precision", "prepare", "primary", "print", "privileges", "proc", "procedure",
666 "processexit", "public", "raiserror", "read", "readtext", "reconfigure", "references", "repeatable", "replication",
667 "restore", "restrict", "return", "revert", "revoke", "right", "rollback", "rowcount", "rowguidcol", "rule", "save",
668 "schema", "securityaudit", "select", "semantickeyphrasetable", "semanticsimilaritydetailstable",
669 "semanticsimilaritytable", "serializable", "session_user", "set", "setuser", "shutdown", "some", "statistics", "sum",
670 "system_user", "table", "tablesample", "tape", "temp", "temporary", "textsize", "then", "to", "top", "tran",
671 "transaction", "trigger", "truncate", "try_convert", "tsequal", "uncommitted", "union", "unique", "unpivot", "update",
672 "updatetext", "use", "user", "values", "varying", "view", "waitfor", "when", "where", "while", "with", "within group",
675 return $reserved_words;