blob: f4394c6856f16498b04410c8e8f9f77a30886865 [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 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 Andreeva287a342012-11-05 23:19:59 +020085 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000086 *
Andrey Andreeveaa60c72012-11-06 01:11:22 +020087 * @param object &$db Database object
Andrey Andreeva287a342012-11-05 23:19:59 +020088 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000089 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +020090 public function __construct(&$db)
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +020092 parent::__construct($db);
Derek Allard2067d1a2008-11-13 22:59:24 +000093
Andrey Andreeva287a342012-11-05 23:19:59 +020094 $this->_create_table .= ' DEFAULT CHARSET '.$this->db->char_set.' COLLATE '.$this->db->dbcollat;
95 }
96
97 // --------------------------------------------------------------------
98
99 /**
100 * ALTER TABLE
101 *
102 * @param string $alter_type ALTER type
103 * @param string $table Table name
104 * @param mixed $field Column definition
105 * @return string|string[]
106 */
107 protected function _alter_table($alter_type, $table, $field)
108 {
109 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200111 return parent::_alter_table($alter_type, $table, $field);
112 }
113
114 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
115 for ($i = 0, $c = count($field); $i < $c; $i++)
116 {
117 if ($field[$i]['_literal'] !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200119 $field[$i] = ($alter_type === 'ADD')
120 ? "\n\tADD ".$field[$i]['_literal']
121 : "\n\tMODIFY ".$field[$i]['_literal'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
123 else
124 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200125 if ($alter_type === 'ADD')
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200127 $field[$i]['_literal'] = "\n\tADD ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129 else
130 {
Andrey Andreeve8b89632012-11-08 12:05:00 +0200131 $field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Andrey Andreeve8b89632012-11-08 12:05:00 +0200134 $field[$i] = $field[$i]['_literal'].$this->_process_column($field[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 }
136 }
137
Andrey Andreeva287a342012-11-05 23:19:59 +0200138 return array($sql.implode(',', $field));
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200144 * Process column
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200146 * @param array $field
Andrey Andreev2caf2892012-01-27 00:12:03 +0200147 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200149 protected function _process_column($field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Andrey Andreevb67277b2012-11-12 12:51:14 +0200151 $extra_clause = isset($field['after'])
152 ? ' AFTER '.$this->db->escape_identifiers($field['after']) : '';
153
154 if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE)
155 {
156 $extra_clause = ' FIRST';
157 }
158
Andrey Andreeva287a342012-11-05 23:19:59 +0200159 return $this->db->escape_identifiers($field['name'])
Andrey Andreev7ade8b72012-11-22 13:12:22 +0200160 .(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
Andrey Andreeva287a342012-11-05 23:19:59 +0200161 .' '.$field['type'].$field['length']
162 .$field['unsigned']
163 .$field['null']
164 .$field['default']
165 .$field['auto_increment']
Andrey Andreevb67277b2012-11-12 12:51:14 +0200166 .$field['unique']
167 .$extra_clause;
Andrey Andreeva287a342012-11-05 23:19:59 +0200168 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000169
Andrey Andreeva287a342012-11-05 23:19:59 +0200170 // --------------------------------------------------------------------
171
172 /**
173 * Process indexes
174 *
175 * @param string $table (ignored)
176 * @return string
177 */
Andrey Andreev35451022012-11-25 17:20:04 +0200178 protected function _process_indexes($table)
Andrey Andreeva287a342012-11-05 23:19:59 +0200179 {
180 $sql = '';
181
182 for ($i = 0, $c = count($this->keys); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreev35451022012-11-25 17:20:04 +0200184 if (is_array($this->keys[$i]))
185 {
186 for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++)
187 {
188 if ( ! isset($this->fields[$this->keys[$i][$i2]]))
189 {
190 unset($this->keys[$i][$i2]);
191 continue;
192 }
193 }
194 }
195 elseif ( ! isset($this->fields[$this->keys[$i]]))
Andrey Andreeva287a342012-11-05 23:19:59 +0200196 {
197 unset($this->keys[$i]);
198 continue;
199 }
200
201 is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
202
203 $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
204 .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 }
206
Andrey Andreeva287a342012-11-05 23:19:59 +0200207 $this->keys = array();
208
209 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212}
213
214/* End of file mysql_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400215/* Location: ./system/database/drivers/mysql/mysql_forge.php */