blob: 8b82ee314394950c43099c128dd1763c8ebb97f2 [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.
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 }
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 */
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 /**
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
253 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
254
255 if ($query->num_rows() == 0)
256 return '0';
257
258 $row = $query->row();
259 return $row->numrows;
260 }
261
262 // --------------------------------------------------------------------
263
264 /**
admin3dd978f2006-09-30 19:24:45 +0000265 * List databases
266 *
267 * @access private
268 * @return bool
269 */
270 function _list_databases()
271 {
272 return "EXEC sp_helpdb"; // Can also be: EXEC sp_databases
273 }
274
275 // --------------------------------------------------------------------
276
277 /**
278 * List table query
279 *
280 * Generates a platform-specific query string so that the table names can be fetched
281 *
282 * @access private
283 * @return string
284 */
285 function _list_tables()
286 {
287 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
288 }
289
290 // --------------------------------------------------------------------
291
292 /**
admin9cd4e8e2006-09-25 23:26:25 +0000293 * List columnn query
294 *
295 * Generates a platform-specific query string so that the column names can be fetched
296 *
297 * @access private
298 * @param string the table name
299 * @return string
300 */
301 function _list_columns($table = '')
302 {
303 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
304 }
305
306 // --------------------------------------------------------------------
307
308 /**
309 * Field data query
310 *
311 * Generates a platform-specific query so that the column data can be retrieved
312 *
313 * @access public
314 * @param string the table name
315 * @return object
316 */
317 function _field_data($table)
318 {
319 return "SELECT TOP 1 FROM ".$this->_escape_table($table);
320 }
321
322 // --------------------------------------------------------------------
323
324 /**
adminea8ca452006-09-24 18:11:44 +0000325 * The error message string
326 *
adminbb1d4392006-09-24 20:14:38 +0000327 * @access private
adminea8ca452006-09-24 18:11:44 +0000328 * @return string
329 */
adminbb1d4392006-09-24 20:14:38 +0000330 function _error_message()
adminea8ca452006-09-24 18:11:44 +0000331 {
332 // Are errros even supported in MS SQL?
333 return '';
334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * The error message number
340 *
adminbb1d4392006-09-24 20:14:38 +0000341 * @access private
adminea8ca452006-09-24 18:11:44 +0000342 * @return integer
343 */
adminbb1d4392006-09-24 20:14:38 +0000344 function _error_number()
adminea8ca452006-09-24 18:11:44 +0000345 {
346 // Are error numbers supported?
347 return '';
348 }
349
350 // --------------------------------------------------------------------
351
352 /**
353 * Escape Table Name
354 *
355 * This function adds backticks if the table name has a period
356 * in it. Some DBs will get cranky unless periods are escaped
357 *
adminbb1d4392006-09-24 20:14:38 +0000358 * @access private
adminea8ca452006-09-24 18:11:44 +0000359 * @param string the table name
360 * @return string
361 */
adminbb1d4392006-09-24 20:14:38 +0000362 function _escape_table($table)
adminea8ca452006-09-24 18:11:44 +0000363 {
364 if (stristr($table, '.'))
365 {
366 $table = preg_replace("/\./", "`.`", $table);
367 }
368
369 return $table;
adminbb1d4392006-09-24 20:14:38 +0000370 }
adminea8ca452006-09-24 18:11:44 +0000371
372 // --------------------------------------------------------------------
373
374 /**
375 * Insert statement
376 *
377 * Generates a platform-specific insert string from the supplied data
378 *
379 * @access public
380 * @param string the table name
381 * @param array the insert keys
382 * @param array the insert values
383 * @return string
384 */
385 function _insert($table, $keys, $values)
386 {
adminbb1d4392006-09-24 20:14:38 +0000387 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
adminea8ca452006-09-24 18:11:44 +0000388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Update statement
394 *
395 * Generates a platform-specific update string from the supplied data
396 *
397 * @access public
398 * @param string the table name
399 * @param array the update data
400 * @param array the where clause
401 * @return string
402 */
403 function _update($table, $values, $where)
404 {
405 foreach($values as $key => $val)
406 {
407 $valstr[] = $key." = ".$val;
408 }
409
adminbb1d4392006-09-24 20:14:38 +0000410 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
adminea8ca452006-09-24 18:11:44 +0000411 }
412
413 // --------------------------------------------------------------------
414
415 /**
416 * Delete statement
417 *
418 * Generates a platform-specific delete string from the supplied data
419 *
420 * @access public
421 * @param string the table name
422 * @param array the where clause
423 * @return string
424 */
425 function _delete($table, $where)
426 {
adminbb1d4392006-09-24 20:14:38 +0000427 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
adminea8ca452006-09-24 18:11:44 +0000428 }
adminbb1d4392006-09-24 20:14:38 +0000429
430 // --------------------------------------------------------------------
431
432 /**
433 * Limit string
434 *
435 * Generates a platform-specific LIMIT clause
436 *
437 * @access public
438 * @param string the sql query string
439 * @param integer the number of rows to limit the query to
440 * @param integer the offset value
441 * @return string
442 */
443 function _limit($sql, $limit, $offset)
444 {
445 $i = $limit + $offset;
adminea8ca452006-09-24 18:11:44 +0000446
adminbb1d4392006-09-24 20:14:38 +0000447 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
448 }
449
450 // --------------------------------------------------------------------
451
452 /**
453 * Close DB Connection
454 *
455 * @access public
456 * @param resource
457 * @return void
458 */
459 function _close($conn_id)
460 {
461 mssql_close($conn_id);
admina5e812c2006-09-25 02:17:30 +0000462 }
adminea8ca452006-09-24 18:11:44 +0000463
464}
465
466
467?>