blob: 5a6560b2587caa8acc6c8eda886f76fd3c5ace5b [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 Andreev1ff49e02012-01-27 11:30:41 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1ff49e02012-01-27 11:30:41 +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 * MySQLi 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_mysqli_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')
Phil Sturgeonca640472012-03-09 17:19:35 +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 Andreev1ff49e02012-01-27 11:30:41 +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 Andreeva287a342012-11-05 23:19:59 +0200151 return $this->db->escape_identifiers($field['name'])
152 .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name']))
153 .' '.$field['type'].$field['length']
154 .$field['unsigned']
155 .$field['null']
156 .$field['default']
157 .$field['auto_increment']
158 .$field['unique'];
159 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000160
Andrey Andreeva287a342012-11-05 23:19:59 +0200161 // --------------------------------------------------------------------
162
163 /**
164 * Process indexes
165 *
166 * @param string $table (ignored)
167 * @return string
168 */
169 protected function _process_indexes($table = NULL)
170 {
171 $sql = '';
172
173 for ($i = 0, $c = count($this->keys); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200175 if ( ! isset($this->fields[$this->keys[$i]]))
176 {
177 unset($this->keys[$i]);
178 continue;
179 }
180
181 is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
182
183 $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
184 .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
186
Andrey Andreeva287a342012-11-05 23:19:59 +0200187 $this->keys = array();
188
189 return $sql;
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192}
193
194/* End of file mysqli_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400195/* Location: ./system/database/drivers/mysqli/mysqli_forge.php */