blob: 38fce113494af2d35fda64f68e59c35b4a1d258a [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 }
69
70 // --------------------------------------------------------------------
71
72 /**
73 * Execute the query
74 *
75 * @access private called by the base class
76 * @param string an SQL query
77 * @return resource
78 */
79 function _execute($sql)
80 {
81 $sql = $this->_prep_query($sql);
82 return @mssql_query($sql, $this->conn_id);
83 }
84
85 // --------------------------------------------------------------------
86
87 /**
88 * Prep the query
89 *
90 * If needed, each database adapter can prep the query string
91 *
92 * @access private called by execute()
93 * @param string an SQL query
94 * @return string
95 */
96 function _prep_query($sql)
97 {
98 return $sql;
99 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Begin Transaction
105 *
106 * @access public
107 * @return bool
108 */
109 function trans_begin($test_mode = FALSE)
110 {
111 if ( ! $this->trans_enabled)
112 {
113 return TRUE;
114 }
115
116 // When transactions are nested we only begin/commit/rollback the outermost ones
117 if ($this->_trans_depth > 0)
118 {
119 return TRUE;
120 }
121
122 // Reset the transaction failure flag.
123 // If the $test_mode flag is set to TRUE transactions will be rolled back
124 // even if the queries produce a successful result.
125 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
126
127 $this->simple_query('BEGIN TRAN');
128 return TRUE;
129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Commit Transaction
135 *
136 * @access public
137 * @return bool
138 */
139 function trans_commit()
140 {
141 if ( ! $this->trans_enabled)
142 {
143 return TRUE;
144 }
145
146 // When transactions are nested we only begin/commit/rollback the outermost ones
147 if ($this->_trans_depth > 0)
148 {
149 return TRUE;
150 }
151
152 $this->simple_query('COMMIT TRAN');
153 return TRUE;
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Rollback Transaction
160 *
161 * @access public
162 * @return bool
163 */
164 function trans_rollback()
165 {
166 if ( ! $this->trans_enabled)
167 {
168 return TRUE;
169 }
170
171 // When transactions are nested we only begin/commit/rollback the outermost ones
172 if ($this->_trans_depth > 0)
173 {
174 return TRUE;
175 }
176
177 $this->simple_query('ROLLBACK TRAN');
178 return TRUE;
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Escape String
185 *
186 * @access public
187 * @param string
188 * @return string
189 */
190 function escape_str($str)
191 {
192 // Escape single quotes
193 return str_replace("'", "''", $str);
194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Affected Rows
200 *
201 * @access public
202 * @return integer
203 */
204 function affected_rows()
205 {
206 return @mssql_rows_affected($this->conn_id);
207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Insert ID
213 *
214 * @access public
215 * @return integer
216 */
217 function insert_id()
218 {
219 // Not supported in MS SQL?
220 return 0;
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * "Count All" query
227 *
228 * Generates a platform-specific query string that counts all records in
229 * the specified database
230 *
231 * @access public
232 * @param string
233 * @return string
234 */
235 function count_all($table = '')
236 {
237 if ($table == '')
238 return '0';
239
240 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
241
242 if ($query->num_rows() == 0)
243 return '0';
244
245 $row = $query->row();
246 return $row->numrows;
247 }
248
249 // --------------------------------------------------------------------
250
251 /**
252 * The error message string
253 *
adminbb1d4392006-09-24 20:14:38 +0000254 * @access private
adminea8ca452006-09-24 18:11:44 +0000255 * @return string
256 */
adminbb1d4392006-09-24 20:14:38 +0000257 function _error_message()
adminea8ca452006-09-24 18:11:44 +0000258 {
259 // Are errros even supported in MS SQL?
260 return '';
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * The error message number
267 *
adminbb1d4392006-09-24 20:14:38 +0000268 * @access private
adminea8ca452006-09-24 18:11:44 +0000269 * @return integer
270 */
adminbb1d4392006-09-24 20:14:38 +0000271 function _error_number()
adminea8ca452006-09-24 18:11:44 +0000272 {
273 // Are error numbers supported?
274 return '';
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * Escape Table Name
281 *
282 * This function adds backticks if the table name has a period
283 * in it. Some DBs will get cranky unless periods are escaped
284 *
adminbb1d4392006-09-24 20:14:38 +0000285 * @access private
adminea8ca452006-09-24 18:11:44 +0000286 * @param string the table name
287 * @return string
288 */
adminbb1d4392006-09-24 20:14:38 +0000289 function _escape_table($table)
adminea8ca452006-09-24 18:11:44 +0000290 {
291 if (stristr($table, '.'))
292 {
293 $table = preg_replace("/\./", "`.`", $table);
294 }
295
296 return $table;
adminbb1d4392006-09-24 20:14:38 +0000297 }
adminea8ca452006-09-24 18:11:44 +0000298
299 // --------------------------------------------------------------------
300
301 /**
302 * Insert statement
303 *
304 * Generates a platform-specific insert string from the supplied data
305 *
306 * @access public
307 * @param string the table name
308 * @param array the insert keys
309 * @param array the insert values
310 * @return string
311 */
312 function _insert($table, $keys, $values)
313 {
adminbb1d4392006-09-24 20:14:38 +0000314 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
adminea8ca452006-09-24 18:11:44 +0000315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Update statement
321 *
322 * Generates a platform-specific update string from the supplied data
323 *
324 * @access public
325 * @param string the table name
326 * @param array the update data
327 * @param array the where clause
328 * @return string
329 */
330 function _update($table, $values, $where)
331 {
332 foreach($values as $key => $val)
333 {
334 $valstr[] = $key." = ".$val;
335 }
336
adminbb1d4392006-09-24 20:14:38 +0000337 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
adminea8ca452006-09-24 18:11:44 +0000338 }
339
340 // --------------------------------------------------------------------
341
342 /**
343 * Delete statement
344 *
345 * Generates a platform-specific delete string from the supplied data
346 *
347 * @access public
348 * @param string the table name
349 * @param array the where clause
350 * @return string
351 */
352 function _delete($table, $where)
353 {
adminbb1d4392006-09-24 20:14:38 +0000354 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
adminea8ca452006-09-24 18:11:44 +0000355 }
adminbb1d4392006-09-24 20:14:38 +0000356
357 // --------------------------------------------------------------------
358
359 /**
360 * Limit string
361 *
362 * Generates a platform-specific LIMIT clause
363 *
364 * @access public
365 * @param string the sql query string
366 * @param integer the number of rows to limit the query to
367 * @param integer the offset value
368 * @return string
369 */
370 function _limit($sql, $limit, $offset)
371 {
372 $i = $limit + $offset;
adminea8ca452006-09-24 18:11:44 +0000373
adminbb1d4392006-09-24 20:14:38 +0000374 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Close DB Connection
381 *
382 * @access public
383 * @param resource
384 * @return void
385 */
386 function _close($conn_id)
387 {
388 mssql_close($conn_id);
admina5e812c2006-09-25 02:17:30 +0000389 }
adminea8ca452006-09-24 18:11:44 +0000390
391}
392
393
394?>