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.

326 lines
8.3 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 Oracle 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_oci_driver extends CI_DB_pdo_driver {
  53. /**
  54. * Sub-driver
  55. *
  56. * @var string
  57. */
  58. public $subdriver = 'oci';
  59. // --------------------------------------------------------------------
  60. /**
  61. * List of reserved identifiers
  62. *
  63. * Identifiers that must NOT be escaped.
  64. *
  65. * @var string[]
  66. */
  67. protected $_reserved_identifiers = array('*', 'rownum');
  68. /**
  69. * ORDER BY random keyword
  70. *
  71. * @var array
  72. */
  73. protected $_random_keyword = array('ASC', 'ASC'); // Currently not supported
  74. /**
  75. * COUNT string
  76. *
  77. * @used-by CI_DB_driver::count_all()
  78. * @used-by CI_DB_query_builder::count_all_results()
  79. *
  80. * @var string
  81. */
  82. protected $_count_string = 'SELECT COUNT(1) AS ';
  83. // --------------------------------------------------------------------
  84. /**
  85. * Class constructor
  86. *
  87. * Builds the DSN if not already set.
  88. *
  89. * @param array $params
  90. * @return void
  91. */
  92. public function __construct($params)
  93. {
  94. parent::__construct($params);
  95. if (empty($this->dsn))
  96. {
  97. $this->dsn = 'oci:dbname=';
  98. // Oracle has a slightly different PDO DSN format (Easy Connect),
  99. // which also supports pre-defined DSNs.
  100. if (empty($this->hostname) && empty($this->port))
  101. {
  102. $this->dsn .= $this->database;
  103. }
  104. else
  105. {
  106. $this->dsn .= '//'.(empty($this->hostname) ? '127.0.0.1' : $this->hostname)
  107. .(empty($this->port) ? '' : ':'.$this->port).'/';
  108. empty($this->database) OR $this->dsn .= $this->database;
  109. }
  110. empty($this->char_set) OR $this->dsn .= ';charset='.$this->char_set;
  111. }
  112. elseif ( ! empty($this->char_set) && strpos($this->dsn, 'charset=', 4) === FALSE)
  113. {
  114. $this->dsn .= ';charset='.$this->char_set;
  115. }
  116. }
  117. // --------------------------------------------------------------------
  118. /**
  119. * Database version number
  120. *
  121. * @return string
  122. */
  123. public function version()
  124. {
  125. if (isset($this->data_cache['version']))
  126. {
  127. return $this->data_cache['version'];
  128. }
  129. $version_string = parent::version();
  130. if (preg_match('#Release\s(?<version>\d+(?:\.\d+)+)#', $version_string, $match))
  131. {
  132. return $this->data_cache['version'] = $match[1];
  133. }
  134. return FALSE;
  135. }
  136. // --------------------------------------------------------------------
  137. /**
  138. * Show table query
  139. *
  140. * Generates a platform-specific query string so that the table names can be fetched
  141. *
  142. * @param bool $prefix_limit
  143. * @return string
  144. */
  145. protected function _list_tables($prefix_limit = FALSE)
  146. {
  147. $sql = 'SELECT "TABLE_NAME" FROM "ALL_TABLES"';
  148. if ($prefix_limit === TRUE && $this->dbprefix !== '')
  149. {
  150. return $sql.' WHERE "TABLE_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
  151. .sprintf($this->_like_escape_str, $this->_like_escape_chr);
  152. }
  153. return $sql;
  154. }
  155. // --------------------------------------------------------------------
  156. /**
  157. * Show column query
  158. *
  159. * Generates a platform-specific query string so that the column names can be fetched
  160. *
  161. * @param string $table
  162. * @return string
  163. */
  164. protected function _list_columns($table = '')
  165. {
  166. if (strpos($table, '.') !== FALSE)
  167. {
  168. sscanf($table, '%[^.].%s', $owner, $table);
  169. }
  170. else
  171. {
  172. $owner = $this->username;
  173. }
  174. return 'SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS
  175. WHERE UPPER(OWNER) = '.$this->escape(strtoupper($owner)).'
  176. AND UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
  177. }
  178. // --------------------------------------------------------------------
  179. /**
  180. * Returns an object with field data
  181. *
  182. * @param string $table
  183. * @return array
  184. */
  185. public function field_data($table)
  186. {
  187. if (strpos($table, '.') !== FALSE)
  188. {
  189. sscanf($table, '%[^.].%s', $owner, $table);
  190. }
  191. else
  192. {
  193. $owner = $this->username;
  194. }
  195. $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHAR_LENGTH, DATA_PRECISION, DATA_LENGTH, DATA_DEFAULT, NULLABLE
  196. FROM ALL_TAB_COLUMNS
  197. WHERE UPPER(OWNER) = '.$this->escape(strtoupper($owner)).'
  198. AND UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
  199. if (($query = $this->query($sql)) === FALSE)
  200. {
  201. return FALSE;
  202. }
  203. $query = $query->result_object();
  204. $retval = array();
  205. for ($i = 0, $c = count($query); $i < $c; $i++)
  206. {
  207. $retval[$i] = new stdClass();
  208. $retval[$i]->name = $query[$i]->COLUMN_NAME;
  209. $retval[$i]->type = $query[$i]->DATA_TYPE;
  210. $length = ($query[$i]->CHAR_LENGTH > 0)
  211. ? $query[$i]->CHAR_LENGTH : $query[$i]->DATA_PRECISION;
  212. if ($length === NULL)
  213. {
  214. $length = $query[$i]->DATA_LENGTH;
  215. }
  216. $retval[$i]->max_length = $length;
  217. $default = $query[$i]->DATA_DEFAULT;
  218. if ($default === NULL && $query[$i]->NULLABLE === 'N')
  219. {
  220. $default = '';
  221. }
  222. $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
  223. }
  224. return $retval;
  225. }
  226. // --------------------------------------------------------------------
  227. /**
  228. * Insert batch statement
  229. *
  230. * @param string $table Table name
  231. * @param array $keys INSERT keys
  232. * @param array $values INSERT values
  233. * @return string
  234. */
  235. protected function _insert_batch($table, $keys, $values)
  236. {
  237. $keys = implode(', ', $keys);
  238. $sql = "INSERT ALL\n";
  239. for ($i = 0, $c = count($values); $i < $c; $i++)
  240. {
  241. $sql .= ' INTO '.$table.' ('.$keys.') VALUES '.$values[$i]."\n";
  242. }
  243. return $sql.'SELECT * FROM dual';
  244. }
  245. // --------------------------------------------------------------------
  246. /**
  247. * Delete statement
  248. *
  249. * Generates a platform-specific delete string from the supplied data
  250. *
  251. * @param string $table
  252. * @return string
  253. */
  254. protected function _delete($table)
  255. {
  256. if ($this->qb_limit)
  257. {
  258. $this->where('rownum <= ',$this->qb_limit, FALSE);
  259. $this->qb_limit = FALSE;
  260. }
  261. return parent::_delete($table);
  262. }
  263. // --------------------------------------------------------------------
  264. /**
  265. * LIMIT
  266. *
  267. * Generates a platform-specific LIMIT clause
  268. *
  269. * @param string $sql SQL Query
  270. * @return string
  271. */
  272. protected function _limit($sql)
  273. {
  274. if (version_compare($this->version(), '12.1', '>='))
  275. {
  276. // OFFSET-FETCH can be used only with the ORDER BY clause
  277. empty($this->qb_orderby) && $sql .= ' ORDER BY 1';
  278. return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY';
  279. }
  280. return 'SELECT * FROM (SELECT inner_query.*, rownum rnum FROM ('.$sql.') inner_query WHERE rownum < '.($this->qb_offset + $this->qb_limit + 1).')'
  281. .($this->qb_offset ? ' WHERE rnum >= '.($this->qb_offset + 1): '');
  282. }
  283. }