blob: 42b69b55aff47f14ab8661e89ff5b0a812834686 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev1ff49e02012-01-27 11:30:41 +02008 *
Andrey Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
Andrey Andreev1ff49e02012-01-27 11:30:41 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * MySQLi Forge Class
42 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020043 * @package CodeIgniter
44 * @subpackage Drivers
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49class CI_DB_mysqli_forge extends CI_DB_forge {
Barry Mienydd671972010-10-04 16:33:58 +020050
Andrey Andreevc98e93a2012-11-02 02:04:59 +020051 /**
52 * CREATE DATABASE statement
53 *
54 * @var string
55 */
Andrey Andreevd947eba2012-04-09 14:58:28 +030056 protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s';
Derek Allard2067d1a2008-11-13 22:59:24 +000057
Andrey Andreeva287a342012-11-05 23:19:59 +020058 /**
Andrey Andreevb0a97c12012-11-11 13:58:53 +020059 * CREATE TABLE keys flag
60 *
61 * Whether table keys are created from within the
62 * CREATE TABLE statement.
63 *
64 * @var bool
65 */
66 protected $_create_table_keys = TRUE;
67
68 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020069 * UNSIGNED support
70 *
71 * @var array
72 */
73 protected $_unsigned = array(
74 'TINYINT',
75 'SMALLINT',
76 'MEDIUMINT',
77 'INT',
78 'INTEGER',
79 'BIGINT',
80 'REAL',
81 'DOUBLE',
82 'DOUBLE PRECISION',
83 'FLOAT',
84 'DECIMAL',
85 'NUMERIC'
86 );
87
88 /**
89 * NULL value representation in CREATE/ALTER TABLE statements
90 *
91 * @var string
92 */
Andrey Andreev5289f272014-11-03 22:45:28 +020093 protected $_null = 'NULL';
Andrey Andreeva287a342012-11-05 23:19:59 +020094
Andrey Andreevc98e93a2012-11-02 02:04:59 +020095 // --------------------------------------------------------------------
96
Derek Allard2067d1a2008-11-13 22:59:24 +000097 /**
Andrey Andreev27f798b2014-01-20 18:19:13 +020098 * CREATE TABLE attributes
Derek Allard2067d1a2008-11-13 22:59:24 +000099 *
Andrey Andreev27f798b2014-01-20 18:19:13 +0200100 * @param array $attributes Associative array of table attributes
101 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 */
Andrey Andreev27f798b2014-01-20 18:19:13 +0200103 protected function _create_table_attr($attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Andrey Andreev27f798b2014-01-20 18:19:13 +0200105 $sql = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000106
Andrey Andreev27f798b2014-01-20 18:19:13 +0200107 foreach (array_keys($attributes) as $key)
108 {
109 if (is_string($key))
110 {
111 $sql .= ' '.strtoupper($key).' = '.$attributes[$key];
112 }
113 }
114
115 if ( ! empty($this->db->char_set) && ! strpos($sql, 'CHARACTER SET') && ! strpos($sql, 'CHARSET'))
116 {
117 $sql .= ' DEFAULT CHARACTER SET = '.$this->db->char_set;
118 }
119
120 if ( ! empty($this->db->dbcollat) && ! strpos($sql, 'COLLATE'))
121 {
122 $sql .= ' COLLATE = '.$this->db->dbcollat;
123 }
124
125 return $sql;
Andrey Andreeva287a342012-11-05 23:19:59 +0200126 }
127
128 // --------------------------------------------------------------------
129
130 /**
131 * ALTER TABLE
132 *
133 * @param string $alter_type ALTER type
134 * @param string $table Table name
135 * @param mixed $field Column definition
136 * @return string|string[]
137 */
138 protected function _alter_table($alter_type, $table, $field)
139 {
140 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200142 return parent::_alter_table($alter_type, $table, $field);
143 }
144
145 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
146 for ($i = 0, $c = count($field); $i < $c; $i++)
147 {
148 if ($field[$i]['_literal'] !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200150 $field[$i] = ($alter_type === 'ADD')
151 ? "\n\tADD ".$field[$i]['_literal']
152 : "\n\tMODIFY ".$field[$i]['_literal'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
154 else
155 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200156 if ($alter_type === 'ADD')
Phil Sturgeonca640472012-03-09 17:19:35 +0000157 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200158 $field[$i]['_literal'] = "\n\tADD ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160 else
161 {
Andrey Andreeve8b89632012-11-08 12:05:00 +0200162 $field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Andrey Andreeve8b89632012-11-08 12:05:00 +0200165 $field[$i] = $field[$i]['_literal'].$this->_process_column($field[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167 }
168
Andrey Andreeva287a342012-11-05 23:19:59 +0200169 return array($sql.implode(',', $field));
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200175 * Process column
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200177 * @param array $field
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200178 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200180 protected function _process_column($field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreevb67277b2012-11-12 12:51:14 +0200182 $extra_clause = isset($field['after'])
183 ? ' AFTER '.$this->db->escape_identifiers($field['after']) : '';
184
185 if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE)
186 {
187 $extra_clause = ' FIRST';
188 }
189
Andrey Andreeva287a342012-11-05 23:19:59 +0200190 return $this->db->escape_identifiers($field['name'])
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200191 .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
Andrey Andreeva287a342012-11-05 23:19:59 +0200192 .' '.$field['type'].$field['length']
193 .$field['unsigned']
194 .$field['null']
195 .$field['default']
196 .$field['auto_increment']
Andrey Andreevb67277b2012-11-12 12:51:14 +0200197 .$field['unique']
Andrey Andreev5289f272014-11-03 22:45:28 +0200198 .(empty($field['comment']) ? '' : ' COMMENT '.$field['comment'])
Andrey Andreevb67277b2012-11-12 12:51:14 +0200199 .$extra_clause;
Andrey Andreeva287a342012-11-05 23:19:59 +0200200 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
Andrey Andreeva287a342012-11-05 23:19:59 +0200202 // --------------------------------------------------------------------
203
204 /**
205 * Process indexes
206 *
207 * @param string $table (ignored)
208 * @return string
209 */
Andrey Andreev35451022012-11-25 17:20:04 +0200210 protected function _process_indexes($table)
Andrey Andreeva287a342012-11-05 23:19:59 +0200211 {
212 $sql = '';
213
214 for ($i = 0, $c = count($this->keys); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Andrey Andreev35451022012-11-25 17:20:04 +0200216 if (is_array($this->keys[$i]))
217 {
218 for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++)
219 {
220 if ( ! isset($this->fields[$this->keys[$i][$i2]]))
221 {
222 unset($this->keys[$i][$i2]);
223 continue;
224 }
225 }
226 }
227 elseif ( ! isset($this->fields[$this->keys[$i]]))
Andrey Andreeva287a342012-11-05 23:19:59 +0200228 {
229 unset($this->keys[$i]);
230 continue;
231 }
232
233 is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
234
235 $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
236 .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 }
238
Andrey Andreeva287a342012-11-05 23:19:59 +0200239 $this->keys = array();
240
241 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244}