PageRenderTime 3ms CodeModel.GetById 34ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/system/database/drivers/interbase/interbase_driver.php

https://github.com/andigehle/CodeIgniter
PHP | 486 lines | 188 code | 69 blank | 229 comment | 18 complexity | 970b8f8a76a7e20d29cf3d87351559e3 MD5 | raw file
  1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2/**
  3 * CodeIgniter
  4 *
  5 * An open source application development framework for PHP 5.2.4 or newer
  6 *
  7 * NOTICE OF LICENSE
  8 *
  9 * Licensed under the Open Software License version 3.0
 10 *
 11 * This source file is subject to the Open Software License (OSL 3.0) that is
 12 * bundled with this package in the files license.txt / license.rst.  It is
 13 * also available through the world wide web at this URL:
 14 * http://opensource.org/licenses/OSL-3.0
 15 * If you did not receive a copy of the license and are unable to obtain it
 16 * through the world wide web, please send an email to
 17 * licensing@ellislab.com so we can send you a copy immediately.
 18 *
 19 * @package		CodeIgniter
 20 * @author		EllisLab Dev Team
 21 * @copyright	Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
 22 * @license		http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 23 * @link		http://codeigniter.com
 24 * @since		Version 3.0
 25 * @filesource
 26 */
 27
 28/**
 29 * Firebird/Interbase Database Adapter Class
 30 *
 31 * Note: _DB is an extender class that the app controller
 32 * creates dynamically based on whether the query builder
 33 * class is being used or not.
 34 *
 35 * @package		CodeIgniter
 36 * @subpackage	Drivers
 37 * @category	Database
 38 * @author		EllisLab Dev Team
 39 * @link		http://codeigniter.com/user_guide/database/
 40 */
 41class CI_DB_interbase_driver extends CI_DB {
 42
 43	public $dbdriver = 'interbase';
 44
 45	// The character used to escape with
 46	protected $_escape_char = '"';
 47
 48	// clause and character used for LIKE escape sequences
 49	protected $_like_escape_str = " ESCAPE '%s' ";
 50	protected $_like_escape_chr = '!';
 51
 52	/**
 53	 * The syntax to count rows is slightly different across different
 54	 * database engines, so this string appears in each driver and is
 55	 * used for the count_all() and count_all_results() functions.
 56	 */
 57	protected $_count_string	= 'SELECT COUNT(*) AS ';
 58	protected $_random_keyword	= ' Random()'; // database specific random keyword
 59
 60	// Keeps track of the resource for the current transaction
 61	protected $trans;
 62
 63	/**
 64	 * Non-persistent database connection
 65	 *
 66	 * @return	resource
 67	 */
 68	public function db_connect()
 69	{
 70		return @ibase_connect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
 71	}
 72
 73	// --------------------------------------------------------------------
 74
 75	/**
 76	 * Persistent database connection
 77	 *
 78	 * @return	resource
 79	 */
 80	public function db_pconnect()
 81	{
 82		return @ibase_pconnect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
 83	}
 84
 85	// --------------------------------------------------------------------
 86
 87	/**
 88	 * Database version number
 89	 *
 90	 * @return	string
 91	 */
 92	public function version()
 93	{
 94		if (isset($this->data_cache['version']))
 95		{
 96			return $this->data_cache['version'];
 97		}
 98
 99		if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
100		{
101			$this->data_cache['version'] = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
102
103			// Don't keep the service open
104			ibase_service_detach($service);
105			return $this->data_cache['version'];
106		}
107
108		return FALSE;
109	}
110
111	// --------------------------------------------------------------------
112
113	/**
114	 * Execute the query
115	 *
116	 * @param	string	an SQL query
117	 * @return	resource
118	 */
119	protected function _execute($sql)
120	{
121		return @ibase_query($this->conn_id, $sql);
122	}
123
124	// --------------------------------------------------------------------
125
126	/**
127	 * Begin Transaction
128	 *
129	 * @return	bool
130	 */
131	public function trans_begin($test_mode = FALSE)
132	{
133		// When transactions are nested we only begin/commit/rollback the outermost ones
134		if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
135		{
136			return TRUE;
137		}
138
139		// Reset the transaction failure flag.
140		// If the $test_mode flag is set to TRUE transactions will be rolled back
141		// even if the queries produce a successful result.
142		$this->_trans_failure = ($test_mode === TRUE);
143
144		$this->trans = @ibase_trans($this->conn_id);
145
146		return TRUE;
147	}
148
149	// --------------------------------------------------------------------
150
151	/**
152	 * Commit Transaction
153	 *
154	 * @return	bool
155	 */
156	public function trans_commit()
157	{
158		// When transactions are nested we only begin/commit/rollback the outermost ones
159		if ( ! $this->trans_enabled OR $this->_trans->depth > 0)
160		{
161			return TRUE;
162		}
163
164		return @ibase_commit($this->trans);
165	}
166
167	// --------------------------------------------------------------------
168
169	/**
170	 * Rollback Transaction
171	 *
172	 * @return	bool
173	 */
174	public function trans_rollback()
175	{
176		// When transactions are nested we only begin/commit/rollback the outermost ones
177		if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
178		{
179			return TRUE;
180		}
181
182		return @ibase_rollback($this->trans);
183	}
184
185	// --------------------------------------------------------------------
186
187	/**
188	 * Escape String
189	 *
190	 * @param	string
191	 * @param	bool	whether or not the string will be used in a LIKE condition
192	 * @return	string
193	 */
194	public function escape_str($str, $like = FALSE)
195	{
196		if (is_array($str))
197		{
198			foreach ($str as $key => $val)
199			{
200				$str[$key] = $this->escape_str($val, $like);
201			}
202
203			return $str;
204		}
205
206		// escape LIKE condition wildcards
207		if ($like === TRUE)
208		{
209			return str_replace(array($this->_like_escape_chr, '%', '_'),
210						array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
211						$str);
212		}
213
214		return $str;
215	}
216
217	// --------------------------------------------------------------------
218
219	/**
220	 * Affected Rows
221	 *
222	 * @return	int
223	 */
224	public function affected_rows()
225	{
226		return @ibase_affected_rows($this->conn_id);
227	}
228
229	// --------------------------------------------------------------------
230
231	/**
232	 * Insert ID
233	 *
234	 * @param	string	$generator_name
235	 * @param	int	$inc_by
236	 * @return	int
237	 */
238	public function insert_id($generator_name, $inc_by=0)
239	{
240		//If a generator hasn't been used before it will return 0
241		return ibase_gen_id('"'.$generator_name.'"', $inc_by);
242	}
243
244	// --------------------------------------------------------------------
245
246	/**
247	 * "Count All" query
248	 *
249	 * Generates a platform-specific query string that counts all records in
250	 * the specified database
251	 *
252	 * @param	string
253	 * @return	string
254	 */
255	public function count_all($table = '')
256	{
257		if ($table == '')
258		{
259			return 0;
260		}
261
262		$query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
263		if ($query->num_rows() == 0)
264		{
265			return 0;
266		}
267
268		$query = $query->row();
269		$this->_reset_select();
270		return (int) $query->numrows;
271	}
272
273	// --------------------------------------------------------------------
274
275	/**
276	 * List table query
277	 *
278	 * Generates a platform-specific query string so that the table names can be fetched
279	 *
280	 * @param	bool
281	 * @return	string
282	 */
283	protected function _list_tables($prefix_limit = FALSE)
284	{
285		$sql = 'SELECT "RDB$RELATION_NAME" FROM "RDB$RELATIONS" WHERE "RDB$RELATION_NAME" NOT LIKE \'RDB$%\' AND "RDB$RELATION_NAME" NOT LIKE \'MON$%\'';
286
287		if ($prefix_limit !== FALSE && $this->dbprefix != '')
288		{
289			return $sql.' AND "RDB$RELATION_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
290		}
291
292		return $sql;
293	}
294
295	// --------------------------------------------------------------------
296
297	/**
298	 * Show column query
299	 *
300	 * Generates a platform-specific query string so that the column names can be fetched
301	 *
302	 * @param	string	the table name
303	 * @return	string
304	 */
305	protected function _list_columns($table = '')
306	{
307		return 'SELECT "RDB$FIELD_NAME" FROM "RDB$RELATION_FIELDS" WHERE "RDB$RELATION_NAME" = \''.$this->escape_str($table)."'";
308	}
309
310	// --------------------------------------------------------------------
311
312	/**
313	 * Field data query
314	 *
315	 * Generates a platform-specific query so that the column data can be retrieved
316	 *
317	 * @param	string	the table name
318	 * @return	string
319	 */
320	protected function _field_data($table)
321	{
322		// Need to find a more efficient way to do this
323		// but Interbase/Firebird seems to lack the
324		// limit clause
325		return 'SELECT * FROM '.$table;
326	}
327
328	// --------------------------------------------------------------------
329
330	/**
331	 * Error
332	 *
333	 * Returns an array containing code and message of the last
334	 * database error that has occured.
335	 *
336	 * @return	array
337	 */
338	public function error()
339	{
340		return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
341	}
342
343	// --------------------------------------------------------------------
344
345	/**
346	 * From Tables
347	 *
348	 * This public function implicitly groups FROM tables so there is no confusion
349	 * about operator precedence in harmony with SQL standards
350	 *
351	 * @param	array
352	 * @return	string
353	 */
354	protected function _from_tables($tables)
355	{
356		if ( ! is_array($tables))
357		{
358			$tables = array($tables);
359		}
360
361		//Interbase/Firebird doesn't like grouped tables
362		return implode(', ', $tables);
363	}
364
365	// --------------------------------------------------------------------
366
367	/**
368	 * Update statement
369	 *
370	 * Generates a platform-specific update string from the supplied data
371	 *
372	 * @param	string	the table name
373	 * @param	array	the update data
374	 * @param	array	the where clause
375	 * @param	array	the orderby clause
376	 * @param	array	the limit clause (ignored)
377	 * @param	array	the like clause
378	 * @return	string
379	 */
380	protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
381	{
382		foreach ($values as $key => $val)
383		{
384			$valstr[] = $key.' = '.$val;
385		}
386
387		$where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
388
389		if ( ! empty($like))
390		{
391			$where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
392		}
393
394		return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
395			.$where
396			.(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '');
397	}
398
399
400	// --------------------------------------------------------------------
401
402	/**
403	 * Truncate statement
404	 *
405	 * Generates a platform-specific truncate string from the supplied data
406	 *
407	 * If the database does not support the truncate() command,
408	 * then this method maps to 'DELETE FROM table'
409	 *
410	 * @param	string	the table name
411	 * @return	string
412	 */
413	protected function _truncate($table)
414	{
415		return 'DELETE FROM '.$table;
416	}
417
418	// --------------------------------------------------------------------
419
420	/**
421	 * Delete statement
422	 *
423	 * Generates a platform-specific delete string from the supplied data
424	 *
425	 * @param	string	the table name
426	 * @param	array	the where clause
427	 * @param	array	the like clause
428	 * @param	string	the limit clause (ignored)
429	 * @return	string
430	 */
431	protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
432	{
433		$conditions = array();
434
435		empty($where) OR $conditions[] = implode(' ', $where);
436		empty($like) OR $conditions[] = implode(' ', $like);
437
438		return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '');
439	}
440
441	// --------------------------------------------------------------------
442
443	/**
444	 * Limit string
445	 *
446	 * Generates a platform-specific LIMIT clause
447	 *
448	 * @param	string	the sql query string
449	 * @param	int	the number of rows to limit the query to
450	 * @param	int	the offset value
451	 * @return	string
452	 */
453	protected function _limit($sql, $limit, $offset)
454	{
455		// Limit clause depends on if Interbase or Firebird
456		if (stripos($this->version(), 'firebird') !== FALSE)
457		{
458			$select = 'FIRST '. (int) $limit
459				.($offset > 0 ? ' SKIP '. (int) $offset : '');
460		}
461		else
462		{
463			$select = 'ROWS '
464				.($offset > 0 ? (int) $offset.' TO '.($limit + $offset) : (int) $limit);
465		}
466
467		return preg_replace('`SELECT`i', 'SELECT '.$select, $sql);
468	}
469
470	// --------------------------------------------------------------------
471
472	/**
473	 * Close DB Connection
474	 *
475	 * @param	resource
476	 * @return	void
477	 */
478	protected function _close($conn_id)
479	{
480		@ibase_close($conn_id);
481	}
482
483}
484
485/* End of file interbase_driver.php */
486/* Location: ./system/database/drivers/interbase/interbase_driver.php */