3 * Oracle 8i database connection/querying layer
5 * ALPHA VERSION - not debugged!!
7 * SourceForge: Breaking Down the Barriers to Open Source Development
8 * Copyright 1999-2001 (c) VA Linux Systems
9 * http://sourceforge.net
14 * An array of an array of associative arrays
15 * containing row data from queries (3D array)
17 * Say that 50 times backwards while standing on your head and you
18 * get a free SourceForge cookie!
20 * @var array $sys_db_oci_commit_mode
22 $sys_db_oci_commit_mode='OCI_COMMIT_ON_SUCCESS';
25 * db_connect() - Connect to the database
27 * Notice the global vars that must be set up
28 * Sets up a global $gfconn variable which is used
29 * in other functions in this library
31 function db_connect() {
33 $gfconn = @ocilogon(forge_get_config('database_user'),forge_get_config('database_password'),forge_get_config('database_name'));
38 * db_query() - Query the database
40 * NOTE - the OCI version of this may be somewhat inefficient
41 * for large result sets (hundreds or thousands of rows selected)
42 * However - most queries are returning 25-50 rows
44 * @param string SQL statement
45 * @param int How many rows do you want returned
46 * @param int Of matching rows, return only rows starting here
48 function db_query($qstring,$limit='-1',$offset=0) {
49 global $QUERY_COUNT,$sys_db_results,$sys_db_row_pointer,$sys_db_oci_commit_mode;
52 $stmt=@ociparse($gfconn,$qstring);
57 if (!$offset || $offset < 0) {
62 $res=@ociexecute($stmt,$sys_db_oci_commit_mode);
67 //if offset, seek to starting point
68 //potentially expensive if large offset
69 //however there is no data_seek feature AFAICT
72 for ($i=0; $i<$offset; $i++) {
74 @ocifetchinto($res,$x);
76 //if no data be returned
87 @ocifetchinto($res,$x,'OCI_ASSOC+OCI_RETURN_NULLS+OCI_RETURN_LOBS');
88 $sys_db_results[$res][$i-1]=$x;
90 //see if data is being returned && we are
91 //still within the requested $limit
92 if (count($x) < 1 || (($limit > 0) && ($i >= $limit))) {
96 $sys_db_row_pointer[$res]=0;
103 * db_begin() - Begin a transaction
105 function db_begin() {
106 global $sys_db_oci_commit_mode;
107 $sys_db_oci_commit_mode='OCI_DEFAULT';
111 * db_commit() - Commit a transaction
113 function db_commit() {
114 global $sys_db_oci_commit_mode,$gfconn;
115 $sys_db_oci_commit_mode='OCI_COMMIT_ON_SUCCESS';
116 return ocicommit($gfconn);
120 * db_rollback() - Rollback a transaction
122 function db_rollback() {
123 global $sys_db_oci_commit_mode,$gfconn;
124 $sys_db_oci_commit_mode='OCI_COMMIT_ON_SUCCESS';
125 return ocirollback($gfconn);
129 * db_numrows() - Returns the number of rows in this result set
131 * @param string Query result set handle
133 function db_numrows($qhandle) {
134 global $sys_db_results;
135 // return only if qhandle exists, otherwise 0
137 return @count($sys_db_results[$qhandle]);
144 * db_free_result() - Frees a database result properly
146 * @param string Query result set handle
148 function db_free_result($qhandle) {
149 global $sys_db_results;
150 unset($sys_db_results[$qhandle]);
151 return @ocifreestatement($qhandle);
155 * db_reset_result() - Reset a result set
157 * Reset is useful for db_fetch_array
158 * sometimes you need to start over
160 * @param string Query result set handle
161 * @param int Row number
163 function db_reset_result($qhandle,$row=0) {
164 global $sys_db_row_pointer;
165 return $sys_db_row_pointer[$qhandle]=$row;
169 * db_result() - Returns a field from a result set
171 * @param string Query result set handle
172 * @param int Row number
173 * @param string Field name
175 function db_result($qhandle,$row,$field) {
176 global $sys_db_results;
177 return $sys_db_results[$qhandle][$row]["$field"];
181 * db_numfields() - Returns the number of fields in this result set
183 * @param sting Query result set handle
185 function db_numfields($lhandle) {
186 return @ocinumcols($lhandle);
190 * db_fieldname() - Returns the number of rows changed in the last query
192 * @param string Query result set handle
193 * @param int Column number
195 function db_fieldname($lhandle,$fnumber) {
196 return @ocicolumnname($lhandle,$fnumber);
200 * db_affected_rows() - Returns the number of rows changed in the last query
202 * @param string Query result set handle
204 function db_affected_rows($qhandle) {
205 return @ocirowcount($qhandle);
209 * db_fetch_array() - Fetch an array
211 * Returns an associative array from
212 * the current row of this database result
213 * Use db_reset_result to seek a particular row
215 * @param string Query result set handle
217 function db_fetch_array($qhandle) {
218 global $sys_db_results,$sys_db_row_pointer;
219 return $sys_db_results[$qhandle][$sys_db_row_pointer++];
223 * db_insertid() - Returns the last primary key from an insert
225 * @param string Query result set handle
226 * @param string The name of the table you inserted into
227 * @param string The field name of the primary key
229 function db_insertid($qhandle,$table_name,$pkey_field_name) {
230 $res=db_query("SELECT max($pkey_field_name) AS id FROM $table_name");
231 if ($res && db_numrows($res) > 0) {
232 return @db_result($res,0,'id');
239 * db_error() - Returns the last error from the database
241 function db_error() {
243 $err= @ocierror($gfconn);
245 return $err['message'];