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