blob: cb2f48dc3e54656582cf8cb9fd75f83558a77af1 [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 }
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 /**
admin9cd4e8e2006-09-25 23:26:25 +0000280 * List columnn query
281 *
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 {
306 return "SELECT TOP 1 FROM ".$this->_escape_table($table);
307 }
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 {
351 if (stristr($table, '.'))
352 {
353 $table = preg_replace("/\./", "`.`", $table);
354 }
355
356 return $table;
adminbb1d4392006-09-24 20:14:38 +0000357 }
adminea8ca452006-09-24 18:11:44 +0000358
359 // --------------------------------------------------------------------
360
361 /**
362 * Insert statement
363 *
364 * Generates a platform-specific insert string from the supplied data
365 *
366 * @access public
367 * @param string the table name
368 * @param array the insert keys
369 * @param array the insert values
370 * @return string
371 */
372 function _insert($table, $keys, $values)
373 {
adminbb1d4392006-09-24 20:14:38 +0000374 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
adminea8ca452006-09-24 18:11:44 +0000375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Update statement
381 *
382 * Generates a platform-specific update string from the supplied data
383 *
384 * @access public
385 * @param string the table name
386 * @param array the update data
387 * @param array the where clause
388 * @return string
389 */
390 function _update($table, $values, $where)
391 {
392 foreach($values as $key => $val)
393 {
394 $valstr[] = $key." = ".$val;
395 }
396
adminbb1d4392006-09-24 20:14:38 +0000397 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
adminea8ca452006-09-24 18:11:44 +0000398 }
399
400 // --------------------------------------------------------------------
401
402 /**
403 * Delete statement
404 *
405 * Generates a platform-specific delete string from the supplied data
406 *
407 * @access public
408 * @param string the table name
409 * @param array the where clause
410 * @return string
411 */
412 function _delete($table, $where)
413 {
adminbb1d4392006-09-24 20:14:38 +0000414 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
adminea8ca452006-09-24 18:11:44 +0000415 }
adminbb1d4392006-09-24 20:14:38 +0000416
417 // --------------------------------------------------------------------
418
419 /**
420 * Limit string
421 *
422 * Generates a platform-specific LIMIT clause
423 *
424 * @access public
425 * @param string the sql query string
426 * @param integer the number of rows to limit the query to
427 * @param integer the offset value
428 * @return string
429 */
430 function _limit($sql, $limit, $offset)
431 {
432 $i = $limit + $offset;
adminea8ca452006-09-24 18:11:44 +0000433
adminbb1d4392006-09-24 20:14:38 +0000434 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * Close DB Connection
441 *
442 * @access public
443 * @param resource
444 * @return void
445 */
446 function _close($conn_id)
447 {
448 mssql_close($conn_id);
admina5e812c2006-09-25 02:17:30 +0000449 }
adminea8ca452006-09-24 18:11:44 +0000450
451}
452
453
454?>