PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/system/database/drivers/pdo/subdrivers/pdo_dblib_driver.php

https://gitlab.com/betanurlaila/UI_onlineshop
PHP | 332 lines | 246 code | 9 blank | 77 comment | 2 complexity | 40025722ee9905c93c7602ff21861bc8 MD5 | raw file
  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 - 2016, 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 - 2016, 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 DBLIB 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_dblib_driver extends CI_DB_pdo_driver {
  53. /**
  54. * Sub-driver
  55. *
  56. * @var string
  57. */
  58. public $subdriver = 'dblib';
  59. // --------------------------------------------------------------------
  60. /**
  61. * ORDER BY random keyword
  62. *
  63. * @var array
  64. */
  65. protected $_random_keyword = array('NEWID()', 'RAND(%d)');
  66. /**
  67. * Quoted identifier flag
  68. *
  69. * Whether to use SQL-92 standard quoted identifier
  70. * (double quotes) or brackets for identifier escaping.
  71. *
  72. * @var bool
  73. */
  74. protected $_quoted_identifier;
  75. // --------------------------------------------------------------------
  76. /**
  77. * Class constructor
  78. *
  79. * Builds the DSN if not already set.
  80. *
  81. * @param array $params
  82. * @return void
  83. */
  84. public function __construct($params)
  85. {
  86. parent::__construct($params);
  87. if (empty($this->dsn))
  88. {
  89. $this->dsn = $params['subdriver'].':host='.(empty($this->hostname) ? '127.0.0.1' : $this->hostname);
  90. if ( ! empty($this->port))
  91. {
  92. $this->dsn .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port;
  93. }
  94. empty($this->database) OR $this->dsn .= ';dbname='.$this->database;
  95. empty($this->char_set) OR $this->dsn .= ';charset='.$this->char_set;
  96. empty($this->appname) OR $this->dsn .= ';appname='.$this->appname;
  97. }
  98. else
  99. {
  100. if ( ! empty($this->char_set) && strpos($this->dsn, 'charset=', 6) === FALSE)
  101. {
  102. $this->dsn .= ';charset='.$this->char_set;
  103. }
  104. $this->subdriver = 'dblib';
  105. }
  106. }
  107. // --------------------------------------------------------------------
  108. /**
  109. * Database connection
  110. *
  111. * @param bool $persistent
  112. * @return object
  113. */
  114. public function db_connect($persistent = FALSE)
  115. {
  116. $this->conn_id = parent::db_connect($persistent);
  117. if ( ! is_object($this->conn_id))
  118. {
  119. return $this->conn_id;
  120. }
  121. // Determine how identifiers are escaped
  122. $query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
  123. $query = $query->row_array();
  124. $this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
  125. $this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
  126. return $this->conn_id;
  127. }
  128. // --------------------------------------------------------------------
  129. /**
  130. * Show table query
  131. *
  132. * Generates a platform-specific query string so that the table names can be fetched
  133. *
  134. * @param bool $prefix_limit
  135. * @return string
  136. */
  137. protected function _list_tables($prefix_limit = FALSE)
  138. {
  139. $sql = 'SELECT '.$this->escape_identifiers('name')
  140. .' FROM '.$this->escape_identifiers('sysobjects')
  141. .' WHERE '.$this->escape_identifiers('type')." = 'U'";
  142. if ($prefix_limit === TRUE && $this->dbprefix !== '')
  143. {
  144. $sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
  145. .sprintf($this->_like_escape_str, $this->_like_escape_chr);
  146. }
  147. return $sql.' ORDER BY '.$this->escape_identifiers('name');
  148. }
  149. // --------------------------------------------------------------------
  150. /**
  151. * Show column query
  152. *
  153. * Generates a platform-specific query string so that the column names can be fetched
  154. *
  155. * @param string $table
  156. * @return string
  157. */
  158. protected function _list_columns($table = '')
  159. {
  160. return 'SELECT COLUMN_NAME
  161. FROM INFORMATION_SCHEMA.Columns
  162. WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
  163. }
  164. // --------------------------------------------------------------------
  165. /**
  166. * Returns an object with field data
  167. *
  168. * @param string $table
  169. * @return array
  170. */
  171. public function field_data($table)
  172. {
  173. $sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
  174. FROM INFORMATION_SCHEMA.Columns
  175. WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
  176. if (($query = $this->query($sql)) === FALSE)
  177. {
  178. return FALSE;
  179. }
  180. $query = $query->result_object();
  181. $retval = array();
  182. for ($i = 0, $c = count($query); $i < $c; $i++)
  183. {
  184. $retval[$i] = new stdClass();
  185. $retval[$i]->name = $query[$i]->COLUMN_NAME;
  186. $retval[$i]->type = $query[$i]->DATA_TYPE;
  187. $retval[$i]->max_length = ($query[$i]->CHARACTER_MAXIMUM_LENGTH > 0) ? $query[$i]->CHARACTER_MAXIMUM_LENGTH : $query[$i]->NUMERIC_PRECISION;
  188. $retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
  189. }
  190. return $retval;
  191. }
  192. // --------------------------------------------------------------------
  193. /**
  194. * Update statement
  195. *
  196. * Generates a platform-specific update string from the supplied data
  197. *
  198. * @param string $table
  199. * @param array $values
  200. * @return string
  201. */
  202. protected function _update($table, $values)
  203. {
  204. $this->qb_limit = FALSE;
  205. $this->qb_orderby = array();
  206. return parent::_update($table, $values);
  207. }
  208. // --------------------------------------------------------------------
  209. /**
  210. * Delete statement
  211. *
  212. * Generates a platform-specific delete string from the supplied data
  213. *
  214. * @param string $table
  215. * @return string
  216. */
  217. protected function _delete($table)
  218. {
  219. if ($this->qb_limit)
  220. {
  221. return 'WITH ci_delete AS (SELECT TOP '.$this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete';
  222. }
  223. return parent::_delete($table);
  224. }
  225. // --------------------------------------------------------------------
  226. /**
  227. * LIMIT
  228. *
  229. * Generates a platform-specific LIMIT clause
  230. *
  231. * @param string $sql SQL Query
  232. * @return string
  233. */
  234. protected function _limit($sql)
  235. {
  236. $limit = $this->qb_offset + $this->qb_limit;
  237. // As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
  238. // however an ORDER BY clause is required for it to work
  239. if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby))
  240. {
  241. $orderby = $this->_compile_order_by();
  242. // We have to strip the ORDER BY clause
  243. $sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
  244. // Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
  245. if (count($this->qb_select) === 0)
  246. {
  247. $select = '*'; // Inevitable
  248. }
  249. else
  250. {
  251. // Use only field names and their aliases, everything else is out of our scope.
  252. $select = array();
  253. $field_regexp = ($this->_quoted_identifier)
  254. ? '("[^\"]+")' : '(\[[^\]]+\])';
  255. for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
  256. {
  257. $select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
  258. ? $m[1] : $this->qb_select[$i];
  259. }
  260. $select = implode(', ', $select);
  261. }
  262. return 'SELECT '.$select." FROM (\n\n"
  263. .preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
  264. ."\n\n) ".$this->escape_identifiers('CI_subquery')
  265. ."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
  266. }
  267. return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
  268. }
  269. // --------------------------------------------------------------------
  270. /**
  271. * Insert batch statement
  272. *
  273. * Generates a platform-specific insert string from the supplied data.
  274. *
  275. * @param string $table Table name
  276. * @param array $keys INSERT keys
  277. * @param array $values INSERT values
  278. * @return string|bool
  279. */
  280. protected function _insert_batch($table, $keys, $values)
  281. {
  282. // Multiple-value inserts are only supported as of SQL Server 2008
  283. if (version_compare($this->version(), '10', '>='))
  284. {
  285. return parent::_insert_batch($table, $keys, $values);
  286. }
  287. return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE;
  288. }
  289. }