blob: 47cfa6bbee90aa993ba7d8be3aff48c4473b70ea [file] [log] [blame]
adminea8ca452006-09-24 18:11:44 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
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, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminea8ca452006-09-24 18:11:44 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminea8ca452006-09-24 18:11:44 +000016// ------------------------------------------------------------------------
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 {
adminca335fc2006-10-23 21:18:36 +000041 return @mssql_connect($this->hostname, $this->username, $this->password);
adminea8ca452006-09-24 18:11:44 +000042 }
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 {
adminca335fc2006-10-23 21:18:36 +000054 return @mssql_pconnect($this->hostname, $this->username, $this->password);
adminea8ca452006-09-24 18:11:44 +000055 }
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 }
admin9cd4e8e2006-09-25 23:26:25 +000069
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 }
adminea8ca452006-09-24 18:11:44 +000082
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 */
admine334c472006-10-21 19:44:22 +0000109 function _prep_query($sql)
110 {
adminea8ca452006-09-24 18:11:44 +0000111 return $sql;
admine334c472006-10-21 19:44:22 +0000112 }
adminea8ca452006-09-24 18:11:44 +0000113
114 // --------------------------------------------------------------------
115
116 /**
117 * Begin Transaction
admine334c472006-10-21 19:44:22 +0000118 *
adminea8ca452006-09-24 18:11:44 +0000119 * @access public
admine334c472006-10-21 19:44:22 +0000120 * @return bool
adminea8ca452006-09-24 18:11:44 +0000121 */
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.
admine334c472006-10-21 19:44:22 +0000136 // If the $test_mode flag is set to TRUE transactions will be rolled back
137 // even if the queries produce a successful result.
adminea8ca452006-09-24 18:11:44 +0000138 $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
admine334c472006-10-21 19:44:22 +0000148 *
adminea8ca452006-09-24 18:11:44 +0000149 * @access public
admine334c472006-10-21 19:44:22 +0000150 * @return bool
adminea8ca452006-09-24 18:11:44 +0000151 */
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
admine334c472006-10-21 19:44:22 +0000173 *
adminea8ca452006-09-24 18:11:44 +0000174 * @access public
admine334c472006-10-21 19:44:22 +0000175 * @return bool
adminea8ca452006-09-24 18:11:44 +0000176 */
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 /**
225 * Insert ID
226 *
227 * @access public
228 * @return integer
229 */
230 function insert_id()
231 {
232 // Not supported in MS SQL?
233 return 0;
234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * "Count All" query
240 *
241 * Generates a platform-specific query string that counts all records in
242 * the specified database
243 *
244 * @access public
245 * @param string
246 * @return string
247 */
248 function count_all($table = '')
249 {
250 if ($table == '')
251 return '0';
252
adminca335fc2006-10-23 21:18:36 +0000253 $query = $this->query("SELECT COUNT(*) AS numrows FROM ".$this->dbprefix.$table);
adminea8ca452006-09-24 18:11:44 +0000254
255 if ($query->num_rows() == 0)
256 return '0';
257
258 $row = $query->row();
259 return $row->numrows;
260 }
admin3dd978f2006-09-30 19:24:45 +0000261
262 // --------------------------------------------------------------------
263
264 /**
265 * List table query
266 *
267 * Generates a platform-specific query string so that the table names can be fetched
268 *
269 * @access private
270 * @return string
271 */
272 function _list_tables()
273 {
274 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
adminfafe28b2006-10-21 19:08:17 +0000280 * List column query
admin9cd4e8e2006-09-25 23:26:25 +0000281 *
282 * Generates a platform-specific query string so that the column names can be fetched
283 *
284 * @access private
285 * @param string the table name
286 * @return string
287 */
288 function _list_columns($table = '')
289 {
290 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Field data query
297 *
298 * Generates a platform-specific query so that the column data can be retrieved
299 *
300 * @access public
301 * @param string the table name
302 * @return object
303 */
304 function _field_data($table)
305 {
adminca335fc2006-10-23 21:18:36 +0000306 return "SELECT TOP 1 * FROM ".$this->_escape_table($table);
admin9cd4e8e2006-09-25 23:26:25 +0000307 }
308
309 // --------------------------------------------------------------------
310
311 /**
adminea8ca452006-09-24 18:11:44 +0000312 * The error message string
313 *
adminbb1d4392006-09-24 20:14:38 +0000314 * @access private
adminea8ca452006-09-24 18:11:44 +0000315 * @return string
316 */
adminbb1d4392006-09-24 20:14:38 +0000317 function _error_message()
adminea8ca452006-09-24 18:11:44 +0000318 {
319 // Are errros even supported in MS SQL?
320 return '';
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * The error message number
327 *
adminbb1d4392006-09-24 20:14:38 +0000328 * @access private
adminea8ca452006-09-24 18:11:44 +0000329 * @return integer
330 */
adminbb1d4392006-09-24 20:14:38 +0000331 function _error_number()
adminea8ca452006-09-24 18:11:44 +0000332 {
333 // Are error numbers supported?
334 return '';
335 }
336
337 // --------------------------------------------------------------------
338
339 /**
340 * Escape Table Name
341 *
342 * This function adds backticks if the table name has a period
343 * in it. Some DBs will get cranky unless periods are escaped
344 *
adminbb1d4392006-09-24 20:14:38 +0000345 * @access private
adminea8ca452006-09-24 18:11:44 +0000346 * @param string the table name
347 * @return string
348 */
adminbb1d4392006-09-24 20:14:38 +0000349 function _escape_table($table)
adminea8ca452006-09-24 18:11:44 +0000350 {
adminca335fc2006-10-23 21:18:36 +0000351 // I don't believe this is necessary with MS SQL. Not sure, though. - Rick
352
353 /*
adminea8ca452006-09-24 18:11:44 +0000354 if (stristr($table, '.'))
355 {
356 $table = preg_replace("/\./", "`.`", $table);
357 }
adminca335fc2006-10-23 21:18:36 +0000358 */
adminea8ca452006-09-24 18:11:44 +0000359
360 return $table;
adminbb1d4392006-09-24 20:14:38 +0000361 }
adminea8ca452006-09-24 18:11:44 +0000362
363 // --------------------------------------------------------------------
364
365 /**
366 * Insert statement
367 *
368 * Generates a platform-specific insert string from the supplied data
369 *
370 * @access public
371 * @param string the table name
372 * @param array the insert keys
373 * @param array the insert values
374 * @return string
375 */
376 function _insert($table, $keys, $values)
377 {
adminbb1d4392006-09-24 20:14:38 +0000378 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
adminea8ca452006-09-24 18:11:44 +0000379 }
380
381 // --------------------------------------------------------------------
382
383 /**
384 * Update statement
385 *
386 * Generates a platform-specific update string from the supplied data
387 *
388 * @access public
389 * @param string the table name
390 * @param array the update data
391 * @param array the where clause
392 * @return string
393 */
394 function _update($table, $values, $where)
395 {
396 foreach($values as $key => $val)
397 {
398 $valstr[] = $key." = ".$val;
399 }
400
adminbb1d4392006-09-24 20:14:38 +0000401 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
adminea8ca452006-09-24 18:11:44 +0000402 }
403
404 // --------------------------------------------------------------------
405
406 /**
407 * Delete statement
408 *
409 * Generates a platform-specific delete string from the supplied data
410 *
411 * @access public
412 * @param string the table name
413 * @param array the where clause
414 * @return string
415 */
416 function _delete($table, $where)
417 {
adminbb1d4392006-09-24 20:14:38 +0000418 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
adminea8ca452006-09-24 18:11:44 +0000419 }
adminbb1d4392006-09-24 20:14:38 +0000420
421 // --------------------------------------------------------------------
422
423 /**
424 * Limit string
425 *
426 * Generates a platform-specific LIMIT clause
427 *
428 * @access public
429 * @param string the sql query string
430 * @param integer the number of rows to limit the query to
431 * @param integer the offset value
432 * @return string
433 */
434 function _limit($sql, $limit, $offset)
435 {
436 $i = $limit + $offset;
adminea8ca452006-09-24 18:11:44 +0000437
adminbb1d4392006-09-24 20:14:38 +0000438 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
439 }
440
441 // --------------------------------------------------------------------
442
443 /**
444 * Close DB Connection
445 *
446 * @access public
447 * @param resource
448 * @return void
449 */
450 function _close($conn_id)
451 {
adminbefd4c22006-10-26 01:49:26 +0000452 @mssql_close($conn_id);
admina5e812c2006-09-25 02:17:30 +0000453 }
adminea8ca452006-09-24 18:11:44 +0000454
455}
456
457
458?>