blob: 05db86253665901791b64eb374231b94f44e25d7 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev2caf2892012-01-27 00:12:03 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev2caf2892012-01-27 00:12:03 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * MySQL Forge Class
31 *
32 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050033 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * @link http://codeigniter.com/user_guide/database/
35 */
36class CI_DB_mysql_forge extends CI_DB_forge {
Barry Mienydd671972010-10-04 16:33:58 +020037
Andrey Andreevd947eba2012-04-09 14:58:28 +030038 protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s';
Derek Allard2067d1a2008-11-13 22:59:24 +000039
40 /**
41 * Process Fields
42 *
Derek Allard2067d1a2008-11-13 22:59:24 +000043 * @param mixed the fields
44 * @return string
45 */
Andrey Andreev820999c2012-03-20 15:15:57 +020046 protected function _process_fields($fields)
Derek Allard2067d1a2008-11-13 22:59:24 +000047 {
48 $current_field_count = 0;
49 $sql = '';
50
Andrey Andreev2caf2892012-01-27 00:12:03 +020051 foreach ($fields as $field => $attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000052 {
53 // Numeric field names aren't allowed in databases, so if the key is
54 // numeric, we know it was assigned by PHP and the developer manually
55 // entered the field information, so we'll simply add it to the list
56 if (is_numeric($field))
57 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020058 $sql .= "\n\t".$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +000059 }
60 else
61 {
62 $attributes = array_change_key_case($attributes, CASE_UPPER);
Barry Mienydd671972010-10-04 16:33:58 +020063
Andrey Andreev5ef194d2012-06-08 01:12:54 +030064 $sql .= "\n\t".$this->db->escape_identifiers($field);
65
Phil Sturgeone389b0e2012-06-14 17:37:18 -050066 empty($attributes['NAME']) OR $sql .= ' '.$this->db->escape_identifiers($attributes['NAME']).' ';
Derek Allard2067d1a2008-11-13 22:59:24 +000067
Andrey Andreev2caf2892012-01-27 00:12:03 +020068 if ( ! empty($attributes['TYPE']))
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
Derek Jones37f4b9c2011-07-01 17:56:50 -050070 $sql .= ' '.$attributes['TYPE'];
Barry Mienydd671972010-10-04 16:33:58 +020071
Andrey Andreev2caf2892012-01-27 00:12:03 +020072 if ( ! empty($attributes['CONSTRAINT']))
Phil Sturgeon27324cd2011-01-05 16:32:57 +000073 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020074 switch (strtolower($attributes['TYPE']))
Phil Sturgeon27324cd2011-01-05 16:32:57 +000075 {
76 case 'decimal':
77 case 'float':
78 case 'numeric':
79 $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
Andrey Andreev2caf2892012-01-27 00:12:03 +020080 break;
Phil Sturgeon27324cd2011-01-05 16:32:57 +000081 case 'enum':
82 case 'set':
83 $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
Andrey Andreev2caf2892012-01-27 00:12:03 +020084 break;
Phil Sturgeon27324cd2011-01-05 16:32:57 +000085 default:
86 $sql .= '('.$attributes['CONSTRAINT'].')';
87 }
88 }
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Andrey Andreev5ef194d2012-06-08 01:12:54 +030091 if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
92 {
93 $sql .= ' UNSIGNED';
94 }
95
96 if (isset($attributes['DEFAULT']))
97 {
98 $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
99 }
100
101 $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
102 ? ' NULL' : ' NOT NULL';
103
104 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
105 {
106 $sql .= ' AUTO_INCREMENT';
107 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 // don't add a comma on the end of the last field
111 if (++$current_field_count < count($fields))
112 {
113 $sql .= ',';
114 }
115 }
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 return $sql;
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Create Table
124 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * @param string the table name
126 * @param mixed the fields
127 * @param mixed primary key(s)
128 * @param mixed key(s)
Andrey Andreev2caf2892012-01-27 00:12:03 +0200129 * @param bool should 'IF NOT EXISTS' be added to the SQL
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 * @return bool
131 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300132 protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
134 $sql = 'CREATE TABLE ';
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 if ($if_not_exists === TRUE)
137 {
138 $sql .= 'IF NOT EXISTS ';
139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300141 $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields);
Derek Allard2067d1a2008-11-13 22:59:24 +0000142
143 if (count($primary_keys) > 0)
144 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300145 $key_name = $this->db->escape_identifiers(implode('_', $primary_keys));
Andrey Andreev9637b402012-06-08 15:39:24 +0300146 $sql .= ",\n\tPRIMARY KEY ".$key_name.' ('.implode(', ', $this->db->escape_identifiers($primary_keys)).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
148
149 if (is_array($keys) && count($keys) > 0)
150 {
151 foreach ($keys as $key)
152 {
153 if (is_array($key))
154 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300155 $key_name = $this->db->escape_identifiers(implode('_', $key));
Andrey Andreev9637b402012-06-08 15:39:24 +0300156 $key = $this->db->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158 else
159 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300160 $key_name = $this->db->escape_identifiers($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 $key = array($key_name);
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Andrey Andreev2caf2892012-01-27 00:12:03 +0200164 $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
166 }
167
Andrey Andreev2caf2892012-01-27 00:12:03 +0200168 return $sql."\n) DEFAULT CHARACTER SET ".$this->db->char_set.' COLLATE '.$this->db->dbcollat.';';
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
171 // --------------------------------------------------------------------
172
173 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * Alter table query
175 *
176 * Generates a platform-specific query so that a table can be altered
Andrey Andreev2caf2892012-01-27 00:12:03 +0200177 * Called by add_column(), drop_column() and column_alter()
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 * @param string the ALTER type (ADD, DROP, CHANGE)
180 * @param string the column name
181 * @param array fields
182 * @param string the field after which we should add the new field
Andrey Andreev2caf2892012-01-27 00:12:03 +0200183 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 */
Andrey Andreeva0d4e412012-04-09 15:06:40 +0300185 protected function _alter_table($alter_type, $table, $fields, $after_field = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300187 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000188
189 // DROP has everything it needs now.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200190 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300192 return $sql.$this->db->escape_identifiers($fields);
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
194
Andrey Andreev2caf2892012-01-27 00:12:03 +0200195 return $sql.$this->_process_fields($fields)
Andrey Andreev5ef194d2012-06-08 01:12:54 +0300196 .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199}
200
201/* End of file mysql_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400202/* Location: ./system/database/drivers/mysql/mysql_forge.php */