pdo_informix_driver.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 - 2019, 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 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://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 Informix 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_informix_driver extends CI_DB_pdo_driver {
  53. /**
  54. * Sub-driver
  55. *
  56. * @var string
  57. */
  58. public $subdriver = 'informix';
  59. // --------------------------------------------------------------------
  60. /**
  61. * ORDER BY random keyword
  62. *
  63. * @var array
  64. */
  65. protected $_random_keyword = array('ASC', 'ASC'); // Currently not supported
  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 = 'informix:';
  81. // Pre-defined DSN
  82. if (empty($this->hostname) && empty($this->host) && empty($this->port) && empty($this->service))
  83. {
  84. if (isset($this->DSN))
  85. {
  86. $this->dsn .= 'DSN='.$this->DSN;
  87. }
  88. elseif ( ! empty($this->database))
  89. {
  90. $this->dsn .= 'DSN='.$this->database;
  91. }
  92. return;
  93. }
  94. if (isset($this->host))
  95. {
  96. $this->dsn .= 'host='.$this->host;
  97. }
  98. else
  99. {
  100. $this->dsn .= 'host='.(empty($this->hostname) ? '127.0.0.1' : $this->hostname);
  101. }
  102. if (isset($this->service))
  103. {
  104. $this->dsn .= '; service='.$this->service;
  105. }
  106. elseif ( ! empty($this->port))
  107. {
  108. $this->dsn .= '; service='.$this->port;
  109. }
  110. empty($this->database) OR $this->dsn .= '; database='.$this->database;
  111. empty($this->server) OR $this->dsn .= '; server='.$this->server;
  112. $this->dsn .= '; protocol='.(isset($this->protocol) ? $this->protocol : 'onsoctcp')
  113. .'; EnableScrollableCursors=1';
  114. }
  115. }
  116. // --------------------------------------------------------------------
  117. /**
  118. * Show table query
  119. *
  120. * Generates a platform-specific query string so that the table names can be fetched
  121. *
  122. * @param bool $prefix_limit
  123. * @return string
  124. */
  125. protected function _list_tables($prefix_limit = FALSE)
  126. {
  127. $sql = 'SELECT "tabname" FROM "systables"
  128. WHERE "tabid" > 99 AND "tabtype" = \'T\' AND LOWER("owner") = '.$this->escape(strtolower($this->username));
  129. if ($prefix_limit === TRUE && $this->dbprefix !== '')
  130. {
  131. $sql .= ' AND "tabname" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
  132. .sprintf($this->_like_escape_str, $this->_like_escape_chr);
  133. }
  134. return $sql;
  135. }
  136. // --------------------------------------------------------------------
  137. /**
  138. * Show column query
  139. *
  140. * Generates a platform-specific query string so that the column names can be fetched
  141. *
  142. * @param string $table
  143. * @return string
  144. */
  145. protected function _list_columns($table = '')
  146. {
  147. if (strpos($table, '.') !== FALSE)
  148. {
  149. sscanf($table, '%[^.].%s', $owner, $table);
  150. }
  151. else
  152. {
  153. $owner = $this->username;
  154. }
  155. return 'SELECT "colname" FROM "systables", "syscolumns"
  156. WHERE "systables"."tabid" = "syscolumns"."tabid"
  157. AND "systables"."tabtype" = \'T\'
  158. AND LOWER("systables"."owner") = '.$this->escape(strtolower($owner)).'
  159. AND LOWER("systables"."tabname") = '.$this->escape(strtolower($table));
  160. }
  161. // --------------------------------------------------------------------
  162. /**
  163. * Returns an object with field data
  164. *
  165. * @param string $table
  166. * @return array
  167. */
  168. public function field_data($table)
  169. {
  170. $sql = 'SELECT "syscolumns"."colname" AS "name",
  171. CASE "syscolumns"."coltype"
  172. WHEN 0 THEN \'CHAR\'
  173. WHEN 1 THEN \'SMALLINT\'
  174. WHEN 2 THEN \'INTEGER\'
  175. WHEN 3 THEN \'FLOAT\'
  176. WHEN 4 THEN \'SMALLFLOAT\'
  177. WHEN 5 THEN \'DECIMAL\'
  178. WHEN 6 THEN \'SERIAL\'
  179. WHEN 7 THEN \'DATE\'
  180. WHEN 8 THEN \'MONEY\'
  181. WHEN 9 THEN \'NULL\'
  182. WHEN 10 THEN \'DATETIME\'
  183. WHEN 11 THEN \'BYTE\'
  184. WHEN 12 THEN \'TEXT\'
  185. WHEN 13 THEN \'VARCHAR\'
  186. WHEN 14 THEN \'INTERVAL\'
  187. WHEN 15 THEN \'NCHAR\'
  188. WHEN 16 THEN \'NVARCHAR\'
  189. WHEN 17 THEN \'INT8\'
  190. WHEN 18 THEN \'SERIAL8\'
  191. WHEN 19 THEN \'SET\'
  192. WHEN 20 THEN \'MULTISET\'
  193. WHEN 21 THEN \'LIST\'
  194. WHEN 22 THEN \'Unnamed ROW\'
  195. WHEN 40 THEN \'LVARCHAR\'
  196. WHEN 41 THEN \'BLOB/CLOB/BOOLEAN\'
  197. WHEN 4118 THEN \'Named ROW\'
  198. ELSE "syscolumns"."coltype"
  199. END AS "type",
  200. "syscolumns"."collength" as "max_length",
  201. CASE "sysdefaults"."type"
  202. WHEN \'L\' THEN "sysdefaults"."default"
  203. ELSE NULL
  204. END AS "default"
  205. FROM "syscolumns", "systables", "sysdefaults"
  206. WHERE "syscolumns"."tabid" = "systables"."tabid"
  207. AND "systables"."tabid" = "sysdefaults"."tabid"
  208. AND "syscolumns"."colno" = "sysdefaults"."colno"
  209. AND "systables"."tabtype" = \'T\'
  210. AND LOWER("systables"."owner") = '.$this->escape(strtolower($this->username)).'
  211. AND LOWER("systables"."tabname") = '.$this->escape(strtolower($table)).'
  212. ORDER BY "syscolumns"."colno"';
  213. return (($query = $this->query($sql)) !== FALSE)
  214. ? $query->result_object()
  215. : FALSE;
  216. }
  217. // --------------------------------------------------------------------
  218. /**
  219. * Update statement
  220. *
  221. * Generates a platform-specific update string from the supplied data
  222. *
  223. * @param string $table
  224. * @param array $values
  225. * @return string
  226. */
  227. protected function _update($table, $values)
  228. {
  229. $this->qb_limit = FALSE;
  230. $this->qb_orderby = array();
  231. return parent::_update($table, $values);
  232. }
  233. // --------------------------------------------------------------------
  234. /**
  235. * Truncate statement
  236. *
  237. * Generates a platform-specific truncate string from the supplied data
  238. *
  239. * If the database does not support the TRUNCATE statement,
  240. * then this method maps to 'DELETE FROM table'
  241. *
  242. * @param string $table
  243. * @return string
  244. */
  245. protected function _truncate($table)
  246. {
  247. return 'TRUNCATE TABLE ONLY '.$table;
  248. }
  249. // --------------------------------------------------------------------
  250. /**
  251. * Delete statement
  252. *
  253. * Generates a platform-specific delete string from the supplied data
  254. *
  255. * @param string $table
  256. * @return string
  257. */
  258. protected function _delete($table)
  259. {
  260. $this->qb_limit = FALSE;
  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. $select = 'SELECT '.($this->qb_offset ? 'SKIP '.$this->qb_offset : '').'FIRST '.$this->qb_limit.' ';
  275. return preg_replace('/^(SELECT\s)/i', $select, $sql, 1);
  276. }
  277. }