blob: 3f5e5334545702f071d34e30262d11ca66473228 [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 /**
admin9cd4e8e2006-09-25 23:26:25 +0000265 * List columnn query
266 *
267 * Generates a platform-specific query string so that the column names can be fetched
268 *
269 * @access private
270 * @param string the table name
271 * @return string
272 */
273 function _list_columns($table = '')
274 {
275 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
276 }
277
278 // --------------------------------------------------------------------
279
280 /**
281 * Field data query
282 *
283 * Generates a platform-specific query so that the column data can be retrieved
284 *
285 * @access public
286 * @param string the table name
287 * @return object
288 */
289 function _field_data($table)
290 {
291 return "SELECT TOP 1 FROM ".$this->_escape_table($table);
292 }
293
294 // --------------------------------------------------------------------
295
296 /**
adminea8ca452006-09-24 18:11:44 +0000297 * The error message string
298 *
adminbb1d4392006-09-24 20:14:38 +0000299 * @access private
adminea8ca452006-09-24 18:11:44 +0000300 * @return string
301 */
adminbb1d4392006-09-24 20:14:38 +0000302 function _error_message()
adminea8ca452006-09-24 18:11:44 +0000303 {
304 // Are errros even supported in MS SQL?
305 return '';
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * The error message number
312 *
adminbb1d4392006-09-24 20:14:38 +0000313 * @access private
adminea8ca452006-09-24 18:11:44 +0000314 * @return integer
315 */
adminbb1d4392006-09-24 20:14:38 +0000316 function _error_number()
adminea8ca452006-09-24 18:11:44 +0000317 {
318 // Are error numbers supported?
319 return '';
320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * Escape Table Name
326 *
327 * This function adds backticks if the table name has a period
328 * in it. Some DBs will get cranky unless periods are escaped
329 *
adminbb1d4392006-09-24 20:14:38 +0000330 * @access private
adminea8ca452006-09-24 18:11:44 +0000331 * @param string the table name
332 * @return string
333 */
adminbb1d4392006-09-24 20:14:38 +0000334 function _escape_table($table)
adminea8ca452006-09-24 18:11:44 +0000335 {
336 if (stristr($table, '.'))
337 {
338 $table = preg_replace("/\./", "`.`", $table);
339 }
340
341 return $table;
adminbb1d4392006-09-24 20:14:38 +0000342 }
adminea8ca452006-09-24 18:11:44 +0000343
344 // --------------------------------------------------------------------
345
346 /**
347 * Insert statement
348 *
349 * Generates a platform-specific insert string from the supplied data
350 *
351 * @access public
352 * @param string the table name
353 * @param array the insert keys
354 * @param array the insert values
355 * @return string
356 */
357 function _insert($table, $keys, $values)
358 {
adminbb1d4392006-09-24 20:14:38 +0000359 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
adminea8ca452006-09-24 18:11:44 +0000360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * Update statement
366 *
367 * Generates a platform-specific update string from the supplied data
368 *
369 * @access public
370 * @param string the table name
371 * @param array the update data
372 * @param array the where clause
373 * @return string
374 */
375 function _update($table, $values, $where)
376 {
377 foreach($values as $key => $val)
378 {
379 $valstr[] = $key." = ".$val;
380 }
381
adminbb1d4392006-09-24 20:14:38 +0000382 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
adminea8ca452006-09-24 18:11:44 +0000383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Delete statement
389 *
390 * Generates a platform-specific delete string from the supplied data
391 *
392 * @access public
393 * @param string the table name
394 * @param array the where clause
395 * @return string
396 */
397 function _delete($table, $where)
398 {
adminbb1d4392006-09-24 20:14:38 +0000399 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
adminea8ca452006-09-24 18:11:44 +0000400 }
adminbb1d4392006-09-24 20:14:38 +0000401
402 // --------------------------------------------------------------------
403
404 /**
405 * Limit string
406 *
407 * Generates a platform-specific LIMIT clause
408 *
409 * @access public
410 * @param string the sql query string
411 * @param integer the number of rows to limit the query to
412 * @param integer the offset value
413 * @return string
414 */
415 function _limit($sql, $limit, $offset)
416 {
417 $i = $limit + $offset;
adminea8ca452006-09-24 18:11:44 +0000418
adminbb1d4392006-09-24 20:14:38 +0000419 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Close DB Connection
426 *
427 * @access public
428 * @param resource
429 * @return void
430 */
431 function _close($conn_id)
432 {
433 mssql_close($conn_id);
admina5e812c2006-09-25 02:17:30 +0000434 }
adminea8ca452006-09-24 18:11:44 +0000435
436}
437
438
439?>