blob: 6130bf4cb6d3f13a28fbadedd1428e4ba02c4072 [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 {
Rick Elliscba158c2008-10-17 04:18:04 +000079 // Note: The brackets are required in the event that the DB name
80 // contains reserved characters
81 return @mssql_select_db('['.$this->database.']', $this->conn_id);
Derek Allardd2df9bc2007-04-15 17:41:17 +000082 }
83
84 // --------------------------------------------------------------------
85
86 /**
Derek Allard39b622d2008-01-16 21:10:09 +000087 * Set client character set
88 *
89 * @access public
90 * @param string
91 * @param string
92 * @return resource
93 */
94 function db_set_charset($charset, $collation)
95 {
Rick Ellisff734012008-09-30 20:38:12 +000096 // @todo - add support if needed
Derek Allard39b622d2008-01-16 21:10:09 +000097 return TRUE;
98 }
99
100 // --------------------------------------------------------------------
101
102 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000103 * Execute the query
104 *
105 * @access private called by the base class
106 * @param string an SQL query
107 * @return resource
108 */
109 function _execute($sql)
110 {
111 $sql = $this->_prep_query($sql);
112 return @mssql_query($sql, $this->conn_id);
113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Prep the query
119 *
120 * If needed, each database adapter can prep the query string
121 *
122 * @access private called by execute()
123 * @param string an SQL query
124 * @return string
125 */
126 function _prep_query($sql)
127 {
128 return $sql;
129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Begin Transaction
135 *
136 * @access public
137 * @return bool
138 */
139 function trans_begin($test_mode = FALSE)
140 {
Derek Jones0b59f272008-05-13 04:22:33 +0000141 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000142 {
143 return TRUE;
144 }
145
146 // When transactions are nested we only begin/commit/rollback the outermost ones
147 if ($this->_trans_depth > 0)
148 {
149 return TRUE;
150 }
151
152 // Reset the transaction failure flag.
153 // If the $test_mode flag is set to TRUE transactions will be rolled back
154 // even if the queries produce a successful result.
155 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
156
157 $this->simple_query('BEGIN TRAN');
158 return TRUE;
159 }
160
161 // --------------------------------------------------------------------
162
163 /**
164 * Commit Transaction
165 *
166 * @access public
167 * @return bool
168 */
169 function trans_commit()
170 {
Derek Jones0b59f272008-05-13 04:22:33 +0000171 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000172 {
173 return TRUE;
174 }
175
176 // When transactions are nested we only begin/commit/rollback the outermost ones
177 if ($this->_trans_depth > 0)
178 {
179 return TRUE;
180 }
181
182 $this->simple_query('COMMIT TRAN');
183 return TRUE;
184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Rollback Transaction
190 *
191 * @access public
192 * @return bool
193 */
194 function trans_rollback()
195 {
Derek Jones0b59f272008-05-13 04:22:33 +0000196 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000197 {
198 return TRUE;
199 }
200
201 // When transactions are nested we only begin/commit/rollback the outermost ones
202 if ($this->_trans_depth > 0)
203 {
204 return TRUE;
205 }
206
207 $this->simple_query('ROLLBACK TRAN');
208 return TRUE;
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Escape String
215 *
216 * @access public
217 * @param string
218 * @return string
219 */
220 function escape_str($str)
Rick Ellis06a2e742008-10-07 01:04:15 +0000221 {
222 // Access the CI object
Rick Ellisca86a7c2008-10-07 01:16:57 +0000223 $CI =& get_instance();
Rick Ellis06a2e742008-10-07 01:04:15 +0000224
Derek Allardd2df9bc2007-04-15 17:41:17 +0000225 // Escape single quotes
Rick Ellis06a2e742008-10-07 01:04:15 +0000226 return str_replace("'", "''", $CI->input->_remove_invisible_characters($str));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Affected Rows
233 *
234 * @access public
235 * @return integer
236 */
237 function affected_rows()
238 {
239 return @mssql_rows_affected($this->conn_id);
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
Rick Ellis1db17b52007-06-11 05:33:21 +0000245 * Insert ID
246 *
247 * Returns the last id created in the Identity column.
248 *
249 * @access public
250 * @return integer
251 */
Derek Allardd2df9bc2007-04-15 17:41:17 +0000252 function insert_id()
253 {
Rick Ellis1db17b52007-06-11 05:33:21 +0000254 $ver = self::_parse_major_version($this->version());
255 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
256 $query = $this->query($sql);
257 $row = $query->row();
258 return $row->last_id;
259 }
260
261 // --------------------------------------------------------------------
262
263 /**
264 * Parse major version
265 *
266 * Grabs the major version number from the
267 * database server version string passed in.
268 *
269 * @access private
270 * @param string $version
271 * @return int16 major version number
272 */
273 function _parse_major_version($version)
274 {
275 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
276 return $ver_info[1]; // return the major version b/c that's all we're interested in.
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Version number query string
283 *
284 * @access public
285 * @return string
286 */
287 function _version()
288 {
289 return "SELECT @@VERSION AS ver";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * "Count All" query
296 *
297 * Generates a platform-specific query string that counts all records in
298 * the specified database
299 *
300 * @access public
301 * @param string
302 * @return string
303 */
304 function count_all($table = '')
305 {
306 if ($table == '')
307 return '0';
308
Rick Ellis59523592008-10-17 04:07:40 +0000309 $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 +0000310
Derek Allardd2df9bc2007-04-15 17:41:17 +0000311 if ($query->num_rows() == 0)
312 return '0';
313
314 $row = $query->row();
315 return $row->numrows;
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * List table query
322 *
323 * Generates a platform-specific query string so that the table names can be fetched
324 *
325 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000326 * @param boolean
Derek Allardd2df9bc2007-04-15 17:41:17 +0000327 * @return string
328 */
Derek Allard39b622d2008-01-16 21:10:09 +0000329 function _list_tables($prefix_limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000330 {
Derek Allard39b622d2008-01-16 21:10:09 +0000331 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
332
333 // for future compatibility
334 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
335 {
336 //$sql .= " LIKE '".$this->dbprefix."%'";
337 return FALSE; // not currently supported
338 }
339
340 return $sql;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * List column query
347 *
348 * Generates a platform-specific query string so that the column names can be fetched
349 *
350 * @access private
351 * @param string the table name
352 * @return string
353 */
354 function _list_columns($table = '')
355 {
Rick Ellis59523592008-10-17 04:07:40 +0000356 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000357 }
358
359 // --------------------------------------------------------------------
360
361 /**
362 * Field data query
363 *
364 * Generates a platform-specific query so that the column data can be retrieved
365 *
366 * @access public
367 * @param string the table name
368 * @return object
369 */
370 function _field_data($table)
371 {
Rick Ellis59523592008-10-17 04:07:40 +0000372 return "SELECT TOP 1 * FROM ".$table;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000373 }
374
375 // --------------------------------------------------------------------
376
377 /**
378 * The error message string
379 *
380 * @access private
381 * @return string
382 */
383 function _error_message()
384 {
385 // Are errros even supported in MS SQL?
386 return '';
387 }
388
389 // --------------------------------------------------------------------
390
391 /**
392 * The error message number
393 *
394 * @access private
395 * @return integer
396 */
397 function _error_number()
398 {
399 // Are error numbers supported?
400 return '';
401 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000402
403 // --------------------------------------------------------------------
404
405 /**
Rick Ellis59523592008-10-17 04:07:40 +0000406 * Escape the SQL Identifiers
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000407 *
Rick Ellis59523592008-10-17 04:07:40 +0000408 * This function escapes column and table names
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000409 *
410 * @access private
Rick Ellis59523592008-10-17 04:07:40 +0000411 * @param string
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000412 * @return string
413 */
Rick Ellis59523592008-10-17 04:07:40 +0000414 function _escape_identifiers($item)
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000415 {
Rick Ellis59523592008-10-17 04:07:40 +0000416 if ($this->_escape_char == '')
Derek Allard15648132008-02-10 21:46:18 +0000417 {
Rick Ellis218839a2008-10-07 01:15:48 +0000418 return $item;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000419 }
Rick Ellis59523592008-10-17 04:07:40 +0000420
421 if (strpos($item, '.') !== FALSE)
Derek Allard15648132008-02-10 21:46:18 +0000422 {
Rick Ellis59523592008-10-17 04:07:40 +0000423 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard15648132008-02-10 21:46:18 +0000424 }
Rick Ellis59523592008-10-17 04:07:40 +0000425 else
426 {
427 $str = $this->_escape_char.$item.$this->_escape_char;
428 }
429
430 // remove duplicates if the user already included the escape
431 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Derek Allard39b622d2008-01-16 21:10:09 +0000432 }
Rick Ellis59523592008-10-17 04:07:40 +0000433
Derek Allard39b622d2008-01-16 21:10:09 +0000434 // --------------------------------------------------------------------
435
436 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000437 * From Tables
438 *
439 * This function implicitly groups FROM tables so there is no confusion
440 * about operator precedence in harmony with SQL standards
441 *
442 * @access public
443 * @param type
444 * @return type
445 */
446 function _from_tables($tables)
447 {
Derek Jones0b59f272008-05-13 04:22:33 +0000448 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000449 {
450 $tables = array($tables);
451 }
452
453 return implode(', ', $tables);
454 }
455
456 // --------------------------------------------------------------------
457
458 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000459 * Insert statement
460 *
461 * Generates a platform-specific insert string from the supplied data
462 *
463 * @access public
464 * @param string the table name
465 * @param array the insert keys
466 * @param array the insert values
467 * @return string
468 */
469 function _insert($table, $keys, $values)
470 {
Rick Ellis59523592008-10-17 04:07:40 +0000471 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000472 }
473
474 // --------------------------------------------------------------------
475
476 /**
477 * Update statement
478 *
479 * Generates a platform-specific update string from the supplied data
480 *
481 * @access public
482 * @param string the table name
483 * @param array the update data
484 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000485 * @param array the orderby clause
486 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000487 * @return string
488 */
Derek Allard39b622d2008-01-16 21:10:09 +0000489 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000490 {
491 foreach($values as $key => $val)
492 {
493 $valstr[] = $key." = ".$val;
494 }
Derek Allardda6d2402007-12-19 14:49:29 +0000495
Derek Jones0b59f272008-05-13 04:22:33 +0000496 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000497
498 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000499
Rick Ellis59523592008-10-17 04:07:40 +0000500 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
501
Derek Allard32cf7eb2008-02-05 16:03:50 +0000502 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
Rick Ellis59523592008-10-17 04:07:40 +0000503
Derek Allard32cf7eb2008-02-05 16:03:50 +0000504 $sql .= $orderby.$limit;
505
506 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000507 }
508
509
510 // --------------------------------------------------------------------
511
512 /**
513 * Truncate statement
514 *
515 * Generates a platform-specific truncate string from the supplied data
516 * If the database does not support the truncate() command
517 * This function maps to "DELETE FROM table"
518 *
519 * @access public
520 * @param string the table name
521 * @return string
522 */
523 function _truncate($table)
524 {
Rick Ellis59523592008-10-17 04:07:40 +0000525 return "TRUNCATE ".$table;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000526 }
527
528 // --------------------------------------------------------------------
529
530 /**
531 * Delete statement
532 *
533 * Generates a platform-specific delete string from the supplied data
534 *
535 * @access public
536 * @param string the table name
537 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000538 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000539 * @return string
540 */
Derek Allard39b622d2008-01-16 21:10:09 +0000541 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000542 {
Derek Allard39b622d2008-01-16 21:10:09 +0000543 $conditions = '';
544
Derek Jones0b59f272008-05-13 04:22:33 +0000545 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000546 {
547 $conditions = "\nWHERE ";
548 $conditions .= implode("\n", $this->ar_where);
549
550 if (count($where) > 0 && count($like) > 0)
551 {
552 $conditions .= " AND ";
553 }
554 $conditions .= implode("\n", $like);
555 }
556
Derek Jones0b59f272008-05-13 04:22:33 +0000557 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000558
Derek Allard39b622d2008-01-16 21:10:09 +0000559 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000560 }
561
562 // --------------------------------------------------------------------
563
564 /**
565 * Limit string
566 *
567 * Generates a platform-specific LIMIT clause
568 *
569 * @access public
570 * @param string the sql query string
571 * @param integer the number of rows to limit the query to
572 * @param integer the offset value
573 * @return string
574 */
575 function _limit($sql, $limit, $offset)
576 {
577 $i = $limit + $offset;
578
579 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
580 }
581
582 // --------------------------------------------------------------------
583
584 /**
585 * Close DB Connection
586 *
587 * @access public
588 * @param resource
589 * @return void
590 */
591 function _close($conn_id)
592 {
593 @mssql_close($conn_id);
594 }
595
596}
597
598
Derek Jones0b59f272008-05-13 04:22:33 +0000599
600/* End of file mssql_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000601/* Location: ./system/database/drivers/mssql/mssql_driver.php */