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