Subversion-Projekte lars-tiefland.ci

Revision

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