cubrid_forge.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author Esen Sagynov
  9. * @copyright Copyright (c) 2008 - 2011, 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. * CUBRID Forge Class
  18. *
  19. * @category Database
  20. * @author Esen Sagynov
  21. * @link http://codeigniter.com/user_guide/database/
  22. */
  23. class CI_DB_cubrid_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. // CUBRID does not allow to create a database in SQL. The GUI tools
  34. // have to be used for this purpose.
  35. return FALSE;
  36. }
  37. // --------------------------------------------------------------------
  38. /**
  39. * Drop database
  40. *
  41. * @access private
  42. * @param string the database name
  43. * @return bool
  44. */
  45. function _drop_database($name)
  46. {
  47. // CUBRID does not allow to drop a database in SQL. The GUI tools
  48. // have to be used for this purpose.
  49. return FALSE;
  50. }
  51. // --------------------------------------------------------------------
  52. /**
  53. * Process Fields
  54. *
  55. * @access private
  56. * @param mixed the fields
  57. * @return string
  58. */
  59. function _process_fields($fields)
  60. {
  61. $current_field_count = 0;
  62. $sql = '';
  63. foreach ($fields as $field=>$attributes)
  64. {
  65. // Numeric field names aren't allowed in databases, so if the key is
  66. // numeric, we know it was assigned by PHP and the developer manually
  67. // entered the field information, so we'll simply add it to the list
  68. if (is_numeric($field))
  69. {
  70. $sql .= "\n\t$attributes";
  71. }
  72. else
  73. {
  74. $attributes = array_change_key_case($attributes, CASE_UPPER);
  75. $sql .= "\n\t\"" . $this->db->_protect_identifiers($field) . "\"";
  76. if (array_key_exists('NAME', $attributes))
  77. {
  78. $sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' ';
  79. }
  80. if (array_key_exists('TYPE', $attributes))
  81. {
  82. $sql .= ' '.$attributes['TYPE'];
  83. if (array_key_exists('CONSTRAINT', $attributes))
  84. {
  85. switch ($attributes['TYPE'])
  86. {
  87. case 'decimal':
  88. case 'float':
  89. case 'numeric':
  90. $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
  91. break;
  92. case 'enum': // As of version 8.4.0 CUBRID does not support
  93. // enum data type.
  94. break;
  95. case 'set':
  96. $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
  97. break;
  98. default:
  99. $sql .= '('.$attributes['CONSTRAINT'].')';
  100. }
  101. }
  102. }
  103. if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
  104. {
  105. //$sql .= ' UNSIGNED';
  106. // As of version 8.4.0 CUBRID does not support UNSIGNED INTEGER data type.
  107. // Will be supported in the next release as a part of MySQL Compatibility.
  108. }
  109. if (array_key_exists('DEFAULT', $attributes))
  110. {
  111. $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
  112. }
  113. if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
  114. {
  115. $sql .= ' NULL';
  116. }
  117. else
  118. {
  119. $sql .= ' NOT NULL';
  120. }
  121. if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
  122. {
  123. $sql .= ' AUTO_INCREMENT';
  124. }
  125. if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
  126. {
  127. $sql .= ' UNIQUE';
  128. }
  129. }
  130. // don't add a comma on the end of the last field
  131. if (++$current_field_count < count($fields))
  132. {
  133. $sql .= ',';
  134. }
  135. }
  136. return $sql;
  137. }
  138. // --------------------------------------------------------------------
  139. /**
  140. * Create Table
  141. *
  142. * @access private
  143. * @param string the table name
  144. * @param mixed the fields
  145. * @param mixed primary key(s)
  146. * @param mixed key(s)
  147. * @param boolean should 'IF NOT EXISTS' be added to the SQL
  148. * @return bool
  149. */
  150. function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
  151. {
  152. $sql = 'CREATE TABLE ';
  153. if ($if_not_exists === TRUE)
  154. {
  155. //$sql .= 'IF NOT EXISTS ';
  156. // As of version 8.4.0 CUBRID does not support this SQL syntax.
  157. }
  158. $sql .= $this->db->_escape_identifiers($table)." (";
  159. $sql .= $this->_process_fields($fields);
  160. // If there is a PK defined
  161. if (count($primary_keys) > 0)
  162. {
  163. $key_name = "pk_" . $table . "_" .
  164. $this->db->_protect_identifiers(implode('_', $primary_keys));
  165. $primary_keys = $this->db->_protect_identifiers($primary_keys);
  166. $sql .= ",\n\tCONSTRAINT " . $key_name . " PRIMARY KEY(" . implode(', ', $primary_keys) . ")";
  167. }
  168. if (is_array($keys) && count($keys) > 0)
  169. {
  170. foreach ($keys as $key)
  171. {
  172. if (is_array($key))
  173. {
  174. $key_name = $this->db->_protect_identifiers(implode('_', $key));
  175. $key = $this->db->_protect_identifiers($key);
  176. }
  177. else
  178. {
  179. $key_name = $this->db->_protect_identifiers($key);
  180. $key = array($key_name);
  181. }
  182. $sql .= ",\n\tKEY \"{$key_name}\" (" . implode(', ', $key) . ")";
  183. }
  184. }
  185. $sql .= "\n);";
  186. return $sql;
  187. }
  188. // --------------------------------------------------------------------
  189. /**
  190. * Drop Table
  191. *
  192. * @access private
  193. * @return string
  194. */
  195. function _drop_table($table)
  196. {
  197. return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
  198. }
  199. // --------------------------------------------------------------------
  200. /**
  201. * Alter table query
  202. *
  203. * Generates a platform-specific query so that a table can be altered
  204. * Called by add_column(), drop_column(), and column_alter(),
  205. *
  206. * @access private
  207. * @param string the ALTER type (ADD, DROP, CHANGE)
  208. * @param string the column name
  209. * @param array fields
  210. * @param string the field after which we should add the new field
  211. * @return object
  212. */
  213. function _alter_table($alter_type, $table, $fields, $after_field = '')
  214. {
  215. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
  216. // DROP has everything it needs now.
  217. if ($alter_type == 'DROP')
  218. {
  219. return $sql.$this->db->_protect_identifiers($fields);
  220. }
  221. $sql .= $this->_process_fields($fields);
  222. if ($after_field != '')
  223. {
  224. $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
  225. }
  226. return $sql;
  227. }
  228. // --------------------------------------------------------------------
  229. /**
  230. * Rename a table
  231. *
  232. * Generates a platform-specific query so that a table can be renamed
  233. *
  234. * @access private
  235. * @param string the old table name
  236. * @param string the new table name
  237. * @return string
  238. */
  239. function _rename_table($table_name, $new_table_name)
  240. {
  241. $sql = 'RENAME TABLE '.$this->db->_protect_identifiers($table_name)." AS ".$this->db->_protect_identifiers($new_table_name);
  242. return $sql;
  243. }
  244. }
  245. /* End of file cubrid_forge.php */
  246. /* Location: ./system/database/drivers/cubrid/cubrid_forge.php */