PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/drivers/mssql/mssql_forge.php

https://github.com/bwghughes/houghandco
PHP | 219 lines | 108 code | 34 blank | 77 comment | 22 complexity | b3c75906e52171eecf64785aab044084 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * MS SQL Forge Class
  18. *
  19. * @category Database
  20. * @author ExpressionEngine Dev Team
  21. * @link http://codeigniter.com/user_guide/database/
  22. */
  23. class CI_DB_mssql_forge extends CI_DB_forge {
  24. /**
  25. * Create database
  26. *
  27. * @access private
  28. * @param string the database name
  29. * @return bool
  30. */
  31. function _create_database($name)
  32. {
  33. return "CREATE DATABASE ".$name;
  34. }
  35. // --------------------------------------------------------------------
  36. /**
  37. * Drop database
  38. *
  39. * @access private
  40. * @param string the database name
  41. * @return bool
  42. */
  43. function _drop_database($name)
  44. {
  45. return "DROP DATABASE ".$name;
  46. }
  47. // --------------------------------------------------------------------
  48. /**
  49. * Drop Table
  50. *
  51. * @access private
  52. * @return bool
  53. */
  54. function _drop_table($table)
  55. {
  56. return "DROP TABLE ".$this->db->_escape_table($table);
  57. }
  58. // --------------------------------------------------------------------
  59. /**
  60. * Create Table
  61. *
  62. * @access private
  63. * @param string the table name
  64. * @param array the fields
  65. * @param mixed primary key(s)
  66. * @param mixed key(s)
  67. * @param boolean should 'IF NOT EXISTS' be added to the SQL
  68. * @return bool
  69. */
  70. function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
  71. {
  72. $sql = 'CREATE TABLE ';
  73. if ($if_not_exists === TRUE)
  74. {
  75. $sql .= 'IF NOT EXISTS ';
  76. }
  77. $sql .= $this->db->_escape_table($table)." (";
  78. $current_field_count = 0;
  79. foreach ($fields as $field=>$attributes)
  80. {
  81. // Numeric field names aren't allowed in databases, so if the key is
  82. // numeric, we know it was assigned by PHP and the developer manually
  83. // entered the field information, so we'll simply add it to the list
  84. if (is_numeric($field))
  85. {
  86. $sql .= "\n\t$attributes";
  87. }
  88. else
  89. {
  90. $attributes = array_change_key_case($attributes, CASE_UPPER);
  91. $sql .= "\n\t".$this->db->_protect_identifiers($field);
  92. $sql .= ' '.$attributes['TYPE'];
  93. if (array_key_exists('CONSTRAINT', $attributes))
  94. {
  95. $sql .= '('.$attributes['CONSTRAINT'].')';
  96. }
  97. if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
  98. {
  99. $sql .= ' UNSIGNED';
  100. }
  101. if (array_key_exists('DEFAULT', $attributes))
  102. {
  103. $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
  104. }
  105. if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
  106. {
  107. $sql .= ' NULL';
  108. }
  109. else
  110. {
  111. $sql .= ' NOT NULL';
  112. }
  113. if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
  114. {
  115. $sql .= ' AUTO_INCREMENT';
  116. }
  117. }
  118. // don't add a comma on the end of the last field
  119. if (++$current_field_count < count($fields))
  120. {
  121. $sql .= ',';
  122. }
  123. }
  124. if (count($primary_keys) > 0)
  125. {
  126. $primary_keys = $this->db->_protect_identifiers($primary_keys);
  127. $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
  128. }
  129. if (is_array($keys) && count($keys) > 0)
  130. {
  131. $keys = $this->db->_protect_identifiers($keys);
  132. foreach ($keys as $key)
  133. {
  134. $sql .= ",\n\tFOREIGN KEY ($key)";
  135. }
  136. }
  137. $sql .= "\n)";
  138. return $sql;
  139. }
  140. // --------------------------------------------------------------------
  141. /**
  142. * Alter table query
  143. *
  144. * Generates a platform-specific query so that a table can be altered
  145. * Called by add_column(), drop_column(), and column_alter(),
  146. *
  147. * @access private
  148. * @param string the ALTER type (ADD, DROP, CHANGE)
  149. * @param string the column name
  150. * @param string the table name
  151. * @param string the column definition
  152. * @param string the default value
  153. * @param boolean should 'NOT NULL' be added
  154. * @param string the field after which we should add the new field
  155. * @return object
  156. */
  157. function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
  158. {
  159. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
  160. // DROP has everything it needs now.
  161. if ($alter_type == 'DROP')
  162. {
  163. return $sql;
  164. }
  165. $sql .= " $column_definition";
  166. if ($default_value != '')
  167. {
  168. $sql .= " DEFAULT \"$default_value\"";
  169. }
  170. if ($null === NULL)
  171. {
  172. $sql .= ' NULL';
  173. }
  174. else
  175. {
  176. $sql .= ' NOT NULL';
  177. }
  178. if ($after_field != '')
  179. {
  180. $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
  181. }
  182. return $sql;
  183. }
  184. }
  185. ?>