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 2.1.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * PDO 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_driver extends CI_DB {
54
 
55
	/**
56
	 * Database driver
57
	 *
58
	 * @var	string
59
	 */
60
	public $dbdriver = 'pdo';
61
 
62
	/**
63
	 * PDO Options
64
	 *
65
	 * @var	array
66
	 */
67
	public $options = array();
68
 
69
	// --------------------------------------------------------------------
70
 
71
	/**
72
	 * Class constructor
73
	 *
74
	 * Validates the DSN string and/or detects the subdriver.
75
	 *
76
	 * @param	array	$params
77
	 * @return	void
78
	 */
79
	public function __construct($params)
80
	{
81
		parent::__construct($params);
82
 
83
		if (preg_match('/([^:]+):/', $this->dsn, $match) && count($match) === 2)
84
		{
85
			// If there is a minimum valid dsn string pattern found, we're done
86
			// This is for general PDO users, who tend to have a full DSN string.
87
			$this->subdriver = $match[1];
88
			return;
89
		}
90
		// Legacy support for DSN specified in the hostname field
91
		elseif (preg_match('/([^:]+):/', $this->hostname, $match) && count($match) === 2)
92
		{
93
			$this->dsn = $this->hostname;
94
			$this->hostname = NULL;
95
			$this->subdriver = $match[1];
96
			return;
97
		}
98
		elseif (in_array($this->subdriver, array('mssql', 'sybase'), TRUE))
99
		{
100
			$this->subdriver = 'dblib';
101
		}
102
		elseif ($this->subdriver === '4D')
103
		{
104
			$this->subdriver = '4d';
105
		}
106
		elseif ( ! in_array($this->subdriver, array('4d', 'cubrid', 'dblib', 'firebird', 'ibm', 'informix', 'mysql', 'oci', 'odbc', 'pgsql', 'sqlite', 'sqlsrv'), TRUE))
107
		{
108
			log_message('error', 'PDO: Invalid or non-existent subdriver');
109
 
110
			if ($this->db_debug)
111
			{
112
				show_error('Invalid or non-existent PDO subdriver');
113
			}
114
		}
115
 
116
		$this->dsn = NULL;
117
	}
118
 
119
	// --------------------------------------------------------------------
120
 
121
	/**
122
	 * Database connection
123
	 *
124
	 * @param	bool	$persistent
125
	 * @return	object
126
	 */
127
	public function db_connect($persistent = FALSE)
128
	{
129
		if ($persistent === TRUE)
130
		{
131
			$this->options[PDO::ATTR_PERSISTENT] = TRUE;
132
		}
133
 
134
		try
135
		{
136
			return new PDO($this->dsn, $this->username, $this->password, $this->options);
137
		}
138
		catch (PDOException $e)
139
		{
140
			if ($this->db_debug && empty($this->failover))
141
			{
142
				$this->display_error($e->getMessage(), '', TRUE);
143
			}
144
 
145
			return FALSE;
146
		}
147
	}
148
 
149
	// --------------------------------------------------------------------
150
 
151
	/**
152
	 * Database version number
153
	 *
154
	 * @return	string
155
	 */
156
	public function version()
157
	{
158
		if (isset($this->data_cache['version']))
159
		{
160
			return $this->data_cache['version'];
161
		}
162
 
163
		// Not all subdrivers support the getAttribute() method
164
		try
165
		{
166
			return $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
167
		}
168
		catch (PDOException $e)
169
		{
170
			return parent::version();
171
		}
172
	}
173
 
174
	// --------------------------------------------------------------------
175
 
176
	/**
177
	 * Execute the query
178
	 *
179
	 * @param	string	$sql	SQL query
180
	 * @return	mixed
181
	 */
182
	protected function _execute($sql)
183
	{
184
		return $this->conn_id->query($sql);
185
	}
186
 
187
	// --------------------------------------------------------------------
188
 
189
	/**
190
	 * Begin Transaction
191
	 *
192
	 * @return	bool
193
	 */
194
	protected function _trans_begin()
195
	{
196
		return $this->conn_id->beginTransaction();
197
	}
198
 
199
	// --------------------------------------------------------------------
200
 
201
	/**
202
	 * Commit Transaction
203
	 *
204
	 * @return	bool
205
	 */
206
	protected function _trans_commit()
207
	{
208
		return $this->conn_id->commit();
209
	}
210
 
211
	// --------------------------------------------------------------------
212
 
213
	/**
214
	 * Rollback Transaction
215
	 *
216
	 * @return	bool
217
	 */
218
	protected function _trans_rollback()
219
	{
220
		return $this->conn_id->rollBack();
221
	}
222
 
223
	// --------------------------------------------------------------------
224
 
225
	/**
2107 lars 226
	 * Platform-dependent string escape
68 lars 227
	 *
228
	 * @param	string
229
	 * @return	string
230
	 */
231
	protected function _escape_str($str)
232
	{
233
		// Escape the string
234
		$str = $this->conn_id->quote($str);
235
 
236
		// If there are duplicated quotes, trim them away
237
		return ($str[0] === "'")
238
			? substr($str, 1, -1)
239
			: $str;
240
	}
241
 
242
	// --------------------------------------------------------------------
243
 
244
	/**
245
	 * Affected Rows
246
	 *
247
	 * @return	int
248
	 */
249
	public function affected_rows()
250
	{
251
		return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
252
	}
253
 
254
	// --------------------------------------------------------------------
255
 
256
	/**
257
	 * Insert ID
258
	 *
259
	 * @param	string	$name
260
	 * @return	int
261
	 */
262
	public function insert_id($name = NULL)
263
	{
264
		return $this->conn_id->lastInsertId($name);
265
	}
266
 
267
	// --------------------------------------------------------------------
268
 
269
	/**
270
	 * Field data query
271
	 *
272
	 * Generates a platform-specific query so that the column data can be retrieved
273
	 *
274
	 * @param	string	$table
275
	 * @return	string
276
	 */
277
	protected function _field_data($table)
278
	{
279
		return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
280
	}
281
 
282
	// --------------------------------------------------------------------
283
 
284
	/**
285
	 * Error
286
	 *
287
	 * Returns an array containing code and message of the last
2107 lars 288
	 * database error that has occurred.
68 lars 289
	 *
290
	 * @return	array
291
	 */
292
	public function error()
293
	{
294
		$error = array('code' => '00000', 'message' => '');
295
		$pdo_error = $this->conn_id->errorInfo();
296
 
297
		if (empty($pdo_error[0]))
298
		{
299
			return $error;
300
		}
301
 
302
		$error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
303
		if (isset($pdo_error[2]))
304
		{
305
			 $error['message'] = $pdo_error[2];
306
		}
307
 
308
		return $error;
309
	}
310
 
311
	// --------------------------------------------------------------------
312
 
313
	/**
314
	 * Truncate statement
315
	 *
316
	 * Generates a platform-specific truncate string from the supplied data
317
	 *
318
	 * If the database does not support the TRUNCATE statement,
319
	 * then this method maps to 'DELETE FROM table'
320
	 *
321
	 * @param	string	$table
322
	 * @return	string
323
	 */
324
	protected function _truncate($table)
325
	{
326
		return 'TRUNCATE TABLE '.$table;
327
	}
328
 
329
}