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.

205 lines
5.6 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 1.3.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Postgre Forge Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Drivers
  44. * @category Database
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/database/
  47. */
  48. class CI_DB_postgre_forge extends CI_DB_forge {
  49. /**
  50. * UNSIGNED support
  51. *
  52. * @var array
  53. */
  54. protected $_unsigned = array(
  55. 'INT2' => 'INTEGER',
  56. 'SMALLINT' => 'INTEGER',
  57. 'INT' => 'BIGINT',
  58. 'INT4' => 'BIGINT',
  59. 'INTEGER' => 'BIGINT',
  60. 'INT8' => 'NUMERIC',
  61. 'BIGINT' => 'NUMERIC',
  62. 'REAL' => 'DOUBLE PRECISION',
  63. 'FLOAT' => 'DOUBLE PRECISION'
  64. );
  65. /**
  66. * NULL value representation in CREATE/ALTER TABLE statements
  67. *
  68. * @var string
  69. */
  70. protected $_null = 'NULL';
  71. // --------------------------------------------------------------------
  72. /**
  73. * Class constructor
  74. *
  75. * @param object &$db Database object
  76. * @return void
  77. */
  78. public function __construct(&$db)
  79. {
  80. parent::__construct($db);
  81. if (version_compare($this->db->version(), '9.0', '>'))
  82. {
  83. $this->create_table_if = 'CREATE TABLE IF NOT EXISTS';
  84. }
  85. }
  86. // --------------------------------------------------------------------
  87. /**
  88. * ALTER TABLE
  89. *
  90. * @param string $alter_type ALTER type
  91. * @param string $table Table name
  92. * @param mixed $field Column definition
  93. * @return string|string[]
  94. */
  95. protected function _alter_table($alter_type, $table, $field)
  96. {
  97. if (in_array($alter_type, array('DROP', 'ADD'), TRUE))
  98. {
  99. return parent::_alter_table($alter_type, $table, $field);
  100. }
  101. $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
  102. $sqls = array();
  103. for ($i = 0, $c = count($field); $i < $c; $i++)
  104. {
  105. if ($field[$i]['_literal'] !== FALSE)
  106. {
  107. return FALSE;
  108. }
  109. if (version_compare($this->db->version(), '8', '>=') && isset($field[$i]['type']))
  110. {
  111. $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
  112. .' TYPE '.$field[$i]['type'].$field[$i]['length'];
  113. }
  114. if ( ! empty($field[$i]['default']))
  115. {
  116. $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
  117. .' SET DEFAULT '.$field[$i]['default'];
  118. }
  119. if (isset($field[$i]['null']))
  120. {
  121. $sqls[] = $sql.' ALTER COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
  122. .($field[$i]['null'] === TRUE ? ' DROP NOT NULL' : ' SET NOT NULL');
  123. }
  124. if ( ! empty($field[$i]['new_name']))
  125. {
  126. $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name'])
  127. .' TO '.$this->db->escape_identifiers($field[$i]['new_name']);
  128. }
  129. if ( ! empty($field[$i]['comment']))
  130. {
  131. $sqls[] = 'COMMENT ON COLUMN '
  132. .$this->db->escape_identifiers($table).'.'.$this->db->escape_identifiers($field[$i]['name'])
  133. .' IS '.$field[$i]['comment'];
  134. }
  135. }
  136. return $sqls;
  137. }
  138. // --------------------------------------------------------------------
  139. /**
  140. * Field attribute TYPE
  141. *
  142. * Performs a data type mapping between different databases.
  143. *
  144. * @param array &$attributes
  145. * @return void
  146. */
  147. protected function _attr_type(&$attributes)
  148. {
  149. // Reset field lengths for data types that don't support it
  150. if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== FALSE)
  151. {
  152. $attributes['CONSTRAINT'] = NULL;
  153. }
  154. switch (strtoupper($attributes['TYPE']))
  155. {
  156. case 'TINYINT':
  157. $attributes['TYPE'] = 'SMALLINT';
  158. $attributes['UNSIGNED'] = FALSE;
  159. return;
  160. case 'MEDIUMINT':
  161. $attributes['TYPE'] = 'INTEGER';
  162. $attributes['UNSIGNED'] = FALSE;
  163. return;
  164. default: return;
  165. }
  166. }
  167. // --------------------------------------------------------------------
  168. /**
  169. * Field attribute AUTO_INCREMENT
  170. *
  171. * @param array &$attributes
  172. * @param array &$field
  173. * @return void
  174. */
  175. protected function _attr_auto_increment(&$attributes, &$field)
  176. {
  177. if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
  178. {
  179. $field['type'] = ($field['type'] === 'NUMERIC')
  180. ? 'BIGSERIAL'
  181. : 'SERIAL';
  182. }
  183. }
  184. }