pdo_pgsql_forge.php 5.7 KB

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