blob: 89610455bb0b90963a02a6b8e3e0c3052b1c6ac3 [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 Allard6ddb5a12007-12-18 17:22:50 +000038 var $_count_string = "SELECT COUNT(*) AS numrows ";
Derek Allard694b5b82007-12-18 15:58:03 +000039
40 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000041 * Non-persistent database connection
42 *
43 * @access private called by the base class
44 * @return resource
45 */
46 function db_connect()
47 {
48 return @mssql_connect($this->hostname, $this->username, $this->password);
49 }
50
51 // --------------------------------------------------------------------
52
53 /**
54 * Persistent database connection
55 *
56 * @access private called by the base class
57 * @return resource
58 */
59 function db_pconnect()
60 {
61 return @mssql_pconnect($this->hostname, $this->username, $this->password);
62 }
63
64 // --------------------------------------------------------------------
65
66 /**
67 * Select the database
68 *
69 * @access private called by the base class
70 * @return resource
71 */
72 function db_select()
73 {
74 return @mssql_select_db($this->database, $this->conn_id);
75 }
76
77 // --------------------------------------------------------------------
78
79 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000080 * Execute the query
81 *
82 * @access private called by the base class
83 * @param string an SQL query
84 * @return resource
85 */
86 function _execute($sql)
87 {
88 $sql = $this->_prep_query($sql);
89 return @mssql_query($sql, $this->conn_id);
90 }
91
92 // --------------------------------------------------------------------
93
94 /**
95 * Prep the query
96 *
97 * If needed, each database adapter can prep the query string
98 *
99 * @access private called by execute()
100 * @param string an SQL query
101 * @return string
102 */
103 function _prep_query($sql)
104 {
105 return $sql;
106 }
107
108 // --------------------------------------------------------------------
109
110 /**
111 * Begin Transaction
112 *
113 * @access public
114 * @return bool
115 */
116 function trans_begin($test_mode = FALSE)
117 {
118 if ( ! $this->trans_enabled)
119 {
120 return TRUE;
121 }
122
123 // When transactions are nested we only begin/commit/rollback the outermost ones
124 if ($this->_trans_depth > 0)
125 {
126 return TRUE;
127 }
128
129 // Reset the transaction failure flag.
130 // If the $test_mode flag is set to TRUE transactions will be rolled back
131 // even if the queries produce a successful result.
132 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
133
134 $this->simple_query('BEGIN TRAN');
135 return TRUE;
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Commit Transaction
142 *
143 * @access public
144 * @return bool
145 */
146 function trans_commit()
147 {
148 if ( ! $this->trans_enabled)
149 {
150 return TRUE;
151 }
152
153 // When transactions are nested we only begin/commit/rollback the outermost ones
154 if ($this->_trans_depth > 0)
155 {
156 return TRUE;
157 }
158
159 $this->simple_query('COMMIT TRAN');
160 return TRUE;
161 }
162
163 // --------------------------------------------------------------------
164
165 /**
166 * Rollback Transaction
167 *
168 * @access public
169 * @return bool
170 */
171 function trans_rollback()
172 {
173 if ( ! $this->trans_enabled)
174 {
175 return TRUE;
176 }
177
178 // When transactions are nested we only begin/commit/rollback the outermost ones
179 if ($this->_trans_depth > 0)
180 {
181 return TRUE;
182 }
183
184 $this->simple_query('ROLLBACK TRAN');
185 return TRUE;
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Escape String
192 *
193 * @access public
194 * @param string
195 * @return string
196 */
197 function escape_str($str)
198 {
199 // Escape single quotes
200 return str_replace("'", "''", $str);
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Affected Rows
207 *
208 * @access public
209 * @return integer
210 */
211 function affected_rows()
212 {
213 return @mssql_rows_affected($this->conn_id);
214 }
215
216 // --------------------------------------------------------------------
217
218 /**
Rick Ellis1db17b52007-06-11 05:33:21 +0000219 * Insert ID
220 *
221 * Returns the last id created in the Identity column.
222 *
223 * @access public
224 * @return integer
225 */
Derek Allardd2df9bc2007-04-15 17:41:17 +0000226 function insert_id()
227 {
Rick Ellis1db17b52007-06-11 05:33:21 +0000228 $ver = self::_parse_major_version($this->version());
229 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
230 $query = $this->query($sql);
231 $row = $query->row();
232 return $row->last_id;
233 }
234
235 // --------------------------------------------------------------------
236
237 /**
238 * Parse major version
239 *
240 * Grabs the major version number from the
241 * database server version string passed in.
242 *
243 * @access private
244 * @param string $version
245 * @return int16 major version number
246 */
247 function _parse_major_version($version)
248 {
249 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
250 return $ver_info[1]; // return the major version b/c that's all we're interested in.
251 }
252
253 // --------------------------------------------------------------------
254
255 /**
256 * Version number query string
257 *
258 * @access public
259 * @return string
260 */
261 function _version()
262 {
263 return "SELECT @@VERSION AS ver";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000264 }
265
266 // --------------------------------------------------------------------
267
268 /**
269 * "Count All" query
270 *
271 * Generates a platform-specific query string that counts all records in
272 * the specified database
273 *
274 * @access public
275 * @param string
276 * @return string
277 */
278 function count_all($table = '')
279 {
280 if ($table == '')
281 return '0';
282
Derek Allard6ddb5a12007-12-18 17:22:50 +0000283 $query = $this->query($this->_count_string . "FROM ".$this->dbprefix.$table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000284
285 if ($query->num_rows() == 0)
286 return '0';
287
288 $row = $query->row();
289 return $row->numrows;
290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * List table query
296 *
297 * Generates a platform-specific query string so that the table names can be fetched
298 *
299 * @access private
300 * @return string
301 */
302 function _list_tables()
303 {
304 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
305 }
306
307 // --------------------------------------------------------------------
308
309 /**
310 * List column query
311 *
312 * Generates a platform-specific query string so that the column names can be fetched
313 *
314 * @access private
315 * @param string the table name
316 * @return string
317 */
318 function _list_columns($table = '')
319 {
320 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * Field data query
327 *
328 * Generates a platform-specific query so that the column data can be retrieved
329 *
330 * @access public
331 * @param string the table name
332 * @return object
333 */
334 function _field_data($table)
335 {
336 return "SELECT TOP 1 * FROM ".$this->_escape_table($table);
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * The error message string
343 *
344 * @access private
345 * @return string
346 */
347 function _error_message()
348 {
349 // Are errros even supported in MS SQL?
350 return '';
351 }
352
353 // --------------------------------------------------------------------
354
355 /**
356 * The error message number
357 *
358 * @access private
359 * @return integer
360 */
361 function _error_number()
362 {
363 // Are error numbers supported?
364 return '';
365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * Escape Table Name
371 *
372 * This function adds backticks if the table name has a period
373 * in it. Some DBs will get cranky unless periods are escaped
374 *
375 * @access private
376 * @param string the table name
377 * @return string
378 */
379 function _escape_table($table)
380 {
381 // I don't believe this is necessary with MS SQL. Not sure, though. - Rick
382
383 /*
384 if (stristr($table, '.'))
385 {
386 $table = preg_replace("/\./", "`.`", $table);
387 }
388 */
389
390 return $table;
391 }
392
393 // --------------------------------------------------------------------
394
395 /**
396 * Insert statement
397 *
398 * Generates a platform-specific insert string from the supplied data
399 *
400 * @access public
401 * @param string the table name
402 * @param array the insert keys
403 * @param array the insert values
404 * @return string
405 */
406 function _insert($table, $keys, $values)
407 {
408 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Update statement
415 *
416 * Generates a platform-specific update string from the supplied data
417 *
418 * @access public
419 * @param string the table name
420 * @param array the update data
421 * @param array the where clause
422 * @return string
423 */
424 function _update($table, $values, $where)
425 {
426 foreach($values as $key => $val)
427 {
428 $valstr[] = $key." = ".$val;
429 }
430
431 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
432 }
433
434 // --------------------------------------------------------------------
435
436 /**
437 * Delete statement
438 *
439 * Generates a platform-specific delete string from the supplied data
440 *
441 * @access public
442 * @param string the table name
443 * @param array the where clause
444 * @return string
445 */
446 function _delete($table, $where)
447 {
448 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
449 }
450
451 // --------------------------------------------------------------------
452
453 /**
454 * Limit string
455 *
456 * Generates a platform-specific LIMIT clause
457 *
458 * @access public
459 * @param string the sql query string
460 * @param integer the number of rows to limit the query to
461 * @param integer the offset value
462 * @return string
463 */
464 function _limit($sql, $limit, $offset)
465 {
466 $i = $limit + $offset;
467
468 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
469 }
470
471 // --------------------------------------------------------------------
472
473 /**
474 * Close DB Connection
475 *
476 * @access public
477 * @param resource
478 * @return void
479 */
480 function _close($conn_id)
481 {
482 @mssql_close($conn_id);
483 }
484
485}
486
487
adminea8ca452006-09-24 18:11:44 +0000488?>