Take two:
[www-register-wizard.git] / database / drivers / oci8 / oci8_driver.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * oci8 Database Adapter Class\r
20  *\r
21  * Note: _DB is an extender class that the app controller\r
22  * creates dynamically based on whether the active record\r
23  * class is being used or not.\r
24  *\r
25  * @package             CodeIgniter\r
26  * @subpackage  Drivers\r
27  * @category    Database\r
28  * @author              ExpressionEngine Dev Team\r
29  * @link                http://codeigniter.com/user_guide/database/\r
30  */\r
31 \r
32 /**\r
33  * oci8 Database Adapter Class\r
34  *\r
35  * This is a modification of the DB_driver class to\r
36  * permit access to oracle databases\r
37  *\r
38  * NOTE: this uses the PHP 4 oci methods\r
39  *\r
40  * @author        Kelly McArdle\r
41  *\r
42  */\r
43 \r
44 class CI_DB_oci8_driver extends CI_DB {\r
45 \r
46         var $dbdriver = 'oci8';\r
47         \r
48         // The character used for excaping\r
49         var $_escape_char = '"';\r
50 \r
51         /**\r
52          * The syntax to count rows is slightly different across different\r
53          * database engines, so this string appears in each driver and is\r
54          * used for the count_all() and count_all_results() functions.\r
55          */\r
56         var $_count_string = "SELECT COUNT(1) AS ";\r
57         var $_random_keyword = ' ASC'; // not currently supported\r
58 \r
59         // Set "auto commit" by default\r
60         var $_commit = OCI_COMMIT_ON_SUCCESS;\r
61 \r
62         // need to track statement id and cursor id\r
63         var $stmt_id;\r
64         var $curs_id;\r
65 \r
66         // if we use a limit, we will add a field that will\r
67         // throw off num_fields later\r
68         var $limit_used;\r
69 \r
70         /**\r
71          * Non-persistent database connection\r
72          *\r
73          * @access  private called by the base class\r
74          * @return  resource\r
75          */\r
76         function db_connect()\r
77         {\r
78                 return @ocilogon($this->username, $this->password, $this->hostname);\r
79         }\r
80 \r
81         // --------------------------------------------------------------------\r
82 \r
83         /**\r
84          * Persistent database connection\r
85          *\r
86          * @access  private called by the base class\r
87          * @return  resource\r
88          */\r
89         function db_pconnect()\r
90         {\r
91                 return @ociplogon($this->username, $this->password, $this->hostname);\r
92         }\r
93 \r
94         // --------------------------------------------------------------------\r
95 \r
96         /**\r
97          * Select the database\r
98          *\r
99          * @access  private called by the base class\r
100          * @return  resource\r
101          */\r
102         function db_select()\r
103         {\r
104                 return TRUE;\r
105         }\r
106 \r
107         // --------------------------------------------------------------------\r
108 \r
109         /**\r
110          * Set client character set\r
111          *\r
112          * @access      public\r
113          * @param       string\r
114          * @param       string\r
115          * @return      resource\r
116          */\r
117         function db_set_charset($charset, $collation)\r
118         {\r
119                 // @todo - add support if needed\r
120                 return TRUE;\r
121         }\r
122 \r
123         // --------------------------------------------------------------------\r
124         \r
125         /**\r
126          * Version number query string\r
127          *\r
128          * @access  public\r
129          * @return  string\r
130          */\r
131         function _version()\r
132         {\r
133                 return ociserverversion($this->conn_id);\r
134         }\r
135 \r
136         // --------------------------------------------------------------------\r
137 \r
138         /**\r
139          * Execute the query\r
140          *\r
141          * @access  private called by the base class\r
142          * @param   string  an SQL query\r
143          * @return  resource\r
144          */\r
145         function _execute($sql)\r
146         {\r
147                 // oracle must parse the query before it is run. All of the actions with\r
148                 // the query are based on the statement id returned by ociparse\r
149                 $this->stmt_id = FALSE;\r
150                 $this->_set_stmt_id($sql);\r
151                 ocisetprefetch($this->stmt_id, 1000);\r
152                 return @ociexecute($this->stmt_id, $this->_commit);\r
153         }\r
154 \r
155         /**\r
156          * Generate a statement ID\r
157          *\r
158          * @access  private\r
159          * @param   string  an SQL query\r
160          * @return  none\r
161          */\r
162         function _set_stmt_id($sql)\r
163         {\r
164                 if ( ! is_resource($this->stmt_id))\r
165                 {\r
166                         $this->stmt_id = ociparse($this->conn_id, $this->_prep_query($sql));\r
167                 }\r
168         }\r
169 \r
170         // --------------------------------------------------------------------\r
171 \r
172         /**\r
173          * Prep the query\r
174          *\r
175          * If needed, each database adapter can prep the query string\r
176          *\r
177          * @access  private called by execute()\r
178          * @param   string  an SQL query\r
179          * @return  string\r
180          */\r
181         function _prep_query($sql)\r
182         {\r
183                 return $sql;\r
184         }\r
185 \r
186         // --------------------------------------------------------------------\r
187 \r
188         /**\r
189          * getCursor.  Returns a cursor from the datbase\r
190          *\r
191          * @access  public\r
192          * @return  cursor id\r
193          */\r
194         function get_cursor()\r
195         {\r
196                 $this->curs_id = ocinewcursor($this->conn_id);\r
197                 return $this->curs_id;\r
198         }\r
199 \r
200         // --------------------------------------------------------------------\r
201 \r
202         /**\r
203          * Stored Procedure.  Executes a stored procedure\r
204          *\r
205          * @access  public\r
206          * @param   package      package stored procedure is in\r
207          * @param   procedure   stored procedure to execute\r
208          * @param   params        array of parameters\r
209          * @return  array\r
210          *\r
211          * params array keys\r
212          *\r
213          * KEY    OPTIONAL      NOTES\r
214          * name         no              the name of the parameter should be in :<param_name> format\r
215          * value        no              the value of the parameter.  If this is an OUT or IN OUT parameter,\r
216          *                                      this should be a reference to a variable\r
217          * type         yes             the type of the parameter\r
218          * length       yes             the max size of the parameter\r
219          */\r
220         function stored_procedure($package, $procedure, $params)\r
221         {\r
222                 if ($package == '' OR $procedure == '' OR ! is_array($params))\r
223                 {\r
224                         if ($this->db_debug)\r
225                         {\r
226                                 log_message('error', 'Invalid query: '.$package.'.'.$procedure);\r
227                                 return $this->display_error('db_invalid_query');\r
228                         }\r
229                         return FALSE;\r
230                 }\r
231                 \r
232                 // build the query string\r
233                 $sql = "begin $package.$procedure(";\r
234 \r
235                 $have_cursor = FALSE;\r
236                 foreach($params as $param)\r
237                 {\r
238                         $sql .= $param['name'] . ",";\r
239                         \r
240                         if (array_key_exists('type', $param) && ($param['type'] == OCI_B_CURSOR))\r
241                         {\r
242                                 $have_cursor = TRUE;\r
243                         }\r
244                 }\r
245                 $sql = trim($sql, ",") . "); end;";\r
246                                 \r
247                 $this->stmt_id = FALSE;\r
248                 $this->_set_stmt_id($sql);\r
249                 $this->_bind_params($params);\r
250                 $this->query($sql, FALSE, $have_cursor);\r
251         }\r
252         \r
253         // --------------------------------------------------------------------\r
254 \r
255         /**\r
256          * Bind parameters\r
257          *\r
258          * @access  private\r
259          * @return  none\r
260          */\r
261         function _bind_params($params)\r
262         {\r
263                 if ( ! is_array($params) OR ! is_resource($this->stmt_id))\r
264                 {\r
265                         return;\r
266                 }\r
267                 \r
268                 foreach ($params as $param)\r
269                 {\r
270                         foreach (array('name', 'value', 'type', 'length') as $val)\r
271                         {\r
272                                 if ( ! isset($param[$val]))\r
273                                 {\r
274                                         $param[$val] = '';\r
275                                 }\r
276                         }\r
277 \r
278                         ocibindbyname($this->stmt_id, $param['name'], $param['value'], $param['length'], $param['type']);\r
279                 }\r
280         }\r
281 \r
282         // --------------------------------------------------------------------\r
283 \r
284         /**\r
285          * Begin Transaction\r
286          *\r
287          * @access      public\r
288          * @return      bool            \r
289          */     \r
290         function trans_begin($test_mode = FALSE)\r
291         {\r
292                 if ( ! $this->trans_enabled)\r
293                 {\r
294                         return TRUE;\r
295                 }\r
296                 \r
297                 // When transactions are nested we only begin/commit/rollback the outermost ones\r
298                 if ($this->_trans_depth > 0)\r
299                 {\r
300                         return TRUE;\r
301                 }\r
302                 \r
303                 // Reset the transaction failure flag.\r
304                 // If the $test_mode flag is set to TRUE transactions will be rolled back\r
305                 // even if the queries produce a successful result.\r
306                 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;\r
307                 \r
308                 $this->_commit = OCI_DEFAULT;\r
309                 return TRUE;\r
310         }\r
311 \r
312         // --------------------------------------------------------------------\r
313 \r
314         /**\r
315          * Commit Transaction\r
316          *\r
317          * @access      public\r
318          * @return      bool            \r
319          */     \r
320         function trans_commit()\r
321         {\r
322                 if ( ! $this->trans_enabled)\r
323                 {\r
324                         return TRUE;\r
325                 }\r
326 \r
327                 // When transactions are nested we only begin/commit/rollback the outermost ones\r
328                 if ($this->_trans_depth > 0)\r
329                 {\r
330                         return TRUE;\r
331                 }\r
332 \r
333                 $ret = OCIcommit($this->conn_id);\r
334                 $this->_commit = OCI_COMMIT_ON_SUCCESS;\r
335                 return $ret;\r
336         }\r
337 \r
338         // --------------------------------------------------------------------\r
339 \r
340         /**\r
341          * Rollback Transaction\r
342          *\r
343          * @access      public\r
344          * @return      bool            \r
345          */     \r
346         function trans_rollback()\r
347         {\r
348                 if ( ! $this->trans_enabled)\r
349                 {\r
350                         return TRUE;\r
351                 }\r
352 \r
353                 // When transactions are nested we only begin/commit/rollback the outermost ones\r
354                 if ($this->_trans_depth > 0)\r
355                 {\r
356                         return TRUE;\r
357                 }\r
358 \r
359                 $ret = OCIrollback($this->conn_id);\r
360                 $this->_commit = OCI_COMMIT_ON_SUCCESS;\r
361                 return $ret;\r
362         }\r
363 \r
364         // --------------------------------------------------------------------\r
365 \r
366         /**\r
367          * Escape String\r
368          *\r
369          * @access  public\r
370          * @param   string\r
371          * @return  string\r
372          */\r
373         function escape_str($str)\r
374         {\r
375                 // Access the CI object\r
376                 $CI =& get_instance();\r
377 \r
378                 return $CI->_remove_invisible_characters($str);\r
379         }\r
380 \r
381         // --------------------------------------------------------------------\r
382 \r
383         /**\r
384          * Affected Rows\r
385          *\r
386          * @access  public\r
387          * @return  integer\r
388          */\r
389         function affected_rows()\r
390         {\r
391                 return @ocirowcount($this->stmt_id);\r
392         }\r
393 \r
394         // --------------------------------------------------------------------\r
395 \r
396         /**\r
397          * Insert ID\r
398          *\r
399          * @access  public\r
400          * @return  integer\r
401          */\r
402         function insert_id()\r
403         {\r
404                 // not supported in oracle\r
405                 return $this->display_error('db_unsupported_function');\r
406         }\r
407 \r
408         // --------------------------------------------------------------------\r
409 \r
410         /**\r
411          * "Count All" query\r
412          *\r
413          * Generates a platform-specific query string that counts all records in\r
414          * the specified database\r
415          *\r
416          * @access  public\r
417          * @param   string\r
418          * @return  string\r
419          */\r
420         function count_all($table = '')\r
421         {\r
422                 if ($table == '')\r
423                         return '0';\r
424 \r
425                 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));\r
426 \r
427                 if ($query == FALSE)\r
428                         {\r
429                         return 0;\r
430                         }\r
431 \r
432                 $row = $query->row();\r
433                 return $row->NUMROWS;\r
434         }\r
435 \r
436         // --------------------------------------------------------------------\r
437 \r
438         /**\r
439          * Show table query\r
440          *\r
441          * Generates a platform-specific query string so that the table names can be fetched\r
442          *\r
443          * @access  private\r
444          * @param       boolean\r
445          * @return  string\r
446          */\r
447         function _list_tables($prefix_limit = FALSE)\r
448         {\r
449                 $sql = "SELECT TABLE_NAME FROM ALL_TABLES";\r
450 \r
451                 if ($prefix_limit !== FALSE AND $this->dbprefix != '')\r
452                 {\r
453                         $sql .= " WHERE TABLE_NAME LIKE '".$this->dbprefix."%'";\r
454                 }\r
455                 \r
456                 return $sql;\r
457         }\r
458 \r
459         // --------------------------------------------------------------------\r
460 \r
461         /**\r
462          * Show column query\r
463          *\r
464          * Generates a platform-specific query string so that the column names can be fetched\r
465          *\r
466          * @access  public\r
467          * @param   string  the table name\r
468          * @return  string\r
469          */\r
470         function _list_columns($table = '')\r
471         {\r
472                 return "SELECT COLUMN_NAME FROM all_tab_columns WHERE table_name = '$table'";\r
473         }\r
474 \r
475         // --------------------------------------------------------------------\r
476 \r
477         /**\r
478          * Field data query\r
479          *\r
480          * Generates a platform-specific query so that the column data can be retrieved\r
481          *\r
482          * @access  public\r
483          * @param   string  the table name\r
484          * @return  object\r
485          */\r
486         function _field_data($table)\r
487         {\r
488                 return "SELECT * FROM ".$table." where rownum = 1";\r
489         }\r
490 \r
491         // --------------------------------------------------------------------\r
492 \r
493         /**\r
494          * The error message string\r
495          *\r
496          * @access  private\r
497          * @return  string\r
498          */\r
499         function _error_message()\r
500         {\r
501                 $error = ocierror($this->conn_id);\r
502                 return $error['message'];\r
503         }\r
504 \r
505         // --------------------------------------------------------------------\r
506 \r
507         /**\r
508          * The error message number\r
509          *\r
510          * @access  private\r
511          * @return  integer\r
512          */\r
513         function _error_number()\r
514         {\r
515                 $error = ocierror($this->conn_id);\r
516                 return $error['code'];\r
517         }\r
518         \r
519         // --------------------------------------------------------------------\r
520 \r
521         /**\r
522          * Escape the SQL Identifiers\r
523          *\r
524          * This function escapes column and table names\r
525          *\r
526          * @access      private\r
527          * @param       string\r
528          * @return      string\r
529          */\r
530         function _escape_identifiers($item)\r
531         {\r
532                 if ($this->_escape_char == '')\r
533                 {\r
534                         return $item;\r
535                 }\r
536         \r
537                 if (strpos($item, '.') !== FALSE)\r
538                 {\r
539                         $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;                    \r
540                 }\r
541                 else\r
542                 {\r
543                         $str = $this->_escape_char.$item.$this->_escape_char;\r
544                 }\r
545                 \r
546                 // remove duplicates if the user already included the escape\r
547                 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);\r
548         }\r
549         \r
550         // --------------------------------------------------------------------\r
551 \r
552         /**\r
553          * From Tables\r
554          *\r
555          * This function implicitly groups FROM tables so there is no confusion\r
556          * about operator precedence in harmony with SQL standards\r
557          *\r
558          * @access      public\r
559          * @param       type\r
560          * @return      type\r
561          */\r
562         function _from_tables($tables)\r
563         {\r
564                 if ( ! is_array($tables))\r
565                 {\r
566                         $tables = array($tables);\r
567                 }\r
568                 \r
569                 return implode(', ', $tables);\r
570         }\r
571 \r
572         // --------------------------------------------------------------------\r
573         \r
574         /**\r
575          * Insert statement\r
576          *\r
577          * Generates a platform-specific insert string from the supplied data\r
578          *\r
579          * @access  public\r
580          * @param   string  the table name\r
581          * @param   array   the insert keys\r
582          * @param   array   the insert values\r
583          * @return  string\r
584          */\r
585         function _insert($table, $keys, $values)\r
586         {\r
587         return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";\r
588         }\r
589 \r
590         // --------------------------------------------------------------------\r
591 \r
592         /**\r
593          * Update statement\r
594          *\r
595          * Generates a platform-specific update string from the supplied data\r
596          *\r
597          * @access      public\r
598          * @param       string  the table name\r
599          * @param       array   the update data\r
600          * @param       array   the where clause\r
601          * @param       array   the orderby clause\r
602          * @param       array   the limit clause\r
603          * @return      string\r
604          */\r
605         function _update($table, $values, $where, $orderby = array(), $limit = FALSE)\r
606         {\r
607                 foreach($values as $key => $val)\r
608                 {\r
609                         $valstr[] = $key." = ".$val;\r
610                 }\r
611                 \r
612                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;\r
613                 \r
614                 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';\r
615         \r
616                 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);\r
617 \r
618                 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';\r
619 \r
620                 $sql .= $orderby.$limit;\r
621                 \r
622                 return $sql;\r
623         }\r
624 \r
625         // --------------------------------------------------------------------\r
626 \r
627         /**\r
628          * Truncate statement\r
629          *\r
630          * Generates a platform-specific truncate string from the supplied data\r
631          * If the database does not support the truncate() command\r
632          * This function maps to "DELETE FROM table"\r
633          *\r
634          * @access      public\r
635          * @param       string  the table name\r
636          * @return      string\r
637          */     \r
638         function _truncate($table)\r
639         {\r
640                 return "TRUNCATE TABLE ".$table;\r
641         }\r
642         \r
643         // --------------------------------------------------------------------\r
644 \r
645         /**\r
646          * Delete statement\r
647          *\r
648          * Generates a platform-specific delete string from the supplied data\r
649          *\r
650          * @access      public\r
651          * @param       string  the table name\r
652          * @param       array   the where clause\r
653          * @param       string  the limit clause\r
654          * @return      string\r
655          */     \r
656         function _delete($table, $where = array(), $like = array(), $limit = FALSE)\r
657         {\r
658                 $conditions = '';\r
659 \r
660                 if (count($where) > 0 OR count($like) > 0)\r
661                 {\r
662                         $conditions = "\nWHERE ";\r
663                         $conditions .= implode("\n", $this->ar_where);\r
664 \r
665                         if (count($where) > 0 && count($like) > 0)\r
666                         {\r
667                                 $conditions .= " AND ";\r
668                         }\r
669                         $conditions .= implode("\n", $like);\r
670                 }\r
671 \r
672                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;\r
673         \r
674                 return "DELETE FROM ".$table.$conditions.$limit;\r
675         }\r
676 \r
677         // --------------------------------------------------------------------\r
678 \r
679         /**\r
680          * Limit string\r
681          *\r
682          * Generates a platform-specific LIMIT clause\r
683          *\r
684          * @access  public\r
685          * @param   string  the sql query string\r
686          * @param   integer the number of rows to limit the query to\r
687          * @param   integer the offset value\r
688          * @return  string\r
689          */\r
690         function _limit($sql, $limit, $offset)\r
691         {\r
692                 $limit = $offset + $limit;\r
693                 $newsql = "SELECT * FROM (select inner_query.*, rownum rnum FROM ($sql) inner_query WHERE rownum < $limit)";\r
694 \r
695                 if ($offset != 0)\r
696                 {\r
697                         $newsql .= " WHERE rnum >= $offset";\r
698                 }\r
699 \r
700                 // remember that we used limits\r
701                 $this->limit_used = TRUE;\r
702 \r
703                 return $newsql;\r
704         }       \r
705 \r
706         // --------------------------------------------------------------------\r
707 \r
708         /**\r
709          * Close DB Connection\r
710          *\r
711          * @access  public\r
712          * @param   resource\r
713          * @return  void\r
714          */\r
715         function _close($conn_id)\r
716         {\r
717                 @ocilogoff($conn_id);\r
718         }\r
719 \r
720 \r
721 }\r
722 \r
723 \r
724 \r
725 /* End of file oci8_driver.php */\r
726 /* Location: ./system/database/drivers/oci8/oci8_driver.php */