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.

405 lines
9.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 2.1.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CUBRID 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 Esen Sagynov
  50. * @link https://codeigniter.com/user_guide/database/
  51. */
  52. class CI_DB_cubrid_driver extends CI_DB {
  53. /**
  54. * Database driver
  55. *
  56. * @var string
  57. */
  58. public $dbdriver = 'cubrid';
  59. /**
  60. * Auto-commit flag
  61. *
  62. * @var bool
  63. */
  64. public $auto_commit = TRUE;
  65. // --------------------------------------------------------------------
  66. /**
  67. * Identifier escape character
  68. *
  69. * @var string
  70. */
  71. protected $_escape_char = '`';
  72. /**
  73. * ORDER BY random keyword
  74. *
  75. * @var array
  76. */
  77. protected $_random_keyword = array('RANDOM()', 'RANDOM(%d)');
  78. // --------------------------------------------------------------------
  79. /**
  80. * Class constructor
  81. *
  82. * @param array $params
  83. * @return void
  84. */
  85. public function __construct($params)
  86. {
  87. parent::__construct($params);
  88. if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:[^:]*:[^:]*:(\?.+)?$/', $this->dsn, $matches))
  89. {
  90. if (stripos($matches[2], 'autocommit=off') !== FALSE)
  91. {
  92. $this->auto_commit = FALSE;
  93. }
  94. }
  95. else
  96. {
  97. // If no port is defined by the user, use the default value
  98. empty($this->port) OR $this->port = 33000;
  99. }
  100. }
  101. // --------------------------------------------------------------------
  102. /**
  103. * Non-persistent database connection
  104. *
  105. * @param bool $persistent
  106. * @return resource
  107. */
  108. public function db_connect($persistent = FALSE)
  109. {
  110. if (preg_match('/^CUBRID:[^:]+(:[0-9][1-9]{0,4})?:[^:]+:([^:]*):([^:]*):(\?.+)?$/', $this->dsn, $matches))
  111. {
  112. $func = ($persistent !== TRUE) ? 'cubrid_connect_with_url' : 'cubrid_pconnect_with_url';
  113. return ($matches[2] === '' && $matches[3] === '' && $this->username !== '' && $this->password !== '')
  114. ? $func($this->dsn, $this->username, $this->password)
  115. : $func($this->dsn);
  116. }
  117. $func = ($persistent !== TRUE) ? 'cubrid_connect' : 'cubrid_pconnect';
  118. return ($this->username !== '')
  119. ? $func($this->hostname, $this->port, $this->database, $this->username, $this->password)
  120. : $func($this->hostname, $this->port, $this->database);
  121. }
  122. // --------------------------------------------------------------------
  123. /**
  124. * Reconnect
  125. *
  126. * Keep / reestablish the db connection if no queries have been
  127. * sent for a length of time exceeding the server's idle timeout
  128. *
  129. * @return void
  130. */
  131. public function reconnect()
  132. {
  133. if (cubrid_ping($this->conn_id) === FALSE)
  134. {
  135. $this->conn_id = FALSE;
  136. }
  137. }
  138. // --------------------------------------------------------------------
  139. /**
  140. * Database version number
  141. *
  142. * @return string
  143. */
  144. public function version()
  145. {
  146. if (isset($this->data_cache['version']))
  147. {
  148. return $this->data_cache['version'];
  149. }
  150. return ( ! $this->conn_id OR ($version = cubrid_get_server_info($this->conn_id)) === FALSE)
  151. ? FALSE
  152. : $this->data_cache['version'] = $version;
  153. }
  154. // --------------------------------------------------------------------
  155. /**
  156. * Execute the query
  157. *
  158. * @param string $sql an SQL query
  159. * @return resource
  160. */
  161. protected function _execute($sql)
  162. {
  163. return cubrid_query($sql, $this->conn_id);
  164. }
  165. // --------------------------------------------------------------------
  166. /**
  167. * Begin Transaction
  168. *
  169. * @return bool
  170. */
  171. protected function _trans_begin()
  172. {
  173. if (($autocommit = cubrid_get_autocommit($this->conn_id)) === NULL)
  174. {
  175. return FALSE;
  176. }
  177. elseif ($autocommit === TRUE)
  178. {
  179. return cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_FALSE);
  180. }
  181. return TRUE;
  182. }
  183. // --------------------------------------------------------------------
  184. /**
  185. * Commit Transaction
  186. *
  187. * @return bool
  188. */
  189. protected function _trans_commit()
  190. {
  191. if ( ! cubrid_commit($this->conn_id))
  192. {
  193. return FALSE;
  194. }
  195. if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
  196. {
  197. return cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
  198. }
  199. return TRUE;
  200. }
  201. // --------------------------------------------------------------------
  202. /**
  203. * Rollback Transaction
  204. *
  205. * @return bool
  206. */
  207. protected function _trans_rollback()
  208. {
  209. if ( ! cubrid_rollback($this->conn_id))
  210. {
  211. return FALSE;
  212. }
  213. if ($this->auto_commit && ! cubrid_get_autocommit($this->conn_id))
  214. {
  215. cubrid_set_autocommit($this->conn_id, CUBRID_AUTOCOMMIT_TRUE);
  216. }
  217. return TRUE;
  218. }
  219. // --------------------------------------------------------------------
  220. /**
  221. * Platform-dependent string escape
  222. *
  223. * @param string
  224. * @return string
  225. */
  226. protected function _escape_str($str)
  227. {
  228. return cubrid_real_escape_string($str, $this->conn_id);
  229. }
  230. // --------------------------------------------------------------------
  231. /**
  232. * Affected Rows
  233. *
  234. * @return int
  235. */
  236. public function affected_rows()
  237. {
  238. return cubrid_affected_rows();
  239. }
  240. // --------------------------------------------------------------------
  241. /**
  242. * Insert ID
  243. *
  244. * @return int
  245. */
  246. public function insert_id()
  247. {
  248. return cubrid_insert_id($this->conn_id);
  249. }
  250. // --------------------------------------------------------------------
  251. /**
  252. * List table query
  253. *
  254. * Generates a platform-specific query string so that the table names can be fetched
  255. *
  256. * @param bool $prefix_limit
  257. * @return string
  258. */
  259. protected function _list_tables($prefix_limit = FALSE)
  260. {
  261. $sql = 'SHOW TABLES';
  262. if ($prefix_limit !== FALSE && $this->dbprefix !== '')
  263. {
  264. return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
  265. }
  266. return $sql;
  267. }
  268. // --------------------------------------------------------------------
  269. /**
  270. * Show column query
  271. *
  272. * Generates a platform-specific query string so that the column names can be fetched
  273. *
  274. * @param string $table
  275. * @return string
  276. */
  277. protected function _list_columns($table = '')
  278. {
  279. return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
  280. }
  281. // --------------------------------------------------------------------
  282. /**
  283. * Returns an object with field data
  284. *
  285. * @param string $table
  286. * @return array
  287. */
  288. public function field_data($table)
  289. {
  290. if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
  291. {
  292. return FALSE;
  293. }
  294. $query = $query->result_object();
  295. $retval = array();
  296. for ($i = 0, $c = count($query); $i < $c; $i++)
  297. {
  298. $retval[$i] = new stdClass();
  299. $retval[$i]->name = $query[$i]->Field;
  300. sscanf($query[$i]->Type, '%[a-z](%d)',
  301. $retval[$i]->type,
  302. $retval[$i]->max_length
  303. );
  304. $retval[$i]->default = $query[$i]->Default;
  305. $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
  306. }
  307. return $retval;
  308. }
  309. // --------------------------------------------------------------------
  310. /**
  311. * Error
  312. *
  313. * Returns an array containing code and message of the last
  314. * database error that has occurred.
  315. *
  316. * @return array
  317. */
  318. public function error()
  319. {
  320. return array('code' => cubrid_errno($this->conn_id), 'message' => cubrid_error($this->conn_id));
  321. }
  322. // --------------------------------------------------------------------
  323. /**
  324. * FROM tables
  325. *
  326. * Groups tables in FROM clauses if needed, so there is no confusion
  327. * about operator precedence.
  328. *
  329. * @return string
  330. */
  331. protected function _from_tables()
  332. {
  333. if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
  334. {
  335. return '('.implode(', ', $this->qb_from).')';
  336. }
  337. return implode(', ', $this->qb_from);
  338. }
  339. // --------------------------------------------------------------------
  340. /**
  341. * Close DB Connection
  342. *
  343. * @return void
  344. */
  345. protected function _close()
  346. {
  347. cubrid_close($this->conn_id);
  348. }
  349. }