Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * MySQLi Forge Class |
| 31 | * |
| 32 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 33 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 34 | * @link http://codeigniter.com/user_guide/database/ |
| 35 | */ |
| 36 | class CI_DB_mysqli_forge extends CI_DB_forge { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 37 | |
Andrey Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 38 | /** |
| 39 | * CREATE DATABASE statement |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 43 | protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 45 | /** |
| 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 Andreev | c98e93a | 2012-11-02 02:04:59 +0200 | [diff] [blame] | 72 | // -------------------------------------------------------------------- |
| 73 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 75 | * Class constructor |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 77 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 79 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 81 | parent::__construct(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 83 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 100 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 108 | $field[$i] = ($alter_type === 'ADD') |
| 109 | ? "\n\tADD ".$field[$i]['_literal'] |
| 110 | : "\n\tMODIFY ".$field[$i]['_literal']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 114 | if ($alter_type === 'ADD') |
Phil Sturgeon | ca64047 | 2012-03-09 17:19:35 +0000 | [diff] [blame] | 115 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 116 | $field[$i]['_literal'] = "\n\tADD "; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | } |
| 118 | else |
| 119 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 120 | $field[$i]['_literal'] = empty($field['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE "; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 123 | $field[$i] = $field['_literal'].$this->_process_column($field[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 127 | return array($sql.implode(',', $field)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 133 | * Process column |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 135 | * @param array $field |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 136 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 138 | protected function _process_column($field) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 140 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 150 | // -------------------------------------------------------------------- |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 164 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame^] | 176 | $this->keys = array(); |
| 177 | |
| 178 | return $sql; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* End of file mysqli_forge.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 184 | /* Location: ./system/database/drivers/mysqli/mysqli_forge.php */ |