postgre_forge.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Postgre Forge Class
00020  *
00021  * @category    Database
00022  * @author              ExpressionEngine Dev Team
00023  * @link                http://codeigniter.com/user_guide/database/
00024  */
00025 class CI_DB_postgre_forge extends CI_DB_forge {
00026 
00027         /**
00028          * Create database
00029          *
00030          * @access      private
00031          * @param       string  the database name
00032          * @return      bool
00033          */
00034         function _create_database($name)
00035         {
00036                 return "CREATE DATABASE ".$name;
00037         }
00038 
00039         // --------------------------------------------------------------------
00040 
00041         /**
00042          * Drop database
00043          *
00044          * @access      private
00045          * @param       string  the database name
00046          * @return      bool
00047          */
00048         function _drop_database($name)
00049         {
00050                 return "DROP DATABASE ".$name;
00051         }
00052 
00053         // --------------------------------------------------------------------
00054 
00055         /**
00056          * Create Table
00057          *
00058          * @access      private
00059          * @param       string  the table name
00060          * @param       array   the fields
00061          * @param       mixed   primary key(s)
00062          * @param       mixed   key(s)
00063          * @param       boolean should 'IF NOT EXISTS' be added to the SQL
00064          * @return      bool
00065          */
00066         function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
00067         {
00068                 $sql = 'CREATE TABLE ';
00069                 
00070                 if ($if_not_exists === TRUE)
00071                 {
00072                         $sql .= 'IF NOT EXISTS ';
00073                 }
00074                 
00075                 $sql .= $this->db->_escape_identifiers($table)." (";
00076                 $current_field_count = 0;
00077 
00078                 foreach ($fields as $field=>$attributes)
00079                 {
00080                         // Numeric field names aren't allowed in databases, so if the key is
00081                         // numeric, we know it was assigned by PHP and the developer manually
00082                         // entered the field information, so we'll simply add it to the list
00083                         if (is_numeric($field))
00084                         {
00085                                 $sql .= "\n\t$attributes";
00086                         }
00087                         else
00088                         {
00089                                 $attributes = array_change_key_case($attributes, CASE_UPPER);
00090                                 
00091                                 $sql .= "\n\t".$this->db->_protect_identifiers($field);
00092                                 
00093                                 $sql .=  ' '.$attributes['TYPE'];
00094         
00095                                 if (array_key_exists('CONSTRAINT', $attributes))
00096                                 {
00097                                         $sql .= '('.$attributes['CONSTRAINT'].')';
00098                                 }
00099         
00100                                 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
00101                                 {
00102                                         $sql .= ' UNSIGNED';
00103                                 }
00104         
00105                                 if (array_key_exists('DEFAULT', $attributes))
00106                                 {
00107                                         $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
00108                                 }
00109         
00110                                 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
00111                                 {
00112                                         $sql .= ' NULL';
00113                                 }
00114                                 else
00115                                 {
00116                                         $sql .= ' NOT NULL';                    
00117                                 }
00118         
00119                                 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
00120                                 {
00121                                         $sql .= ' AUTO_INCREMENT';
00122                                 }
00123                         }
00124                         
00125                         // don't add a comma on the end of the last field
00126                         if (++$current_field_count < count($fields))
00127                         {
00128                                 $sql .= ',';
00129                         }
00130                 }
00131 
00132                 if (count($primary_keys) > 0)
00133                 {
00134                         $primary_keys = $this->db->_protect_identifiers($primary_keys);
00135                         $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
00136                 }
00137                 
00138                 if (is_array($keys) && count($keys) > 0)
00139                 {
00140                         foreach ($keys as $key)
00141                         {
00142                                 if (is_array($key))
00143                                 {
00144                                         $key = $this->db->_protect_identifiers($key);   
00145                                 }
00146                                 else
00147                                 {
00148                                         $key = array($this->db->_protect_identifiers($key));
00149                                 }
00150                                 
00151                                 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
00152                         }
00153                 }
00154 
00155                 $sql .= "\n);";
00156 
00157                 return $sql;
00158         }
00159 
00160         // --------------------------------------------------------------------
00161 
00162         /**
00163          * Drop Table
00164          *
00165          * @access      private
00166          * @return      bool
00167          */
00168         function _drop_table($table)
00169         {
00170                 return "DROP TABLE ".$this->db->_escape_identifiers($table)." CASCADE";
00171         }
00172 
00173         // --------------------------------------------------------------------
00174 
00175         /**
00176          * Alter table query
00177          *
00178          * Generates a platform-specific query so that a table can be altered
00179          * Called by add_column(), drop_column(), and column_alter(),
00180          *
00181          * @access      private
00182          * @param       string  the ALTER type (ADD, DROP, CHANGE)
00183          * @param       string  the column name
00184          * @param       string  the table name
00185          * @param       string  the column definition
00186          * @param       string  the default value
00187          * @param       boolean should 'NOT NULL' be added
00188          * @param       string  the field after which we should add the new field
00189          * @return      object
00190          */
00191         function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
00192         {
00193                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
00194 
00195                 // DROP has everything it needs now.
00196                 if ($alter_type == 'DROP')
00197                 {
00198                         return $sql;
00199                 }
00200 
00201                 $sql .= " $column_definition";
00202 
00203                 if ($default_value != '')
00204                 {
00205                         $sql .= " DEFAULT \"$default_value\"";
00206                 }
00207 
00208                 if ($null === NULL)
00209                 {
00210                         $sql .= ' NULL';
00211                 }
00212                 else
00213                 {
00214                         $sql .= ' NOT NULL';
00215                 }
00216 
00217                 if ($after_field != '')
00218                 {
00219                         $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
00220                 }
00221                 
00222                 return $sql;
00223                 
00224         }
00225 
00226         // --------------------------------------------------------------------
00227 
00228         /**
00229          * Rename a table
00230          *
00231          * Generates a platform-specific query so that a table can be renamed
00232          *
00233          * @access      private
00234          * @param       string  the old table name
00235          * @param       string  the new table name
00236          * @return      string
00237          */
00238         function _rename_table($table_name, $new_table_name)
00239         {
00240                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
00241                 return $sql;
00242         }
00243 
00244 
00245 }
00246 
00247 /* End of file postgre_forge.php */
00248 /* Location: ./system/database/drivers/postgre/postgre_forge.php */