blob: fbc0701c084fbd8f52201b58e42b66659f08b3da [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
Rick Ellis59523592008-10-17 04:07:40 +000033 var $dbdriver = 'mssql';
34
35 // The character used for escaping
36 var $_escape_char = '';
Derek Allardd2df9bc2007-04-15 17:41:17 +000037 /**
Derek Allard694b5b82007-12-18 15:58:03 +000038 * The syntax to count rows is slightly different across different
39 * database engines, so this string appears in each driver and is
40 * used for the count_all() and count_all_results() functions.
41 */
Derek Allard39b622d2008-01-16 21:10:09 +000042 var $_count_string = "SELECT COUNT(*) AS ";
43 var $_random_keyword = ' ASC'; // not currently supported
Rick Ellis5aa8c602008-10-07 01:24:07 +000044
Derek Allard694b5b82007-12-18 15:58:03 +000045 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000046 * Non-persistent database connection
47 *
48 * @access private called by the base class
49 * @return resource
50 */
51 function db_connect()
52 {
53 return @mssql_connect($this->hostname, $this->username, $this->password);
54 }
55
56 // --------------------------------------------------------------------
57
58 /**
59 * Persistent database connection
60 *
61 * @access private called by the base class
62 * @return resource
63 */
64 function db_pconnect()
65 {
66 return @mssql_pconnect($this->hostname, $this->username, $this->password);
67 }
68
69 // --------------------------------------------------------------------
70
71 /**
72 * Select the database
73 *
74 * @access private called by the base class
75 * @return resource
76 */
77 function db_select()
78 {
79 return @mssql_select_db($this->database, $this->conn_id);
80 }
81
82 // --------------------------------------------------------------------
83
84 /**
Derek Allard39b622d2008-01-16 21:10:09 +000085 * Set client character set
86 *
87 * @access public
88 * @param string
89 * @param string
90 * @return resource
91 */
92 function db_set_charset($charset, $collation)
93 {
Rick Ellisff734012008-09-30 20:38:12 +000094 // @todo - add support if needed
Derek Allard39b622d2008-01-16 21:10:09 +000095 return TRUE;
96 }
97
98 // --------------------------------------------------------------------
99
100 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000101 * Execute the query
102 *
103 * @access private called by the base class
104 * @param string an SQL query
105 * @return resource
106 */
107 function _execute($sql)
108 {
109 $sql = $this->_prep_query($sql);
110 return @mssql_query($sql, $this->conn_id);
111 }
112
113 // --------------------------------------------------------------------
114
115 /**
116 * Prep the query
117 *
118 * If needed, each database adapter can prep the query string
119 *
120 * @access private called by execute()
121 * @param string an SQL query
122 * @return string
123 */
124 function _prep_query($sql)
125 {
126 return $sql;
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Begin Transaction
133 *
134 * @access public
135 * @return bool
136 */
137 function trans_begin($test_mode = FALSE)
138 {
Derek Jones0b59f272008-05-13 04:22:33 +0000139 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000140 {
141 return TRUE;
142 }
143
144 // When transactions are nested we only begin/commit/rollback the outermost ones
145 if ($this->_trans_depth > 0)
146 {
147 return TRUE;
148 }
149
150 // Reset the transaction failure flag.
151 // If the $test_mode flag is set to TRUE transactions will be rolled back
152 // even if the queries produce a successful result.
153 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
154
155 $this->simple_query('BEGIN TRAN');
156 return TRUE;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Commit Transaction
163 *
164 * @access public
165 * @return bool
166 */
167 function trans_commit()
168 {
Derek Jones0b59f272008-05-13 04:22:33 +0000169 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000170 {
171 return TRUE;
172 }
173
174 // When transactions are nested we only begin/commit/rollback the outermost ones
175 if ($this->_trans_depth > 0)
176 {
177 return TRUE;
178 }
179
180 $this->simple_query('COMMIT TRAN');
181 return TRUE;
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Rollback Transaction
188 *
189 * @access public
190 * @return bool
191 */
192 function trans_rollback()
193 {
Derek Jones0b59f272008-05-13 04:22:33 +0000194 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000195 {
196 return TRUE;
197 }
198
199 // When transactions are nested we only begin/commit/rollback the outermost ones
200 if ($this->_trans_depth > 0)
201 {
202 return TRUE;
203 }
204
205 $this->simple_query('ROLLBACK TRAN');
206 return TRUE;
207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Escape String
213 *
214 * @access public
215 * @param string
216 * @return string
217 */
218 function escape_str($str)
Rick Ellis06a2e742008-10-07 01:04:15 +0000219 {
220 // Access the CI object
Rick Ellisca86a7c2008-10-07 01:16:57 +0000221 $CI =& get_instance();
Rick Ellis06a2e742008-10-07 01:04:15 +0000222
Derek Allardd2df9bc2007-04-15 17:41:17 +0000223 // Escape single quotes
Rick Ellis06a2e742008-10-07 01:04:15 +0000224 return str_replace("'", "''", $CI->input->_remove_invisible_characters($str));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * Affected Rows
231 *
232 * @access public
233 * @return integer
234 */
235 function affected_rows()
236 {
237 return @mssql_rows_affected($this->conn_id);
238 }
239
240 // --------------------------------------------------------------------
241
242 /**
Rick Ellis1db17b52007-06-11 05:33:21 +0000243 * Insert ID
244 *
245 * Returns the last id created in the Identity column.
246 *
247 * @access public
248 * @return integer
249 */
Derek Allardd2df9bc2007-04-15 17:41:17 +0000250 function insert_id()
251 {
Rick Ellis1db17b52007-06-11 05:33:21 +0000252 $ver = self::_parse_major_version($this->version());
253 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
254 $query = $this->query($sql);
255 $row = $query->row();
256 return $row->last_id;
257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Parse major version
263 *
264 * Grabs the major version number from the
265 * database server version string passed in.
266 *
267 * @access private
268 * @param string $version
269 * @return int16 major version number
270 */
271 function _parse_major_version($version)
272 {
273 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
274 return $ver_info[1]; // return the major version b/c that's all we're interested in.
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * Version number query string
281 *
282 * @access public
283 * @return string
284 */
285 function _version()
286 {
287 return "SELECT @@VERSION AS ver";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * "Count All" query
294 *
295 * Generates a platform-specific query string that counts all records in
296 * the specified database
297 *
298 * @access public
299 * @param string
300 * @return string
301 */
302 function count_all($table = '')
303 {
304 if ($table == '')
305 return '0';
306
Rick Ellis59523592008-10-17 04:07:40 +0000307 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allardf6cd45c2008-01-18 14:31:51 +0000308
Derek Allardd2df9bc2007-04-15 17:41:17 +0000309 if ($query->num_rows() == 0)
310 return '0';
311
312 $row = $query->row();
313 return $row->numrows;
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * List table query
320 *
321 * Generates a platform-specific query string so that the table names can be fetched
322 *
323 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000324 * @param boolean
Derek Allardd2df9bc2007-04-15 17:41:17 +0000325 * @return string
326 */
Derek Allard39b622d2008-01-16 21:10:09 +0000327 function _list_tables($prefix_limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000328 {
Derek Allard39b622d2008-01-16 21:10:09 +0000329 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
330
331 // for future compatibility
332 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
333 {
334 //$sql .= " LIKE '".$this->dbprefix."%'";
335 return FALSE; // not currently supported
336 }
337
338 return $sql;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000339 }
340
341 // --------------------------------------------------------------------
342
343 /**
344 * List column query
345 *
346 * Generates a platform-specific query string so that the column names can be fetched
347 *
348 * @access private
349 * @param string the table name
350 * @return string
351 */
352 function _list_columns($table = '')
353 {
Rick Ellis59523592008-10-17 04:07:40 +0000354 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * Field data query
361 *
362 * Generates a platform-specific query so that the column data can be retrieved
363 *
364 * @access public
365 * @param string the table name
366 * @return object
367 */
368 function _field_data($table)
369 {
Rick Ellis59523592008-10-17 04:07:40 +0000370 return "SELECT TOP 1 * FROM ".$table;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * The error message string
377 *
378 * @access private
379 * @return string
380 */
381 function _error_message()
382 {
383 // Are errros even supported in MS SQL?
384 return '';
385 }
386
387 // --------------------------------------------------------------------
388
389 /**
390 * The error message number
391 *
392 * @access private
393 * @return integer
394 */
395 function _error_number()
396 {
397 // Are error numbers supported?
398 return '';
399 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000400
401 // --------------------------------------------------------------------
402
403 /**
Rick Ellis59523592008-10-17 04:07:40 +0000404 * Escape the SQL Identifiers
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000405 *
Rick Ellis59523592008-10-17 04:07:40 +0000406 * This function escapes column and table names
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000407 *
408 * @access private
Rick Ellis59523592008-10-17 04:07:40 +0000409 * @param string
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000410 * @return string
411 */
Rick Ellis59523592008-10-17 04:07:40 +0000412 function _escape_identifiers($item)
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000413 {
Rick Ellis59523592008-10-17 04:07:40 +0000414 if ($this->_escape_char == '')
Derek Allard15648132008-02-10 21:46:18 +0000415 {
Rick Ellis218839a2008-10-07 01:15:48 +0000416 return $item;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000417 }
Rick Ellis59523592008-10-17 04:07:40 +0000418
419 if (strpos($item, '.') !== FALSE)
Derek Allard15648132008-02-10 21:46:18 +0000420 {
Rick Ellis59523592008-10-17 04:07:40 +0000421 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard15648132008-02-10 21:46:18 +0000422 }
Rick Ellis59523592008-10-17 04:07:40 +0000423 else
424 {
425 $str = $this->_escape_char.$item.$this->_escape_char;
426 }
427
428 // remove duplicates if the user already included the escape
429 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Derek Allard39b622d2008-01-16 21:10:09 +0000430 }
Rick Ellis59523592008-10-17 04:07:40 +0000431
Derek Allard39b622d2008-01-16 21:10:09 +0000432 // --------------------------------------------------------------------
433
434 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000435 * From Tables
436 *
437 * This function implicitly groups FROM tables so there is no confusion
438 * about operator precedence in harmony with SQL standards
439 *
440 * @access public
441 * @param type
442 * @return type
443 */
444 function _from_tables($tables)
445 {
Derek Jones0b59f272008-05-13 04:22:33 +0000446 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000447 {
448 $tables = array($tables);
449 }
450
451 return implode(', ', $tables);
452 }
453
454 // --------------------------------------------------------------------
455
456 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000457 * Insert statement
458 *
459 * Generates a platform-specific insert string from the supplied data
460 *
461 * @access public
462 * @param string the table name
463 * @param array the insert keys
464 * @param array the insert values
465 * @return string
466 */
467 function _insert($table, $keys, $values)
468 {
Rick Ellis59523592008-10-17 04:07:40 +0000469 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * Update statement
476 *
477 * Generates a platform-specific update string from the supplied data
478 *
479 * @access public
480 * @param string the table name
481 * @param array the update data
482 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000483 * @param array the orderby clause
484 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000485 * @return string
486 */
Derek Allard39b622d2008-01-16 21:10:09 +0000487 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000488 {
489 foreach($values as $key => $val)
490 {
491 $valstr[] = $key." = ".$val;
492 }
Derek Allardda6d2402007-12-19 14:49:29 +0000493
Derek Jones0b59f272008-05-13 04:22:33 +0000494 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000495
496 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000497
Rick Ellis59523592008-10-17 04:07:40 +0000498 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
499
Derek Allard32cf7eb2008-02-05 16:03:50 +0000500 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
Rick Ellis59523592008-10-17 04:07:40 +0000501
Derek Allard32cf7eb2008-02-05 16:03:50 +0000502 $sql .= $orderby.$limit;
503
504 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000505 }
506
507
508 // --------------------------------------------------------------------
509
510 /**
511 * Truncate statement
512 *
513 * Generates a platform-specific truncate string from the supplied data
514 * If the database does not support the truncate() command
515 * This function maps to "DELETE FROM table"
516 *
517 * @access public
518 * @param string the table name
519 * @return string
520 */
521 function _truncate($table)
522 {
Rick Ellis59523592008-10-17 04:07:40 +0000523 return "TRUNCATE ".$table;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000524 }
525
526 // --------------------------------------------------------------------
527
528 /**
529 * Delete statement
530 *
531 * Generates a platform-specific delete string from the supplied data
532 *
533 * @access public
534 * @param string the table name
535 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000536 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000537 * @return string
538 */
Derek Allard39b622d2008-01-16 21:10:09 +0000539 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000540 {
Derek Allard39b622d2008-01-16 21:10:09 +0000541 $conditions = '';
542
Derek Jones0b59f272008-05-13 04:22:33 +0000543 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000544 {
545 $conditions = "\nWHERE ";
546 $conditions .= implode("\n", $this->ar_where);
547
548 if (count($where) > 0 && count($like) > 0)
549 {
550 $conditions .= " AND ";
551 }
552 $conditions .= implode("\n", $like);
553 }
554
Derek Jones0b59f272008-05-13 04:22:33 +0000555 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000556
Derek Allard39b622d2008-01-16 21:10:09 +0000557 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000558 }
559
560 // --------------------------------------------------------------------
561
562 /**
563 * Limit string
564 *
565 * Generates a platform-specific LIMIT clause
566 *
567 * @access public
568 * @param string the sql query string
569 * @param integer the number of rows to limit the query to
570 * @param integer the offset value
571 * @return string
572 */
573 function _limit($sql, $limit, $offset)
574 {
575 $i = $limit + $offset;
576
577 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
578 }
579
580 // --------------------------------------------------------------------
581
582 /**
583 * Close DB Connection
584 *
585 * @access public
586 * @param resource
587 * @return void
588 */
589 function _close($conn_id)
590 {
591 @mssql_close($conn_id);
592 }
593
594}
595
596
Derek Jones0b59f272008-05-13 04:22:33 +0000597
598/* End of file mssql_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000599/* Location: ./system/database/drivers/mssql/mssql_driver.php */