blob: ecd404b523f6f02bc5dbd0a421638d40ff119504 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
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 {
135 if ( ! $this->trans_enabled)
136 {
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 {
165 if ( ! $this->trans_enabled)
166 {
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 {
190 if ( ! $this->trans_enabled)
191 {
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 /*
411 if (stristr($table, '.'))
412 {
413 $table = preg_replace("/\./", "`.`", $table);
414 }
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 */
433 function _protect_identifiers($item, $affect_spaces = TRUE, $first_word_only = FALSE)
434 {
435 // MSSQL doesn't use backticks
Derek Allard9b3e7b52008-02-04 23:20:34 +0000436 if (strpos($item, '.') !== FALSE)
437 {
438 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
439 $table_name = substr($item, 0, strpos($item, '.')+1);
440 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
441 }
442
Derek Allard39b622d2008-01-16 21:10:09 +0000443 return $item;
444 }
445
446 // --------------------------------------------------------------------
447
448 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000449 * From Tables
450 *
451 * This function implicitly groups FROM tables so there is no confusion
452 * about operator precedence in harmony with SQL standards
453 *
454 * @access public
455 * @param type
456 * @return type
457 */
458 function _from_tables($tables)
459 {
460 if (! is_array($tables))
461 {
462 $tables = array($tables);
463 }
464
465 return implode(', ', $tables);
466 }
467
468 // --------------------------------------------------------------------
469
470 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000471 * Insert statement
472 *
473 * Generates a platform-specific insert string from the supplied data
474 *
475 * @access public
476 * @param string the table name
477 * @param array the insert keys
478 * @param array the insert values
479 * @return string
480 */
481 function _insert($table, $keys, $values)
482 {
483 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
484 }
485
486 // --------------------------------------------------------------------
487
488 /**
489 * Update statement
490 *
491 * Generates a platform-specific update string from the supplied data
492 *
493 * @access public
494 * @param string the table name
495 * @param array the update data
496 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000497 * @param array the orderby clause
498 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000499 * @return string
500 */
Derek Allard39b622d2008-01-16 21:10:09 +0000501 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000502 {
503 foreach($values as $key => $val)
504 {
505 $valstr[] = $key." = ".$val;
506 }
Derek Allardda6d2402007-12-19 14:49:29 +0000507
508 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000509
510 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000511
Derek Allard39b622d2008-01-16 21:10:09 +0000512 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
513 }
514
515
516 // --------------------------------------------------------------------
517
518 /**
519 * Truncate statement
520 *
521 * Generates a platform-specific truncate string from the supplied data
522 * If the database does not support the truncate() command
523 * This function maps to "DELETE FROM table"
524 *
525 * @access public
526 * @param string the table name
527 * @return string
528 */
529 function _truncate($table)
530 {
531 return "TRUNCATE ".$this->_escape_table($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000532 }
533
534 // --------------------------------------------------------------------
535
536 /**
537 * Delete statement
538 *
539 * Generates a platform-specific delete string from the supplied data
540 *
541 * @access public
542 * @param string the table name
543 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000544 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000545 * @return string
546 */
Derek Allard39b622d2008-01-16 21:10:09 +0000547 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000548 {
Derek Allard39b622d2008-01-16 21:10:09 +0000549 $conditions = '';
550
551 if (count($where) > 0 || count($like) > 0)
552 {
553 $conditions = "\nWHERE ";
554 $conditions .= implode("\n", $this->ar_where);
555
556 if (count($where) > 0 && count($like) > 0)
557 {
558 $conditions .= " AND ";
559 }
560 $conditions .= implode("\n", $like);
561 }
562
Derek Allarde77d77c2007-12-19 15:01:55 +0000563 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
564
Derek Allard39b622d2008-01-16 21:10:09 +0000565 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000566 }
567
568 // --------------------------------------------------------------------
569
570 /**
571 * Limit string
572 *
573 * Generates a platform-specific LIMIT clause
574 *
575 * @access public
576 * @param string the sql query string
577 * @param integer the number of rows to limit the query to
578 * @param integer the offset value
579 * @return string
580 */
581 function _limit($sql, $limit, $offset)
582 {
583 $i = $limit + $offset;
584
585 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
586 }
587
588 // --------------------------------------------------------------------
589
590 /**
591 * Close DB Connection
592 *
593 * @access public
594 * @param resource
595 * @return void
596 */
597 function _close($conn_id)
598 {
599 @mssql_close($conn_id);
600 }
601
602}
603
604
adminea8ca452006-09-24 18:11:44 +0000605?>