blob: 5f30bf905adb2e6c88d4c3606eae5c0c8fe1a75f [file] [log] [blame]
admin7b613c72006-09-24 18:05:17 +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 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 *
254 * @access public
255 * @return string
256 */
257 function error_message()
258 {
259 // Are errros even supported in MS SQL?
260 return '';
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * The error message number
267 *
268 * @access public
269 * @return integer
270 */
271 function error_number()
272 {
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 *
285 * @access public
286 * @param string the table name
287 * @return string
288 */
289 function escape_table($table)
290 {
291 if (stristr($table, '.'))
292 {
293 $table = preg_replace("/\./", "`.`", $table);
294 }
295
296 return $table;
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Field data query
303 *
304 * Generates a platform-specific query so that the column data can be retrieved
305 *
306 * @access public
307 * @param string the table name
308 * @return object
309 */
310 function _field_data($table)
311 {
312 $sql = "SELECT TOP 1 FROM ".$this->escape_table($table);
313 $query = $this->query($sql);
314 return $query->field_data();
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Insert statement
321 *
322 * Generates a platform-specific insert string from the supplied data
323 *
324 * @access public
325 * @param string the table name
326 * @param array the insert keys
327 * @param array the insert values
328 * @return string
329 */
330 function _insert($table, $keys, $values)
331 {
332 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
333 }
334
335 // --------------------------------------------------------------------
336
337 /**
338 * Update statement
339 *
340 * Generates a platform-specific update string from the supplied data
341 *
342 * @access public
343 * @param string the table name
344 * @param array the update data
345 * @param array the where clause
346 * @return string
347 */
348 function _update($table, $values, $where)
349 {
350 foreach($values as $key => $val)
351 {
352 $valstr[] = $key." = ".$val;
353 }
354
355 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * Delete statement
362 *
363 * Generates a platform-specific delete string from the supplied data
364 *
365 * @access public
366 * @param string the table name
367 * @param array the where clause
368 * @return string
369 */
370 function _delete($table, $where)
371 {
372 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
373 }
374
375 // --------------------------------------------------------------------
376
377 /**
378 * Version number query string
379 *
380 * @access public
381 * @return string
382 */
383 function _version()
384 {
385 return "SELECT version() AS ver";
386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Show table query
392 *
393 * Generates a platform-specific query string so that the table names can be fetched
394 *
395 * @access public
396 * @return string
397 */
398 function _show_tables()
399 {
400 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Show columnn query
407 *
408 * Generates a platform-specific query string so that the column names can be fetched
409 *
410 * @access public
411 * @param string the table name
412 * @return string
413 */
414 function _show_columns($table = '')
415 {
416 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->escape_table($table)."'";
417 }
418
419 // --------------------------------------------------------------------
420
421 /**
422 * Limit string
423 *
424 * Generates a platform-specific LIMIT clause
425 *
426 * @access public
427 * @param string the sql query string
428 * @param integer the number of rows to limit the query to
429 * @param integer the offset value
430 * @return string
431 */
432 function _limit($sql, $limit, $offset)
433 {
434 $i = $limit + $offset;
435
436 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
437 }
438
439 // --------------------------------------------------------------------
440
441 /**
442 * Close DB Connection
443 *
444 * @access public
445 * @param resource
446 * @return void
447 */
448 function _close($conn_id)
449 {
450 mssql_close($conn_id);
451 }
452
453
454}
455
456
457?>