blob: 291e168ebfa3cf957d9ed73b8e21207684bd978f [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
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, EllisLab, 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 * oci8 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 */
31
32/**
33 * oci8 Database Adapter Class
34 *
35 * This is a modification of the DB_driver class to
36 * permit access to oracle databases
37 *
38 * NOTE: this uses the PHP 4 oci methods
39 *
40 * @author Kelly McArdle
41 *
42 */
43
44class CI_DB_oci8_driver extends CI_DB {
45
46 // Set "auto commit" by default
47 var $_commit = OCI_COMMIT_ON_SUCCESS;
48
49 // need to track statement id and cursor id
50 var $stmt_id;
51 var $curs_id;
52
53 // if we use a limit, we will add a field that will
54 // throw off num_fields later
55 var $limit_used;
56
57 /**
58 * Non-persistent database connection
59 *
60 * @access private called by the base class
61 * @return resource
62 */
63 function db_connect()
64 {
65 return @ocilogon($this->username, $this->password, $this->hostname);
66 }
67
68 // --------------------------------------------------------------------
69
70 /**
71 * Persistent database connection
72 *
73 * @access private called by the base class
74 * @return resource
75 */
76 function db_pconnect()
77 {
78 return @ociplogon($this->username, $this->password, $this->hostname);
79 }
80
81 // --------------------------------------------------------------------
82
83 /**
84 * Select the database
85 *
86 * @access private called by the base class
87 * @return resource
88 */
89 function db_select()
90 {
91 return TRUE;
92 }
93
94 // --------------------------------------------------------------------
95
96 /**
97 * Version number query string
98 *
99 * @access public
100 * @return string
101 */
102 function _version()
103 {
104 return ociserverversion($this->conn_id);
105 }
106
107 // --------------------------------------------------------------------
108
109 /**
110 * Execute the query
111 *
112 * @access private called by the base class
113 * @param string an SQL query
114 * @return resource
115 */
116 function _execute($sql)
117 {
118 // oracle must parse the query before it is run. All of the actions with
119 // the query are based on the statement id returned by ociparse
120 $this->_set_stmt_id($sql);
121 ocisetprefetch($this->stmt_id, 1000);
122 return @ociexecute($this->stmt_id, $this->_commit);
123 }
124
125 /**
126 * Generate a statement ID
127 *
128 * @access private
129 * @param string an SQL query
130 * @return none
131 */
132 function _set_stmt_id($sql)
133 {
134 if ( ! is_resource($this->stmt_id))
135 {
136 $this->stmt_id = ociparse($this->conn_id, $this->_prep_query($sql));
137 }
138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Prep the query
144 *
145 * If needed, each database adapter can prep the query string
146 *
147 * @access private called by execute()
148 * @param string an SQL query
149 * @return string
150 */
151 function _prep_query($sql)
152 {
153 return $sql;
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * getCursor. Returns a cursor from the datbase
160 *
161 * @access public
162 * @return cursor id
163 */
164 function get_cursor()
165 {
166 $this->curs_id = ocinewcursor($this->conn_id);
167 return $this->curs_id;
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Stored Procedure. Executes a stored procedure
174 *
175 * @access public
176 * @param package package stored procedure is in
177 * @param procedure stored procedure to execute
178 * @param params array of parameters
179 * @return array
180 *
181 * params array keys
182 *
183 * KEY OPTIONAL NOTES
184 * name no the name of the parameter should be in :<param_name> format
185 * value no the value of the parameter. If this is an OUT or IN OUT parameter,
186 * this should be a reference to a variable
187 * type yes the type of the parameter
188 * length yes the max size of the parameter
189 */
190 function stored_procedure($package, $procedure, $params)
191 {
192 if ($package == '' OR $procedure == '' OR ! is_array($params))
193 {
194 if ($this->db_debug)
195 {
196 log_message('error', 'Invalid query: '.$package.'.'.$procedure);
197 return $this->display_error('db_invalid_query');
198 }
199 return FALSE;
200 }
201
202 // build the query string
203 $sql = "begin $package.$procedure(";
204
205 $have_cursor = FALSE;
206 foreach($params as $param)
207 {
208 $sql .= $param['name'] . ",";
209
210 if (array_key_exists('type', $param) && ($param['type'] == OCI_B_CURSOR))
211 {
212 $have_cursor = TRUE;
213 }
214 }
215 $sql = trim($sql, ",") . "); end;";
216
217 $this->stmt_id = FALSE;
218 $this->_set_stmt_id($sql);
219 $this->_bind_params($params);
220 $this->query($sql, FALSE, $have_cursor);
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Bind parameters
227 *
228 * @access private
229 * @return none
230 */
231 function _bind_params($params)
232 {
233 if ( ! is_array($params) OR ! is_resource($this->stmt_id))
234 {
235 return;
236 }
237
238 foreach ($params as $param)
239 {
240 foreach (array('name', 'value', 'type', 'length') as $val)
241 {
242 if ( ! isset($param[$val]))
243 {
244 $param[$val] = '';
245 }
246 }
247
248 ocibindbyname($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);
249 }
250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Begin Transaction
256 *
257 * @access public
258 * @return bool
259 */
260 function trans_begin($test_mode = FALSE)
261 {
262 if ( ! $this->trans_enabled)
263 {
264 return TRUE;
265 }
266
267 // When transactions are nested we only begin/commit/rollback the outermost ones
268 if ($this->_trans_depth > 0)
269 {
270 return TRUE;
271 }
272
273 // Reset the transaction failure flag.
274 // If the $test_mode flag is set to TRUE transactions will be rolled back
275 // even if the queries produce a successful result.
276 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
277
278 $this->_commit = OCI_DEFAULT;
279 return TRUE;
280 }
281
282 // --------------------------------------------------------------------
283
284 /**
285 * Commit Transaction
286 *
287 * @access public
288 * @return bool
289 */
290 function trans_commit()
291 {
292 if ( ! $this->trans_enabled)
293 {
294 return TRUE;
295 }
296
297 // When transactions are nested we only begin/commit/rollback the outermost ones
298 if ($this->_trans_depth > 0)
299 {
300 return TRUE;
301 }
302
303 $ret = OCIcommit($this->conn_id);
304 $this->_commit = OCI_COMMIT_ON_SUCCESS;
305 return $ret;
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * Rollback Transaction
312 *
313 * @access public
314 * @return bool
315 */
316 function trans_rollback()
317 {
318 if ( ! $this->trans_enabled)
319 {
320 return TRUE;
321 }
322
323 // When transactions are nested we only begin/commit/rollback the outermost ones
324 if ($this->_trans_depth > 0)
325 {
326 return TRUE;
327 }
328
329 $ret = OCIrollback($this->conn_id);
330 $this->_commit = OCI_COMMIT_ON_SUCCESS;
331 return $ret;
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Escape String
338 *
339 * @access public
340 * @param string
341 * @return string
342 */
343 function escape_str($str)
344 {
345 return $str;
346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * Affected Rows
352 *
353 * @access public
354 * @return integer
355 */
356 function affected_rows()
357 {
358 return @ocirowcount($this->stmt_id);
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Insert ID
365 *
366 * @access public
367 * @return integer
368 */
369 function insert_id()
370 {
371 // not supported in oracle
372 return 0;
373 }
374
375 // --------------------------------------------------------------------
376
377 /**
378 * "Count All" query
379 *
380 * Generates a platform-specific query string that counts all records in
381 * the specified database
382 *
383 * @access public
384 * @param string
385 * @return string
386 */
387 function count_all($table = '')
388 {
389 if ($table == '')
390 return '0';
391
392 $query = $this->query("SELECT COUNT(1) AS numrows FROM ".$table);
393
394 if ($query == FALSE)
395 {
396 return 0;
397 }
398
399 $row = $query->row();
400 return $row->NUMROWS;
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Show table query
407 *
408 * Generates a platform-specific query string so that the table names can be fetched
409 *
410 * @access private
411 * @return string
412 */
413 function _list_tables()
414 {
415 return "SELECT TABLE_NAME FROM ALL_TABLES";
416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Show column query
422 *
423 * Generates a platform-specific query string so that the column names can be fetched
424 *
425 * @access public
426 * @param string the table name
427 * @return string
428 */
429 function _list_columns($table = '')
430 {
431 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";
432 }
433
434 // --------------------------------------------------------------------
435
436 /**
437 * Field data query
438 *
439 * Generates a platform-specific query so that the column data can be retrieved
440 *
441 * @access public
442 * @param string the table name
443 * @return object
444 */
445 function _field_data($table)
446 {
447 return "SELECT * FROM ".$this->_escape_table($table)." where rownum = 1";
448 }
449
450 // --------------------------------------------------------------------
451
452 /**
453 * The error message string
454 *
455 * @access private
456 * @return string
457 */
458 function _error_message()
459 {
460 $error = ocierror($this->conn_id);
461 return $error['message'];
462 }
463
464 // --------------------------------------------------------------------
465
466 /**
467 * The error message number
468 *
469 * @access private
470 * @return integer
471 */
472 function _error_number()
473 {
474 $error = ocierror($this->conn_id);
475 return $error['code'];
476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Escape Table Name
482 *
483 * This function adds backticks if the table name has a period
484 * in it. Some DBs will get cranky unless periods are escaped
485 *
486 * @access private
487 * @param string the table name
488 * @return string
489 */
490 function _escape_table($table)
491 {
492 if (stristr($table, '.'))
493 {
494 $table = preg_replace("/\./", "`.`", $table);
495 }
496
497 return $table;
498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Insert statement
504 *
505 * Generates a platform-specific insert string from the supplied data
506 *
507 * @access public
508 * @param string the table name
509 * @param array the insert keys
510 * @param array the insert values
511 * @return string
512 */
513 function _insert($table, $keys, $values)
514 {
515 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
516 }
517
518 // --------------------------------------------------------------------
519
520 /**
521 * Update statement
522 *
523 * Generates a platform-specific update string from the supplied data
524 *
525 * @access public
526 * @param string the table name
527 * @param array the update data
528 * @param array the where clause
529 * @return string
530 */
531 function _update($table, $values, $where)
532 {
533 foreach($values as $key => $val)
534 {
535 $valstr[] = $key." = ".$val;
536 }
537
538 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
539 }
540
541 // --------------------------------------------------------------------
542
543 /**
544 * Delete statement
545 *
546 * Generates a platform-specific delete string from the supplied data
547 *
548 * @access public
549 * @param string the table name
550 * @param array the where clause
551 * @return string
552 */
553 function _delete($table, $where)
554 {
555 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
556 }
557
558 // --------------------------------------------------------------------
559
560 /**
561 * Limit string
562 *
563 * Generates a platform-specific LIMIT clause
564 *
565 * @access public
566 * @param string the sql query string
567 * @param integer the number of rows to limit the query to
568 * @param integer the offset value
569 * @return string
570 */
571 function _limit($sql, $limit, $offset)
572 {
573 $limit = $offset + $limit;
574 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";
575
576 if ($offset != 0)
577 {
578 $newsql .= " WHERE rnum >= $offset";
579 }
580
581 // remember that we used limits
582 $this->limit_used = TRUE;
583
584 return $newsql;
585 }
586
587 // --------------------------------------------------------------------
588
589 /**
590 * Close DB Connection
591 *
592 * @access public
593 * @param resource
594 * @return void
595 */
596 function _close($conn_id)
597 {
598 @ocilogoff($conn_id);
599 }
600
601
602}
603
604
admin99e1b292006-09-24 18:12:43 +0000605?>