You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
7.6 KiB

7 years ago
  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. *
  9. * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
  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/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 3.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * PDO Firebird Database Adapter Class
  41. *
  42. * Note: _DB is an extender class that the app controller
  43. * creates dynamically based on whether the query builder
  44. * class is being used or not.
  45. *
  46. * @package CodeIgniter
  47. * @subpackage Drivers
  48. * @category Database
  49. * @author EllisLab Dev Team
  50. * @link https://codeigniter.com/user_guide/database/
  51. */
  52. class CI_DB_pdo_firebird_driver extends CI_DB_pdo_driver {
  53. /**
  54. * Sub-driver
  55. *
  56. * @var string
  57. */
  58. public $subdriver = 'firebird';
  59. // --------------------------------------------------------------------
  60. /**
  61. * ORDER BY random keyword
  62. *
  63. * @var array
  64. */
  65. protected $_random_keyword = array('RAND()', 'RAND()');
  66. // --------------------------------------------------------------------
  67. /**
  68. * Class constructor
  69. *
  70. * Builds the DSN if not already set.
  71. *
  72. * @param array $params
  73. * @return void
  74. */
  75. public function __construct($params)
  76. {
  77. parent::__construct($params);
  78. if (empty($this->dsn))
  79. {
  80. $this->dsn = 'firebird:';
  81. if ( ! empty($this->database))
  82. {
  83. $this->dsn .= 'dbname='.$this->database;
  84. }
  85. elseif ( ! empty($this->hostname))
  86. {
  87. $this->dsn .= 'dbname='.$this->hostname;
  88. }
  89. empty($this->char_set) OR $this->dsn .= ';charset='.$this->char_set;
  90. empty($this->role) OR $this->dsn .= ';role='.$this->role;
  91. }
  92. elseif ( ! empty($this->char_set) && strpos($this->dsn, 'charset=', 9) === FALSE)
  93. {
  94. $this->dsn .= ';charset='.$this->char_set;
  95. }
  96. }
  97. // --------------------------------------------------------------------
  98. /**
  99. * Show table query
  100. *
  101. * Generates a platform-specific query string so that the table names can be fetched
  102. *
  103. * @param bool $prefix_limit
  104. * @return string
  105. */
  106. protected function _list_tables($prefix_limit = FALSE)
  107. {
  108. $sql = 'SELECT "RDB$RELATION_NAME" FROM "RDB$RELATIONS" WHERE "RDB$RELATION_NAME" NOT LIKE \'RDB$%\' AND "RDB$RELATION_NAME" NOT LIKE \'MON$%\'';
  109. if ($prefix_limit === TRUE && $this->dbprefix !== '')
  110. {
  111. return $sql.' AND "RDB$RELATION_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
  112. .sprintf($this->_like_escape_str, $this->_like_escape_chr);
  113. }
  114. return $sql;
  115. }
  116. // --------------------------------------------------------------------
  117. /**
  118. * Show column query
  119. *
  120. * Generates a platform-specific query string so that the column names can be fetched
  121. *
  122. * @param string $table
  123. * @return string
  124. */
  125. protected function _list_columns($table = '')
  126. {
  127. return 'SELECT "RDB$FIELD_NAME" FROM "RDB$RELATION_FIELDS" WHERE "RDB$RELATION_NAME" = '.$this->escape($table);
  128. }
  129. // --------------------------------------------------------------------
  130. /**
  131. * Returns an object with field data
  132. *
  133. * @param string $table
  134. * @return array
  135. */
  136. public function field_data($table)
  137. {
  138. $sql = 'SELECT "rfields"."RDB$FIELD_NAME" AS "name",
  139. CASE "fields"."RDB$FIELD_TYPE"
  140. WHEN 7 THEN \'SMALLINT\'
  141. WHEN 8 THEN \'INTEGER\'
  142. WHEN 9 THEN \'QUAD\'
  143. WHEN 10 THEN \'FLOAT\'
  144. WHEN 11 THEN \'DFLOAT\'
  145. WHEN 12 THEN \'DATE\'
  146. WHEN 13 THEN \'TIME\'
  147. WHEN 14 THEN \'CHAR\'
  148. WHEN 16 THEN \'INT64\'
  149. WHEN 27 THEN \'DOUBLE\'
  150. WHEN 35 THEN \'TIMESTAMP\'
  151. WHEN 37 THEN \'VARCHAR\'
  152. WHEN 40 THEN \'CSTRING\'
  153. WHEN 261 THEN \'BLOB\'
  154. ELSE NULL
  155. END AS "type",
  156. "fields"."RDB$FIELD_LENGTH" AS "max_length",
  157. "rfields"."RDB$DEFAULT_VALUE" AS "default"
  158. FROM "RDB$RELATION_FIELDS" "rfields"
  159. JOIN "RDB$FIELDS" "fields" ON "rfields"."RDB$FIELD_SOURCE" = "fields"."RDB$FIELD_NAME"
  160. WHERE "rfields"."RDB$RELATION_NAME" = '.$this->escape($table).'
  161. ORDER BY "rfields"."RDB$FIELD_POSITION"';
  162. return (($query = $this->query($sql)) !== FALSE)
  163. ? $query->result_object()
  164. : FALSE;
  165. }
  166. // --------------------------------------------------------------------
  167. /**
  168. * Update statement
  169. *
  170. * Generates a platform-specific update string from the supplied data
  171. *
  172. * @param string $table
  173. * @param array $values
  174. * @return string
  175. */
  176. protected function _update($table, $values)
  177. {
  178. $this->qb_limit = FALSE;
  179. return parent::_update($table, $values);
  180. }
  181. // --------------------------------------------------------------------
  182. /**
  183. * Truncate statement
  184. *
  185. * Generates a platform-specific truncate string from the supplied data
  186. *
  187. * If the database does not support the TRUNCATE statement,
  188. * then this method maps to 'DELETE FROM table'
  189. *
  190. * @param string $table
  191. * @return string
  192. */
  193. protected function _truncate($table)
  194. {
  195. return 'DELETE FROM '.$table;
  196. }
  197. // --------------------------------------------------------------------
  198. /**
  199. * Delete statement
  200. *
  201. * Generates a platform-specific delete string from the supplied data
  202. *
  203. * @param string $table
  204. * @return string
  205. */
  206. protected function _delete($table)
  207. {
  208. $this->qb_limit = FALSE;
  209. return parent::_delete($table);
  210. }
  211. // --------------------------------------------------------------------
  212. /**
  213. * LIMIT
  214. *
  215. * Generates a platform-specific LIMIT clause
  216. *
  217. * @param string $sql SQL Query
  218. * @return string
  219. */
  220. protected function _limit($sql)
  221. {
  222. // Limit clause depends on if Interbase or Firebird
  223. if (stripos($this->version(), 'firebird') !== FALSE)
  224. {
  225. $select = 'FIRST '.$this->qb_limit
  226. .($this->qb_offset > 0 ? ' SKIP '.$this->qb_offset : '');
  227. }
  228. else
  229. {
  230. $select = 'ROWS '
  231. .($this->qb_offset > 0 ? $this->qb_offset.' TO '.($this->qb_limit + $this->qb_offset) : $this->qb_limit);
  232. }
  233. return preg_replace('`SELECT`i', 'SELECT '.$select, $sql);
  234. }
  235. // --------------------------------------------------------------------
  236. /**
  237. * Insert batch statement
  238. *
  239. * Generates a platform-specific insert string from the supplied data.
  240. *
  241. * @param string $table Table name
  242. * @param array $keys INSERT keys
  243. * @param array $values INSERT values
  244. * @return string|bool
  245. */
  246. protected function _insert_batch($table, $keys, $values)
  247. {
  248. return ($this->db_debug) ? $this->display_error('db_unsupported_feature') : FALSE;
  249. }
  250. }