blob: 390840094227702d92ede69d534193eb18de19e7 [file] [log] [blame]
Alex Bilbie84445d02011-03-10 16:43:39 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Alex Bilbie84445d02011-03-10 16:43:39 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Alex Bilbie84445d02011-03-10 16:43:39 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000031 * SQLSRV Database Adapter Class
Alex Bilbie84445d02011-03-10 16:43:39 +000032 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Alex Bilbie84445d02011-03-10 16:43:39 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000043class CI_DB_sqlsrv_driver extends CI_DB {
Alex Bilbie84445d02011-03-10 16:43:39 +000044
45 var $dbdriver = 'sqlsrv';
46
47 // The character used for escaping
48 var $_escape_char = '';
49
50 // clause and character used for LIKE escape sequences
51 var $_like_escape_str = " ESCAPE '%s' ";
52 var $_like_escape_chr = '!';
53
54 /**
55 * The syntax to count rows is slightly different across different
56 * database engines, so this string appears in each driver and is
57 * used for the count_all() and count_all_results() functions.
58 */
59 var $_count_string = "SELECT COUNT(*) AS ";
60 var $_random_keyword = ' ASC'; // not currently supported
61
62 /**
63 * Non-persistent database connection
64 *
65 * @access private called by the base class
66 * @return resource
67 */
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000068 function db_connect($pooling = false)
Alex Bilbie84445d02011-03-10 16:43:39 +000069 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000070 // Check for a UTF-8 charset being passed as CI's default 'utf8'.
71 $character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
72
73 $connection = array(
74 'UID' => empty($this->username) ? '' : $this->username,
75 'PWD' => empty($this->password) ? '' : $this->password,
76 'Database' => $this->database,
77 'ConnectionPooling' => $pooling ? 1 : 0,
78 'CharacterSet' => $character_set,
79 'ReturnDatesAsStrings' => 1
80 );
81
82 // If the username and password are both empty, assume this is a
83 // 'Windows Authentication Mode' connection.
84 if(empty($connection['UID']) && empty($connection['PWD'])) {
85 unset($connection['UID'], $connection['PWD']);
Alex Bilbie84445d02011-03-10 16:43:39 +000086 }
87
Alex Bilbie3a43c7a2011-03-18 19:48:04 +000088 return sqlsrv_connect($this->hostname, $connection);
Alex Bilbie84445d02011-03-10 16:43:39 +000089 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * Persistent database connection
95 *
96 * @access private called by the base class
97 * @return resource
98 */
99 function db_pconnect()
100 {
Kyle Farris37e351f2011-09-07 11:14:46 -0300101 return $this->db_connect(TRUE);
Alex Bilbie84445d02011-03-10 16:43:39 +0000102 }
103
104 // --------------------------------------------------------------------
105
106 /**
107 * Reconnect
108 *
109 * Keep / reestablish the db connection if no queries have been
110 * sent for a length of time exceeding the server's idle timeout
111 *
112 * @access public
113 * @return void
114 */
115 function reconnect()
116 {
117 // not implemented in MSSQL
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Select the database
124 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200125 * @param string database name
126 * @return bool
Alex Bilbie84445d02011-03-10 16:43:39 +0000127 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200128 public function db_select($database = '')
Alex Bilbie84445d02011-03-10 16:43:39 +0000129 {
Andrey Andreev11454e02012-02-22 16:05:47 +0200130 return $this->_execute('USE '.($database == '' ? $this->database : $database));
Alex Bilbie84445d02011-03-10 16:43:39 +0000131 }
132
133 // --------------------------------------------------------------------
134
135 /**
136 * Set client character set
137 *
138 * @access public
139 * @param string
140 * @param string
141 * @return resource
142 */
143 function db_set_charset($charset, $collation)
144 {
145 // @todo - add support if needed
146 return TRUE;
147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Execute the query
153 *
154 * @access private called by the base class
155 * @param string an SQL query
156 * @return resource
157 */
158 function _execute($sql)
159 {
160 $sql = $this->_prep_query($sql);
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000161 return sqlsrv_query($this->conn_id, $sql, null, array(
162 'Scrollable' => SQLSRV_CURSOR_STATIC,
163 'SendStreamParamsAtExec' => true
164 ));
Alex Bilbie84445d02011-03-10 16:43:39 +0000165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Prep the query
171 *
172 * If needed, each database adapter can prep the query string
173 *
174 * @access private called by execute()
175 * @param string an SQL query
176 * @return string
177 */
178 function _prep_query($sql)
179 {
180 return $sql;
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Begin Transaction
187 *
188 * @access public
189 * @return bool
190 */
191 function trans_begin($test_mode = FALSE)
192 {
193 if ( ! $this->trans_enabled)
194 {
195 return TRUE;
196 }
197
198 // When transactions are nested we only begin/commit/rollback the outermost ones
199 if ($this->_trans_depth > 0)
200 {
201 return TRUE;
202 }
203
204 // Reset the transaction failure flag.
205 // If the $test_mode flag is set to TRUE transactions will be rolled back
206 // even if the queries produce a successful result.
207 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
208
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000209 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Commit Transaction
216 *
217 * @access public
218 * @return bool
219 */
220 function trans_commit()
221 {
222 if ( ! $this->trans_enabled)
223 {
224 return TRUE;
225 }
226
227 // When transactions are nested we only begin/commit/rollback the outermost ones
228 if ($this->_trans_depth > 0)
229 {
230 return TRUE;
231 }
232
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000233 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Rollback Transaction
240 *
241 * @access public
242 * @return bool
243 */
244 function trans_rollback()
245 {
246 if ( ! $this->trans_enabled)
247 {
248 return TRUE;
249 }
250
251 // When transactions are nested we only begin/commit/rollback the outermost ones
252 if ($this->_trans_depth > 0)
253 {
254 return TRUE;
255 }
256
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000257 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Escape String
264 *
265 * @access public
266 * @param string
267 * @param bool whether or not the string will be used in a LIKE condition
268 * @return string
269 */
270 function escape_str($str, $like = FALSE)
271 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000272 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000273 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000274 }
275
276 // --------------------------------------------------------------------
277
278 /**
279 * Affected Rows
280 *
281 * @access public
282 * @return integer
283 */
284 function affected_rows()
285 {
Alex Bilbie56e20402011-03-12 23:43:54 +0000286 return @sqlrv_rows_affected($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Insert ID
293 *
294 * Returns the last id created in the Identity column.
295 *
296 * @access public
297 * @return integer
298 */
299 function insert_id()
300 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000301 return $this->query('select @@IDENTITY as insert_id')->row('insert_id');
Alex Bilbie84445d02011-03-10 16:43:39 +0000302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Parse major version
308 *
309 * Grabs the major version number from the
310 * database server version string passed in.
311 *
312 * @access private
313 * @param string $version
314 * @return int16 major version number
315 */
316 function _parse_major_version($version)
317 {
318 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
319 return $ver_info[1]; // return the major version b/c that's all we're interested in.
320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * Version number query string
326 *
327 * @access public
328 * @return string
329 */
330 function _version()
331 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000332 $info = sqlsrv_server_info($this->conn_id);
333 return sprintf("select '%s' as ver", $info['SQLServerVersion']);
Alex Bilbie84445d02011-03-10 16:43:39 +0000334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * "Count All" query
340 *
341 * Generates a platform-specific query string that counts all records in
342 * the specified database
343 *
344 * @access public
345 * @param string
346 * @return string
347 */
348 function count_all($table = '')
349 {
350 if ($table == '')
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000351 return '0';
352
353 $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table);
354
Alex Bilbie84445d02011-03-10 16:43:39 +0000355 if ($query->num_rows() == 0)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000356 return '0';
Alex Bilbie84445d02011-03-10 16:43:39 +0000357
358 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500359 $this->_reset_select();
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000360 return $row->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * List table query
367 *
368 * Generates a platform-specific query string so that the table names can be fetched
369 *
370 * @access private
371 * @param boolean
372 * @return string
373 */
374 function _list_tables($prefix_limit = FALSE)
375 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000376 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +0000377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * List column query
383 *
384 * Generates a platform-specific query string so that the column names can be fetched
385 *
386 * @access private
387 * @param string the table name
388 * @return string
389 */
390 function _list_columns($table = '')
391 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000392 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000393 }
394
395 // --------------------------------------------------------------------
396
397 /**
398 * Field data query
399 *
400 * Generates a platform-specific query so that the column data can be retrieved
401 *
402 * @access public
403 * @param string the table name
404 * @return object
405 */
406 function _field_data($table)
407 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000408 return "SELECT TOP 1 * FROM " . $this->_escape_table($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * The error message string
415 *
416 * @access private
417 * @return string
418 */
419 function _error_message()
420 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000421 $error = array_shift(sqlsrv_errors());
422 return !empty($error['message']) ? $error['message'] : null;
Alex Bilbie84445d02011-03-10 16:43:39 +0000423 }
424
425 // --------------------------------------------------------------------
426
427 /**
428 * The error message number
429 *
430 * @access private
431 * @return integer
432 */
433 function _error_number()
434 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000435 $error = array_shift(sqlsrv_errors());
436 return isset($error['SQLSTATE']) ? $error['SQLSTATE'] : null;
Alex Bilbie84445d02011-03-10 16:43:39 +0000437 }
438
439 // --------------------------------------------------------------------
440
441 /**
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000442 * Escape Table Name
443 *
444 * This function adds backticks if the table name has a period
445 * in it. Some DBs will get cranky unless periods are escaped
446 *
447 * @access private
448 * @param string the table name
449 * @return string
450 */
451 function _escape_table($table)
452 {
453 return $table;
454 }
455
456
457 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000458 * Escape the SQL Identifiers
459 *
460 * This function escapes column and table names
461 *
462 * @access private
463 * @param string
464 * @return string
465 */
466 function _escape_identifiers($item)
467 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000468 return $item;
Alex Bilbie84445d02011-03-10 16:43:39 +0000469 }
470
471 // --------------------------------------------------------------------
472
473 /**
474 * From Tables
475 *
476 * This function implicitly groups FROM tables so there is no confusion
477 * about operator precedence in harmony with SQL standards
478 *
479 * @access public
480 * @param type
481 * @return type
482 */
483 function _from_tables($tables)
484 {
485 if ( ! is_array($tables))
486 {
487 $tables = array($tables);
488 }
489
490 return implode(', ', $tables);
491 }
492
493 // --------------------------------------------------------------------
494
495 /**
496 * Insert statement
497 *
498 * Generates a platform-specific insert string from the supplied data
499 *
500 * @access public
501 * @param string the table name
502 * @param array the insert keys
503 * @param array the insert values
504 * @return string
505 */
506 function _insert($table, $keys, $values)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000507 {
508 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Alex Bilbie84445d02011-03-10 16:43:39 +0000509 }
510
511 // --------------------------------------------------------------------
512
513 /**
514 * Update statement
515 *
516 * Generates a platform-specific update string from the supplied data
517 *
518 * @access public
519 * @param string the table name
520 * @param array the update data
521 * @param array the where clause
522 * @param array the orderby clause
523 * @param array the limit clause
524 * @return string
525 */
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000526 function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000527 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000528 foreach($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000529 {
530 $valstr[] = $key." = ".$val;
531 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000532
533 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000534 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000535
Alex Bilbie84445d02011-03-10 16:43:39 +0000536 // --------------------------------------------------------------------
537
538 /**
539 * Truncate statement
540 *
541 * Generates a platform-specific truncate string from the supplied data
542 * If the database does not support the truncate() command
543 * This function maps to "DELETE FROM table"
544 *
545 * @access public
546 * @param string the table name
547 * @return string
548 */
549 function _truncate($table)
550 {
551 return "TRUNCATE ".$table;
552 }
553
554 // --------------------------------------------------------------------
555
556 /**
557 * Delete statement
558 *
559 * Generates a platform-specific delete string from the supplied data
560 *
561 * @access public
562 * @param string the table name
563 * @param array the where clause
564 * @param string the limit clause
565 * @return string
566 */
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000567 function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000568 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000569 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000570 }
571
572 // --------------------------------------------------------------------
573
574 /**
575 * Limit string
576 *
577 * Generates a platform-specific LIMIT clause
578 *
579 * @access public
580 * @param string the sql query string
581 * @param integer the number of rows to limit the query to
582 * @param integer the offset value
583 * @return string
584 */
585 function _limit($sql, $limit, $offset)
586 {
587 $i = $limit + $offset;
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000588
589 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000590 }
591
592 // --------------------------------------------------------------------
593
594 /**
595 * Close DB Connection
596 *
597 * @access public
598 * @param resource
599 * @return void
600 */
601 function _close($conn_id)
602 {
603 @sqlsrv_close($conn_id);
604 }
605
606}
607
608
609
610/* End of file mssql_driver.php */
Andrey Andreev11454e02012-02-22 16:05:47 +0200611/* Location: ./system/database/drivers/mssql/mssql_driver.php */