PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/drivers/cubrid/cubrid_forge.php

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