blob: 9c50209ec54fad41916c32b59b8857bfe8c6730f [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 Andreev024ba2d2012-02-24 11:40:36 +0200130 if ($database === '')
131 {
132 $database = $this->database;
133 }
134
135 if ($this->_execute('USE '.$database))
136 {
137 $this->database = $database;
138 return TRUE;
139 }
140
141 return FALSE;
Alex Bilbie84445d02011-03-10 16:43:39 +0000142 }
143
144 // --------------------------------------------------------------------
145
146 /**
147 * Set client character set
148 *
149 * @access public
150 * @param string
151 * @param string
152 * @return resource
153 */
154 function db_set_charset($charset, $collation)
155 {
156 // @todo - add support if needed
157 return TRUE;
158 }
159
160 // --------------------------------------------------------------------
161
162 /**
163 * Execute the query
164 *
165 * @access private called by the base class
166 * @param string an SQL query
167 * @return resource
168 */
169 function _execute($sql)
170 {
171 $sql = $this->_prep_query($sql);
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000172 return sqlsrv_query($this->conn_id, $sql, null, array(
173 'Scrollable' => SQLSRV_CURSOR_STATIC,
174 'SendStreamParamsAtExec' => true
175 ));
Alex Bilbie84445d02011-03-10 16:43:39 +0000176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Prep the query
182 *
183 * If needed, each database adapter can prep the query string
184 *
185 * @access private called by execute()
186 * @param string an SQL query
187 * @return string
188 */
189 function _prep_query($sql)
190 {
191 return $sql;
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Begin Transaction
198 *
199 * @access public
200 * @return bool
201 */
202 function trans_begin($test_mode = FALSE)
203 {
204 if ( ! $this->trans_enabled)
205 {
206 return TRUE;
207 }
208
209 // When transactions are nested we only begin/commit/rollback the outermost ones
210 if ($this->_trans_depth > 0)
211 {
212 return TRUE;
213 }
214
215 // Reset the transaction failure flag.
216 // If the $test_mode flag is set to TRUE transactions will be rolled back
217 // even if the queries produce a successful result.
218 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
219
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000220 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Commit Transaction
227 *
228 * @access public
229 * @return bool
230 */
231 function trans_commit()
232 {
233 if ( ! $this->trans_enabled)
234 {
235 return TRUE;
236 }
237
238 // When transactions are nested we only begin/commit/rollback the outermost ones
239 if ($this->_trans_depth > 0)
240 {
241 return TRUE;
242 }
243
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000244 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * Rollback Transaction
251 *
252 * @access public
253 * @return bool
254 */
255 function trans_rollback()
256 {
257 if ( ! $this->trans_enabled)
258 {
259 return TRUE;
260 }
261
262 // When transactions are nested we only begin/commit/rollback the outermost ones
263 if ($this->_trans_depth > 0)
264 {
265 return TRUE;
266 }
267
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000268 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Escape String
275 *
276 * @access public
277 * @param string
278 * @param bool whether or not the string will be used in a LIKE condition
279 * @return string
280 */
281 function escape_str($str, $like = FALSE)
282 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000283 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000284 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * Affected Rows
291 *
292 * @access public
293 * @return integer
294 */
295 function affected_rows()
296 {
Alex Bilbie56e20402011-03-12 23:43:54 +0000297 return @sqlrv_rows_affected($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Insert ID
304 *
305 * Returns the last id created in the Identity column.
306 *
307 * @access public
308 * @return integer
309 */
310 function insert_id()
311 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000312 return $this->query('select @@IDENTITY as insert_id')->row('insert_id');
Alex Bilbie84445d02011-03-10 16:43:39 +0000313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Parse major version
319 *
320 * Grabs the major version number from the
321 * database server version string passed in.
322 *
323 * @access private
324 * @param string $version
325 * @return int16 major version number
326 */
327 function _parse_major_version($version)
328 {
329 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
330 return $ver_info[1]; // return the major version b/c that's all we're interested in.
331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * Version number query string
337 *
338 * @access public
339 * @return string
340 */
341 function _version()
342 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000343 $info = sqlsrv_server_info($this->conn_id);
344 return sprintf("select '%s' as ver", $info['SQLServerVersion']);
Alex Bilbie84445d02011-03-10 16:43:39 +0000345 }
346
347 // --------------------------------------------------------------------
348
349 /**
350 * "Count All" query
351 *
352 * Generates a platform-specific query string that counts all records in
353 * the specified database
354 *
355 * @access public
356 * @param string
357 * @return string
358 */
359 function count_all($table = '')
360 {
361 if ($table == '')
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000362 return '0';
363
364 $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table);
365
Alex Bilbie84445d02011-03-10 16:43:39 +0000366 if ($query->num_rows() == 0)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000367 return '0';
Alex Bilbie84445d02011-03-10 16:43:39 +0000368
369 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500370 $this->_reset_select();
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000371 return $row->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000372 }
373
374 // --------------------------------------------------------------------
375
376 /**
377 * List table query
378 *
379 * Generates a platform-specific query string so that the table names can be fetched
380 *
381 * @access private
382 * @param boolean
383 * @return string
384 */
385 function _list_tables($prefix_limit = FALSE)
386 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000387 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +0000388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * List column query
394 *
395 * Generates a platform-specific query string so that the column names can be fetched
396 *
397 * @access private
398 * @param string the table name
399 * @return string
400 */
401 function _list_columns($table = '')
402 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000403 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * Field data query
410 *
411 * Generates a platform-specific query so that the column data can be retrieved
412 *
413 * @access public
414 * @param string the table name
415 * @return object
416 */
417 function _field_data($table)
418 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000419 return "SELECT TOP 1 * FROM " . $this->_escape_table($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * The error message string
426 *
427 * @access private
428 * @return string
429 */
430 function _error_message()
431 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000432 $error = array_shift(sqlsrv_errors());
433 return !empty($error['message']) ? $error['message'] : null;
Alex Bilbie84445d02011-03-10 16:43:39 +0000434 }
435
436 // --------------------------------------------------------------------
437
438 /**
439 * The error message number
440 *
441 * @access private
442 * @return integer
443 */
444 function _error_number()
445 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000446 $error = array_shift(sqlsrv_errors());
447 return isset($error['SQLSTATE']) ? $error['SQLSTATE'] : null;
Alex Bilbie84445d02011-03-10 16:43:39 +0000448 }
449
450 // --------------------------------------------------------------------
451
452 /**
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000453 * Escape Table Name
454 *
455 * This function adds backticks if the table name has a period
456 * in it. Some DBs will get cranky unless periods are escaped
457 *
458 * @access private
459 * @param string the table name
460 * @return string
461 */
462 function _escape_table($table)
463 {
464 return $table;
465 }
466
467
468 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000469 * Escape the SQL Identifiers
470 *
471 * This function escapes column and table names
472 *
473 * @access private
474 * @param string
475 * @return string
476 */
477 function _escape_identifiers($item)
478 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000479 return $item;
Alex Bilbie84445d02011-03-10 16:43:39 +0000480 }
481
482 // --------------------------------------------------------------------
483
484 /**
485 * From Tables
486 *
487 * This function implicitly groups FROM tables so there is no confusion
488 * about operator precedence in harmony with SQL standards
489 *
490 * @access public
491 * @param type
492 * @return type
493 */
494 function _from_tables($tables)
495 {
496 if ( ! is_array($tables))
497 {
498 $tables = array($tables);
499 }
500
501 return implode(', ', $tables);
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Insert statement
508 *
509 * Generates a platform-specific insert string from the supplied data
510 *
511 * @access public
512 * @param string the table name
513 * @param array the insert keys
514 * @param array the insert values
515 * @return string
516 */
517 function _insert($table, $keys, $values)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000518 {
519 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Alex Bilbie84445d02011-03-10 16:43:39 +0000520 }
521
522 // --------------------------------------------------------------------
523
524 /**
525 * Update statement
526 *
527 * Generates a platform-specific update string from the supplied data
528 *
529 * @access public
530 * @param string the table name
531 * @param array the update data
532 * @param array the where clause
533 * @param array the orderby clause
534 * @param array the limit clause
535 * @return string
536 */
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000537 function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000538 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000539 foreach($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000540 {
541 $valstr[] = $key." = ".$val;
542 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000543
544 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000545 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000546
Alex Bilbie84445d02011-03-10 16:43:39 +0000547 // --------------------------------------------------------------------
548
549 /**
550 * Truncate statement
551 *
552 * Generates a platform-specific truncate string from the supplied data
553 * If the database does not support the truncate() command
554 * This function maps to "DELETE FROM table"
555 *
556 * @access public
557 * @param string the table name
558 * @return string
559 */
560 function _truncate($table)
561 {
562 return "TRUNCATE ".$table;
563 }
564
565 // --------------------------------------------------------------------
566
567 /**
568 * Delete statement
569 *
570 * Generates a platform-specific delete string from the supplied data
571 *
572 * @access public
573 * @param string the table name
574 * @param array the where clause
575 * @param string the limit clause
576 * @return string
577 */
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000578 function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000579 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000580 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000581 }
582
583 // --------------------------------------------------------------------
584
585 /**
586 * Limit string
587 *
588 * Generates a platform-specific LIMIT clause
589 *
590 * @access public
591 * @param string the sql query string
592 * @param integer the number of rows to limit the query to
593 * @param integer the offset value
594 * @return string
595 */
596 function _limit($sql, $limit, $offset)
597 {
598 $i = $limit + $offset;
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000599
600 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000601 }
602
603 // --------------------------------------------------------------------
604
605 /**
606 * Close DB Connection
607 *
608 * @access public
609 * @param resource
610 * @return void
611 */
612 function _close($conn_id)
613 {
614 @sqlsrv_close($conn_id);
615 }
616
617}
618
619
620
621/* End of file mssql_driver.php */
Andrey Andreev11454e02012-02-22 16:05:47 +0200622/* Location: ./system/database/drivers/mssql/mssql_driver.php */