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.

251 lines
6.3 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 3.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Interbase/Firebird Forge Class
  41. *
  42. * @category Database
  43. * @author EllisLab Dev Team
  44. * @link https://codeigniter.com/user_guide/database/
  45. */
  46. class CI_DB_ibase_forge extends CI_DB_forge {
  47. /**
  48. * CREATE TABLE IF statement
  49. *
  50. * @var string
  51. */
  52. protected $_create_table_if = FALSE;
  53. /**
  54. * RENAME TABLE statement
  55. *
  56. * @var string
  57. */
  58. protected $_rename_table = FALSE;
  59. /**
  60. * DROP TABLE IF statement
  61. *
  62. * @var string
  63. */
  64. protected $_drop_table_if = FALSE;
  65. /**
  66. * UNSIGNED support
  67. *
  68. * @var array
  69. */
  70. protected $_unsigned = array(
  71. 'SMALLINT' => 'INTEGER',
  72. 'INTEGER' => 'INT64',
  73. 'FLOAT' => 'DOUBLE PRECISION'
  74. );
  75. /**
  76. * NULL value representation in CREATE/ALTER TABLE statements
  77. *
  78. * @var string
  79. */
  80. protected $_null = 'NULL';
  81. // --------------------------------------------------------------------
  82. /**
  83. * Create database
  84. *
  85. * @param string $db_name
  86. * @return bool
  87. */
  88. public function create_database($db_name)
  89. {
  90. // Firebird databases are flat files, so a path is required
  91. // Hostname is needed for remote access
  92. empty($this->db->hostname) OR $db_name = $this->hostname.':'.$db_name;
  93. return parent::create_database('"'.$db_name.'"');
  94. }
  95. // --------------------------------------------------------------------
  96. /**
  97. * Drop database
  98. *
  99. * @param string $db_name (ignored)
  100. * @return bool
  101. */
  102. public function drop_database($db_name)
  103. {
  104. if ( ! ibase_drop_db($this->conn_id))
  105. {
  106. return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE;
  107. }
  108. elseif ( ! empty($this->db->data_cache['db_names']))
  109. {
  110. $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE);
  111. if ($key !== FALSE)
  112. {
  113. unset($this->db->data_cache['db_names'][$key]);
  114. }
  115. }
  116. return TRUE;
  117. }
  118. // --------------------------------------------------------------------
  119. /**
  120. * ALTER TABLE
  121. *
  122. * @param string $alter_type ALTER type
  123. * @param string $table Table name
  124. * @param mixed $field Column definition
  125. * @return string|string[]
  126. */
  127. protected function _alter_table($alter_type, $table, $field)
  128. {
  129. if (in_array($alter_type, array('DROP', 'ADD'), TRUE))
  130. {
  131. return parent::_alter_table($alter_type, $table, $field);
  132. }
  133. $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
  134. $sqls = array();
  135. for ($i = 0, $c = count($field); $i < $c; $i++)
  136. {
  137. if ($field[$i]['_literal'] !== FALSE)
  138. {
  139. return FALSE;
  140. }
  141. if (isset($field[$i]['type']))
  142. {
  143. $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identififers($field[$i]['name'])
  144. .' TYPE '.$field[$i]['type'].$field[$i]['length'];
  145. }
  146. if ( ! empty($field[$i]['default']))
  147. {
  148. $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
  149. .' SET DEFAULT '.$field[$i]['default'];
  150. }
  151. if (isset($field[$i]['null']))
  152. {
  153. $sqls[] = 'UPDATE "RDB$RELATION_FIELDS" SET "RDB$NULL_FLAG" = '
  154. .($field[$i]['null'] === TRUE ? 'NULL' : '1')
  155. .' WHERE "RDB$FIELD_NAME" = '.$this->db->escape($field[$i]['name'])
  156. .' AND "RDB$RELATION_NAME" = '.$this->db->escape($table);
  157. }
  158. if ( ! empty($field[$i]['new_name']))
  159. {
  160. $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
  161. .' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
  162. }
  163. }
  164. return $sqls;
  165. }
  166. // --------------------------------------------------------------------
  167. /**
  168. * Process column
  169. *
  170. * @param array $field
  171. * @return string
  172. */
  173. protected function _process_column($field)
  174. {
  175. return $this->db->escape_identifiers($field['name'])
  176. .' '.$field['type'].$field['length']
  177. .$field['null']
  178. .$field['unique']
  179. .$field['default'];
  180. }
  181. // --------------------------------------------------------------------
  182. /**
  183. * Field attribute TYPE
  184. *
  185. * Performs a data type mapping between different databases.
  186. *
  187. * @param array &$attributes
  188. * @return void
  189. */
  190. protected function _attr_type(&$attributes)
  191. {
  192. switch (strtoupper($attributes['TYPE']))
  193. {
  194. case 'TINYINT':
  195. $attributes['TYPE'] = 'SMALLINT';
  196. $attributes['UNSIGNED'] = FALSE;
  197. return;
  198. case 'MEDIUMINT':
  199. $attributes['TYPE'] = 'INTEGER';
  200. $attributes['UNSIGNED'] = FALSE;
  201. return;
  202. case 'INT':
  203. $attributes['TYPE'] = 'INTEGER';
  204. return;
  205. case 'BIGINT':
  206. $attributes['TYPE'] = 'INT64';
  207. return;
  208. default: return;
  209. }
  210. }
  211. // --------------------------------------------------------------------
  212. /**
  213. * Field attribute AUTO_INCREMENT
  214. *
  215. * @param array &$attributes
  216. * @param array &$field
  217. * @return void
  218. */
  219. protected function _attr_auto_increment(&$attributes, &$field)
  220. {
  221. // Not supported
  222. }
  223. }