blob: 3b3cbdee0b18e082696edbaf2c94d35ff99d4ced [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 Andreevc98e93a2012-11-02 02:04:59 +020038 /**
39 * CREATE DATABASE statement
40 *
41 * @var string
42 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030043 protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s';
Derek Allard2067d1a2008-11-13 22:59:24 +000044
Andrey Andreeva287a342012-11-05 23:19:59 +020045 /**
Andrey Andreevb0a97c12012-11-11 13:58:53 +020046 * CREATE TABLE keys flag
47 *
48 * Whether table keys are created from within the
49 * CREATE TABLE statement.
50 *
51 * @var bool
52 */
53 protected $_create_table_keys = TRUE;
54
55 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020056 * UNSIGNED support
57 *
58 * @var array
59 */
60 protected $_unsigned = array(
61 'TINYINT',
62 'SMALLINT',
63 'MEDIUMINT',
64 'INT',
65 'INTEGER',
66 'BIGINT',
67 'REAL',
68 'DOUBLE',
69 'DOUBLE PRECISION',
70 'FLOAT',
71 'DECIMAL',
72 'NUMERIC'
73 );
74
75 /**
76 * NULL value representation in CREATE/ALTER TABLE statements
77 *
78 * @var string
79 */
80 protected $_null = 'NULL';
81
Andrey Andreevc98e93a2012-11-02 02:04:59 +020082 // --------------------------------------------------------------------
83
Derek Allard2067d1a2008-11-13 22:59:24 +000084 /**
Andrey Andreev27f798b2014-01-20 18:19:13 +020085 * CREATE TABLE attributes
Derek Allard2067d1a2008-11-13 22:59:24 +000086 *
Andrey Andreev27f798b2014-01-20 18:19:13 +020087 * @param array $attributes Associative array of table attributes
88 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +000089 */
Andrey Andreev27f798b2014-01-20 18:19:13 +020090 protected function _create_table_attr($attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreev27f798b2014-01-20 18:19:13 +020092 $sql = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000093
Andrey Andreev27f798b2014-01-20 18:19:13 +020094 foreach (array_keys($attributes) as $key)
95 {
96 if (is_string($key))
97 {
98 $sql .= ' '.strtoupper($key).' = '.$attributes[$key];
99 }
100 }
101
102 if ( ! empty($this->db->char_set) && ! strpos($sql, 'CHARACTER SET') && ! strpos($sql, 'CHARSET'))
103 {
104 $sql .= ' DEFAULT CHARACTER SET = '.$this->db->char_set;
105 }
106
107 if ( ! empty($this->db->dbcollat) && ! strpos($sql, 'COLLATE'))
108 {
109 $sql .= ' COLLATE = '.$this->db->dbcollat;
110 }
111
112 return $sql;
Andrey Andreeva287a342012-11-05 23:19:59 +0200113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * ALTER TABLE
119 *
120 * @param string $alter_type ALTER type
121 * @param string $table Table name
122 * @param mixed $field Column definition
123 * @return string|string[]
124 */
125 protected function _alter_table($alter_type, $table, $field)
126 {
127 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200129 return parent::_alter_table($alter_type, $table, $field);
130 }
131
132 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
133 for ($i = 0, $c = count($field); $i < $c; $i++)
134 {
135 if ($field[$i]['_literal'] !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200137 $field[$i] = ($alter_type === 'ADD')
138 ? "\n\tADD ".$field[$i]['_literal']
139 : "\n\tMODIFY ".$field[$i]['_literal'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
141 else
142 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200143 if ($alter_type === 'ADD')
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200145 $field[$i]['_literal'] = "\n\tADD ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
147 else
148 {
Andrey Andreeve8b89632012-11-08 12:05:00 +0200149 $field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Andrey Andreeve8b89632012-11-08 12:05:00 +0200152 $field[$i] = $field[$i]['_literal'].$this->_process_column($field[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
154 }
155
Andrey Andreeva287a342012-11-05 23:19:59 +0200156 return array($sql.implode(',', $field));
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158
159 // --------------------------------------------------------------------
160
161 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200162 * Process column
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200164 * @param array $field
Andrey Andreev2caf2892012-01-27 00:12:03 +0200165 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200167 protected function _process_column($field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreevb67277b2012-11-12 12:51:14 +0200169 $extra_clause = isset($field['after'])
170 ? ' AFTER '.$this->db->escape_identifiers($field['after']) : '';
171
172 if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE)
173 {
174 $extra_clause = ' FIRST';
175 }
176
Andrey Andreeva287a342012-11-05 23:19:59 +0200177 return $this->db->escape_identifiers($field['name'])
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200178 .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
Andrey Andreeva287a342012-11-05 23:19:59 +0200179 .' '.$field['type'].$field['length']
180 .$field['unsigned']
181 .$field['null']
182 .$field['default']
183 .$field['auto_increment']
Andrey Andreevb67277b2012-11-12 12:51:14 +0200184 .$field['unique']
185 .$extra_clause;
Andrey Andreeva287a342012-11-05 23:19:59 +0200186 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000187
Andrey Andreeva287a342012-11-05 23:19:59 +0200188 // --------------------------------------------------------------------
189
190 /**
191 * Process indexes
192 *
193 * @param string $table (ignored)
194 * @return string
195 */
Andrey Andreev35451022012-11-25 17:20:04 +0200196 protected function _process_indexes($table)
Andrey Andreeva287a342012-11-05 23:19:59 +0200197 {
198 $sql = '';
199
200 for ($i = 0, $c = count($this->keys); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Andrey Andreev35451022012-11-25 17:20:04 +0200202 if (is_array($this->keys[$i]))
203 {
204 for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++)
205 {
206 if ( ! isset($this->fields[$this->keys[$i][$i2]]))
207 {
208 unset($this->keys[$i][$i2]);
209 continue;
210 }
211 }
212 }
213 elseif ( ! isset($this->fields[$this->keys[$i]]))
Andrey Andreeva287a342012-11-05 23:19:59 +0200214 {
215 unset($this->keys[$i]);
216 continue;
217 }
218
219 is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
220
221 $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
222 .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
224
Andrey Andreeva287a342012-11-05 23:19:59 +0200225 $this->keys = array();
226
227 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230}
231
232/* End of file mysql_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400233/* Location: ./system/database/drivers/mysql/mysql_forge.php */