blob: 32c0537c3083c18b879f67e589ebe71272e06df4 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +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/libraries/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 */
admine885d782006-09-23 20:25:05 +000079 function _execute($sql)
adminb0dd10f2006-08-25 17:25:49 +000080 {
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 */
adminb071bb52006-08-26 19:28:37 +000096 function _prep_query($sql)
adminb0dd10f2006-08-25 17:25:49 +000097 {
98 return $sql;
99 }
admine885d782006-09-23 20:25:05 +0000100
101 // --------------------------------------------------------------------
102
103 /**
104 * Begin Transaction
105 *
106 * @access public
107 * @return bool
108 */
admin8b180be2006-09-24 01:12:22 +0000109 function trans_begin($test_mode = FALSE)
admine885d782006-09-23 20:25:05 +0000110 {
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
admin8b180be2006-09-24 01:12:22 +0000122 // 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
admine885d782006-09-23 20:25:05 +0000127 $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 }
adminb0dd10f2006-08-25 17:25:49 +0000180
181 // --------------------------------------------------------------------
182
183 /**
184 * Escape String
185 *
186 * @access public
187 * @param string
188 * @return string
189 */
190 function escape_str($str)
191 {
admin1082bdd2006-08-27 19:32:02 +0000192 // Escape single quotes
193 return str_replace("'", "''", $str);
adminb0dd10f2006-08-25 17:25:49 +0000194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Close DB Connection
200 *
201 * @access public
202 * @param resource
203 * @return void
204 */
205 function destroy($conn_id)
206 {
207 mssql_close($conn_id);
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Affected Rows
214 *
215 * @access public
216 * @return integer
217 */
218 function affected_rows()
219 {
220 return @mssql_rows_affected($this->conn_id);
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Insert ID
227 *
228 * @access public
229 * @return integer
230 */
231 function insert_id()
232 {
233 // Not supported in MS SQL?
234 return 0;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * "Count All" query
241 *
242 * Generates a platform-specific query string that counts all records in
243 * the specified database
244 *
245 * @access public
246 * @param string
247 * @return string
248 */
249 function count_all($table = '')
250 {
251 if ($table == '')
252 return '0';
253
254 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
255
256 if ($query->num_rows() == 0)
257 return '0';
258
259 $row = $query->row();
260 return $row->numrows;
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * The error message string
267 *
268 * @access public
269 * @return string
270 */
271 function error_message()
272 {
273 // Are errros even supported in MS SQL?
274 return '';
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * The error message number
281 *
282 * @access public
283 * @return integer
284 */
285 function error_number()
286 {
287 // Are error numbers supported?
288 return '';
289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * Escape Table Name
295 *
296 * This function adds backticks if the table name has a period
297 * in it. Some DBs will get cranky unless periods are escaped
298 *
299 * @access public
300 * @param string the table name
301 * @return string
302 */
303 function escape_table($table)
304 {
305 if (stristr($table, '.'))
306 {
307 $table = preg_replace("/\./", "`.`", $table);
308 }
309
310 return $table;
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Field data query
317 *
318 * Generates a platform-specific query so that the column data can be retrieved
319 *
320 * @access public
321 * @param string the table name
322 * @return object
323 */
324 function _field_data($table)
325 {
326 $sql = "SELECT TOP 1 FROM ".$this->escape_table($table);
327 $query = $this->query($sql);
328 return $query->field_data();
329 }
330
331 // --------------------------------------------------------------------
332
333 /**
334 * Insert statement
335 *
336 * Generates a platform-specific insert string from the supplied data
337 *
338 * @access public
339 * @param string the table name
340 * @param array the insert keys
341 * @param array the insert values
342 * @return string
343 */
344 function _insert($table, $keys, $values)
345 {
346 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * Update statement
353 *
354 * Generates a platform-specific update string from the supplied data
355 *
356 * @access public
357 * @param string the table name
358 * @param array the update data
359 * @param array the where clause
360 * @return string
361 */
362 function _update($table, $values, $where)
363 {
364 foreach($values as $key => $val)
365 {
366 $valstr[] = $key." = ".$val;
367 }
368
369 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Delete statement
376 *
377 * Generates a platform-specific delete string from the supplied data
378 *
379 * @access public
380 * @param string the table name
381 * @param array the where clause
382 * @return string
383 */
384 function _delete($table, $where)
385 {
386 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
387 }
388
389 // --------------------------------------------------------------------
390
391 /**
392 * Version number query string
393 *
394 * @access public
395 * @return string
396 */
397 function _version()
398 {
399 return "SELECT version() AS ver";
400 }
401
402 // --------------------------------------------------------------------
403
404 /**
405 * Show table query
406 *
407 * Generates a platform-specific query string so that the table names can be fetched
408 *
409 * @access public
410 * @return string
411 */
412 function _show_tables()
413 {
414 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Show columnn query
421 *
422 * Generates a platform-specific query string so that the column names can be fetched
423 *
424 * @access public
425 * @param string the table name
426 * @return string
427 */
428 function _show_columns($table = '')
429 {
430 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->escape_table($table)."'";
431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Limit string
437 *
438 * Generates a platform-specific LIMIT clause
439 *
440 * @access public
441 * @param string the sql query string
442 * @param integer the number of rows to limit the query to
443 * @param integer the offset value
444 * @return string
445 */
446 function _limit($sql, $limit, $offset)
447 {
448 $i = $limit + $offset;
449
450 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
451 }
452
453}
454
455
456
457/**
458 * MS SQL Result Class
459 *
460 * This class extends the parent result class: CI_DB_result
461 *
462 * @category Database
463 * @author Rick Ellis
464 * @link http://www.codeigniter.com/user_guide/libraries/database/
465 */
466class CI_DB_mssql_result extends CI_DB_result {
467
468 /**
469 * Number of rows in the result set
470 *
471 * @access public
472 * @return integer
473 */
474 function num_rows()
475 {
476 return @mssql_num_rows($this->result_id);
477 }
478
479 // --------------------------------------------------------------------
480
481 /**
482 * Number of fields in the result set
483 *
484 * @access public
485 * @return integer
486 */
487 function num_fields()
488 {
489 return @mssql_num_fields($this->result_id);
490 }
491
492 // --------------------------------------------------------------------
493
494 /**
495 * Field data
496 *
497 * Generates an array of objects containing field meta-data
498 *
499 * @access public
500 * @return array
501 */
502 function field_data()
503 {
504 $retval = array();
505 while ($field = mssql_fetch_field($this->result_id))
506 {
admine348efb2006-09-20 21:13:26 +0000507 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000508 $F->name = $field->name;
509 $F->type = $field->type;
510 $F->max_length = $field->max_length;
511 $F->primary_key = 0;
512 $F->default = '';
513
514 $retval[] = $F;
515 }
516
517 return $retval;
518 }
519
520 // --------------------------------------------------------------------
521
522 /**
523 * Result - associative array
524 *
525 * Returns the result set as an array
526 *
527 * @access private
528 * @return array
529 */
530 function _fetch_assoc()
531 {
532 return mssql_fetch_assoc($this->result_id);
533 }
534
535 // --------------------------------------------------------------------
536
537 /**
538 * Result - object
539 *
540 * Returns the result set as an object
541 *
542 * @access private
543 * @return object
544 */
545 function _fetch_object()
546 {
547 return mssql_fetch_object($this->result_id);
548 }
549
550}
551
552?>