Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
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
 *
2414 lars 9
 * Copyright (c) 2014 - 2019, 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/)
2414 lars 32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	https://opensource.org/licenses/MIT	MIT License
68 lars 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 DBLIB 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_dblib_driver extends CI_DB_pdo_driver {
54
 
55
	/**
56
	 * Sub-driver
57
	 *
58
	 * @var	string
59
	 */
60
	public $subdriver = 'dblib';
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 = $params['subdriver'].':host='.(empty($this->hostname) ? '127.0.0.1' : $this->hostname);
98
 
99
			if ( ! empty($this->port))
100
			{
101
				$this->dsn .= (DIRECTORY_SEPARATOR === '\\' ? ',' : ':').$this->port;
102
			}
103
 
104
			empty($this->database) OR $this->dsn .= ';dbname='.$this->database;
105
			empty($this->char_set) OR $this->dsn .= ';charset='.$this->char_set;
106
			empty($this->appname) OR $this->dsn .= ';appname='.$this->appname;
107
		}
108
		else
109
		{
110
			if ( ! empty($this->char_set) && strpos($this->dsn, 'charset=', 6) === FALSE)
111
			{
112
				$this->dsn .= ';charset='.$this->char_set;
113
			}
114
 
115
			$this->subdriver = 'dblib';
116
		}
117
	}
118
 
119
	// --------------------------------------------------------------------
120
 
121
	/**
122
	 * Database connection
123
	 *
124
	 * @param	bool	$persistent
125
	 * @return	object
126
	 */
127
	public function db_connect($persistent = FALSE)
128
	{
129
		if ($persistent === TRUE)
130
		{
131
			log_message('debug', "dblib driver doesn't support persistent connections");
132
		}
133
 
134
		$this->conn_id = parent::db_connect(FALSE);
135
 
136
		if ( ! is_object($this->conn_id))
137
		{
138
			return $this->conn_id;
139
		}
140
 
141
		// Determine how identifiers are escaped
142
		$query = $this->query('SELECT CASE WHEN (@@OPTIONS | 256) = @@OPTIONS THEN 1 ELSE 0 END AS qi');
143
		$query = $query->row_array();
144
		$this->_quoted_identifier = empty($query) ? FALSE : (bool) $query['qi'];
145
		$this->_escape_char = ($this->_quoted_identifier) ? '"' : array('[', ']');
146
 
147
		return $this->conn_id;
148
	}
149
 
150
	// --------------------------------------------------------------------
151
 
152
	/**
153
	 * Show table query
154
	 *
155
	 * Generates a platform-specific query string so that the table names can be fetched
156
	 *
157
	 * @param	bool	$prefix_limit
158
	 * @return	string
159
	 */
160
	protected function _list_tables($prefix_limit = FALSE)
161
	{
162
		$sql = 'SELECT '.$this->escape_identifiers('name')
163
			.' FROM '.$this->escape_identifiers('sysobjects')
164
			.' WHERE '.$this->escape_identifiers('type')." = 'U'";
165
 
166
		if ($prefix_limit === TRUE && $this->dbprefix !== '')
167
		{
168
			$sql .= ' AND '.$this->escape_identifiers('name')." LIKE '".$this->escape_like_str($this->dbprefix)."%' "
169
				.sprintf($this->_like_escape_str, $this->_like_escape_chr);
170
		}
171
 
172
		return $sql.' ORDER BY '.$this->escape_identifiers('name');
173
	}
174
 
175
	// --------------------------------------------------------------------
176
 
177
	/**
178
	 * Show column query
179
	 *
180
	 * Generates a platform-specific query string so that the column names can be fetched
181
	 *
182
	 * @param	string	$table
183
	 * @return	string
184
	 */
185
	protected function _list_columns($table = '')
186
	{
187
		return 'SELECT COLUMN_NAME
188
			FROM INFORMATION_SCHEMA.Columns
189
			WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
190
	}
191
 
192
	// --------------------------------------------------------------------
193
 
194
	/**
195
	 * Returns an object with field data
196
	 *
197
	 * @param	string	$table
198
	 * @return	array
199
	 */
200
	public function field_data($table)
