Subversion-Projekte lars-tiefland.codeigniter

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP 4.3.2 or newer
6
 *
7
 * @package		CodeIgniter
8
 * @author		ExpressionEngine Dev Team
9
 * @copyright	Copyright (c) 2008, EllisLab, Inc.
10
 * @license		http://codeigniter.com/user_guide/license.html
11
 * @link		http://codeigniter.com
12
 * @since		Version 1.0
13
 * @filesource
14
 */
15
 
16
// ------------------------------------------------------------------------
17
 
18
/**
19
 * ODBC Database Adapter Class
20
 *
21
 * Note: _DB is an extender class that the app controller
22
 * creates dynamically based on whether the active record
23
 * class is being used or not.
24
 *
25
 * @package		CodeIgniter
26
 * @subpackage	Drivers
27
 * @category	Database
28
 * @author		ExpressionEngine Dev Team
29
 * @link		http://codeigniter.com/user_guide/database/
30
 */
31
class CI_DB_odbc_driver extends CI_DB {
32
 
33
	var $dbdriver = 'odbc';
34
 
35
	// the character used to excape - not necessary for ODBC
36
	var $_escape_char = '';
37
 
38
	/**
39
	 * The syntax to count rows is slightly different across different
40
	 * database engines, so this string appears in each driver and is
41
	 * used for the count_all() and count_all_results() functions.
42
	 */
43
	var $_count_string = "SELECT COUNT(*) AS ";
44
	var $_random_keyword;
45
 
46
 
47
	function CI_DB_odbc_driver($params)
48
	{
49
		parent::CI_DB($params);
50
 
51
		$this->_random_keyword = ' RND('.time().')'; // database specific random keyword
52
	}
53
 
54
	/**
55
	 * Non-persistent database connection
56
	 *
57
	 * @access	private called by the base class
58
	 * @return	resource
59
	 */
60
	function db_connect()
61
	{
62
		return @odbc_connect($this->hostname, $this->username, $this->password);
63
	}
64
 
65
	// --------------------------------------------------------------------
66
 
67
	/**
68
	 * Persistent database connection
69
	 *
70
	 * @access	private called by the base class
71
	 * @return	resource
72
	 */
73
	function db_pconnect()
74
	{
75
		return @odbc_pconnect($this->hostname, $this->username, $this->password);
76
	}
77
 
78
	// --------------------------------------------------------------------
79
 
80
	/**
81
	 * Select the database
82
	 *
83
	 * @access	private called by the base class
84
	 * @return	resource
85
	 */
86
	function db_select()
87
	{
88
		// Not needed for ODBC
89
		return TRUE;
90
	}
91
 
92
	// --------------------------------------------------------------------
93
 
94
	/**
95
	 * Set client character set
96
	 *
97
	 * @access	public
98
	 * @param	string
99
	 * @param	string
100
	 * @return	resource
101
	 */
102
	function db_set_charset($charset, $collation)
103
	{
104
		// @todo - add support if needed
105
		return TRUE;
106
	}
107
 
108
	// --------------------------------------------------------------------
109
 
110
	/**
111
	 * Version number query string
112
	 *
113
	 * @access	public
114
	 * @return	string
115
	 */
116
	function _version()
117
	{
118
		return "SELECT version() AS ver";
119
	}
120
 
121
	// --------------------------------------------------------------------
122
 
123
	/**
124
	 * Execute the query
125
	 *
126
	 * @access	private called by the base class
127
	 * @param	string	an SQL query
128
	 * @return	resource
129
	 */
130
	function _execute($sql)
131
	{
132
		$sql = $this->_prep_query($sql);
133
		return @odbc_exec($this->conn_id, $sql);
134
	}
135
 
136
	// --------------------------------------------------------------------
137
 
138
	/**
139
	 * Prep the query
140
	 *
141
	 * If needed, each database adapter can prep the query string
142
	 *
143
	 * @access	private called by execute()
144
	 * @param	string	an SQL query
145
	 * @return	string
146
	 */
147
	function _prep_query($sql)
148
	{
149
		return $sql;
150
	}
151
 
152
	// --------------------------------------------------------------------
153
 
154
	/**
155
	 * Begin Transaction
156
	 *
157
	 * @access	public
158
	 * @return	bool
159
	 */
160
	function trans_begin($test_mode = FALSE)
161
	{
162
		if ( ! $this->trans_enabled)
163
		{
164
			return TRUE;
165
		}
166
 
167
		// When transactions are nested we only begin/commit/rollback the outermost ones
168
		if ($this->_trans_depth > 0)
169
		{
170
			return TRUE;
171
		}
172
 
173
		// Reset the transaction failure flag.
174
		// If the $test_mode flag is set to TRUE transactions will be rolled back
175
		// even if the queries produce a successful result.
176
		$this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
177
 
178
		return odbc_autocommit($this->conn_id, FALSE);
179
	}
180
 
181
	// --------------------------------------------------------------------
182
 
183
	/**
184
	 * Commit Transaction
185
	 *
186
	 * @access	public
187
	 * @return	bool
188
	 */
189
	function trans_commit()
190
	{
191
		if ( ! $this->trans_enabled)
192
		{
193
			return TRUE;
194
		}
195
 
196
		// When transactions are nested we only begin/commit/rollback the outermost ones
197
		if ($this->_trans_depth > 0)
198
		{
199
			return TRUE;
200
		}
201
 
202
		$ret = odbc_commit($this->conn_id);
203
		odbc_autocommit($this->conn_id, TRUE);
204
		return $ret;
205
	}
206
 
207
	// --------------------------------------------------------------------
208
 
209
	/**
210
	 * Rollback Transaction
211
	 *
212
	 * @access	public
213
	 * @return	bool
214
	 */
215
	function trans_rollback()
216
	{
217
		if ( ! $this->trans_enabled)
218
		{
219
			return TRUE;
220
		}
221
 
222
		// When transactions are nested we only begin/commit/rollback the outermost ones
223
		if ($this->_trans_depth > 0)
224
		{
225
			return TRUE;
226
		}
227
 
228
		$ret = odbc_rollback($this->conn_id);
229
		odbc_autocommit($this->conn_id, TRUE);
230
		return $ret;
231
	}
232
 
233
	// --------------------------------------------------------------------
234
 
235
	/**
236
	 * Escape String
237
	 *
238
	 * @access	public
239
	 * @param	string
240
	 * @return	string
241
	 */
242
	function escape_str($str)
243
	{
244
		// Access the CI object
245
		$CI =& get_instance();
246
 
247
		// ODBC doesn't require escaping
248
		return $CI->_remove_invisible_characters($str);
249
	}
250
 
251
	// --------------------------------------------------------------------
252
 
253
	/**
254
	 * Affected Rows
255
	 *
256
	 * @access	public
257
	 * @return	integer
258
	 */
259
	function affected_rows()
260
	{
261
		return @odbc_num_rows($this->conn_id);
262
	}
263
 
264
	// --------------------------------------------------------------------
265
 
266
	/**
267
	 * Insert ID
268
	 *
269
	 * @access	public
270
	 * @return	integer
271
	 */
272
	function insert_id()
273
	{
274
		return @odbc_insert_id($this->conn_id);
275
	}
276
 
277
	// --------------------------------------------------------------------
278
 
279
	/**
280
	 * "Count All" query
281
	 *
282
	 * Generates a platform-specific query string that counts all records in
283
	 * the specified database
284
	 *
285
	 * @access	public
286
	 * @param	string
287
	 * @return	string
288
	 */
289
	function count_all($table = '')
290
	{
291
		if ($table == '')
292
		{
293
			return 0;
294
		}
295
 
296
		$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
297
 
298
		if ($query->num_rows() == 0)
299
		{
300
			return 0;
301
		}
302
 
303
		$row = $query->row();
304
		return (int) $row->numrows;
305
	}
306
 
307
	// --------------------------------------------------------------------
308
 
309
	/**
310
	 * Show table query
311
	 *
312
	 * Generates a platform-specific query string so that the table names can be fetched
313
	 *
314
	 * @access	private
315
	 * @param	boolean
316
	 * @return	string
317
	 */
318
	function _list_tables($prefix_limit = FALSE)
319
	{
320
		$sql = "SHOW TABLES FROM `".$this->database."`";
321
 
322
		if ($prefix_limit !== FALSE AND $this->dbprefix != '')
323
		{
324
			//$sql .= " LIKE '".$this->dbprefix."%'";
325
			return FALSE; // not currently supported
326
		}
327
 
328
		return $sql;
329
	}
330
 
331
	// --------------------------------------------------------------------
332
 
333
	/**
334
	 * Show column query
335
	 *
336
	 * Generates a platform-specific query string so that the column names can be fetched
337
	 *
338
	 * @access	public
339
	 * @param	string	the table name
340
	 * @return	string
341
	 */
342
	function _list_columns($table = '')
343
	{
344
		return "SHOW COLUMNS FROM ".$table;
345
	}
346
 
347
	// --------------------------------------------------------------------
348
 
349
	/**
350
	 * Field data query
351
	 *
352
	 * Generates a platform-specific query so that the column data can be retrieved
353
	 *
354
	 * @access	public
355
	 * @param	string	the table name
356
	 * @return	object
357
	 */
358
	function _field_data($table)
359
	{
360
		return "SELECT TOP 1 FROM ".$table;
361
	}
362
 
363
	// --------------------------------------------------------------------
364
 
365
	/**
366
	 * The error message string
367
	 *
368
	 * @access	private
369
	 * @return	string
370
	 */
371
	function _error_message()
372
	{
373
		return odbc_errormsg($this->conn_id);
374
	}
375
 
376
	// --------------------------------------------------------------------
377
 
378
	/**
379
	 * The error message number
380
	 *
381
	 * @access	private
382
	 * @return	integer
383
	 */
384
	function _error_number()
385
	{
386
		return odbc_error($this->conn_id);
387
	}
388
 
389
	// --------------------------------------------------------------------
390
 
391
	/**
392
	 * Escape the SQL Identifiers
393
	 *
394
	 * This function escapes column and table names
395
	 *
396
	 * @access	private
397
	 * @param	string
398
	 * @return	string
399
	 */
400
	function _escape_identifiers($item)
401
	{
402
		if ($this->_escape_char == '')
403
		{
404
			return $item;
405
		}
406
 
407
		foreach ($this->_reserved_identifiers as $id)
408
		{
409
			if (strpos($item, '.'.$id) !== FALSE)
410
			{
411
				$str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
412
 
413
				// remove duplicates if the user already included the escape
414
				return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
415
			}
416
		}
417
 
418
		if (strpos($item, '.') !== FALSE)
419
		{
420
			$str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
421
		}
422
		else
423
		{
424
			$str = $this->_escape_char.$item.$this->_escape_char;
425
		}
426
 
427
		// remove duplicates if the user already included the escape
428
		return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
429
	}
430
 
431
	// --------------------------------------------------------------------
432
 
433
	/**
434
	 * From Tables
435
	 *
436
	 * This function implicitly groups FROM tables so there is no confusion
437
	 * about operator precedence in harmony with SQL standards
438
	 *
439
	 * @access	public
440
	 * @param	type
441
	 * @return	type
442
	 */
443
	function _from_tables($tables)
444
	{
445
		if ( ! is_array($tables))
446
		{
447
			$tables = array($tables);
448
		}
449
 
450
		return '('.implode(', ', $tables).')';
451
	}
452
 
453
	// --------------------------------------------------------------------
454
 
455
	/**
456
	 * Insert statement
457
	 *
458
	 * Generates a platform-specific insert string from the supplied data
459
	 *
460
	 * @access	public
461
	 * @param	string	the table name
462
	 * @param	array	the insert keys
463
	 * @param	array	the insert values
464
	 * @return	string
465
	 */
466
	function _insert($table, $keys, $values)
467
	{
468
		return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
469
	}
470
 
471
	// --------------------------------------------------------------------
472
 
473
	/**
474
	 * Update statement
475
	 *
476
	 * Generates a platform-specific update string from the supplied data
477
	 *
478
	 * @access	public
479
	 * @param	string	the table name
480
	 * @param	array	the update data
481
	 * @param	array	the where clause
482
	 * @param	array	the orderby clause
483
	 * @param	array	the limit clause
484
	 * @return	string
485
	 */
486
	function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
487
	{
488
		foreach($values as $key => $val)
489
		{
490
			$valstr[] = $key." = ".$val;
491
		}
492
 
493
		$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
494
 
495
		$orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
496
 
497
		$sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
498
 
499
		$sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
500
 
501
		$sql .= $orderby.$limit;
502
 
503
		return $sql;
504
	}
505
 
506
 
507
	// --------------------------------------------------------------------
508
 
509
	/**
510
	 * Truncate statement
511
	 *
512
	 * Generates a platform-specific truncate string from the supplied data
513
	 * If the database does not support the truncate() command
514
	 * This function maps to "DELETE FROM table"
515
	 *
516
	 * @access	public
517
	 * @param	string	the table name
518
	 * @return	string
519
	 */
520
	function _truncate($table)
521
	{
522
		return $this->_delete($table);
523
	}
524
 
525
	// --------------------------------------------------------------------
526
 
527
	/**
528
	 * Delete statement
529
	 *
530
	 * Generates a platform-specific delete string from the supplied data
531
	 *
532
	 * @access	public
533
	 * @param	string	the table name
534
	 * @param	array	the where clause
535
	 * @param	string	the limit clause
536
	 * @return	string
537
	 */
538
	function _delete($table, $where = array(), $like = array(), $limit = FALSE)
539
	{
540
		$conditions = '';
541
 
542
		if (count($where) > 0 OR count($like) > 0)
543
		{
544
			$conditions = "\nWHERE ";
545
			$conditions .= implode("\n", $this->ar_where);
546
 
547
			if (count($where) > 0 && count($like) > 0)
548
			{
549
				$conditions .= " AND ";
550
			}
551
			$conditions .= implode("\n", $like);
552
		}
553
 
554
		$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
555
 
556
		return "DELETE FROM ".$table.$conditions.$limit;
557
	}
558
 
559
	// --------------------------------------------------------------------
560
 
561
	/**
562
	 * Limit string
563
	 *
564
	 * Generates a platform-specific LIMIT clause
565
	 *
566
	 * @access	public
567
	 * @param	string	the sql query string
568
	 * @param	integer	the number of rows to limit the query to
569
	 * @param	integer	the offset value
570
	 * @return	string
571
	 */
572
	function _limit($sql, $limit, $offset)
573
	{
574
		// Does ODBC doesn't use the LIMIT clause?
575
		return $sql;
576
	}
577
 
578
	// --------------------------------------------------------------------
579
 
580
	/**
581
	 * Close DB Connection
582
	 *
583
	 * @access	public
584
	 * @param	resource
585
	 * @return	void
586
	 */
587
	function _close($conn_id)
588
	{
589
		@odbc_close($conn_id);
590
	}
591
 
592
 
593
}
594
 
595
 
596
 
597
/* End of file odbc_driver.php */
598
/* Location: ./system/database/drivers/odbc/odbc_driver.php */