Subversion-Projekte lars-tiefland.ci

Revision

Revision 2049 | Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
9
 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32
 * @copyright	Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
33
 * @license	http://opensource.org/licenses/MIT	MIT License
34
 * @link	https://codeigniter.com
35
 * @since	Version 3.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * PDO MySQL Forge Class
42
 *
43
 * @category	Database
44
 * @author		EllisLab Dev Team
45
 * @link		https://codeigniter.com/user_guide/database/
46
 */
47
class CI_DB_pdo_mysql_forge extends CI_DB_pdo_forge {
48
 
49
	/**
50
	 * CREATE DATABASE statement
51
	 *
52
	 * @var	string
53
	 */
54
	protected $_create_database	= 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s';
55
 
56
	/**
57
	 * CREATE TABLE IF statement
58
	 *
59
	 * @var	string
60
	 */
61
	protected $_create_table_if	= 'CREATE TABLE IF NOT EXISTS';
62
 
63
	/**
64
	 * CREATE TABLE keys flag
65
	 *
66
	 * Whether table keys are created from within the
67
	 * CREATE TABLE statement.
68
	 *
69
	 * @var	bool
70
	 */
71
	protected $_create_table_keys	= TRUE;
72
 
73
	/**
74
	 * DROP TABLE IF statement
75
	 *
76
	 * @var	string
77
	 */
78
	protected $_drop_table_if	= 'DROP TABLE IF EXISTS';
79
 
80
	/**
81
	 * UNSIGNED support
82
	 *
83
	 * @var	array
84
	 */
85
	protected $_unsigned		= array(
86
		'TINYINT',
87
		'SMALLINT',
88
		'MEDIUMINT',
89
		'INT',
90
		'INTEGER',
91
		'BIGINT',
92
		'REAL',
93
		'DOUBLE',
94
		'DOUBLE PRECISION',
95
		'FLOAT',
96
		'DECIMAL',
97
		'NUMERIC'
98
	);
99
 
100
	/**
101
	 * NULL value representation in CREATE/ALTER TABLE statements
102
	 *
103
	 * @var	string
104
	 */
105
	protected $_null = 'NULL';
106
 
107
	// --------------------------------------------------------------------
108
 
109
	/**
110
	 * CREATE TABLE attributes
111
	 *
112
	 * @param	array	$attributes	Associative array of table attributes
113
	 * @return	string
114
	 */
115
	protected function _create_table_attr($attributes)
116
	{
117
		$sql = '';
118
 
119
		foreach (array_keys($attributes) as $key)
120
		{
121
			if (is_string($key))
122
			{
123
				$sql .= ' '.strtoupper($key).' = '.$attributes[$key];
124
			}
125
		}
126
 
127
		if ( ! empty($this->db->char_set) && ! strpos($sql, 'CHARACTER SET') && ! strpos($sql, 'CHARSET'))
128
		{
129
			$sql .= ' DEFAULT CHARACTER SET = '.$this->db->char_set;
130
		}
131
 
132
		if ( ! empty($this->db->dbcollat) && ! strpos($sql, 'COLLATE'))
133
		{
134
			$sql .= ' COLLATE = '.$this->db->dbcollat;
135
		}
136
 
137
		return $sql;
138
	}
139
 
140
	// --------------------------------------------------------------------
141
 
142
	/**
143
	 * ALTER TABLE
144
	 *
145
	 * @param	string	$alter_type	ALTER type
146
	 * @param	string	$table		Table name
147
	 * @param	mixed	$field		Column definition
148
	 * @return	string|string[]
149
	 */
150
	protected function _alter_table($alter_type, $table, $field)
151
	{
152
		if ($alter_type === 'DROP')
153
		{
154
			return parent::_alter_table($alter_type, $table, $field);
155
		}
156
 
157
		$sql = 'ALTER TABLE '.$this->db->escape_identifiers($table);
158
		for ($i = 0, $c = count($field); $i < $c; $i++)
159
		{
160
			if ($field[$i]['_literal'] !== FALSE)
161
			{
162
				$field[$i] = ($alter_type === 'ADD')
163
						? "\n\tADD ".$field[$i]['_literal']
164
						: "\n\tMODIFY ".$field[$i]['_literal'];
165
			}
166
			else
167
			{
168
				if ($alter_type === 'ADD')
169
				{
170
					$field[$i]['_literal'] = "\n\tADD ";
171
				}
172
				else
173
				{
174
					$field[$i]['_literal'] = empty($field[$i]['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE ";
175
				}
176
 
177
				$field[$i] = $field[$i]['_literal'].$this->_process_column($field[$i]);
178
			}
179
		}
180
 
181
		return array($sql.implode(',', $field));
182
	}
183
 
184
	// --------------------------------------------------------------------
185
 
186
	/**
187
	 * Process column
188
	 *
189
	 * @param	array	$field
190
	 * @return	string
191
	 */
192
	protected function _process_column($field)
193
	{
194
		$extra_clause = isset($field['after'])
195
			? ' AFTER '.$this->db->escape_identifiers($field['after']) : '';
196
 
197
		if (empty($extra_clause) && isset($field['first']) && $field['first'] === TRUE)
198
		{
199
			$extra_clause = ' FIRST';
200
		}
201
 
202
		return $this->db->escape_identifiers($field['name'])
203
			.(empty($field['new_name']) ? '' : ' '.$this->db->escape_identifiers($field['new_name']))
204
			.' '.$field['type'].$field['length']
205
			.$field['unsigned']
206
			.$field['null']
207
			.$field['default']
208
			.$field['auto_increment']
209
			.$field['unique']
210
			.(empty($field['comment']) ? '' : ' COMMENT '.$field['comment'])
211
			.$extra_clause;
212
	}
213
 
214
	// --------------------------------------------------------------------
215
 
216
	/**
217
	 * Process indexes
218
	 *
219
	 * @param	string	$table	(ignored)
220
	 * @return	string
221
	 */
222
	protected function _process_indexes($table)
223
	{
224
		$sql = '';
225
 
226
		for ($i = 0, $c = count($this->keys); $i < $c; $i++)
227
		{
228
			if (is_array($this->keys[$i]))
229
			{
230
				for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++)
231
				{
232
					if ( ! isset($this->fields[$this->keys[$i][$i2]]))
233
					{
234
						unset($this->keys[$i][$i2]);
235
						continue;
236
					}
237
				}
238
			}
239
			elseif ( ! isset($this->fields[$this->keys[$i]]))
240
			{
241
				unset($this->keys[$i]);
242
				continue;
243
			}
244
 
245
			is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]);
246
 
247
			$sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i]))
248
				.' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')';
249
		}
250
 
251
		$this->keys = array();
252
 
253
		return $sql;
254
	}
255
 
256
}