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 | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 77 | * @param object &$db Database object |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 78 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | */ |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 80 | public function __construct(&$db) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | { |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 82 | parent::__construct($db); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 84 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 101 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 109 | $field[$i] = ($alter_type === 'ADD') |
| 110 | ? "\n\tADD ".$field[$i]['_literal'] |
| 111 | : "\n\tMODIFY ".$field[$i]['_literal']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | } |
| 113 | else |
| 114 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 115 | if ($alter_type === 'ADD') |
Phil Sturgeon | ca64047 | 2012-03-09 17:19:35 +0000 | [diff] [blame] | 116 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 117 | $field[$i]['_literal'] = "\n\tADD "; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | { |
Andrey Andreev | e8b8963 | 2012-11-08 12:05:00 +0200 | [diff] [blame^] | 121 | $field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE "; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | |
Andrey Andreev | e8b8963 | 2012-11-08 12:05:00 +0200 | [diff] [blame^] | 124 | $field[$i] = $field[$i]['_literal'].$this->_process_column($field[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 128 | return array($sql.implode(',', $field)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // -------------------------------------------------------------------- |
| 132 | |
| 133 | /** |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 134 | * Process column |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | * |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 136 | * @param array $field |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 137 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | */ |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 139 | protected function _process_column($field) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 141 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 151 | // -------------------------------------------------------------------- |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | { |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 165 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 177 | $this->keys = array(); |
| 178 | |
| 179 | return $sql; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* End of file mysqli_forge.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 185 | /* Location: ./system/database/drivers/mysqli/mysqli_forge.php */ |