blob: 03d7c7199bfb877f5f504765e6dda83e10744379 [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 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000147 * Execute the query
148 *
149 * @access private called by the base class
150 * @param string an SQL query
151 * @return resource
152 */
153 function _execute($sql)
154 {
155 $sql = $this->_prep_query($sql);
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000156 return sqlsrv_query($this->conn_id, $sql, null, array(
157 'Scrollable' => SQLSRV_CURSOR_STATIC,
158 'SendStreamParamsAtExec' => true
159 ));
Alex Bilbie84445d02011-03-10 16:43:39 +0000160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Prep the query
166 *
167 * If needed, each database adapter can prep the query string
168 *
169 * @access private called by execute()
170 * @param string an SQL query
171 * @return string
172 */
173 function _prep_query($sql)
174 {
175 return $sql;
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Begin Transaction
182 *
183 * @access public
184 * @return bool
185 */
186 function trans_begin($test_mode = FALSE)
187 {
188 if ( ! $this->trans_enabled)
189 {
190 return TRUE;
191 }
192
193 // When transactions are nested we only begin/commit/rollback the outermost ones
194 if ($this->_trans_depth > 0)
195 {
196 return TRUE;
197 }
198
199 // Reset the transaction failure flag.
200 // If the $test_mode flag is set to TRUE transactions will be rolled back
201 // even if the queries produce a successful result.
202 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
203
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000204 return sqlsrv_begin_transaction($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Commit Transaction
211 *
212 * @access public
213 * @return bool
214 */
215 function trans_commit()
216 {
217 if ( ! $this->trans_enabled)
218 {
219 return TRUE;
220 }
221
222 // When transactions are nested we only begin/commit/rollback the outermost ones
223 if ($this->_trans_depth > 0)
224 {
225 return TRUE;
226 }
227
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000228 return sqlsrv_commit($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Rollback Transaction
235 *
236 * @access public
237 * @return bool
238 */
239 function trans_rollback()
240 {
241 if ( ! $this->trans_enabled)
242 {
243 return TRUE;
244 }
245
246 // When transactions are nested we only begin/commit/rollback the outermost ones
247 if ($this->_trans_depth > 0)
248 {
249 return TRUE;
250 }
251
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000252 return sqlsrv_rollback($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Escape String
259 *
260 * @access public
261 * @param string
262 * @param bool whether or not the string will be used in a LIKE condition
263 * @return string
264 */
265 function escape_str($str, $like = FALSE)
266 {
Alex Bilbie84445d02011-03-10 16:43:39 +0000267 // Escape single quotes
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000268 return str_replace("'", "''", $str);
Alex Bilbie84445d02011-03-10 16:43:39 +0000269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Affected Rows
275 *
276 * @access public
277 * @return integer
278 */
279 function affected_rows()
280 {
Alex Bilbie56e20402011-03-12 23:43:54 +0000281 return @sqlrv_rows_affected($this->conn_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000282 }
283
284 // --------------------------------------------------------------------
285
286 /**
287 * Insert ID
288 *
289 * Returns the last id created in the Identity column.
290 *
291 * @access public
292 * @return integer
293 */
294 function insert_id()
295 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000296 return $this->query('select @@IDENTITY as insert_id')->row('insert_id');
Alex Bilbie84445d02011-03-10 16:43:39 +0000297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Parse major version
303 *
304 * Grabs the major version number from the
305 * database server version string passed in.
306 *
307 * @access private
308 * @param string $version
309 * @return int16 major version number
310 */
311 function _parse_major_version($version)
312 {
313 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
314 return $ver_info[1]; // return the major version b/c that's all we're interested in.
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Version number query string
321 *
322 * @access public
323 * @return string
324 */
325 function _version()
326 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000327 $info = sqlsrv_server_info($this->conn_id);
328 return sprintf("select '%s' as ver", $info['SQLServerVersion']);
Alex Bilbie84445d02011-03-10 16:43:39 +0000329 }
330
331 // --------------------------------------------------------------------
332
333 /**
334 * "Count All" query
335 *
336 * Generates a platform-specific query string that counts all records in
337 * the specified database
338 *
339 * @access public
340 * @param string
341 * @return string
342 */
343 function count_all($table = '')
344 {
345 if ($table == '')
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000346 return '0';
347
348 $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table);
349
Alex Bilbie84445d02011-03-10 16:43:39 +0000350 if ($query->num_rows() == 0)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000351 return '0';
Alex Bilbie84445d02011-03-10 16:43:39 +0000352
353 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500354 $this->_reset_select();
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000355 return $row->numrows;
Alex Bilbie84445d02011-03-10 16:43:39 +0000356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * List table query
362 *
363 * Generates a platform-specific query string so that the table names can be fetched
364 *
365 * @access private
366 * @param boolean
367 * @return string
368 */
369 function _list_tables($prefix_limit = FALSE)
370 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000371 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Alex Bilbie84445d02011-03-10 16:43:39 +0000372 }
373
374 // --------------------------------------------------------------------
375
376 /**
377 * List column query
378 *
379 * Generates a platform-specific query string so that the column names can be fetched
380 *
381 * @access private
382 * @param string the table name
383 * @return string
384 */
385 function _list_columns($table = '')
386 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000387 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
Alex Bilbie84445d02011-03-10 16:43:39 +0000388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Field data query
394 *
395 * Generates a platform-specific query so that the column data can be retrieved
396 *
397 * @access public
398 * @param string the table name
399 * @return object
400 */
401 function _field_data($table)
402 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000403 return "SELECT TOP 1 * FROM " . $this->_escape_table($table);
Alex Bilbie84445d02011-03-10 16:43:39 +0000404 }
405
406 // --------------------------------------------------------------------
407
408 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200409 * Error
Alex Bilbie84445d02011-03-10 16:43:39 +0000410 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200411 * Returns an array containing code and message of the last
412 * database error that has occured.
Alex Bilbie84445d02011-03-10 16:43:39 +0000413 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200414 * @return array
Alex Bilbie84445d02011-03-10 16:43:39 +0000415 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200416 public function error()
Alex Bilbie84445d02011-03-10 16:43:39 +0000417 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200418 $error = array('code' => '00000', 'message' => '');
419 $sqlsrv_errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
420
421 if ( ! is_array($sqlsrv_errors))
Andrey Andreev850f6012012-03-01 15:58:25 +0200422 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200423 return $error;
Andrey Andreev850f6012012-03-01 15:58:25 +0200424 }
425
Andrey Andreev4be5de12012-03-02 15:45:41 +0200426 $sqlsrv_error = array_shift($sqlsrv_errors);
427 if (isset($sqlsrv_error['SQLSTATE']))
428 {
429 $error['code'] = isset($sqlsrv_error['code']) ? $sqlsrv_error['SQLSTATE'].'/'.$sqlsrv_error['code'] : $sqlsrv_error['SQLSTATE'];
430 }
431 elseif (isset($sqlsrv_error['code']))
432 {
433 $error['code'] = $sqlsrv_error['code'];
434 }
435
436 if (isset($sqlsrv_error['message']))
437 {
438 $error['message'] = $sqlsrv_error['message'];
439 }
440
441 return $error;
Alex Bilbie84445d02011-03-10 16:43:39 +0000442 }
443
444 // --------------------------------------------------------------------
445
446 /**
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000447 * Escape Table Name
448 *
449 * This function adds backticks if the table name has a period
450 * in it. Some DBs will get cranky unless periods are escaped
451 *
452 * @access private
453 * @param string the table name
454 * @return string
455 */
456 function _escape_table($table)
457 {
458 return $table;
459 }
460
461
462 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000463 * Escape the SQL Identifiers
464 *
465 * This function escapes column and table names
466 *
467 * @access private
468 * @param string
469 * @return string
470 */
471 function _escape_identifiers($item)
472 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000473 return $item;
Alex Bilbie84445d02011-03-10 16:43:39 +0000474 }
475
476 // --------------------------------------------------------------------
477
478 /**
479 * From Tables
480 *
481 * This function implicitly groups FROM tables so there is no confusion
482 * about operator precedence in harmony with SQL standards
483 *
484 * @access public
485 * @param type
486 * @return type
487 */
488 function _from_tables($tables)
489 {
490 if ( ! is_array($tables))
491 {
492 $tables = array($tables);
493 }
494
495 return implode(', ', $tables);
496 }
497
498 // --------------------------------------------------------------------
499
500 /**
501 * Insert statement
502 *
503 * Generates a platform-specific insert string from the supplied data
504 *
505 * @access public
506 * @param string the table name
507 * @param array the insert keys
508 * @param array the insert values
509 * @return string
510 */
511 function _insert($table, $keys, $values)
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000512 {
513 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Alex Bilbie84445d02011-03-10 16:43:39 +0000514 }
515
516 // --------------------------------------------------------------------
517
518 /**
519 * Update statement
520 *
521 * Generates a platform-specific update string from the supplied data
522 *
523 * @access public
524 * @param string the table name
525 * @param array the update data
526 * @param array the where clause
527 * @param array the orderby clause
528 * @param array the limit clause
529 * @return string
530 */
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000531 function _update($table, $values, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000532 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000533 foreach($values as $key => $val)
Alex Bilbie84445d02011-03-10 16:43:39 +0000534 {
535 $valstr[] = $key." = ".$val;
536 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000537
538 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000539 }
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000540
Alex Bilbie84445d02011-03-10 16:43:39 +0000541 // --------------------------------------------------------------------
542
543 /**
544 * Truncate statement
545 *
546 * Generates a platform-specific truncate string from the supplied data
547 * If the database does not support the truncate() command
548 * This function maps to "DELETE FROM table"
549 *
550 * @access public
551 * @param string the table name
552 * @return string
553 */
554 function _truncate($table)
555 {
556 return "TRUNCATE ".$table;
557 }
558
559 // --------------------------------------------------------------------
560
561 /**
562 * Delete statement
563 *
564 * Generates a platform-specific delete string from the supplied data
565 *
566 * @access public
567 * @param string the table name
568 * @param array the where clause
569 * @param string the limit clause
570 * @return string
571 */
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000572 function _delete($table, $where)
Alex Bilbie84445d02011-03-10 16:43:39 +0000573 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000574 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
Alex Bilbie84445d02011-03-10 16:43:39 +0000575 }
576
577 // --------------------------------------------------------------------
578
579 /**
580 * Limit string
581 *
582 * Generates a platform-specific LIMIT clause
583 *
584 * @access public
585 * @param string the sql query string
586 * @param integer the number of rows to limit the query to
587 * @param integer the offset value
588 * @return string
589 */
590 function _limit($sql, $limit, $offset)
591 {
592 $i = $limit + $offset;
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000593
594 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
Alex Bilbie84445d02011-03-10 16:43:39 +0000595 }
596
597 // --------------------------------------------------------------------
598
599 /**
600 * Close DB Connection
601 *
602 * @access public
603 * @param resource
604 * @return void
605 */
606 function _close($conn_id)
607 {
608 @sqlsrv_close($conn_id);
609 }
610
611}
612
613
614
615/* End of file mssql_driver.php */
Andrey Andreev11454e02012-02-22 16:05:47 +0200616/* Location: ./system/database/drivers/mssql/mssql_driver.php */