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.

229 lines
5.7 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 ODBC 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_odbc_driver extends CI_DB_pdo_driver {
  53. /**
  54. * Sub-driver
  55. *
  56. * @var string
  57. */
  58. public $subdriver = 'odbc';
  59. /**
  60. * Database schema
  61. *
  62. * @var string
  63. */
  64. public $schema = 'public';
  65. // --------------------------------------------------------------------
  66. /**
  67. * Identifier escape character
  68. *
  69. * Must be empty for ODBC.
  70. *
  71. * @var string
  72. */
  73. protected $_escape_char = '';
  74. /**
  75. * ESCAPE statement string
  76. *
  77. * @var string
  78. */
  79. protected $_like_escape_str = " {escape '%s'} ";
  80. /**
  81. * ORDER BY random keyword
  82. *
  83. * @var array
  84. */
  85. protected $_random_keyword = array('RND()', 'RND(%d)');
  86. // --------------------------------------------------------------------
  87. /**
  88. * Class constructor
  89. *
  90. * Builds the DSN if not already set.
  91. *
  92. * @param array $params
  93. * @return void
  94. */
  95. public function __construct($params)
  96. {
  97. parent::__construct($params);
  98. if (empty($this->dsn))
  99. {
  100. $this->dsn = 'odbc:';
  101. // Pre-defined DSN
  102. if (empty($this->hostname) && empty($this->HOSTNAME) && empty($this->port) && empty($this->PORT))
  103. {
  104. if (isset($this->DSN))
  105. {
  106. $this->dsn .= 'DSN='.$this->DSN;
  107. }
  108. elseif ( ! empty($this->database))
  109. {
  110. $this->dsn .= 'DSN='.$this->database;
  111. }
  112. return;
  113. }
  114. // If the DSN is not pre-configured - try to build an IBM DB2 connection string
  115. $this->dsn .= 'DRIVER='.(isset($this->DRIVER) ? '{'.$this->DRIVER.'}' : '{IBM DB2 ODBC DRIVER}').';';
  116. if (isset($this->DATABASE))
  117. {
  118. $this->dsn .= 'DATABASE='.$this->DATABASE.';';
  119. }
  120. elseif ( ! empty($this->database))
  121. {
  122. $this->dsn .= 'DATABASE='.$this->database.';';
  123. }
  124. if (isset($this->HOSTNAME))
  125. {
  126. $this->dsn .= 'HOSTNAME='.$this->HOSTNAME.';';
  127. }
  128. else
  129. {
  130. $this->dsn .= 'HOSTNAME='.(empty($this->hostname) ? '127.0.0.1;' : $this->hostname.';');
  131. }
  132. if (isset($this->PORT))
  133. {
  134. $this->dsn .= 'PORT='.$this->port.';';
  135. }
  136. elseif ( ! empty($this->port))
  137. {
  138. $this->dsn .= ';PORT='.$this->port.';';
  139. }
  140. $this->dsn .= 'PROTOCOL='.(isset($this->PROTOCOL) ? $this->PROTOCOL.';' : 'TCPIP;');
  141. }
  142. }
  143. // --------------------------------------------------------------------
  144. /**
  145. * Platform-dependent string escape
  146. *
  147. * @param string
  148. * @return string
  149. */
  150. protected function _escape_str($str)
  151. {
  152. $this->display_error('db_unsupported_feature');
  153. }
  154. // --------------------------------------------------------------------
  155. /**
  156. * Determines if a query is a "write" type.
  157. *
  158. * @param string An SQL query string
  159. * @return bool
  160. */
  161. public function is_write_type($sql)
  162. {
  163. if (preg_match('#^(INSERT|UPDATE).*RETURNING\s.+(\,\s?.+)*$#is', $sql))
  164. {
  165. return FALSE;
  166. }
  167. return parent::is_write_type($sql);
  168. }
  169. // --------------------------------------------------------------------
  170. /**
  171. * Show table query
  172. *
  173. * Generates a platform-specific query string so that the table names can be fetched
  174. *
  175. * @param bool $prefix_limit
  176. * @return string
  177. */
  178. protected function _list_tables($prefix_limit = FALSE)
  179. {
  180. $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = '".$this->schema."'";
  181. if ($prefix_limit !== FALSE && $this->dbprefix !== '')
  182. {
  183. return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' "
  184. .sprintf($this->_like_escape_str, $this->_like_escape_chr);
  185. }
  186. return $sql;
  187. }
  188. // --------------------------------------------------------------------
  189. /**
  190. * Show column query
  191. *
  192. * Generates a platform-specific query string so that the column names can be fetched
  193. *
  194. * @param string $table
  195. * @return string
  196. */
  197. protected function _list_columns($table = '')
  198. {
  199. return 'SELECT column_name FROM information_schema.columns WHERE table_name = '.$this->escape($table);
  200. }
  201. }