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