Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Details | Vergleich mit vorheriger | 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
 *
2414 lars 9
 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
68 lars 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/)
2414 lars 32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	https://opensource.org/licenses/MIT	MIT License
68 lars 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
		}
1257 lars 109
		elseif ( ! empty($this->char_set) && strpos($this->dsn, 'charset=', 6) === FALSE)
68 lars 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
		if (isset($this->stricton))
126
		{
127
			if ($this->stricton)
128
			{
129
				$sql = 'CONCAT(@@sql_mode, ",", "STRICT_ALL_TABLES")';
130
			}
131
			else
132
			{
133
				$sql = 'REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
134
                                        @@sql_mode,
135
                                        "STRICT_ALL_TABLES,", ""),
136
                                        ",STRICT_ALL_TABLES", ""),
137
                                        "STRICT_ALL_TABLES", ""),
138
                                        "STRICT_TRANS_TABLES,", ""),
139
                                        ",STRICT_TRANS_TABLES", ""),
140
                                        "STRICT_TRANS_TABLES", "")';
141
			}
142
 
143
			if ( ! empty($sql))
144
			{
145
				if (empty($this->options[PDO::MYSQL_ATTR_INIT_COMMAND]))
146
				{
147
					$this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET SESSION sql_mode = '.$sql;
148
				}
149
				else
150
				{
151
					$this->options[PDO::MYSQL_ATTR_INIT_COMMAND] .= ', @@session.sql_mode = '.$sql;
152
				}
153
			}
154
		}
155
 
156
		if ($this->compress === TRUE)
157
		{
158
			$this->options[PDO::MYSQL_ATTR_COMPRESS] = TRUE;
159
		}
160
 
1257 lars 161
		if (is_array($this->encrypt))
68 lars 162
		{
163
			$ssl = array();
164
			empty($this->encrypt['ssl_key'])    OR $ssl[PDO::MYSQL_ATTR_SSL_KEY]    = $this->encrypt['ssl_key'];
165
			empty($this->encrypt['ssl_cert'])   OR $ssl[PDO::MYSQL_ATTR_SSL_CERT]   = $this->encrypt['ssl_cert'];
166
			empty($this->encrypt['ssl_ca'])     OR $ssl[PDO::MYSQL_ATTR_SSL_CA]     = $this->encrypt['ssl_ca'];
167
			empty($this->encrypt['ssl_capath']) OR $ssl[PDO::MYSQL_ATTR_SSL_CAPATH] = $this->encrypt['ssl_capath'];
168
			empty($this->encrypt['ssl_cipher']) OR $ssl[PDO::MYSQL_ATTR_SSL_CIPHER] = $this->encrypt['ssl_cipher'];
169
 
2414 lars 170
			if (defined('PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT') && isset($this->encrypt['ssl_verify']))
171
			{
172
				$ssl[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = $this->encrypt['ssl_verify'];
173
			}
174
 
68 lars 175
			// DO NOT use array_merge() here!
176
			// It re-indexes numeric keys and the PDO_MYSQL_ATTR_SSL_* constants are integers.
177
			empty($ssl) OR $this->options += $ssl;
178
		}
179
 
180
		// Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails
181
		if (
182
			($pdo = parent::db_connect($persistent)) !== FALSE
183
			&& ! empty($ssl)
184
			&& version_compare($pdo->getAttribute(PDO::ATTR_CLIENT_VERSION), '5.7.3', '<=')
185
			&& empty($pdo->query("SHOW STATUS LIKE 'ssl_cipher'")->fetchObject()->Value)
186
		)
187
		{
188
			$message = 'PDO_MYSQL was configured for an SSL connection, but got an unencrypted connection instead!';
189
			log_message('error', $message);
2107 lars 190
			return ($this->db_debug) ? $this->display_error($message, '', TRUE) : FALSE;
68 lars 191
		}
192
 
193
		return $pdo;
194
	}
195
 
196
	// --------------------------------------------------------------------
197
 
198
	/**
199
	 * Select the database
200
	 *
201
	 * @param	string	$database
202
	 * @return	bool
203
	 */
204
	public function db_select($database = '')
205
	{
206
		if ($database === '')
207
		{
208
			$database = $this->database;
209
		}
210
 
211
		if (FALSE !== $this->simple_query('USE '.$this->escape_identifiers($database)))
212
		{
213
			$this->database = $database;
214
			$this->data_cache = array();
215
			return TRUE;
216
		}
217
 
218
		return FALSE;
219
	}
