pdo_firebird_forge.php 6.1 KB

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