blob: 06a508e718b833ad3df8a2920bb7562c719eb840 [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
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000011 * @link http://www.codeigniter.com
12 * @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
28 * @author Rick Ellis
29 * @link http://www.codeigniter.com/user_guide/database/
30 */
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 Allard6ddb5a12007-12-18 17:22:50 +0000300 $query = $this->query($this->_count_string . "FROM ".$this->dbprefix.$table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000301
302 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
436 return $item;
437 }
438
439 // --------------------------------------------------------------------
440
441 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000442 * Insert statement
443 *
444 * Generates a platform-specific insert string from the supplied data
445 *
446 * @access public
447 * @param string the table name
448 * @param array the insert keys
449 * @param array the insert values
450 * @return string
451 */
452 function _insert($table, $keys, $values)
453 {
454 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
455 }
456
457 // --------------------------------------------------------------------
458
459 /**
460 * Update statement
461 *
462 * Generates a platform-specific update string from the supplied data
463 *
464 * @access public
465 * @param string the table name
466 * @param array the update data
467 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000468 * @param array the orderby clause
469 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000470 * @return string
471 */
Derek Allard39b622d2008-01-16 21:10:09 +0000472 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000473 {
474 foreach($values as $key => $val)
475 {
476 $valstr[] = $key." = ".$val;
477 }
Derek Allardda6d2402007-12-19 14:49:29 +0000478
479 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000480
481 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000482
Derek Allard39b622d2008-01-16 21:10:09 +0000483 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
484 }
485
486
487 // --------------------------------------------------------------------
488
489 /**
490 * Truncate statement
491 *
492 * Generates a platform-specific truncate string from the supplied data
493 * If the database does not support the truncate() command
494 * This function maps to "DELETE FROM table"
495 *
496 * @access public
497 * @param string the table name
498 * @return string
499 */
500 function _truncate($table)
501 {
502 return "TRUNCATE ".$this->_escape_table($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000503 }
504
505 // --------------------------------------------------------------------
506
507 /**
508 * Delete statement
509 *
510 * Generates a platform-specific delete string from the supplied data
511 *
512 * @access public
513 * @param string the table name
514 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000515 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000516 * @return string
517 */
Derek Allard39b622d2008-01-16 21:10:09 +0000518 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000519 {
Derek Allard39b622d2008-01-16 21:10:09 +0000520 $conditions = '';
521
522 if (count($where) > 0 || count($like) > 0)
523 {
524 $conditions = "\nWHERE ";
525 $conditions .= implode("\n", $this->ar_where);
526
527 if (count($where) > 0 && count($like) > 0)
528 {
529 $conditions .= " AND ";
530 }
531 $conditions .= implode("\n", $like);
532 }
533
Derek Allarde77d77c2007-12-19 15:01:55 +0000534 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
535
Derek Allard39b622d2008-01-16 21:10:09 +0000536 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000537 }
538
539 // --------------------------------------------------------------------
540
541 /**
542 * Limit string
543 *
544 * Generates a platform-specific LIMIT clause
545 *
546 * @access public
547 * @param string the sql query string
548 * @param integer the number of rows to limit the query to
549 * @param integer the offset value
550 * @return string
551 */
552 function _limit($sql, $limit, $offset)
553 {
554 $i = $limit + $offset;
555
556 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
557 }
558
559 // --------------------------------------------------------------------
560
561 /**
562 * Close DB Connection
563 *
564 * @access public
565 * @param resource
566 * @return void
567 */
568 function _close($conn_id)
569 {
570 @mssql_close($conn_id);
571 }
572
573}
574
575
adminea8ca452006-09-24 18:11:44 +0000576?>