| 68 |
lars |
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 |
*
|
| 2242 |
lars |
9 |
* Copyright (c) 2014 - 2018, British Columbia Institute of Technology
|
| 68 |
lars |
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/)
|
| 2242 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
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 |
/**
|
|
|
41 |
* PDO SQLSRV Database Adapter Class
|
|
|
42 |
*
|
|
|
43 |
* Note: _DB is an extender class that the app controller
|
|
|
44 |
* creates dynamically based on whether the query builder
|
|
|
45 |
* class is being used or not.
|
|
|
46 |
*
|
|
|
47 |
* @package CodeIgniter
|
|
|
48 |
* @subpackage Drivers
|
|
|
49 |
* @category Database
|
|
|
50 |
* @author EllisLab Dev Team
|
|
|
51 |
* @link https://codeigniter.com/user_guide/database/
|
|
|
52 |
*/
|
|
|
53 |
class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver {
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Sub-driver
|
|
|
57 |
*
|
|
|
58 |
* @var string
|
|
|
59 |
*/
|
|
|
60 |
public $subdriver = 'sqlsrv';
|
|
|
61 |
|
|
|
62 |
// --------------------------------------------------------------------
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* ORDER BY random keyword
|
|
|
66 |
*
|
|
|
67 |
* @var array
|
|
|
68 |
*/
|
|
|
69 |
protected $_random_keyword = array('NEWID()', 'RAND(%d)');
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Quoted identifier flag
|
|
|
73 |
*
|
|
|
74 |
* Whether to use SQL-92 standard quoted identifier
|
|
|
75 |
* (double quotes) or brackets for identifier escaping.
|
|
|
76 |
*
|
|
|
77 |
* @var bool
|
|
|
78 |
*/
|
|
|
79 |
protected $_quoted_identifier;
|
|
|
80 |
|
|
|
81 |
// --------------------------------------------------------------------
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Class constructor
|
|
|
85 |
*
|
|
|
86 |
* Builds the DSN if not already set.
|
|
|
87 |
*
|
|
|
88 |
* @param array $params
|
|
|
89 |
* @return void
|
|
|
90 |
*/
|
|
|
91 |
public function __construct($params)
|
|
|
92 |
{
|
|
|
93 |
parent::__construct($params);
|
|
|
94 |
|
|
|
95 |
if (empty($this->dsn))
|
|
|
96 |
{
|
|
|
97 |
$this->dsn = 'sqlsrv:Server='.(empty($this->hostname) ? '127.0.0.1' : $this->hostname);
|
|
|
98 |
|
|
|
99 |
empty($this->port) OR $this->dsn .= ','.$this->port;
|
|
|
100 |
empty($this->database) OR $this->dsn .= ';Database='.$this->database;
|
|
|
101 |
|
|
|
102 |
// Some custom options
|
|
|
103 |
|
|
|
104 |
if (isset($this->QuotedId))
|
|
|
105 |
{
|
|
|
106 |
$this->dsn .= ';QuotedId='.$this->QuotedId;
|
|
|
107 |
$this->_quoted_identifier = (bool) $this->QuotedId;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
if (isset($this->ConnectionPooling))
|
|
|
111 |
{
|
|
|
112 |
$this->dsn .= ';ConnectionPooling='.$this->ConnectionPooling;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if ($this->encrypt === TRUE)
|
|
|
116 |
{
|
|
|
117 |
$this->dsn .= ';Encrypt=1';
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
if (isset($this->TraceOn))
|
|
|
121 |
{
|
|
|
122 |
$this->dsn .= ';TraceOn='.$this->TraceOn;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
if (isset($this->TrustServerCertificate))
|
|
|
126 |
{
|
|
|
127 |
$this->dsn .= ';TrustServerCertificate='.$this->TrustServerCertificate;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
empty($this->APP) OR $this->dsn .= ';APP='.$this->APP;
|
|
|
131 |
empty($this->Failover_Partner) OR $this->dsn .= ';Failover_Partner='.$this->Failover_Partner;
|
|
|
132 |
empty($this->LoginTimeout) OR $this->dsn .= ';LoginTimeout='.$this->LoginTimeout;
|
|
|
133 |
empty($this->MultipleActiveResultSets) OR $this->dsn .= ';MultipleActiveResultSets='.$this->MultipleActiveResultSets;
|
|
|
134 |
empty($this->TraceFile) OR $this->dsn .= ';TraceFile='.$this->TraceFile;
|
|
|
135 |
empty($this->WSID) OR $this->dsn .= ';WSID='.$this->WSID;
|
|
|
136 |
}
|
|
|
137 |
elseif (preg_match('/QuotedId=(0|1)/', $this->dsn, $match))
|
|
|
138 |
{
|
|
|
139 |
$this->_quoted_identifier = (bool) $match[1];
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
// --------------------------------------------------------------------
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Database connection
|
|
|
147 |
*
|
|
|
148 |
* @param bool $persistent
|
|
|
149 |
* @return object
|
|
|
150 |
*/
|
|
|
151 |
public function db_connect($persistent = FALSE)
|
|
|
152 |
{
|
|
|
153 |
if ( ! empty($this->char_set) && preg_match('/utf[^8]*8/i', $this->char_set))
|
|
|
154 |
{
|
|
|
155 |
$this->options[PDO::SQLSRV_ENCODING_UTF8] = 1;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
$this->conn_id = parent::db_connect($persistent);
|
|
|
159 |
|
|
|
160 |
if ( ! is_object($this->conn_id) OR is_bool($this->_quoted_identifier))
|
|
|
161 |
{
|
|
|
162 |
return $this->conn_id;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
// Determine how identifiers are escaped
|
|
|
166 |
$query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
|
|
|
167 |
$query = $query->row_array();
|
|
|
168 |
$this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
|
|
|
169 |
$this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
|
|
|
170 |
|
|
|
171 |
return $this->conn_id;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
// --------------------------------------------------------------------
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Show table query
|
|
|
178 |
*
|
|
|
179 |
* Generates a platform-specific query string so that the table names can be fetched
|
|
|
180 |
*
|
|
|
181 |
* @param bool $prefix_limit
|
|
|
182 |
* @return string
|
|
|
183 |
*/
|
|
|
184 |
protected function _list_tables($prefix_limit = FALSE)
|
|
|
185 |
{
|
|
|
186 |
$sql = 'SELECT '.$this->escape_identifiers('name')
|
|
|
187 |
.' FROM '.$this->escape_identifiers('sysobjects')
|
|
|
188 |
.' WHERE '.$this->escape_identifiers('type')." = 'U'";
|
|
|
189 |
|
|
|
190 |
if ($prefix_limit === TRUE && $this->dbprefix !== '')
|
|
|
191 |
{
|
|
|
192 |
$sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
|
|
|
193 |
.sprintf($this->_like_escape_str, $this->_like_escape_chr);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
return $sql.' ORDER BY '.$this->escape_identifiers('name');
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
// --------------------------------------------------------------------
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Show column query
|
|
|
203 |
*
|
|
|
204 |
* Generates a platform-specific query string so that the column names can be fetched
|
|
|
205 |
*
|
|
|
206 |
* @param string $table
|
|
|
207 |
* @return string
|
|
|
208 |
*/
|
|
|
209 |
protected function _list_columns($table = '')
|
|
|
210 |
{
|
|
|
211 |
return 'SELECT COLUMN_NAME
|
|
|
212 |
FROM INFORMATION_SCHEMA.Columns
|
|
|
213 |
WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
// --------------------------------------------------------------------
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* Returns an object with field data
|
|
|
220 |
*
|
|
|
221 |
* @param string $table
|
|
|
222 |
* @return array
|
|
|
223 |
*/
|
|
|
224 |
public function field_data($table)
|
|
|
225 |
{
|
|
|
226 |
$sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
|
|
|
227 |
FROM INFORMATION_SCHEMA.Columns
|
|
|
228 |
WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
|
|
|
229 |
|
|
|
230 |
if (($query = $this->query($sql)) === FALSE)
|
|
|
231 |
{
|
|
|
232 |
return FALSE;
|
|
|
233 |
}
|
|
|
234 |
$query = $query->result_object();
|
|
|
235 |
|
|
|
236 |
$retval = array();
|
|
|
237 |
for ($i = 0, $c = count($query); $i < $c; $i++)
|
|
|
238 |
{
|
|
|
239 |
$retval[$i] = new stdClass();
|
|
|
240 |
$retval[$i]->name = $query[$i]->COLUMN_NAME;
|
|
|
241 |
$retval[$i]->type = $query[$i]->DATA_TYPE;
|
|
|
242 |
$retval[$i]->max_length = ($query[$i]->CHARACTER_MAXIMUM_LENGTH > 0) ? $query[$i]->CHARACTER_MAXIMUM_LENGTH : $query[$i]->NUMERIC_PRECISION;
|
|
|
243 |
$retval[$i]->default = $query[$i]->COLUMN_DEFAULT;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
return $retval;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
// --------------------------------------------------------------------
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* Update statement
|
|
|
253 |
*
|
|
|
254 |
* Generates a platform-specific update string from the supplied data
|
|
|
255 |
*
|
|
|
256 |
* @param string $table
|
|
|
257 |
* @param array $values
|
|
|
258 |
* @return string
|
|
|
259 |
*/
|
|
|
260 |
protected function _update($table, $values)
|
|
|
261 |
{
|
|
|
262 |
$this->qb_limit = FALSE;
|
|
|
263 |
$this->qb_orderby = array();
|
|
|
264 |
return parent::_update($table, $values);
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
// --------------------------------------------------------------------
|
|
|
268 |
|
|
|
269 |
/**
|
|
|
270 |
* Delete statement
|
|
|
271 |
*
|
|
|
272 |
* Generates a platform-specific delete string from the supplied data
|
|
|
273 |
*
|
|
|
274 |
* @param string $table
|
|
|
275 |
* @return string
|
|
|
276 |
*/
|
|
|
277 |
protected function _delete($table)
|
|
|
278 |
{
|
|
|
279 |
if ($this->qb_limit)
|
|
|
280 |
{
|
|
|
281 |
return 'WITH ci_delete AS (SELECT TOP '.$this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete';
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
return parent::_delete($table);
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
// --------------------------------------------------------------------
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* LIMIT
|
|
|
291 |
*
|
|
|
292 |
* Generates a platform-specific LIMIT clause
|
|
|
293 |
*
|
|
|
294 |
* @param string $sql SQL Query
|
|
|
295 |
* @return string
|
|
|
296 |
*/
|
|
|
297 |
protected function _limit($sql)
|
|
|
298 |
{
|
|
|
299 |
// As of SQL Server 2012 (11.0.*) OFFSET is supported
|
|
|
300 |
if (version_compare($this->version(), '11', '>='))
|
|
|
301 |
{
|
|
|
302 |
// SQL Server OFFSET-FETCH can be used only with the ORDER BY clause
|
|
|
303 |
empty($this->qb_orderby) && $sql .= ' ORDER BY 1';
|
|
|
304 |
|
|
|
305 |
return $sql.' OFFSET '.(int) $this->qb_offset.' ROWS FETCH NEXT '.$this->qb_limit.' ROWS ONLY';
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
$limit = $this->qb_offset + $this->qb_limit;
|
|
|
309 |
|
|
|
310 |
// An ORDER BY clause is required for ROW_NUMBER() to work
|
|
|
311 |
if ($this->qb_offset && ! empty($this->qb_orderby))
|
|
|
312 |
{
|
|
|
313 |
$orderby = $this->_compile_order_by();
|
|
|
314 |
|
|
|
315 |
// We have to strip the ORDER BY clause
|
|
|
316 |
$sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
|
|
|
317 |
|
|
|
318 |
// Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
|
| 2107 |
lars |
319 |
if (count($this->qb_select) === 0 OR strpos(implode(',', $this->qb_select), '*') !== FALSE)
|
| 68 |
lars |
320 |
{
|
|
|
321 |
$select = '*'; // Inevitable
|
|
|
322 |
}
|
|
|
323 |
else
|
|
|
324 |
{
|
|
|
325 |
// Use only field names and their aliases, everything else is out of our scope.
|
|
|
326 |
$select = array();
|
|
|
327 |
$field_regexp = ($this->_quoted_identifier)
|
|
|
328 |
? '("[^\"]+")' : '(\[[^\]]+\])';
|
|
|
329 |
for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
|
|
|
330 |
{
|
|
|
331 |
$select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
|
|
|
332 |
? $m[1] : $this->qb_select[$i];
|
|
|
333 |
}
|
|
|
334 |
$select = implode(', ', $select);
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
return 'SELECT '.$select." FROM (\n\n"
|
|
|
338 |
.preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
|
|
|
339 |
."\n\n) ".$this->escape_identifiers('CI_subquery')
|
|
|
340 |
."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
// --------------------------------------------------------------------
|
|
|
347 |
|
|
|
348 |
/**
|
|
|
349 |
* Insert batch statement
|
|
|
350 |
*
|
|
|
351 |
* Generates a platform-specific insert string from the supplied data.
|
|
|
352 |
*
|
|
|
353 |
* @param string $table Table name
|
|
|
354 |
* @param array $keys INSERT keys
|
|
|
355 |
* @param array $values INSERT values
|
|
|
356 |
* @return string|bool
|
|
|
357 |
*/
|
|
|
358 |
protected function _insert_batch($table, $keys, $values)
|
|
|
359 |
{
|
|
|
360 |
// Multiple-value inserts are only supported as of SQL Server 2008
|
|
|
361 |
if (version_compare($this->version(), '10', '>='))
|
|
|
362 |
{
|
|
|
363 |
return parent::_insert_batch($table, $keys, $values);
|
|
|
364 |
}
|
|
|
365 |
|
| 2107 |
lars |
366 |
return ($this->db_debug) ? $this->display_error('db_unsupported_feature') : FALSE;
|
| 68 |
lars |
367 |
}
|
|
|
368 |
|
|
|
369 |
}
|