PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/system/database/drivers/postgre/postgre_forge.php

http://github.com/imagecms/ImageCMS
PHP | 300 lines | 218 code | 24 blank | 58 comment | 21 complexity | 5b52a67e493d43d27fc01480adfe7dcc MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * Postgre Forge Class
  19. *
  20. * @category Database
  21. * @author EllisLab Dev Team
  22. * @link http://codeigniter.com/user_guide/database/
  23. */
  24. class CI_DB_postgre_forge extends CI_DB_forge {
  25. /**
  26. * Create database
  27. *
  28. * @access private
  29. * @param string the database name
  30. * @return bool
  31. */
  32. function _create_database($name)
  33. {
  34. return "CREATE DATABASE ".$name;
  35. }
  36. // --------------------------------------------------------------------
  37. /**
  38. * Drop database
  39. *
  40. * @access private
  41. * @param string the database name
  42. * @return bool
  43. */
  44. function _drop_database($name)
  45. {
  46. return "DROP DATABASE ".$name;
  47. }
  48. // --------------------------------------------------------------------
  49. /**
  50. * Create Table
  51. *
  52. * @access private
  53. * @param string the table name
  54. * @param array the fields
  55. * @param mixed primary key(s)
  56. * @param mixed key(s)
  57. * @param boolean should 'IF NOT EXISTS' be added to the SQL
  58. * @return bool
  59. */
  60. function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
  61. {
  62. $sql = 'CREATE TABLE ';
  63. if ($if_not_exists === TRUE)
  64. {
  65. if ($this->db->table_exists($table))
  66. {
  67. return "SELECT * FROM $table"; // Needs to return innocous but valid SQL statement
  68. }
  69. }
  70. $sql .= $this->db->_escape_identifiers($table)." (";
  71. $current_field_count = 0;
  72. foreach ($fields as $field=>$attributes)
  73. {
  74. // Numeric field names aren't allowed in databases, so if the key is
  75. // numeric, we know it was assigned by PHP and the developer manually
  76. // entered the field information, so we'll simply add it to the list
  77. if (is_numeric($field))
  78. {
  79. $sql .= "\n\t$attributes";
  80. }
  81. else
  82. {
  83. $attributes = array_change_key_case($attributes, CASE_UPPER);
  84. $sql .= "\n\t".$this->db->_protect_identifiers($field);
  85. $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
  86. // Convert datatypes to be PostgreSQL-compatible
  87. switch (strtoupper($attributes['TYPE']))
  88. {
  89. case 'TINYINT':
  90. $attributes['TYPE'] = 'SMALLINT';
  91. break;
  92. case 'SMALLINT':
  93. $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
  94. break;
  95. case 'MEDIUMINT':
  96. $attributes['TYPE'] = 'INTEGER';
  97. break;
  98. case 'INT':
  99. $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
  100. break;
  101. case 'BIGINT':
  102. $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
  103. break;
  104. case 'DOUBLE':
  105. $attributes['TYPE'] = 'DOUBLE PRECISION';
  106. break;
  107. case 'DATETIME':
  108. $attributes['TYPE'] = 'TIMESTAMP';
  109. break;
  110. case 'LONGTEXT':
  111. $attributes['TYPE'] = 'TEXT';
  112. break;
  113. case 'BLOB':
  114. $attributes['TYPE'] = 'BYTEA';
  115. break;
  116. }
  117. // If this is an auto-incrementing primary key, use the serial data type instead
  118. if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes)
  119. && $attributes['AUTO_INCREMENT'] === TRUE)
  120. {
  121. $sql .= ' SERIAL';
  122. }
  123. else
  124. {
  125. $sql .= ' '.$attributes['TYPE'];
  126. }
  127. // Modified to prevent constraints with integer data types
  128. if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false)
  129. {
  130. $sql .= '('.$attributes['CONSTRAINT'].')';
  131. }
  132. if (array_key_exists('DEFAULT', $attributes))
  133. {
  134. $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
  135. }
  136. if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
  137. {
  138. $sql .= ' NULL';
  139. }
  140. else
  141. {
  142. $sql .= ' NOT NULL';
  143. }
  144. // Added new attribute to create unqite fields. Also works with MySQL
  145. if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
  146. {
  147. $sql .= ' UNIQUE';
  148. }
  149. }
  150. // don't add a comma on the end of the last field
  151. if (++$current_field_count < count($fields))
  152. {
  153. $sql .= ',';
  154. }
  155. }
  156. if (count($primary_keys) > 0)
  157. {
  158. // Something seems to break when passing an array to _protect_identifiers()
  159. foreach ($primary_keys as $index => $key)
  160. {
  161. $primary_keys[$index] = $this->db->_protect_identifiers($key);
  162. }
  163. $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
  164. }
  165. $sql .= "\n);";
  166. if (is_array($keys) && count($keys) > 0)
  167. {
  168. foreach ($keys as $key)
  169. {
  170. if (is_array($key))
  171. {
  172. $key = $this->db->_protect_identifiers($key);
  173. }
  174. else
  175. {
  176. $key = array($this->db->_protect_identifiers($key));
  177. }
  178. foreach ($key as $field)
  179. {
  180. $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); ";
  181. }
  182. }
  183. }
  184. return $sql;
  185. }
  186. // --------------------------------------------------------------------
  187. /**
  188. * Drop Table
  189. *
  190. * @access private
  191. * @return bool
  192. */
  193. function _drop_table($table)
  194. {
  195. return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE";
  196. }
  197. // --------------------------------------------------------------------
  198. /**
  199. * Alter table query
  200. *
  201. * Generates a platform-specific query so that a table can be altered
  202. * Called by add_column(), drop_column(), and column_alter(),
  203. *
  204. * @access private
  205. * @param string the ALTER type (ADD, DROP, CHANGE)
  206. * @param string the column name
  207. * @param string the table name
  208. * @param string the column definition
  209. * @param string the default value
  210. * @param boolean should 'NOT NULL' be added
  211. * @param string the field after which we should add the new field
  212. * @return object
  213. */
  214. function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
  215. {
  216. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
  217. // DROP has everything it needs now.
  218. if ($alter_type == 'DROP')
  219. {
  220. return $sql;
  221. }
  222. $sql .= " $column_definition";
  223. if ($default_value != '')
  224. {
  225. $sql .= " DEFAULT \"$default_value\"";
  226. }
  227. if ($null === NULL)
  228. {
  229. $sql .= ' NULL';
  230. }
  231. else
  232. {
  233. $sql .= ' NOT NULL';
  234. }
  235. if ($after_field != '')
  236. {
  237. $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
  238. }
  239. return $sql;
  240. }
  241. // --------------------------------------------------------------------
  242. /**
  243. * Rename a table
  244. *
  245. * Generates a platform-specific query so that a table can be renamed
  246. *
  247. * @access private
  248. * @param string the old table name
  249. * @param string the new table name
  250. * @return string
  251. */
  252. function _rename_table($table_name, $new_table_name)
  253. {
  254. $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
  255. return $sql;
  256. }
  257. }
  258. /* End of file postgre_forge.php */
  259. /* Location: ./system/database/drivers/postgre/postgre_forge.php */