201
	{
202
		$sql = 'SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, COLUMN_DEFAULT
203
			FROM INFORMATION_SCHEMA.Columns
204
			WHERE UPPER(TABLE_NAME) = '.$this->escape(strtoupper($table));
205
 
206
		if (($query = $this->query($sql)) === FALSE)
207
		{
208
			return FALSE;
209
		}
210
		$query = $query->result_object();
211
 
212
		$retval = array();
213
		for ($i = 0, $c = count($query); $i < $c; $i++)
214
		{
215
			$retval[$i]			= new stdClass();
216
			$retval[$i]->name		= $query[$i]->COLUMN_NAME;
217
			$retval[$i]->type		= $query[$i]->DATA_TYPE;
218
			$retval[$i]->max_length		= ($query[$i]->CHARACTER_MAXIMUM_LENGTH > 0) ? $query[$i]->CHARACTER_MAXIMUM_LENGTH : $query[$i]->NUMERIC_PRECISION;
219
			$retval[$i]->default		= $query[$i]->COLUMN_DEFAULT;
220
		}
221
 
222
		return $retval;
223
	}
224
 
225
	// --------------------------------------------------------------------
226
 
227
	/**
228
	 * Update statement
229
	 *
230
	 * Generates a platform-specific update string from the supplied data
231
	 *
232
	 * @param	string	$table
233
	 * @param	array	$values
234
	 * @return	string
235
	 */
236
	protected function _update($table, $values)
237
	{
238
		$this->qb_limit = FALSE;
239
		$this->qb_orderby = array();
240
		return parent::_update($table, $values);
241
	}
242
 
243
	// --------------------------------------------------------------------
244
 
245
	/**
246
	 * Delete statement
247
	 *
248
	 * Generates a platform-specific delete string from the supplied data
249
	 *
250
	 * @param	string	$table
251
	 * @return	string
252
	 */
253
	protected function _delete($table)
254
	{
255
		if ($this->qb_limit)
256
		{
257
			return 'WITH ci_delete AS (SELECT TOP '.$this->qb_limit.' * FROM '.$table.$this->_compile_wh('qb_where').') DELETE FROM ci_delete';
258
		}
259
 
260
		return parent::_delete($table);
261
	}
262
 
263
	// --------------------------------------------------------------------
264
 
265
	/**
266
	 * LIMIT
267
	 *
268
	 * Generates a platform-specific LIMIT clause
269
	 *
270
	 * @param	string	$sql	SQL Query
271
	 * @return	string
272
	 */
273
	protected function _limit($sql)
274
	{
275
		$limit = $this->qb_offset + $this->qb_limit;
276
 
277
		// As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
278
		// however an ORDER BY clause is required for it to work
279
		if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby))
280
		{
281
			$orderby = $this->_compile_order_by();
282
 
283
			// We have to strip the ORDER BY clause
284
			$sql = trim(substr($sql, 0, strrpos($sql, $orderby)));
285
 
286
			// Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
2107 lars 287
			if (count($this->qb_select) === 0 OR strpos(implode(',', $this->qb_select), '*') !== FALSE)
68 lars 288
			{
289
				$select = '*'; // Inevitable
290
			}
291
			else
292
			{
293
				// Use only field names and their aliases, everything else is out of our scope.
294
				$select = array();
295
				$field_regexp = ($this->_quoted_identifier)
296
					? '("[^\"]+")' : '(\[[^\]]+\])';
297
				for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
298
				{
299
					$select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
300
						? $m[1] : $this->qb_select[$i];
301
				}
302
				$select = implode(', ', $select);
303
			}
304
 
305
			return 'SELECT '.$select." FROM (\n\n"
306
				.preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
307
				."\n\n) ".$this->escape_identifiers('CI_subquery')
308
				."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
309
		}
310
 
311
		return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
312
	}
313
 
314
	// --------------------------------------------------------------------
315
 
316
	/**
317
	 * Insert batch statement
318
	 *
319
	 * Generates a platform-specific insert string from the supplied data.
320
	 *
321
	 * @param	string	$table	Table name
322
	 * @param	array	$keys	INSERT keys
323
	 * @param	array	$values	INSERT values
324
	 * @return	string|bool
325
	 */
326
	protected function _insert_batch($table, $keys, $values)
327
	{
328
		// Multiple-value inserts are only supported as of SQL Server 2008
329
		if (version_compare($this->version(), '10', '>='))
330
		{
331
			return parent::_insert_batch($table, $keys, $values);
332
		}
333
 
2107 lars 334
		return ($this->db_debug) ? $this->display_error('db_unsupported_feature') : FALSE;
68 lars 335
	}
336
 
2107 lars 337
	// --------------------------------------------------------------------
338
 
339
	/**
340
	 * Database version number
341
	 *
342
	 * @return      string
343
	 */
344
	public function version()
345
	{
346
		if (isset($this->data_cache['version']))
347
		{
348
			return $this->data_cache['version'];
349
		}
350
 
351
		return $this->data_cache['version'] = $this->conn_id->query("SELECT SERVERPROPERTY('ProductVersion') AS ver")->fetchColumn(0);
352
	}
68 lars 353
}