Subversion-Projekte lars-tiefland.ci

Revision

Revision 1257 | 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 Database Adapter Class
42
 *
43
 * Note: _DB is an extender class that the app controller
44
 * creates dynamically based on whether the query builder
45
 * class is being used or not.
46
 *
47
 * @package		CodeIgniter
48
 * @subpackage	Drivers
49
 * @category	Database
50
 * @author		EllisLab Dev Team
51
 * @link		https://codeigniter.com/user_guide/database/
52
 */
53
class CI_DB_pdo_mysql_driver extends CI_DB_pdo_driver {
54
 
55
	/**
56
	 * Sub-driver
57
	 *
58
	 * @var	string
59
	 */
60
	public $subdriver = 'mysql';
61
 
62
	/**
63
	 * Compression flag
64
	 *
65
	 * @var	bool
66
	 */
67
	public $compress = FALSE;
68
 
69
	/**
70
	 * Strict ON flag
71
	 *
72
	 * Whether we're running in strict SQL mode.
73
	 *
74
	 * @var	bool
75
	 */
76
	public $stricton;
77
 
78
	// --------------------------------------------------------------------
79
 
80
	/**
81
	 * Identifier escape character
82
	 *
83
	 * @var	string
84
	 */
85
	protected $_escape_char = '`';
86
 
87
	// --------------------------------------------------------------------
88
 
89
	/**
90
	 * Class constructor
91
	 *
92
	 * Builds the DSN if not already set.
93
	 *
94
	 * @param	array	$params
95
	 * @return	void
96
	 */
97
	public function __construct($params)
98
	{
99
		parent::__construct($params);
100
 
101
		if (empty($this->dsn))
102
		{
103
			$this->dsn = 'mysql:host='.(empty($this->hostname) ? '127.0.0.1' : $this->hostname);
104
 
105
			empty($this->port) OR $this->dsn .= ';port='.$this->port;
106
			empty($this->database) OR $this->dsn .= ';dbname='.$this->database;
107
			empty($this->char_set) OR $this->dsn .= ';charset='.$this->char_set;
108
		}
109
		elseif ( ! empty($this->char_set) && strpos($this->dsn, 'charset=', 6) === FALSE && is_php('5.3.6'))
110
		{
111
			$this->dsn .= ';charset='.$this->char_set;
112
		}
113
	}
114
 
115
	// --------------------------------------------------------------------
116
 
117
	/**
118
	 * Database connection
119
	 *
120
	 * @param	bool	$persistent
121
	 * @return	object
122
	 */
123
	public function db_connect($persistent = FALSE)
124
	{
125
		/* Prior to PHP 5.3.6, even if the charset was supplied in the DSN
126
		 * on connect - it was ignored. This is a work-around for the issue.
127
		 *
128
		 * Reference: http://www.php.net/manual/en/ref.pdo-mysql.connection.php
129
		 */
130
		if ( ! is_php('5.3.6') && ! empty($this->char_set))
131
		{
132
			$this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES '.$this->char_set
133
				.(empty($this->dbcollat) ? '' : ' COLLATE '.$this->dbcollat);
134
		}
135
 
136
		if (isset($this->stricton))
137
		{
138
			if ($this->stricton)
139
			{
140
				$sql = 'CONCAT(@@sql_mode, ",", "STRICT_ALL_TABLES")';
141
			}
142
			else
143
			{
144
				$sql = 'REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
145
                                        @@sql_mode,
146
                                        "STRICT_ALL_TABLES,", ""),
147
                                        ",STRICT_ALL_TABLES", ""),
148
                                        "STRICT_ALL_TABLES", ""),
149
                                        "STRICT_TRANS_TABLES,", ""),
150
                                        ",STRICT_TRANS_TABLES", ""),
151
                                        "STRICT_TRANS_TABLES", "")';
152
			}
153
 
154
			if ( ! empty($sql))
155
			{
156
				if (empty($this->options[PDO::MYSQL_ATTR_INIT_COMMAND]))
157
				{
158
					$this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET SESSION sql_mode = '.$sql;
159
				}
160
				else
161
				{
162
					$this->options[PDO::MYSQL_ATTR_INIT_COMMAND] .= ', @@session.sql_mode = '.$sql;
163
				}
164
			}
165
		}
166
 
167
		if ($this->compress === TRUE)
168
		{
169
			$this->options[PDO::MYSQL_ATTR_COMPRESS] = TRUE;
170
		}
171
 
172
		// SSL support was added to PDO_MYSQL in PHP 5.3.7
173
		if (is_array($this->encrypt) && is_php('5.3.7'))
