odbc_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  * ODBC Forge Class
00020  *
00021  * @category    Database
00022  * @author              ExpressionEngine Dev Team
00023  * @link                http://codeigniter.com/database/
00024  */
00025 class CI_DB_odbc_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()
00035         {
00036                 // ODBC has no "create database" command since it's
00037                 // designed to connect to an existing database
00038                 if ($this->db->db_debug)
00039                 {
00040                         return $this->db->display_error('db_unsuported_feature');
00041                 }
00042                 return FALSE;
00043         }
00044 
00045         // --------------------------------------------------------------------
00046 
00047         /**
00048          * Drop database
00049          *
00050          * @access      private
00051          * @param       string  the database name
00052          * @return      bool
00053          */
00054         function _drop_database($name)
00055         {
00056                 // ODBC has no "drop database" command since it's
00057                 // designed to connect to an existing database          
00058                 if ($this->db->db_debug)
00059                 {
00060                         return $this->db->display_error('db_unsuported_feature');
00061                 }
00062                 return FALSE;
00063         }
00064 
00065         // --------------------------------------------------------------------
00066 
00067         /**
00068          * Create Table
00069          *
00070          * @access      private
00071          * @param       string  the table name
00072          * @param       array   the fields
00073          * @param       mixed   primary key(s)
00074          * @param       mixed   key(s)
00075          * @param       boolean should 'IF NOT EXISTS' be added to the SQL
00076          * @return      bool
00077          */
00078         function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
00079         {
00080                 $sql = 'CREATE TABLE ';
00081                 
00082                 if ($if_not_exists === TRUE)
00083                 {
00084                         $sql .= 'IF NOT EXISTS ';
00085                 }
00086                 
00087                 $sql .= $this->db->_escape_identifiers($table)." (";
00088                 $current_field_count = 0;
00089 
00090                 foreach ($fields as $field=>$attributes)
00091                 {
00092                         // Numeric field names aren't allowed in databases, so if the key is
00093                         // numeric, we know it was assigned by PHP and the developer manually
00094                         // entered the field information, so we'll simply add it to the list
00095                         if (is_numeric($field))
00096                         {
00097                                 $sql .= "\n\t$attributes";
00098                         }
00099                         else
00100                         {
00101                                 $attributes = array_change_key_case($attributes, CASE_UPPER);
00102                                 
00103                                 $sql .= "\n\t".$this->db->_protect_identifiers($field);
00104                                 
00105                                 $sql .=  ' '.$attributes['TYPE'];
00106         
00107                                 if (array_key_exists('CONSTRAINT', $attributes))
00108                                 {
00109                                         $sql .= '('.$attributes['CONSTRAINT'].')';
00110                                 }
00111         
00112                                 if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
00113                                 {
00114                                         $sql .= ' UNSIGNED';
00115                                 }
00116         
00117                                 if (array_key_exists('DEFAULT', $attributes))
00118                                 {
00119                                         $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
00120                                 }
00121         
00122                                 if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
00123                                 {
00124                                         $sql .= ' NULL';
00125                                 }
00126                                 else
00127                                 {
00128                                         $sql .= ' NOT NULL';                    
00129                                 }
00130         
00131                                 if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
00132                                 {
00133                                         $sql .= ' AUTO_INCREMENT';
00134                                 }
00135                         }
00136                         
00137                         // don't add a comma on the end of the last field
00138                         if (++$current_field_count < count($fields))
00139                         {
00140                                 $sql .= ',';
00141                         }
00142                 }
00143 
00144                 if (count($primary_keys) > 0)
00145                 {
00146                         $primary_keys = $this->db->_protect_identifiers($primary_keys);
00147                         $sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
00148                 }
00149                 
00150                 if (is_array($keys) && count($keys) > 0)
00151                 {
00152                         foreach ($keys as $key)
00153                         {
00154                                 if (is_array($key))
00155                                 {
00156                                         $key = $this->db->_protect_identifiers($key);   
00157                                 }
00158                                 else
00159                                 {
00160                                         $key = array($this->db->_protect_identifiers($key));
00161                                 }
00162                                 
00163                                 $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
00164                         }
00165                 }
00166                 
00167                 $sql .= "\n)";
00168 
00169                 return $sql;
00170         }
00171 
00172         // --------------------------------------------------------------------
00173 
00174         /**
00175          * Drop Table
00176          *
00177          * @access      private
00178          * @return      bool
00179          */
00180         function _drop_table($table)
00181         {
00182                 // Not a supported ODBC feature 
00183                 if ($this->db->db_debug)
00184                 {
00185                         return $this->db->display_error('db_unsuported_feature');
00186                 }
00187                 return FALSE;
00188         }
00189 
00190         // --------------------------------------------------------------------
00191 
00192         /**
00193          * Alter table query
00194          *
00195          * Generates a platform-specific query so that a table can be altered
00196          * Called by add_column(), drop_column(), and column_alter(),
00197          *
00198          * @access      private
00199          * @param       string  the ALTER type (ADD, DROP, CHANGE)
00200          * @param       string  the column name
00201          * @param       string  the table name
00202          * @param       string  the column definition
00203          * @param       string  the default value
00204          * @param       boolean should 'NOT NULL' be added
00205          * @param       string  the field after which we should add the new field
00206          * @return      object
00207          */
00208         function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
00209         {
00210                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ".$this->db->_protect_identifiers($column_name);
00211 
00212                 // DROP has everything it needs now.
00213                 if ($alter_type == 'DROP')
00214                 {
00215                         return $sql;
00216                 }
00217 
00218                 $sql .= " $column_definition";
00219 
00220                 if ($default_value != '')
00221                 {
00222                         $sql .= " DEFAULT \"$default_value\"";
00223                 }
00224 
00225                 if ($null === NULL)
00226                 {
00227                         $sql .= ' NULL';
00228                 }
00229                 else
00230                 {
00231                         $sql .= ' NOT NULL';
00232                 }
00233 
00234                 if ($after_field != '')
00235                 {
00236                         $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
00237                 }
00238                 
00239                 return $sql;
00240                 
00241         }
00242 
00243 
00244         // --------------------------------------------------------------------
00245 
00246         /**
00247          * Rename a table
00248          *
00249          * Generates a platform-specific query so that a table can be renamed
00250          *
00251          * @access      private
00252          * @param       string  the old table name
00253          * @param       string  the new table name
00254          * @return      string
00255          */
00256         function _rename_table($table_name, $new_table_name)
00257         {
00258                 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
00259                 return $sql;
00260         }
00261 
00262 
00263 }
00264 
00265 /* End of file odbc_forge.php */
00266 /* Location: ./system/database/drivers/odbc/odbc_forge.php */