You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
5.7 KiB

7 years ago
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 2.1.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CUBRID Forge Class
  41. *
  42. * @category Database
  43. * @author Esen Sagynov
  44. * @link https://codeigniter.com/user_guide/database/
  45. */
  46. class CI_DB_cubrid_forge extends CI_DB_forge {
  47. /**
  48. * CREATE DATABASE statement
  49. *
  50. * @var string
  51. */
  52. protected $_create_database = FALSE;
  53. /**
  54. * CREATE TABLE keys flag
  55. *
  56. * Whether table keys are created from within the
  57. * CREATE TABLE statement.
  58. *
  59. * @var bool
  60. */
  61. protected $_create_table_keys = TRUE;
  62. /**
  63. * DROP DATABASE statement
  64. *
  65. * @var string
  66. */
  67. protected $_drop_database = FALSE;
  68. /**
  69. * CREATE TABLE IF statement
  70. *
  71. * @var string
  72. */
  73. protected $_create_table_if = FALSE;
  74. /**
  75. * UNSIGNED support
  76. *
  77. * @var array
  78. */
  79. protected $_unsigned = array(
  80. 'SHORT' => 'INTEGER',
  81. 'SMALLINT' => 'INTEGER',
  82. 'INT' => 'BIGINT',
  83. 'INTEGER' => 'BIGINT',
  84. 'BIGINT' => 'NUMERIC',
  85. 'FLOAT' => 'DOUBLE',
  86. 'REAL' => 'DOUBLE'
  87. );
  88. // --------------------------------------------------------------------
  89. /**
  90. * ALTER TABLE
  91. *
  92. * @param string $alter_type ALTER type
  93. * @param string $table Table name
  94. * @param mixed $field Column definition
  95. * @return string|string[]
  96. */
  97. protected function _alter_table($alter_type, $table, $field)
  98. {
  99. if (in_array($alter_type, array('DROP', 'ADD'), TRUE))
  100. {
  101. return parent::_alter_table($alter_type, $table, $field);
  102. }
  103. $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
  104. $sqls = array();
  105. for ($i = 0, $c = count($field); $i < $c; $i++)
  106. {
  107. if ($field[$i]['_literal'] !== FALSE)
  108. {
  109. $sqls[] = $sql.' CHANGE '.$field[$i]['_literal'];
  110. }
  111. else
  112. {
  113. $alter_type = empty($field[$i]['new_name']) ? ' MODIFY ' : ' CHANGE ';
  114. $sqls[] = $sql.$alter_type.$this->_process_column($field[$i]);
  115. }
  116. }
  117. return $sqls;
  118. }
  119. // --------------------------------------------------------------------
  120. /**
  121. * Process column
  122. *
  123. * @param array $field
  124. * @return string
  125. */
  126. protected function _process_column($field)
  127. {
  128. $extra_clause = isset($field['after'])
  129. ? ' AFTER '.$this->db->escape_identifiers($field['after']) : '';
  130. if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE)
  131. {
  132. $extra_clause = ' FIRST';
  133. }
  134. return $this->db->escape_identifiers($field['name'])
  135. .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
  136. .' '.$field['type'].$field['length']
  137. .$field['unsigned']
  138. .$field['null']
  139. .$field['default']
  140. .$field['auto_increment']
  141. .$field['unique']
  142. .$extra_clause;
  143. }
  144. // --------------------------------------------------------------------
  145. /**
  146. * Field attribute TYPE
  147. *
  148. * Performs a data type mapping between different databases.
  149. *
  150. * @param array &$attributes
  151. * @return void
  152. */
  153. protected function _attr_type(&$attributes)
  154. {
  155. switch (strtoupper($attributes['TYPE']))
  156. {
  157. case 'TINYINT':
  158. $attributes['TYPE'] = 'SMALLINT';
  159. $attributes['UNSIGNED'] = FALSE;
  160. return;
  161. case 'MEDIUMINT':
  162. $attributes['TYPE'] = 'INTEGER';
  163. $attributes['UNSIGNED'] = FALSE;
  164. return;
  165. case 'LONGTEXT':
  166. $attributes['TYPE'] = 'STRING';
  167. return;
  168. default: return;
  169. }
  170. }
  171. // --------------------------------------------------------------------
  172. /**
  173. * Process indexes
  174. *
  175. * @param string $table (ignored)
  176. * @return string
  177. */
  178. protected function _process_indexes($table)
  179. {
  180. $sql = '';
  181. for ($i = 0, $c = count($this->keys); $i < $c; $i++)
  182. {
  183. if (is_array($this->keys[$i]))
  184. {
  185. for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++)
  186. {
  187. if ( ! isset($this->fields[$this->keys[$i][$i2]]))
  188. {
  189. unset($this->keys[$i][$i2]);
  190. continue;
  191. }
  192. }
  193. }
  194. elseif ( ! isset($this->fields[$this->keys[$i]]))
  195. {
  196. unset($this->keys[$i]);
  197. continue;
  198. }
  199. is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
  200. $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
  201. .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
  202. }
  203. $this->keys = array();
  204. return $sql;
  205. }
  206. }