/halogy/database/drivers/mssql/mssql_forge.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 248 lines · 119 code · 38 blank · 91 comment · 23 complexity · 7986b1e6aad7486099c283b8bdef00bd 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) 2008 - 2009, 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_identifiers($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_identifiers($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. foreach ($keys as $key)
  132. {
  133. if (is_array($key))
  134. {
  135. $key = $this->db->_protect_identifiers($key);
  136. }
  137. else
  138. {
  139. $key = array($this->db->_protect_identifiers($key));
  140. }
  141. $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
  142. }
  143. }
  144. $sql .= "\n)";
  145. return $sql;
  146. }
  147. // --------------------------------------------------------------------
  148. /**
  149. * Alter table query
  150. *
  151. * Generates a platform-specific query so that a table can be altered
  152. * Called by add_column(), drop_column(), and column_alter(),
  153. *
  154. * @access private
  155. * @param string the ALTER type (ADD, DROP, CHANGE)
  156. * @param string the column name
  157. * @param string the table name
  158. * @param string the column definition
  159. * @param string the default value
  160. * @param boolean should 'NOT NULL' be added
  161. * @param string the field after which we should add the new field
  162. * @return object
  163. */
  164. function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
  165. {
  166. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
  167. // DROP has everything it needs now.
  168. if ($alter_type == 'DROP')
  169. {
  170. return $sql;
  171. }
  172. $sql .= " $column_definition";
  173. if ($default_value != '')
  174. {
  175. $sql .= " DEFAULT \"$default_value\"";
  176. }
  177. if ($null === NULL)
  178. {
  179. $sql .= ' NULL';
  180. }
  181. else
  182. {
  183. $sql .= ' NOT NULL';
  184. }
  185. if ($after_field != '')
  186. {
  187. $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
  188. }
  189. return $sql;
  190. }
  191. // --------------------------------------------------------------------
  192. /**
  193. * Rename a table
  194. *
  195. * Generates a platform-specific query so that a table can be renamed
  196. *
  197. * @access private
  198. * @param string the old table name
  199. * @param string the new table name
  200. * @return string
  201. */
  202. function _rename_table($table_name, $new_table_name)
  203. {
  204. // I think this syntax will work, but can find little documentation on renaming tables in MSSQL
  205. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
  206. return $sql;
  207. }
  208. }
  209. /* End of file mssql_forge.php */
  210. /* Location: ./system/database/drivers/mssql/mssql_forge.php */