blob: 34ad9799e3fd309a96f377ce24507dd12969fc56 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000028 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000029 * @link http://codeigniter.com/user_guide/database/
Derek Allardd2df9bc2007-04-15 17:41:17 +000030 */
31class CI_DB_mssql_driver extends CI_DB {
32
33 /**
Derek Allard694b5b82007-12-18 15:58:03 +000034 * The syntax to count rows is slightly different across different
35 * database engines, so this string appears in each driver and is
36 * used for the count_all() and count_all_results() functions.
37 */
Derek Allard39b622d2008-01-16 21:10:09 +000038 var $_count_string = "SELECT COUNT(*) AS ";
39 var $_random_keyword = ' ASC'; // not currently supported
Derek Allard694b5b82007-12-18 15:58:03 +000040
41 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000042 * Non-persistent database connection
43 *
44 * @access private called by the base class
45 * @return resource
46 */
47 function db_connect()
48 {
49 return @mssql_connect($this->hostname, $this->username, $this->password);
50 }
51
52 // --------------------------------------------------------------------
53
54 /**
55 * Persistent database connection
56 *
57 * @access private called by the base class
58 * @return resource
59 */
60 function db_pconnect()
61 {
62 return @mssql_pconnect($this->hostname, $this->username, $this->password);
63 }
64
65 // --------------------------------------------------------------------
66
67 /**
68 * Select the database
69 *
70 * @access private called by the base class
71 * @return resource
72 */
73 function db_select()
74 {
75 return @mssql_select_db($this->database, $this->conn_id);
76 }
77
78 // --------------------------------------------------------------------
79
80 /**
Derek Allard39b622d2008-01-16 21:10:09 +000081 * Set client character set
82 *
83 * @access public
84 * @param string
85 * @param string
86 * @return resource
87 */
88 function db_set_charset($charset, $collation)
89 {
90 // TODO - add support if needed
91 return TRUE;
92 }
93
94 // --------------------------------------------------------------------
95
96 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000097 * Execute the query
98 *
99 * @access private called by the base class
100 * @param string an SQL query
101 * @return resource
102 */
103 function _execute($sql)
104 {
105 $sql = $this->_prep_query($sql);
106 return @mssql_query($sql, $this->conn_id);
107 }
108
109 // --------------------------------------------------------------------
110
111 /**
112 * Prep the query
113 *
114 * If needed, each database adapter can prep the query string
115 *
116 * @access private called by execute()
117 * @param string an SQL query
118 * @return string
119 */
120 function _prep_query($sql)
121 {
122 return $sql;
123 }
124
125 // --------------------------------------------------------------------
126
127 /**
128 * Begin Transaction
129 *
130 * @access public
131 * @return bool
132 */
133 function trans_begin($test_mode = FALSE)
134 {
Derek Jones0b59f272008-05-13 04:22:33 +0000135 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000136 {
137 return TRUE;
138 }
139
140 // When transactions are nested we only begin/commit/rollback the outermost ones
141 if ($this->_trans_depth > 0)
142 {
143 return TRUE;
144 }
145
146 // Reset the transaction failure flag.
147 // If the $test_mode flag is set to TRUE transactions will be rolled back
148 // even if the queries produce a successful result.
149 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
150
151 $this->simple_query('BEGIN TRAN');
152 return TRUE;
153 }
154
155 // --------------------------------------------------------------------
156
157 /**
158 * Commit Transaction
159 *
160 * @access public
161 * @return bool
162 */
163 function trans_commit()
164 {
Derek Jones0b59f272008-05-13 04:22:33 +0000165 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000166 {
167 return TRUE;
168 }
169
170 // When transactions are nested we only begin/commit/rollback the outermost ones
171 if ($this->_trans_depth > 0)
172 {
173 return TRUE;
174 }
175
176 $this->simple_query('COMMIT TRAN');
177 return TRUE;
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Rollback Transaction
184 *
185 * @access public
186 * @return bool
187 */
188 function trans_rollback()
189 {
Derek Jones0b59f272008-05-13 04:22:33 +0000190 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000191 {
192 return TRUE;
193 }
194
195 // When transactions are nested we only begin/commit/rollback the outermost ones
196 if ($this->_trans_depth > 0)
197 {
198 return TRUE;
199 }
200
201 $this->simple_query('ROLLBACK TRAN');
202 return TRUE;
203 }
204
205 // --------------------------------------------------------------------
206
207 /**
208 * Escape String
209 *
210 * @access public
211 * @param string
212 * @return string
213 */
214 function escape_str($str)
215 {
216 // Escape single quotes
217 return str_replace("'", "''", $str);
218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Affected Rows
224 *
225 * @access public
226 * @return integer
227 */
228 function affected_rows()
229 {
230 return @mssql_rows_affected($this->conn_id);
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
Rick Ellis1db17b52007-06-11 05:33:21 +0000236 * Insert ID
237 *
238 * Returns the last id created in the Identity column.
239 *
240 * @access public
241 * @return integer
242 */
Derek Allardd2df9bc2007-04-15 17:41:17 +0000243 function insert_id()
244 {
Rick Ellis1db17b52007-06-11 05:33:21 +0000245 $ver = self::_parse_major_version($this->version());
246 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
247 $query = $this->query($sql);
248 $row = $query->row();
249 return $row->last_id;
250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Parse major version
256 *
257 * Grabs the major version number from the
258 * database server version string passed in.
259 *
260 * @access private
261 * @param string $version
262 * @return int16 major version number
263 */
264 function _parse_major_version($version)
265 {
266 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
267 return $ver_info[1]; // return the major version b/c that's all we're interested in.
268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * Version number query string
274 *
275 * @access public
276 * @return string
277 */
278 function _version()
279 {
280 return "SELECT @@VERSION AS ver";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000281 }
282
283 // --------------------------------------------------------------------
284
285 /**
286 * "Count All" query
287 *
288 * Generates a platform-specific query string that counts all records in
289 * the specified database
290 *
291 * @access public
292 * @param string
293 * @return string
294 */
295 function count_all($table = '')
296 {
297 if ($table == '')
298 return '0';
299
Derek Allardf6cd45c2008-01-18 14:31:51 +0000300 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
301
Derek Allardd2df9bc2007-04-15 17:41:17 +0000302 if ($query->num_rows() == 0)
303 return '0';
304
305 $row = $query->row();
306 return $row->numrows;
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * List table query
313 *
314 * Generates a platform-specific query string so that the table names can be fetched
315 *
316 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000317 * @param boolean
Derek Allardd2df9bc2007-04-15 17:41:17 +0000318 * @return string
319 */
Derek Allard39b622d2008-01-16 21:10:09 +0000320 function _list_tables($prefix_limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000321 {
Derek Allard39b622d2008-01-16 21:10:09 +0000322 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
323
324 // for future compatibility
325 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
326 {
327 //$sql .= " LIKE '".$this->dbprefix."%'";
328 return FALSE; // not currently supported
329 }
330
331 return $sql;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * List column query
338 *
339 * Generates a platform-specific query string so that the column names can be fetched
340 *
341 * @access private
342 * @param string the table name
343 * @return string
344 */
345 function _list_columns($table = '')
346 {
347 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
348 }
349
350 // --------------------------------------------------------------------
351
352 /**
353 * Field data query
354 *
355 * Generates a platform-specific query so that the column data can be retrieved
356 *
357 * @access public
358 * @param string the table name
359 * @return object
360 */
361 function _field_data($table)
362 {
363 return "SELECT TOP 1 * FROM ".$this->_escape_table($table);
364 }
365
366 // --------------------------------------------------------------------
367
368 /**
369 * The error message string
370 *
371 * @access private
372 * @return string
373 */
374 function _error_message()
375 {
376 // Are errros even supported in MS SQL?
377 return '';
378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * The error message number
384 *
385 * @access private
386 * @return integer
387 */
388 function _error_number()
389 {
390 // Are error numbers supported?
391 return '';
392 }
393
394 // --------------------------------------------------------------------
395
396 /**
397 * Escape Table Name
398 *
399 * This function adds backticks if the table name has a period
400 * in it. Some DBs will get cranky unless periods are escaped
401 *
402 * @access private
403 * @param string the table name
404 * @return string
405 */
406 function _escape_table($table)
407 {
408 // I don't believe this is necessary with MS SQL. Not sure, though. - Rick
409
410 /*
Derek Allardc0743382008-02-11 05:54:44 +0000411 if (strpos($table, '.') !== FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000412 {
Derek Allardc0743382008-02-11 05:54:44 +0000413 $table = '"' . str_replace('.', '"."', $table) . '"';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000414 }
415 */
416
417 return $table;
418 }
419
420 // --------------------------------------------------------------------
421
422 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000423 * Protect Identifiers
424 *
425 * This function adds backticks if appropriate based on db type
426 *
427 * @access private
428 * @param mixed the item(s)
429 * @param boolean should spaces be backticked
430 * @param boolean only affect the first word
431 * @return mixed the item with backticks
432 */
Derek Allard15648132008-02-10 21:46:18 +0000433 function _protect_identifiers($item, $first_word_only = FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000434 {
Derek Allard15648132008-02-10 21:46:18 +0000435 if (is_array($item))
Derek Allard9b3e7b52008-02-04 23:20:34 +0000436 {
Derek Allard15648132008-02-10 21:46:18 +0000437 $escaped_array = array();
438
439 foreach($item as $k=>$v)
440 {
441 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
442 }
443
444 return $escaped_array;
445 }
446
447 // This function may get "item1 item2" as a string, and so
448 // we may need ""item1" "item2"" and not ""item1 item2""
449 if (ctype_alnum($item) === FALSE)
450 {
451 if (strpos($item, '.') !== FALSE)
452 {
453 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
454 $table_name = substr($item, 0, strpos($item, '.')+1);
455 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
456 }
457
458 // This function may get "field >= 1", and need it to return ""field" >= 1"
459 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
460
461 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1"$2"$3', $item);
462 }
463 else
464 {
465 return "\"{$item}\"";
Derek Allard9b3e7b52008-02-04 23:20:34 +0000466 }
467
Derek Allard9a4d1da2008-02-25 14:18:38 +0000468 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard15648132008-02-10 21:46:18 +0000469
470 foreach ($exceptions as $exception)
471 {
472
473 if (stristr($item, " \"{$exception}\" ") !== FALSE)
474 {
475 $item = preg_replace('/ "('.preg_quote($exception).')" /i', ' $1 ', $item);
476 }
477 }
Derek Allard39b622d2008-01-16 21:10:09 +0000478 return $item;
479 }
480
481 // --------------------------------------------------------------------
482
483 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000484 * From Tables
485 *
486 * This function implicitly groups FROM tables so there is no confusion
487 * about operator precedence in harmony with SQL standards
488 *
489 * @access public
490 * @param type
491 * @return type
492 */
493 function _from_tables($tables)
494 {
Derek Jones0b59f272008-05-13 04:22:33 +0000495 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000496 {
497 $tables = array($tables);
498 }
499
500 return implode(', ', $tables);
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000506 * Insert statement
507 *
508 * Generates a platform-specific insert string from the supplied data
509 *
510 * @access public
511 * @param string the table name
512 * @param array the insert keys
513 * @param array the insert values
514 * @return string
515 */
516 function _insert($table, $keys, $values)
517 {
518 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Update statement
525 *
526 * Generates a platform-specific update string from the supplied data
527 *
528 * @access public
529 * @param string the table name
530 * @param array the update data
531 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000532 * @param array the orderby clause
533 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000534 * @return string
535 */
Derek Allard39b622d2008-01-16 21:10:09 +0000536 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000537 {
538 foreach($values as $key => $val)
539 {
540 $valstr[] = $key." = ".$val;
541 }
Derek Allardda6d2402007-12-19 14:49:29 +0000542
Derek Jones0b59f272008-05-13 04:22:33 +0000543 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000544
545 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000546
Derek Allard32cf7eb2008-02-05 16:03:50 +0000547 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
548 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
549 $sql .= $orderby.$limit;
550
551 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000552 }
553
554
555 // --------------------------------------------------------------------
556
557 /**
558 * Truncate statement
559 *
560 * Generates a platform-specific truncate string from the supplied data
561 * If the database does not support the truncate() command
562 * This function maps to "DELETE FROM table"
563 *
564 * @access public
565 * @param string the table name
566 * @return string
567 */
568 function _truncate($table)
569 {
570 return "TRUNCATE ".$this->_escape_table($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000571 }
572
573 // --------------------------------------------------------------------
574
575 /**
576 * Delete statement
577 *
578 * Generates a platform-specific delete string from the supplied data
579 *
580 * @access public
581 * @param string the table name
582 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000583 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000584 * @return string
585 */
Derek Allard39b622d2008-01-16 21:10:09 +0000586 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000587 {
Derek Allard39b622d2008-01-16 21:10:09 +0000588 $conditions = '';
589
Derek Jones0b59f272008-05-13 04:22:33 +0000590 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000591 {
592 $conditions = "\nWHERE ";
593 $conditions .= implode("\n", $this->ar_where);
594
595 if (count($where) > 0 && count($like) > 0)
596 {
597 $conditions .= " AND ";
598 }
599 $conditions .= implode("\n", $like);
600 }
601
Derek Jones0b59f272008-05-13 04:22:33 +0000602 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000603
Derek Allard39b622d2008-01-16 21:10:09 +0000604 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000605 }
606
607 // --------------------------------------------------------------------
608
609 /**
610 * Limit string
611 *
612 * Generates a platform-specific LIMIT clause
613 *
614 * @access public
615 * @param string the sql query string
616 * @param integer the number of rows to limit the query to
617 * @param integer the offset value
618 * @return string
619 */
620 function _limit($sql, $limit, $offset)
621 {
622 $i = $limit + $offset;
623
624 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
625 }
626
627 // --------------------------------------------------------------------
628
629 /**
630 * Close DB Connection
631 *
632 * @access public
633 * @param resource
634 * @return void
635 */
636 function _close($conn_id)
637 {
638 @mssql_close($conn_id);
639 }
640
641}
642
643
Derek Jones0b59f272008-05-13 04:22:33 +0000644
645/* End of file mssql_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000646/* Location: ./system/database/drivers/mssql/mssql_driver.php */