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.

244 lines
6.5 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 IBM DB2 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_ibm_driver extends CI_DB_pdo_driver {
  53. /**
  54. * Sub-driver
  55. *
  56. * @var string
  57. */
  58. public $subdriver = 'ibm';
  59. // --------------------------------------------------------------------
  60. /**
  61. * Class constructor
  62. *
  63. * Builds the DSN if not already set.
  64. *
  65. * @param array $params
  66. * @return void
  67. */
  68. public function __construct($params)
  69. {
  70. parent::__construct($params);
  71. if (empty($this->dsn))
  72. {
  73. $this->dsn = 'ibm:';
  74. // Pre-defined DSN
  75. if (empty($this->hostname) && empty($this->HOSTNAME) && empty($this->port) && empty($this->PORT))
  76. {
  77. if (isset($this->DSN))
  78. {
  79. $this->dsn .= 'DSN='.$this->DSN;
  80. }
  81. elseif ( ! empty($this->database))
  82. {
  83. $this->dsn .= 'DSN='.$this->database;
  84. }
  85. return;
  86. }
  87. $this->dsn .= 'DRIVER='.(isset($this->DRIVER) ? '{'.$this->DRIVER.'}' : '{IBM DB2 ODBC DRIVER}').';';
  88. if (isset($this->DATABASE))
  89. {
  90. $this->dsn .= 'DATABASE='.$this->DATABASE.';';
  91. }
  92. elseif ( ! empty($this->database))
  93. {
  94. $this->dsn .= 'DATABASE='.$this->database.';';
  95. }
  96. if (isset($this->HOSTNAME))
  97. {
  98. $this->dsn .= 'HOSTNAME='.$this->HOSTNAME.';';
  99. }
  100. else
  101. {
  102. $this->dsn .= 'HOSTNAME='.(empty($this->hostname) ? '127.0.0.1;' : $this->hostname.';');
  103. }
  104. if (isset($this->PORT))
  105. {
  106. $this->dsn .= 'PORT='.$this->port.';';
  107. }
  108. elseif ( ! empty($this->port))
  109. {
  110. $this->dsn .= ';PORT='.$this->port.';';
  111. }
  112. $this->dsn .= 'PROTOCOL='.(isset($this->PROTOCOL) ? $this->PROTOCOL.';' : 'TCPIP;');
  113. }
  114. }
  115. // --------------------------------------------------------------------
  116. /**
  117. * Show table query
  118. *
  119. * Generates a platform-specific query string so that the table names can be fetched
  120. *
  121. * @param bool $prefix_limit
  122. * @return string
  123. */
  124. protected function _list_tables($prefix_limit = FALSE)
  125. {
  126. $sql = 'SELECT "tabname" FROM "syscat"."tables"
  127. WHERE "type" = \'T\' AND LOWER("tabschema") = '.$this->escape(strtolower($this->database));
  128. if ($prefix_limit === TRUE && $this->dbprefix !== '')
  129. {
  130. $sql .= ' AND "tabname" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
  131. .sprintf($this->_like_escape_str, $this->_like_escape_chr);
  132. }
  133. return $sql;
  134. }
  135. // --------------------------------------------------------------------
  136. /**
  137. * Show column query
  138. *
  139. * Generates a platform-specific query string so that the column names can be fetched
  140. *
  141. * @param string $table
  142. * @return array
  143. */
  144. protected function _list_columns($table = '')
  145. {
  146. return 'SELECT "colname" FROM "syscat"."columns"
  147. WHERE LOWER("tabschema") = '.$this->escape(strtolower($this->database)).'
  148. AND LOWER("tabname") = '.$this->escape(strtolower($table));
  149. }
  150. // --------------------------------------------------------------------
  151. /**
  152. * Returns an object with field data
  153. *
  154. * @param string $table
  155. * @return array
  156. */
  157. public function field_data($table)
  158. {
  159. $sql = 'SELECT "colname" AS "name", "typename" AS "type", "default" AS "default", "length" AS "max_length",
  160. CASE "keyseq" WHEN NULL THEN 0 ELSE 1 END AS "primary_key"
  161. FROM "syscat"."columns"
  162. WHERE LOWER("tabschema") = '.$this->escape(strtolower($this->database)).'
  163. AND LOWER("tabname") = '.$this->escape(strtolower($table)).'
  164. ORDER BY "colno"';
  165. return (($query = $this->query($sql)) !== FALSE)
  166. ? $query->result_object()
  167. : FALSE;
  168. }
  169. // --------------------------------------------------------------------
  170. /**
  171. * Update statement
  172. *
  173. * Generates a platform-specific update string from the supplied data
  174. *
  175. * @param string $table
  176. * @param array $values
  177. * @return string
  178. */
  179. protected function _update($table, $values)
  180. {
  181. $this->qb_limit = FALSE;
  182. $this->qb_orderby = array();
  183. return parent::_update($table, $values);
  184. }
  185. // --------------------------------------------------------------------
  186. /**
  187. * Delete statement
  188. *
  189. * Generates a platform-specific delete string from the supplied data
  190. *
  191. * @param string $table
  192. * @return string
  193. */
  194. protected function _delete($table)
  195. {
  196. $this->qb_limit = FALSE;
  197. return parent::_delete($table);
  198. }
  199. // --------------------------------------------------------------------
  200. /**
  201. * LIMIT
  202. *
  203. * Generates a platform-specific LIMIT clause
  204. *
  205. * @param string $sql SQL Query
  206. * @return string
  207. */
  208. protected function _limit($sql)
  209. {
  210. $sql .= ' FETCH FIRST '.($this->qb_limit + $this->qb_offset).' ROWS ONLY';
  211. return ($this->qb_offset)
  212. ? 'SELECT * FROM ('.$sql.') WHERE rownum > '.$this->qb_offset
  213. : $sql;
  214. }
  215. }