blob: 65397ed8fd374ef18d912674da2968855407c4d7 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * MS SQL Database Adapter Class
20 *
21 * Note: _DB is an extender class that the app controller
22 * creates dynamically based on whether the active record
23 * class is being used or not.
24 *
25 * @package CodeIgniter
26 * @subpackage Drivers
27 * @category Database
28 * @author ExpressionEngine Dev Team
29 * @link http://codeigniter.com/user_guide/database/
30 */
31class CI_DB_mssql_driver extends CI_DB {
32
33 var $dbdriver = 'mssql';
Barry Mienydd671972010-10-04 16:33:58 +020034
Derek Allard2067d1a2008-11-13 22:59:24 +000035 // The character used for escaping
36 var $_escape_char = '';
Derek Jonese4ed5832009-02-20 21:44:59 +000037
38 // clause and character used for LIKE escape sequences
39 var $_like_escape_str = " ESCAPE '%s' ";
40 var $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020041
Derek Allard2067d1a2008-11-13 22:59:24 +000042 /**
43 * The syntax to count rows is slightly different across different
44 * database engines, so this string appears in each driver and is
45 * used for the count_all() and count_all_results() functions.
46 */
47 var $_count_string = "SELECT COUNT(*) AS ";
48 var $_random_keyword = ' ASC'; // not currently supported
49
50 /**
51 * Non-persistent database connection
52 *
53 * @access private called by the base class
54 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020055 */
Derek Allard2067d1a2008-11-13 22:59:24 +000056 function db_connect()
57 {
58 if ($this->port != '')
59 {
60 $this->hostname .= ','.$this->port;
61 }
62
63 return @mssql_connect($this->hostname, $this->username, $this->password);
64 }
Barry Mienydd671972010-10-04 16:33:58 +020065
Derek Allard2067d1a2008-11-13 22:59:24 +000066 // --------------------------------------------------------------------
67
68 /**
69 * Persistent database connection
70 *
71 * @access private called by the base class
72 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020073 */
Derek Allard2067d1a2008-11-13 22:59:24 +000074 function db_pconnect()
75 {
76 if ($this->port != '')
77 {
78 $this->hostname .= ','.$this->port;
79 }
80
81 return @mssql_pconnect($this->hostname, $this->username, $this->password);
82 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Jones87cbafc2009-02-27 16:29:59 +000086 /**
87 * Reconnect
88 *
89 * Keep / reestablish the db connection if no queries have been
90 * sent for a length of time exceeding the server's idle timeout
91 *
92 * @access public
93 * @return void
94 */
95 function reconnect()
96 {
97 // not implemented in MSSQL
98 }
Derek Allard2067d1a2008-11-13 22:59:24 +000099
Derek Jones87cbafc2009-02-27 16:29:59 +0000100 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 /**
103 * Select the database
104 *
105 * @access private called by the base class
106 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200107 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 function db_select()
109 {
110 // Note: The brackets are required in the event that the DB name
111 // contains reserved characters
112 return @mssql_select_db('['.$this->database.']', $this->conn_id);
113 }
114
115 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 /**
118 * Set client character set
119 *
120 * @access public
121 * @param string
122 * @param string
123 * @return resource
124 */
125 function db_set_charset($charset, $collation)
126 {
127 // @todo - add support if needed
128 return TRUE;
129 }
130
131 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 /**
134 * Execute the query
135 *
136 * @access private called by the base class
137 * @param string an SQL query
138 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200139 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 function _execute($sql)
141 {
142 $sql = $this->_prep_query($sql);
143 return @mssql_query($sql, $this->conn_id);
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
147
148 /**
149 * Prep the query
150 *
151 * If needed, each database adapter can prep the query string
152 *
153 * @access private called by execute()
154 * @param string an SQL query
155 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200156 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 function _prep_query($sql)
158 {
159 return $sql;
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Begin Transaction
166 *
167 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200168 * @return bool
169 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 function trans_begin($test_mode = FALSE)
171 {
172 if ( ! $this->trans_enabled)
173 {
174 return TRUE;
175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 // When transactions are nested we only begin/commit/rollback the outermost ones
178 if ($this->_trans_depth > 0)
179 {
180 return TRUE;
181 }
182
183 // Reset the transaction failure flag.
184 // If the $test_mode flag is set to TRUE transactions will be rolled back
185 // even if the queries produce a successful result.
186 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
187
188 $this->simple_query('BEGIN TRAN');
189 return TRUE;
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Commit Transaction
196 *
197 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200198 * @return bool
199 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 function trans_commit()
201 {
202 if ( ! $this->trans_enabled)
203 {
204 return TRUE;
205 }
206
207 // When transactions are nested we only begin/commit/rollback the outermost ones
208 if ($this->_trans_depth > 0)
209 {
210 return TRUE;
211 }
212
213 $this->simple_query('COMMIT TRAN');
214 return TRUE;
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Rollback Transaction
221 *
222 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200223 * @return bool
224 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 function trans_rollback()
226 {
227 if ( ! $this->trans_enabled)
228 {
229 return TRUE;
230 }
231
232 // When transactions are nested we only begin/commit/rollback the outermost ones
233 if ($this->_trans_depth > 0)
234 {
235 return TRUE;
236 }
237
238 $this->simple_query('ROLLBACK TRAN');
239 return TRUE;
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 // --------------------------------------------------------------------
243
244 /**
245 * Escape String
246 *
247 * @access public
248 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000249 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 * @return string
251 */
Derek Jonese4ed5832009-02-20 21:44:59 +0000252 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000254 if (is_array($str))
255 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500256 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200257 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000258 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200259 }
260
261 return $str;
262 }
263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // Escape single quotes
Greg Aker757dda62010-04-14 19:06:19 -0500265 $str = str_replace("'", "''", remove_invisible_characters($str));
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Jonese4ed5832009-02-20 21:44:59 +0000267 // escape LIKE condition wildcards
268 if ($like === TRUE)
269 {
Phil Sturgeon36b0c942011-04-02 12:16:41 +0100270 $str = str_replace(
271 array($this->_like_escape_chr, '%', '_'),
272 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
273 $str
274 );
Derek Jonese4ed5832009-02-20 21:44:59 +0000275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Jonese4ed5832009-02-20 21:44:59 +0000277 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 // --------------------------------------------------------------------
281
282 /**
283 * Affected Rows
284 *
285 * @access public
286 * @return integer
287 */
288 function affected_rows()
289 {
290 return @mssql_rows_affected($this->conn_id);
291 }
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // --------------------------------------------------------------------
294
295 /**
296 * Insert ID
297 *
298 * Returns the last id created in the Identity column.
299 *
300 * @access public
301 * @return integer
302 */
303 function insert_id()
304 {
305 $ver = self::_parse_major_version($this->version());
306 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
307 $query = $this->query($sql);
308 $row = $query->row();
309 return $row->last_id;
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Parse major version
316 *
Barry Mienydd671972010-10-04 16:33:58 +0200317 * Grabs the major version number from the
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * database server version string passed in.
319 *
320 * @access private
321 * @param string $version
322 * @return int16 major version number
323 */
324 function _parse_major_version($version)
325 {
326 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
327 return $ver_info[1]; // return the major version b/c that's all we're interested in.
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * Version number query string
334 *
335 * @access public
336 * @return string
337 */
338 function _version()
339 {
340 return "SELECT @@VERSION AS ver";
341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * "Count All" query
347 *
348 * Generates a platform-specific query string that counts all records in
349 * the specified database
350 *
351 * @access public
352 * @param string
353 * @return string
354 */
355 function count_all($table = '')
356 {
357 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000358 {
359 return 0;
360 }
361
362 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000363
364 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000365 {
366 return 0;
367 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000368
369 $row = $query->row();
Derek Allarde37ab382009-02-03 16:13:57 +0000370 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * List table query
377 *
378 * Generates a platform-specific query string so that the table names can be fetched
379 *
380 * @access private
381 * @param boolean
382 * @return string
383 */
384 function _list_tables($prefix_limit = FALSE)
385 {
386 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 // for future compatibility
389 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
390 {
Greg Aker0d424892010-01-26 02:14:44 +0000391 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 return FALSE; // not currently supported
393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 return $sql;
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * List column query
402 *
403 * Generates a platform-specific query string so that the column names can be fetched
404 *
405 * @access private
406 * @param string the table name
407 * @return string
408 */
409 function _list_columns($table = '')
410 {
Barry Mienydd671972010-10-04 16:33:58 +0200411 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 }
413
414 // --------------------------------------------------------------------
415
416 /**
417 * Field data query
418 *
419 * Generates a platform-specific query so that the column data can be retrieved
420 *
421 * @access public
422 * @param string the table name
423 * @return object
424 */
425 function _field_data($table)
426 {
Barry Mienydd671972010-10-04 16:33:58 +0200427 return "SELECT TOP 1 * FROM ".$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * The error message string
434 *
435 * @access private
436 * @return string
437 */
438 function _error_message()
439 {
Derek Allard64c398b2009-08-17 15:13:45 +0000440 return mssql_get_last_message();
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 // --------------------------------------------------------------------
444
445 /**
446 * The error message number
447 *
448 * @access private
449 * @return integer
450 */
451 function _error_number()
452 {
453 // Are error numbers supported?
454 return '';
455 }
456
457 // --------------------------------------------------------------------
458
459 /**
460 * Escape the SQL Identifiers
461 *
462 * This function escapes column and table names
463 *
464 * @access private
465 * @param string
466 * @return string
467 */
468 function _escape_identifiers($item)
469 {
470 if ($this->_escape_char == '')
471 {
472 return $item;
473 }
474
475 foreach ($this->_reserved_identifiers as $id)
476 {
477 if (strpos($item, '.'.$id) !== FALSE)
478 {
Barry Mienydd671972010-10-04 16:33:58 +0200479 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 // remove duplicates if the user already included the escape
482 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200483 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485
486 if (strpos($item, '.') !== FALSE)
487 {
Barry Mienydd671972010-10-04 16:33:58 +0200488 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490 else
491 {
492 $str = $this->_escape_char.$item.$this->_escape_char;
493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 // remove duplicates if the user already included the escape
496 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 // --------------------------------------------------------------------
500
501 /**
502 * From Tables
503 *
504 * This function implicitly groups FROM tables so there is no confusion
505 * about operator precedence in harmony with SQL standards
506 *
507 * @access public
508 * @param type
509 * @return type
510 */
511 function _from_tables($tables)
512 {
513 if ( ! is_array($tables))
514 {
515 $tables = array($tables);
516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 return implode(', ', $tables);
519 }
520
521 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 /**
524 * Insert statement
525 *
526 * Generates a platform-specific insert string from the supplied data
527 *
528 * @access public
529 * @param string the table name
530 * @param array the insert keys
531 * @param array the insert values
532 * @return string
533 */
534 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200535 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 // --------------------------------------------------------------------
540
541 /**
542 * Update statement
543 *
544 * Generates a platform-specific update string from the supplied data
545 *
546 * @access public
547 * @param string the table name
548 * @param array the update data
549 * @param array the where clause
550 * @param array the orderby clause
551 * @param array the limit clause
552 * @return string
553 */
554 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
555 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500556 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
558 $valstr[] = $key." = ".$val;
559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
566
567 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
568
569 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 return $sql;
572 }
573
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 // --------------------------------------------------------------------
576
577 /**
578 * Truncate statement
579 *
580 * Generates a platform-specific truncate string from the supplied data
581 * If the database does not support the truncate() command
582 * This function maps to "DELETE FROM table"
583 *
584 * @access public
585 * @param string the table name
586 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200587 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 function _truncate($table)
589 {
590 return "TRUNCATE ".$table;
591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 // --------------------------------------------------------------------
594
595 /**
596 * Delete statement
597 *
598 * Generates a platform-specific delete string from the supplied data
599 *
600 * @access public
601 * @param string the table name
602 * @param array the where clause
603 * @param string the limit clause
604 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200605 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
607 {
608 $conditions = '';
609
610 if (count($where) > 0 OR count($like) > 0)
611 {
612 $conditions = "\nWHERE ";
613 $conditions .= implode("\n", $this->ar_where);
614
615 if (count($where) > 0 && count($like) > 0)
616 {
617 $conditions .= " AND ";
618 }
619 $conditions .= implode("\n", $like);
620 }
621
622 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 return "DELETE FROM ".$table.$conditions.$limit;
625 }
626
627 // --------------------------------------------------------------------
628
629 /**
630 * Limit string
631 *
632 * Generates a platform-specific LIMIT clause
633 *
634 * @access public
635 * @param string the sql query string
636 * @param integer the number of rows to limit the query to
637 * @param integer the offset value
638 * @return string
639 */
640 function _limit($sql, $limit, $offset)
641 {
642 $i = $limit + $offset;
Barry Mienydd671972010-10-04 16:33:58 +0200643
644 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 }
646
647 // --------------------------------------------------------------------
648
649 /**
650 * Close DB Connection
651 *
652 * @access public
653 * @param resource
654 * @return void
655 */
656 function _close($conn_id)
657 {
658 @mssql_close($conn_id);
Barry Mienydd671972010-10-04 16:33:58 +0200659 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000660
661}
662
663
664
665/* End of file mssql_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000666/* Location: ./system/database/drivers/mssql/mssql_driver.php */