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 1.3.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * ODBC 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_odbc_driver extends CI_DB_driver {
54
 
55
	/**
56
	 * Database driver
57
	 *
58
	 * @var	string
59
	 */
60
	public $dbdriver = 'odbc';
61
 
62
	/**
63
	 * Database schema
64
	 *
65
	 * @var	string
66
	 */
67
	public $schema = 'public';
68
 
69
	// --------------------------------------------------------------------
70
 
71
	/**
72
	 * Identifier escape character
73
	 *
74
	 * Must be empty for ODBC.
75
	 *
76
	 * @var	string
77
	 */
78
	protected $_escape_char = '';
79
 
80
	/**
81
	 * ESCAPE statement string
82
	 *
83
	 * @var	string
84
	 */
85
	protected $_like_escape_str = " {escape '%s'} ";
86
 
87
	/**
88
	 * ORDER BY random keyword
89
	 *
90
	 * @var	array
91
	 */
92
	protected $_random_keyword = array('RND()', 'RND(%d)');
93
 
94
	// --------------------------------------------------------------------
95
 
96
	/**
97
	 * ODBC result ID resource returned from odbc_prepare()
98
	 *
99
	 * @var	resource
100
	 */
101
	private $odbc_result;
102
 
103
	/**
104
	 * Values to use with odbc_execute() for prepared statements
105
	 *
106
	 * @var	array
107
	 */
108
	private $binds = array();
109
 
110
	// --------------------------------------------------------------------
111
 
112
	/**
113
	 * Class constructor
114
	 *
115
	 * @param	array	$params
116
	 * @return	void
117
	 */
118
	public function __construct($params)
119
	{
120
		parent::__construct($params);
121
 
122
		// Legacy support for DSN in the hostname field
123
		if (empty($this->dsn))
124
		{
125
			$this->dsn = $this->hostname;
126
		}
127
	}
128
 
129
	// --------------------------------------------------------------------
130
 
131
	/**
132
	 * Non-persistent database connection
133
	 *
134
	 * @param	bool	$persistent
135
	 * @return	resource
136
	 */
137
	public function db_connect($persistent = FALSE)
138
	{
139
		return ($persistent === TRUE)
140
			? odbc_pconnect($this->dsn, $this->username, $this->password)
141
			: odbc_connect($this->dsn, $this->username, $this->password);
142
	}
143
 
144
	// --------------------------------------------------------------------
145
 
146
	/**
147
	 * Compile Bindings
148
	 *
149
	 * @param	string	$sql	SQL statement
150
	 * @param	array	$binds	An array of values to bind
151
	 * @return	string
152
	 */
153
	public function compile_binds($sql, $binds)
154
	{
155
		if (empty($binds) OR empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
156
		{
157
			return $sql;
158
		}
159
		elseif ( ! is_array($binds))
160
		{
161
			$binds = array($binds);
162
			$bind_count = 1;
163
		}
164
		else
165
		{
166
			// Make sure we're using numeric keys
167
			$binds = array_values($binds);
168
			$bind_count = count($binds);
169
		}
170
 
171
		// We'll need the marker length later
172
		$ml = strlen($this->bind_marker);
173
 
174
		// Make sure not to replace a chunk inside a string that happens to match the bind marker
2049 lars 175
		if ($c = preg_match_all("/'[^']*'|\"[^\"]*\"/i", $sql, $matches))
68 lars 176
		{
177
			$c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
178
				str_replace($matches[0],
179
					str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
180
					$sql, $c),
181
				$matches, PREG_OFFSET_CAPTURE);
182
 
183
			// Bind values' count must match the count of markers in the query
184
			if ($bind_count !== $c)
185
			{
186
				return $sql;
187
			}
188
		}
189
		elseif (($c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i', $sql, $matches, PREG_OFFSET_CAPTURE)) !== $bind_count)
190
		{
191
			return $sql;
192
		}
193
 
194
		if ($this->bind_marker !== '?')
195
		{
196
			do
197
			{
198
				$c--;
199
				$sql = substr_replace($sql, '?', $matches[0][$c][1], $ml);
200
			}
201
			while ($c !== 0);
202
		}
203
 
204
		if (FALSE !== ($this->odbc_result = odbc_prepare($this->conn_id, $sql)))
205
		{
206
			$this->binds = array_values($binds);
207
		}
208
 
209
		return $sql;
210
	}
211
 
212
	// --------------------------------------------------------------------
213
 
214
	/**
215
	 * Execute the query
216
	 *
217
	 * @param	string	$sql	an SQL query
218
	 * @return	resource
219
	 */
220
	protected function _execute($sql)
221
	{
222
		if ( ! isset($this->odbc_result))
223
		{
224
			return odbc_exec($this->conn_id, $sql);
225
		}
226
		elseif ($this->odbc_result === FALSE)
227
		{
228
			return FALSE;
229
		}
230
 
231
		if (TRUE === ($success = odbc_execute($this->odbc_result, $this->binds)))
232
		{
233
			// For queries that return result sets, return the result_id resource on success
234
			$this->is_write_type($sql) OR $success = $this->odbc_result;
235
		}
236
 
237
		$this->odbc_result = NULL;
238
		$this->binds       = array();
239
 
240
		return $success;
241
	}
242
 
243
	// --------------------------------------------------------------------
244
 
245
	/**
246
	 * Begin Transaction
247
	 *
248
	 * @return	bool
249
	 */
250
	protected function _trans_begin()
251
	{
252
		return odbc_autocommit($this->conn_id, FALSE);
253
	}
254
 
255
	// --------------------------------------------------------------------
256
 
257
	/**
258
	 * Commit Transaction
259
	 *
260
	 * @return	bool
261
	 */
262
	protected function _trans_commit()
263
	{
264
		if (odbc_commit($this->conn_id))
265
		{
266
			odbc_autocommit($this->conn_id, TRUE);
267
			return TRUE;
268
		}
269
 
270
		return FALSE;
271
	}
272
 
273
	// --------------------------------------------------------------------
274
 
275
	/**
276
	 * Rollback Transaction
277
	 *
278
	 * @return	bool
279
	 */
280
	protected function _trans_rollback()
281
	{
282
		if (odbc_rollback($this->conn_id))
283
		{
284
			odbc_autocommit($this->conn_id, TRUE);
285
			return TRUE;
286
		}
287
 
288
		return FALSE;
289
	}
290
 
291
	// --------------------------------------------------------------------
292
 
293
	/**
294
	 * Determines if a query is a "write" type.
295
	 *
296
	 * @param	string	An SQL query string
297
	 * @return	bool
298
	 */
299
	public function is_write_type($sql)
300
	{
1257 lars 301
		if (preg_match('#^(INSERT|UPDATE).*RETURNING\s.+(\,\s?.+)*$#is', $sql))
68 lars 302
		{
303
			return FALSE;
304
		}
305
 
306
		return parent::is_write_type($sql);
307
	}
308
 
309
	// --------------------------------------------------------------------
310
 
311
	/**
2107 lars 312
	 * Platform-dependent string escape
68 lars 313
	 *
314
	 * @param	string
315
	 * @return	string
316
	 */
317
	protected function _escape_str($str)
318
	{
2107 lars 319
		$this->display_error('db_unsupported_feature');
68 lars 320
	}
321
 
322
	// --------------------------------------------------------------------
323
 
324
	/**
325
	 * Affected Rows
326
	 *
327
	 * @return	int
328
	 */
329
	public function affected_rows()
330
	{
331
		return odbc_num_rows($this->result_id);
332
	}
333
 
334
	// --------------------------------------------------------------------
335
 
336
	/**
337
	 * Insert ID
338
	 *
339
	 * @return	bool
340
	 */
341
	public function insert_id()
342
	{
2107 lars 343
		return ($this->db_debug) ? $this->display_error('db_unsupported_feature') : FALSE;
68 lars 344
	}
345
 
346
	// --------------------------------------------------------------------
347
 
348
	/**
349
	 * Show table query
350
	 *
351
	 * Generates a platform-specific query string so that the table names can be fetched
352
	 *
353
	 * @param	bool	$prefix_limit
354
	 * @return	string
355
	 */
356
	protected function _list_tables($prefix_limit = FALSE)
357
	{
358
		$sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
359
 
360
		if ($prefix_limit !== FALSE && $this->dbprefix !== '')
361
		{
362
			return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
363
				.sprintf($this->_like_escape_str, $this->_like_escape_chr);
364
		}
365
 
366
		return $sql;
367
	}
368
 
369
	// --------------------------------------------------------------------
370
 
371
	/**
372
	 * Show column query
373
	 *
374
	 * Generates a platform-specific query string so that the column names can be fetched
375
	 *
376
	 * @param	string	$table
377
	 * @return	string
378
	 */
379
	protected function _list_columns($table = '')
380
	{
381
		return 'SHOW COLUMNS FROM '.$table;
382
	}
383
 
384
	// --------------------------------------------------------------------
385
 
386
	/**
387
	 * Field data query
388
	 *
389
	 * Generates a platform-specific query so that the column data can be retrieved
390
	 *
391
	 * @param	string	$table
392
	 * @return	string
393
	 */
394
	protected function _field_data($table)
395
	{
396
		return 'SELECT TOP 1 FROM '.$table;
397
	}
398
 
399
	// --------------------------------------------------------------------
400
 
401
	/**
402
	 * Error
403
	 *
404
	 * Returns an array containing code and message of the last
2107 lars 405
	 * database error that has occurred.
68 lars 406
	 *
407
	 * @return	array
408
	 */
409
	public function error()
410
	{
411
		return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
412
	}
413
 
414
	// --------------------------------------------------------------------
415
 
416
	/**
417
	 * Close DB Connection
418
	 *
419
	 * @return	void
420
	 */
421
	protected function _close()
422
	{
423
		odbc_close($this->conn_id);
424
	}
425
}