174
		{
175
			$ssl = array();
176
			empty($this->encrypt['ssl_key'])    OR $ssl[PDO::MYSQL_ATTR_SSL_KEY]    = $this->encrypt['ssl_key'];
177
			empty($this->encrypt['ssl_cert'])   OR $ssl[PDO::MYSQL_ATTR_SSL_CERT]   = $this->encrypt['ssl_cert'];
178
			empty($this->encrypt['ssl_ca'])     OR $ssl[PDO::MYSQL_ATTR_SSL_CA]     = $this->encrypt['ssl_ca'];
179
			empty($this->encrypt['ssl_capath']) OR $ssl[PDO::MYSQL_ATTR_SSL_CAPATH] = $this->encrypt['ssl_capath'];
180
			empty($this->encrypt['ssl_cipher']) OR $ssl[PDO::MYSQL_ATTR_SSL_CIPHER] = $this->encrypt['ssl_cipher'];
181
 
182
			// DO NOT use array_merge() here!
183
			// It re-indexes numeric keys and the PDO_MYSQL_ATTR_SSL_* constants are integers.
184
			empty($ssl) OR $this->options += $ssl;
185
		}
186
 
187
		// Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails
188
		if (
189
			($pdo = parent::db_connect($persistent)) !== FALSE
190
			&& ! empty($ssl)
191
			&& version_compare($pdo->getAttribute(PDO::ATTR_CLIENT_VERSION), '5.7.3', '<=')
192
			&& empty($pdo->query("SHOW STATUS LIKE 'ssl_cipher'")->fetchObject()->Value)
193
		)
194
		{
195
			$message = 'PDO_MYSQL was configured for an SSL connection, but got an unencrypted connection instead!';
196
			log_message('error', $message);
197
			return ($this->db->db_debug) ? $this->db->display_error($message, '', TRUE) : FALSE;
198
		}
199
 
200
		return $pdo;
201
	}
202
 
203
	// --------------------------------------------------------------------
204
 
205
	/**
206
	 * Select the database
207
	 *
208
	 * @param	string	$database
209
	 * @return	bool
210
	 */
211
	public function db_select($database = '')
212
	{
213
		if ($database === '')
214
		{
215
			$database = $this->database;
216
		}
217
 
218
		if (FALSE !== $this->simple_query('USE '.$this->escape_identifiers($database)))
219
		{
220
			$this->database = $database;
221
			$this->data_cache = array();
222
			return TRUE;
223
		}
224
 
225
		return FALSE;
226
	}
227
 
228
	// --------------------------------------------------------------------
229
 
230
	/**
231
	 * Show table query
232
	 *
233
	 * Generates a platform-specific query string so that the table names can be fetched
234
	 *
235
	 * @param	bool	$prefix_limit
236
	 * @return	string
237
	 */
238
	protected function _list_tables($prefix_limit = FALSE)
239
	{
240
		$sql = 'SHOW TABLES';
241
 
242
		if ($prefix_limit === TRUE && $this->dbprefix !== '')
243
		{
244
			return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
245
		}
246
 
247
		return $sql;
248
	}
249
 
250
	// --------------------------------------------------------------------
251
 
252
	/**
253
	 * Show column query
254
	 *
255
	 * Generates a platform-specific query string so that the column names can be fetched
256
	 *
257
	 * @param	string	$table
258
	 * @return	string
259
	 */
260
	protected function _list_columns($table = '')
261
	{
262
		return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
263
	}
264
 
265
	// --------------------------------------------------------------------
266
 
267
	/**
268
	 * Returns an object with field data
269
	 *
270
	 * @param	string	$table
271
	 * @return	array
272
	 */
273
	public function field_data($table)
274
	{
275
		if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
276
		{
277
			return FALSE;
278
		}
279
		$query = $query->result_object();
280
 
281
		$retval = array();
282
		for ($i = 0, $c = count($query); $i < $c; $i++)
283
		{
284
			$retval[$i]			= new stdClass();
285
			$retval[$i]->name		= $query[$i]->Field;
286
 
287
			sscanf($query[$i]->Type, '%[a-z](%d)',
288
				$retval[$i]->type,
289
				$retval[$i]->max_length
290
			);
291
 
292
			$retval[$i]->default		= $query[$i]->Default;
293
			$retval[$i]->primary_key	= (int) ($query[$i]->Key === 'PRI');
294
		}
295
 
296
		return $retval;
297
	}
298
 
299
	// --------------------------------------------------------------------
300
 
301
	/**
302
	 * Truncate statement
303
	 *
304
	 * Generates a platform-specific truncate string from the supplied data
305
	 *
306
	 * If the database does not support the TRUNCATE statement,
307
	 * then this method maps to 'DELETE FROM table'
308
	 *
309
	 * @param	string	$table
310
	 * @return	string
311
	 */
312
	protected function _truncate($table)
313
	{
314
		return 'TRUNCATE '.$table;
315
	}
316
 
317
	// --------------------------------------------------------------------
318
 
319
	/**
320
	 * FROM tables
321
	 *
322
	 * Groups tables in FROM clauses if needed, so there is no confusion
323
	 * about operator precedence.
324
	 *
325
	 * @return	string
326
	 */
327
	protected function _from_tables()
328
	{
329
		if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
330
		{
331
			return '('.implode(', ', $this->qb_from).')';
332
		}
333
 
334
		return implode(', ', $this->qb_from);
335
	}
336
 
337
}