Sources for file plugins/database/oracle.php in version 2.0 Beta 4



Click on a comment to hide it. Click here to show all comments.
<?PHP

/*
 * Project:     Onyx: PHP Application Backend
 * File:        plugins/database/database.oracle.php
 *              Database Plugin - Oracle
 *
 * Version:     1.0
 * CVS tag:     $Id: oracle.php,v 1.2 2002/09/09 14:38:09 bok Exp $
 * Author:      Robert Amos <bok[at]ausmac.net>
 *              Andrew Wellington <proton[at]wiretapped.net>
 * Copyright:   2001,2002 Shiznatz Inc.
*/

class Oracle extends Database {



    
/**** Open Persistant connection to the database ****/
        /*
         * Syntax:
         *    $this->_connect(string database_name);
         *
         * Arguments:
         *    database name: String with the name of the database to connect to
         *
         * Returns:
         *    database resource id
        */
        
function _connect ($database_name) {

                
// if we already have a connection open, use it
        
if (isset($this->connection["$database_name"])) {
            return 
$this->connection["$database_name"];
        }

                
// we need a username and a password
                
if (empty($this->username) || empty($this->pass)) {
                        global 
$error;
                        
$error->error('DATABASE_ERROR''Incorrectly configured oracle database.
Username and Password need to be specified.'
);
                }

                
// open the connection
                
$conn = @ora_logon($this->database_username$this->password);

                
// return error if no connection established
                
if (!$conn) {
                        global 
$error;
                        
$error->error('DATABASE_ERROR''Error connecting to the database. The
Oracle server replied: '
.ora_errorcode($conn).': '.ora_error($conn));
                }

        
// set the resource id into the database for later use
        
$this->connection["$database_name"] = $conn;

        
// return it so the calling function can use it
        
return $this->connection["$database_name"];
    }

    
/**** Execute the given sql query ****/
    /*
     * Syntax:
     *        $database->exec(string sql, string database_name [, bool report]);
     *
     * Arugments:
     *        sql: String containing the SQL Query
     *        database_name: Database to run the SQL Query against
     *        report: optional boolean value to tell whether to report errors to the logs or not
     *
     * Returns:
     *        Resource id for the query
    */
    
function exec ($sql$database_name$report '1') {

        
// Open connection, or get the resource id of an already open one
        
$conn $this->_connect($database_name);

                
// open a resource cursor for the query, error if we cant and we're allowed to
report the error
                $cursor 
= @ora_open($conn);
                if (!
$cursor && !$report) {
                        global 
$error;
                        
$error->error('DATABASE_ERROR''Error opening the database cursor. The
oracle server said '
.ora_errorcode($conn).': '.ora_error($conn));
                }

                
// parse the sql query into the result, error if we cant and we're allowed to report
it
                $result 
= @ora_parse($sql$cursor);
                if (!
$result && !$report) {
                        global 
$error;
                        
$error->error('DATABASE_ERROR''Parsing of SQL query failed. the oracle
server replied: '
.ora_errorcode($conn).': '.ora_error($conn));
                }

                
// exec the query and get the result, error if necessary and we can
                
$result = @ora_exec($cursor);
                if (!
$result && !$report) {
                        global 
$error;
                        
$error->error('DATABASE_ERROR''Execution of SQL query failed. The oracle
server replied: '
.ora_errorcode($conn).': '.ora_error($conn));
                }

                
// return the cursor so other functions can use it
                
return $cursor;

    }

    
/**** Fetch the results of a select to an array ****/
    /*
     * Syntax:
     *         $database->fetch_array(int result_id, int row [, bool report]);
     *
     * Arguments:
     *        result_id: a result index, as returned by $database->exec()
     *        row: integer containing the row number to return
     *        report: optional boolean value specifing whether to report errors or not
     *
     * Returns:
     *         false: on error
     *        an array containing the requested row from the result specified
    */
    
function fetch_array ($result_id$row$report '1') {
        global 
$error;

        
// if there are no rows, dont do anything
        
if ($this->numrows($result_id) != "0") {

                        
// seeing as there is no way return one row only, return them all and break
when we have the one we
                        
// want
                        
for ($i 0;$i $this->numrows($result_id);$i++) {
                                
$result = array();
                                @
ora_fetch_into($result_id, &$resultORA_FETCHINTO_NULLS);
                                if (
$i == $row) {
                                        break;
                                }
                        }
                        
                        
// if no results, report error (if we're allowed to)(
                        
if ((count($result) == "0") && !$report) {
                                
$error->error('DATABASE_ERROR''Unable to fetch row $row from the
cursor.'
);
                        }
                        
                        
// return the result
                        
return $result;

        }
                
                
// no rows, cant return anything then can we?
                
return false;
    }
    
    
/**** Fetch results of a select into a multi-dimensional array containing row number and columns
***/
    /*
     * Syntax:
     *        $database->fetch_all(int result_id [, bool report]);
     * 
     * Arguments:
     *        result_id: a result index, as returned by $database->exec()
     *        report: optional boolean value specifing whether to report errors or not
     *
     * Returns:
     *         false: on error
     *        a multi-dimensional array containing the row number and columns
    */
    
function fetch_all ($result_id$report '1') {
        global 
$error;
                
                
// cycle the internal pointer and return the array into the current row-array
                
for ($i 0;$i $this->numrows($result_id);$i++) {
                        @
ora_fetch_into($result_id, &$result[$i], ORA_FETCHINTO_NULLS);
                }

                
// error if any problems
                
if ((count($result) == "0") && !$report) {
                        
$error->error('DATABASE_ERROR''Unable to fetch all $row from the cursor');
                }

        return 
$result;
    }


    
/**** Return a single result (row and column) from a select statement ****/
    /*
     * Syntax:
     *         $database->result(int result_id, int row, string cell_name [, bool report]);
     *
     * Returns:
     *        result_id: a result index, as returned by $database->exec()
     *        row: integer containing the row number to return
     *        cell_name: name of the column to fetch the result from
     *        report: optional boolean value specifing whether to report errors or not
     *
     * Returns:
     *         false: on error
     *        string containing the results of the specified cell
    */
    
function result ($result_id$row$cell_name$report '1') {
        global 
$error;

        
// if there is no rows, dont return any results, obviously
        
if ($this->numrows($result_id) != "0") {

                        
// get the array, return the right key, seeing as there is no built in
function
                        
$result $this->fetch_array($result_id$row);
                        if (!isset(
$result[$cell_name])) {
                                
$error->error('DATABASE_ERROR''Unable to retrieve contents of
$cell_name, row $row, $result_id.'
);
                        }
                        return 
$result[$cell_name];

        }
                
                return 
false;
    }

    
/**** Return the number of rows a select statement retrieved ****/
    /*
     * Syntax:
     *        $database->numrows(int result_id [, bool report]);
     *
     * Arguments:
     *        result_id: a result index, as returned by $database->exec()
     *        report: optional boolean value specifing whether to report errors or not
     *
     * Returns:
     *        false: on error
     *        an integer containing the number of results returned
    */
    
function numrows ($result_id$report '1') {
        global 
$error;

                
// use the oracle numrows function
                
$result = @ora_numrows ($result_id);

        
// return error if any of the above calls failed
        
if (!$result && !$report) {
            
$error->error('DATABASE_ERROR'"Unable to get number of rows in $result_id.");
        }

        
// return the number of rows
        
return $result;
    }


    
/**** Return the number of rows affected by the executed statement ****/
    /*
     * Syntax:
     *         $database->affectedrows(int result_id [, bool report]);
     *
     * Arguments:
     *        result_id: a result index, as returned by $database->exec()
     *        report: optional boolean value specifing whether to report errors or not
     *
     * Returns:
     *        false: on error
     *        an integer containing the number of rows affected
    */
    
function affectedrows ($result_id$report '1') {
        global 
$error;

                
// no appropriate function, error
                
$error->error('DATABASE_ERROR''Affected Rows is not supported by Oracle.');
    }

}

$database = new Database;

?>


Website is Copyright © Odynia.org 2000-2005 - Xnyo is released under a BSD license.