blob: ea3207760b3fdec29896979de3c8893decd146df [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 /**
46 * UNSIGNED support
47 *
48 * @var array
49 */
50 protected $_unsigned = array(
51 'TINYINT',
52 'SMALLINT',
53 'MEDIUMINT',
54 'INT',
55 'INTEGER',
56 'BIGINT',
57 'REAL',
58 'DOUBLE',
59 'DOUBLE PRECISION',
60 'FLOAT',
61 'DECIMAL',
62 'NUMERIC'
63 );
64
65 /**
66 * NULL value representation in CREATE/ALTER TABLE statements
67 *
68 * @var string
69 */
70 protected $_null = 'NULL';
71
Andrey Andreevc98e93a2012-11-02 02:04:59 +020072 // --------------------------------------------------------------------
73
Derek Allard2067d1a2008-11-13 22:59:24 +000074 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020075 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000076 *
Andrey Andreeva287a342012-11-05 23:19:59 +020077 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000078 */
Andrey Andreeva287a342012-11-05 23:19:59 +020079 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000080 {
Andrey Andreeva287a342012-11-05 23:19:59 +020081 parent::__construct();
Derek Allard2067d1a2008-11-13 22:59:24 +000082
Andrey Andreeva287a342012-11-05 23:19:59 +020083 $this->_create_table .= ' DEFAULT CHARSET '.$this->db->char_set.' COLLATE '.$this->db->dbcollat;
84 }
85
86 // --------------------------------------------------------------------
87
88 /**
89 * ALTER TABLE
90 *
91 * @param string $alter_type ALTER type
92 * @param string $table Table name
93 * @param mixed $field Column definition
94 * @return string|string[]
95 */
96 protected function _alter_table($alter_type, $table, $field)
97 {
98 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200100 return parent::_alter_table($alter_type, $table, $field);
101 }
102
103 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
104 for ($i = 0, $c = count($field); $i < $c; $i++)
105 {
106 if ($field[$i]['_literal'] !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200108 $field[$i] = ($alter_type === 'ADD')
109 ? "\n\tADD ".$field[$i]['_literal']
110 : "\n\tMODIFY ".$field[$i]['_literal'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112 else
113 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200114 if ($alter_type === 'ADD')
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200116 $field[$i]['_literal'] = "\n\tADD ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
118 else
119 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200120 $field[$i]['_literal'] = empty($field['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Andrey Andreeva287a342012-11-05 23:19:59 +0200123 $field[$i] = $field['_literal'].$this->_process_column($field[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
125 }
126
Andrey Andreeva287a342012-11-05 23:19:59 +0200127 return array($sql.implode(',', $field));
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129
130 // --------------------------------------------------------------------
131
132 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200133 * Process column
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200135 * @param array $field
Andrey Andreev2caf2892012-01-27 00:12:03 +0200136 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200138 protected function _process_column($field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200140 return $this->db->escape_identifiers($field['name'])
141 .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name']))
142 .' '.$field['type'].$field['length']
143 .$field['unsigned']
144 .$field['null']
145 .$field['default']
146 .$field['auto_increment']
147 .$field['unique'];
148 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000149
Andrey Andreeva287a342012-11-05 23:19:59 +0200150 // --------------------------------------------------------------------
151
152 /**
153 * Process indexes
154 *
155 * @param string $table (ignored)
156 * @return string
157 */
158 protected function _process_indexes($table = NULL)
159 {
160 $sql = '';
161
162 for ($i = 0, $c = count($this->keys); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200164 if ( ! isset($this->fields[$this->keys[$i]]))
165 {
166 unset($this->keys[$i]);
167 continue;
168 }
169
170 is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
171
172 $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
173 .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
Andrey Andreeva287a342012-11-05 23:19:59 +0200176 $this->keys = array();
177
178 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181}
182
183/* End of file mysql_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400184/* Location: ./system/database/drivers/mysql/mysql_forge.php */