blob: fd3f9638a9c7ffcce4e8ac1db45057aefffabe7b [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
Rick Ellis37b3ecf2008-09-12 23:34:18 +00009 * @copyright Copyright (c) 2008, 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 {
Rick Ellisff734012008-09-30 20:38:12 +000090 // @todo - add support if needed
Derek Allard39b622d2008-01-16 21:10:09 +000091 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
Derek Jonesd16bab12008-09-24 18:22:03 +0000217 return str_replace("'", "''", $this->input->_remove_invisible_characters($str));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000218 }
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 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000393
394 // --------------------------------------------------------------------
395
396 /**
397 * Escape Column Name
398 *
399 * This function adds backticks around supplied column name
400 *
401 * @access private
402 * @param string the column name
403 * @return string
404 */
405 function _escape_column($column)
406 {
407 // Not necessary with MS SQL so we simply return the value
408 return $column;
409 }
410
Derek Allardd2df9bc2007-04-15 17:41:17 +0000411 // --------------------------------------------------------------------
412
413 /**
414 * Escape Table Name
415 *
416 * This function adds backticks if the table name has a period
417 * in it. Some DBs will get cranky unless periods are escaped
418 *
419 * @access private
420 * @param string the table name
421 * @return string
422 */
423 function _escape_table($table)
424 {
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000425 // Not necessary with MS SQL so we simply return the value
Derek Allardd2df9bc2007-04-15 17:41:17 +0000426 return $table;
427 }
428
429 // --------------------------------------------------------------------
430
431 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000432 * Protect Identifiers
433 *
434 * This function adds backticks if appropriate based on db type
435 *
436 * @access private
437 * @param mixed the item(s)
438 * @param boolean should spaces be backticked
439 * @param boolean only affect the first word
440 * @return mixed the item with backticks
441 */
Derek Allard15648132008-02-10 21:46:18 +0000442 function _protect_identifiers($item, $first_word_only = FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000443 {
Derek Allard15648132008-02-10 21:46:18 +0000444 if (is_array($item))
Derek Allard9b3e7b52008-02-04 23:20:34 +0000445 {
Derek Allard15648132008-02-10 21:46:18 +0000446 $escaped_array = array();
447
448 foreach($item as $k=>$v)
449 {
450 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
451 }
452
453 return $escaped_array;
454 }
455
456 // This function may get "item1 item2" as a string, and so
457 // we may need ""item1" "item2"" and not ""item1 item2""
458 if (ctype_alnum($item) === FALSE)
459 {
460 if (strpos($item, '.') !== FALSE)
461 {
462 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
463 $table_name = substr($item, 0, strpos($item, '.')+1);
464 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
465 }
466
467 // This function may get "field >= 1", and need it to return ""field" >= 1"
468 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
469
470 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1"$2"$3', $item);
471 }
472 else
473 {
474 return "\"{$item}\"";
Derek Allard9b3e7b52008-02-04 23:20:34 +0000475 }
476
Derek Allard9a4d1da2008-02-25 14:18:38 +0000477 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard15648132008-02-10 21:46:18 +0000478
479 foreach ($exceptions as $exception)
480 {
481
482 if (stristr($item, " \"{$exception}\" ") !== FALSE)
483 {
484 $item = preg_replace('/ "('.preg_quote($exception).')" /i', ' $1 ', $item);
485 }
486 }
Derek Allard39b622d2008-01-16 21:10:09 +0000487 return $item;
488 }
489
490 // --------------------------------------------------------------------
491
492 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000493 * From Tables
494 *
495 * This function implicitly groups FROM tables so there is no confusion
496 * about operator precedence in harmony with SQL standards
497 *
498 * @access public
499 * @param type
500 * @return type
501 */
502 function _from_tables($tables)
503 {
Derek Jones0b59f272008-05-13 04:22:33 +0000504 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000505 {
506 $tables = array($tables);
507 }
508
509 return implode(', ', $tables);
510 }
511
512 // --------------------------------------------------------------------
513
514 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000515 * Insert statement
516 *
517 * Generates a platform-specific insert string from the supplied data
518 *
519 * @access public
520 * @param string the table name
521 * @param array the insert keys
522 * @param array the insert values
523 * @return string
524 */
525 function _insert($table, $keys, $values)
526 {
527 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
528 }
529
530 // --------------------------------------------------------------------
531
532 /**
533 * Update statement
534 *
535 * Generates a platform-specific update string from the supplied data
536 *
537 * @access public
538 * @param string the table name
539 * @param array the update data
540 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000541 * @param array the orderby clause
542 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000543 * @return string
544 */
Derek Allard39b622d2008-01-16 21:10:09 +0000545 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000546 {
547 foreach($values as $key => $val)
548 {
549 $valstr[] = $key." = ".$val;
550 }
Derek Allardda6d2402007-12-19 14:49:29 +0000551
Derek Jones0b59f272008-05-13 04:22:33 +0000552 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000553
554 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000555
Derek Allard32cf7eb2008-02-05 16:03:50 +0000556 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
557 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
558 $sql .= $orderby.$limit;
559
560 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000561 }
562
563
564 // --------------------------------------------------------------------
565
566 /**
567 * Truncate statement
568 *
569 * Generates a platform-specific truncate string from the supplied data
570 * If the database does not support the truncate() command
571 * This function maps to "DELETE FROM table"
572 *
573 * @access public
574 * @param string the table name
575 * @return string
576 */
577 function _truncate($table)
578 {
579 return "TRUNCATE ".$this->_escape_table($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000580 }
581
582 // --------------------------------------------------------------------
583
584 /**
585 * Delete statement
586 *
587 * Generates a platform-specific delete string from the supplied data
588 *
589 * @access public
590 * @param string the table name
591 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000592 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000593 * @return string
594 */
Derek Allard39b622d2008-01-16 21:10:09 +0000595 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000596 {
Derek Allard39b622d2008-01-16 21:10:09 +0000597 $conditions = '';
598
Derek Jones0b59f272008-05-13 04:22:33 +0000599 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000600 {
601 $conditions = "\nWHERE ";
602 $conditions .= implode("\n", $this->ar_where);
603
604 if (count($where) > 0 && count($like) > 0)
605 {
606 $conditions .= " AND ";
607 }
608 $conditions .= implode("\n", $like);
609 }
610
Derek Jones0b59f272008-05-13 04:22:33 +0000611 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000612
Derek Allard39b622d2008-01-16 21:10:09 +0000613 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000614 }
615
616 // --------------------------------------------------------------------
617
618 /**
619 * Limit string
620 *
621 * Generates a platform-specific LIMIT clause
622 *
623 * @access public
624 * @param string the sql query string
625 * @param integer the number of rows to limit the query to
626 * @param integer the offset value
627 * @return string
628 */
629 function _limit($sql, $limit, $offset)
630 {
631 $i = $limit + $offset;
632
633 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
634 }
635
636 // --------------------------------------------------------------------
637
638 /**
639 * Close DB Connection
640 *
641 * @access public
642 * @param resource
643 * @return void
644 */
645 function _close($conn_id)
646 {
647 @mssql_close($conn_id);
648 }
649
650}
651
652
Derek Jones0b59f272008-05-13 04:22:33 +0000653
654/* End of file mssql_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000655/* Location: ./system/database/drivers/mssql/mssql_driver.php */