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

https://bitbucket.org/sarahman/mschool-project · PHP · 299 lines · 218 code · 24 blank · 57 comment · 21 complexity · 1805d9816da3648cf39985d22f0c507c MD5 · raw file

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