blob: 9cee385df7ff4b250d4c3969503441d2df5d8933 [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 Andreeveaa60c72012-11-06 01:11:22 +020077 * @param object &$db Database object
Andrey Andreeva287a342012-11-05 23:19:59 +020078 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000079 */
Andrey Andreeveaa60c72012-11-06 01:11:22 +020080 public function __construct(&$db)
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
Andrey Andreeveaa60c72012-11-06 01:11:22 +020082 parent::__construct($db);
Derek Allard2067d1a2008-11-13 22:59:24 +000083
Andrey Andreeva287a342012-11-05 23:19:59 +020084 $this->_create_table .= ' DEFAULT CHARSET '.$this->db->char_set.' COLLATE '.$this->db->dbcollat;
85 }
86
87 // --------------------------------------------------------------------
88
89 /**
90 * ALTER TABLE
91 *
92 * @param string $alter_type ALTER type
93 * @param string $table Table name
94 * @param mixed $field Column definition
95 * @return string|string[]
96 */
97 protected function _alter_table($alter_type, $table, $field)
98 {
99 if ($alter_type === 'DROP')
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200101 return parent::_alter_table($alter_type, $table, $field);
102 }
103
104 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
105 for ($i = 0, $c = count($field); $i < $c; $i++)
106 {
107 if ($field[$i]['_literal'] !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200109 $field[$i] = ($alter_type === 'ADD')
110 ? "\n\tADD ".$field[$i]['_literal']
111 : "\n\tMODIFY ".$field[$i]['_literal'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113 else
114 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200115 if ($alter_type === 'ADD')
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200117 $field[$i]['_literal'] = "\n\tADD ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
119 else
120 {
Andrey Andreeve8b89632012-11-08 12:05:00 +0200121 $field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Andrey Andreeve8b89632012-11-08 12:05:00 +0200124 $field[$i] = $field[$i]['_literal'].$this->_process_column($field[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 }
126 }
127
Andrey Andreeva287a342012-11-05 23:19:59 +0200128 return array($sql.implode(',', $field));
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130
131 // --------------------------------------------------------------------
132
133 /**
Andrey Andreeva287a342012-11-05 23:19:59 +0200134 * Process column
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 *
Andrey Andreeva287a342012-11-05 23:19:59 +0200136 * @param array $field
Andrey Andreev2caf2892012-01-27 00:12:03 +0200137 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200139 protected function _process_column($field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200141 return $this->db->escape_identifiers($field['name'])
142 .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name']))
143 .' '.$field['type'].$field['length']
144 .$field['unsigned']
145 .$field['null']
146 .$field['default']
147 .$field['auto_increment']
148 .$field['unique'];
149 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000150
Andrey Andreeva287a342012-11-05 23:19:59 +0200151 // --------------------------------------------------------------------
152
153 /**
154 * Process indexes
155 *
156 * @param string $table (ignored)
157 * @return string
158 */
159 protected function _process_indexes($table = NULL)
160 {
161 $sql = '';
162
163 for ($i = 0, $c = count($this->keys); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200165 if ( ! isset($this->fields[$this->keys[$i]]))
166 {
167 unset($this->keys[$i]);
168 continue;
169 }
170
171 is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
172
173 $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
174 .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176
Andrey Andreeva287a342012-11-05 23:19:59 +0200177 $this->keys = array();
178
179 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182}
183
184/* End of file mysql_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400185/* Location: ./system/database/drivers/mysql/mysql_forge.php */