220
 
221
	// --------------------------------------------------------------------
222
 
223
	/**
1257 lars 224
	 * Begin Transaction
225
	 *
226
	 * @return	bool
227
	 */
228
	protected function _trans_begin()
229
	{
230
		$this->conn_id->setAttribute(PDO::ATTR_AUTOCOMMIT, FALSE);
231
		return $this->conn_id->beginTransaction();
232
	}
233
 
234
	// --------------------------------------------------------------------
235
 
236
	/**
237
	 * Commit Transaction
238
	 *
239
	 * @return	bool
240
	 */
241
	protected function _trans_commit()
242
	{
243
		if ($this->conn_id->commit())
244
		{
245
			$this->conn_id->setAttribute(PDO::ATTR_AUTOCOMMIT, TRUE);
246
			return TRUE;
247
		}
248
 
249
		return FALSE;
250
	}
251
 
252
	// --------------------------------------------------------------------
253
 
254
	/**
255
	 * Rollback Transaction
256
	 *
257
	 * @return	bool
258
	 */
259
	protected function _trans_rollback()
260
	{
261
		if ($this->conn_id->rollBack())
262
		{
263
			$this->conn_id->setAttribute(PDO::ATTR_AUTOCOMMIT, TRUE);
264
			return TRUE;
265
		}
266
 
267
		return FALSE;
268
	}
269
 
270
	// --------------------------------------------------------------------
271
 
272
	/**
68 lars 273
	 * Show table query
274
	 *
275
	 * Generates a platform-specific query string so that the table names can be fetched
276
	 *
277
	 * @param	bool	$prefix_limit
278
	 * @return	string
279
	 */
280
	protected function _list_tables($prefix_limit = FALSE)
281
	{
2414 lars 282
		$sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
68 lars 283
 
284
		if ($prefix_limit === TRUE && $this->dbprefix !== '')
285
		{
286
			return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
287
		}
288
 
289
		return $sql;
290
	}
291
 
292
	// --------------------------------------------------------------------
293
 
294
	/**
295
	 * Show column query
296
	 *
297
	 * Generates a platform-specific query string so that the column names can be fetched
298
	 *
299
	 * @param	string	$table
300
	 * @return	string
301
	 */
302
	protected function _list_columns($table = '')
303
	{
304
		return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
305
	}
306
 
307
	// --------------------------------------------------------------------
308
 
309
	/**
310
	 * Returns an object with field data
311
	 *
312
	 * @param	string	$table
313
	 * @return	array
314
	 */
315
	public function field_data($table)
316
	{
317
		if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
318
		{
319
			return FALSE;
320
		}
321
		$query = $query->result_object();
322
 
323
		$retval = array();
324
		for ($i = 0, $c = count($query); $i < $c; $i++)
325
		{
326
			$retval[$i]			= new stdClass();
327
			$retval[$i]->name		= $query[$i]->Field;
328
 
329
			sscanf($query[$i]->Type, '%[a-z](%d)',
330
				$retval[$i]->type,
331
				$retval[$i]->max_length
332
			);
333
 
334
			$retval[$i]->default		= $query[$i]->Default;
335
			$retval[$i]->primary_key	= (int) ($query[$i]->Key === 'PRI');
336
		}
337
 
338
		return $retval;
339
	}
340
 
341
	// --------------------------------------------------------------------
342
 
343
	/**
344
	 * Truncate statement
345
	 *
346
	 * Generates a platform-specific truncate string from the supplied data
347
	 *
348
	 * If the database does not support the TRUNCATE statement,
349
	 * then this method maps to 'DELETE FROM table'
350
	 *
351
	 * @param	string	$table
352
	 * @return	string
353
	 */
354
	protected function _truncate($table)
355
	{
356
		return 'TRUNCATE '.$table;
357
	}
358
 
359
	// --------------------------------------------------------------------
360
 
361
	/**
362
	 * FROM tables
363
	 *
364
	 * Groups tables in FROM clauses if needed, so there is no confusion
365
	 * about operator precedence.
366
	 *
367
	 * @return	string
368
	 */
369
	protected function _from_tables()
370
	{
371
		if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
372
		{
373
			return '('.implode(', ', $this->qb_from).')';
374
		}
375
 
376
		return implode(', ', $this->qb_from);
377
	}
378
 
379
}