Sources for file plugins/database/pgsql.php in version 2.0



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

/*
 * Project:     Onyx: PHP Application Backend
 * File:        plugins/database/pgsql.php
 *              Database Plugin - PostgreSQL
 *
 * Version:     2.0
 * CVS tag:     $Id: pgsql.php,v 1.24 2003/05/31 08:29:39 bok Exp $
 * Author:      Robert Amos <bok[at]ausmac.net>
 *              Andrew Wellington <proton[at]wiretapped.net>
 * Copyright:   2001,2002,2003 odynia.org.
*/

class db_plugin {

    
// initial variables
    
var $_connections = array();    // database connections
    
var $_resources = array();    // misc resources


    /**
      * Method:            _connect
     * Description:    Connect to the PostgreSQL server
     * Arguments:        string     - database name
     *                     string     - server name    (optional)
     *                        integer     - port number    (optional)
     *                        string    - username        (optional)
     *                        string     - password        (optional)
     * Returns:            resource, or false
    **/
    
function _connect ($dbname$server=NULL$port=NULL$user=NULL$passwd=NULL) {

        global 
$onyx_parent;

        
// check first
        
if (empty($dbname))
            return 
false;

        
// do we have a connection to this db open already?
        
if (isset($this->_connections[$dbname]))
            return 
$this->_connections[$dbname];

        
// set up PostgreSQL connection string, add each item if its there
        
$connect_string "";
        if (!
is_null($server)) {
              
$connect_string .= "host=".$server;
        }
        if (!
is_null($port)) {
              
$connect_string .= " port=".$port;
        }
        if (!
is_null($user)) {
              
$connect_string .= " user=".$user;
        }
        if (!
is_null($passwd)) {
              
$connect_string .= " password=".$passwd;
        }
        
$connect_string .= " dbname=".$dbname;

      
// Open connection
        
if ($onyx_parent->use_persistent_db_conns)
         
$conn = @pg_pconnect($connect_string);
        else
          
$conn = @pg_connect($connect_string);

        
// bad connection, set error
        
if (!$conn) {
            
$onyx_parent->trigger_error('Unable to establish connection to PostgerSQL Server:
'
.$connect_stringWARNING);
            return 
false;
        }

        
// set the resource id into the database for later use
        
$this->_connections[$dbname] = $conn;

        
// return it so the calling function can use it
        
return $this->_connections[$dbname];
    }

    
/**
     * Method:            select
     * Description:    Select the appropriate database to connect to
     * Arguments:        string - database name
     * Returns:            resource, or false
    **/ 
    
function select ($dbname) {

        
// check
        
if (empty($dbname))
            return 
false;

        
// already using warez?
        
if (isset($this->_connections[$dbname])) {
            
$this->_resources['db_select'] = $this->_connections[$dbname];
            return 
$this->_connections[$dbname];
        }

        
// mummy!
        
global $onyx_parent;

        
// set variable stuffs
        
$server = ( empty($onyx_parent->db_host) ? NULL $onyx_parent->db_host );
        
$port = ( empty($onyx_parent->db_port) ? NULL $onyx_parent->db_port );
        
$user = ( empty($onyx_parent->db_user) ? NULL $onyx_parent->db_user );
        
$passwd = ( empty($onyx_parent->db_passwd) ? NULL $onyx_parent->db_passwd );

        
// connect!
        
if (!$this->_connect($dbname$server$port$user$passwd))
            return 
false;

        
// set this as the latest resource
        
$this->_resources["db_select"] = $this->_connections[$dbname];

        
// done!
        
return $this->_connections[$dbname];

    }


    
/**
      * Method:            exec
     * Description:    Execute given SQL query against the database
     * Arugments:        string     - sql query
     *                        resource    - Connection Resource    (optional)
     * Returns:            resource, or false
    **/ 
    
function exec ($sql$res=NULL) {

        
// they better give us an SQL query or there'll be trouble!
        
if (empty($sql))
            return 
false;

        
// check magical juarez
        
if (is_null($res))
            
$res $this->_resources["db_select"];
        if (empty(
$res))
            return 
false;

        
// exec the query i guess, not much else to do
        
$result = @pg_query($res$sql);        

        
// problems?
        
if (!$result)
        {
            global 
$onyx_parent;
            
$this->error = @pg_last_error($res);
            
$onyx_parent->trigger_error('SQL Query failed. PostgreSQL returned: '.$this->error.' (SQL:
'
.$sql.')'WARNING);
        } else {
            unset(
$this->error);
        }

        
// more magic
        
$this->_resources["db_exec"] = $result;

        
// return the result index
        
return $result;
    }

    
/**
     * Method:            query
     * Description:    Execute given SQL query against the database
     * Arguments:        string    - SQL query
     *                        resource    - Connection Resource (optional)
     * Returns:            resource, or false
     * Note:                This method is simply a wrapper for $db->exec();
    **/ 
    
function query ($sql$res=NULL) {
        return 
$this->exec($sql$res);
    }

    
/**
     * Method:            fetch
     * Description:    Fetch the results of a $db->exec();
     * Arguments:        integer    - row number        (optional)
     *                        string    - column name        (optional)
     *                        resource    - Result Resource    (optional)
     * Returns:            mixed, or false
    **/ 
    
function fetch ($row=NULL$column=NULL$res=NULL) {

        
// do checking stuffs
        
if (!is_null($row) && !is_null($column))
            return 
$this->fetch_result($row$column$res);

        if (!
is_null($row) && is_null($column))
            return 
$this->fetch_array($row$res);

        if (
is_null($row) && !is_null($column))
            return 
$this->fetch_column($column$res);

        return 
$this->fetch_all_array($res);

        
// wow that was hard, huh?
    
}

    
/**
     * Method:            fetch_all
     * Description:    Fetch the results of an execas an array of objs
     * Arguments:        resource    - Result Resource (optional)
     * Returns:            array, or false
    **/ 
    
function fetch_all ($res=NULL) {
        return 
$this->fetch_all_array($res);
    }

    
/**
     * Method:            fetch_all_objects
     * Description:    Fetch the results of an execas an array of objs
     * Arguments:        resource    - result resource (optional)
     * Returns:            array, or false
    **/ 
    
function fetch_all_objects ($res=NULL) {

        
// juarez?
        
if (is_null($res))
            
$res $this->_resources["db_exec"];
        if (empty(
$res))
            return 
false;

        
// loopy? crazy? damn these comments are boring
        // loop through the rows, grab them all
        
$num $this->num_rows($res);
        for (
$i 0;$i $num;$i++)
            
$result[] = @pg_fetch_object($res$iPGSQL_ASSOC);
        
        
// we warezish arent we? :P
        
return $result;
    }

    
/**
     * Method:            fetch_all_array
     * Description:    Fetch the results of an execas an arrayofarrays
     * Arguments:        resource    - Results Resoruce (optional)
     * Returns:            array, or false
    **/ 
    
function fetch_all_array ($res=NULL) {

        
// juarez?
        
if (is_null($res))
            
$res $this->_resources["db_exec"];
        if (empty(
$res))
            return 
false;

        
// loopy? crazy? damn these comments are boring
        // loop through the rows, grab them all
        
$num $this->num_rows($res);
        for (
$i 0;$i $num;$i++)
            
$result[] = @pg_fetch_array($res$iPGSQL_ASSOC);
        
        
// we warezish arent we? :P
        
return $result;
    }

    
/**
     * Method:            fetch_object
     * Description:    Fetch the specified row as an object.
     * Arguments:        integer    - row number
     *                        resource - result resource (optional)
     * Returns:            array, or false
    **/ 
    
function fetch_object ($row$res=NULL) {

        
// juarez?
        
if (is_null($res))
            
$res $this->_resources["db_exec"];
        if (empty(
$res))
            return 
false;

        
// wow, harsh
        
$result = @pg_fetch_object($res$row);
        
        
// we warezish arent we? :P
        
return $result;
    }

    
/**
     * Method:            fetch_array
     * Description:    Fetch the specified row as an array.
     * Arguments:        integer    - row number
     *                        resource    - result resource (optional)
     * Returns:            array, or false
    **/ 
    
function fetch_array ($row$res=NULL) {

        
// juarez?
        
if (is_null($res))
            
$res $this->_resources["db_exec"];
        if (empty(
$res))
            return 
false;

        
// wow, harsh
        
$result = @pg_fetch_array($res$rowPGSQL_ASSOC);
        
        
// we warezish arent we? :P
        
return $result;
    }

    
/**
     * Method:            fetch_column
     * Description:    Fetch the specified column as an array.
     * Arguments:        string    - column name
     *                        resource    - result resource (optional)
     * Returns:            array, or false
    **/ 
    
function fetch_column ($column$res=NULL) {

        if (empty(
$column)) 
            return 
false;

        
// juarez?
        
if (is_null($res))
            
$res $this->_resources["db_exec"];
        if (empty(
$res))
            return 
false;

        
// loopy? crazy? damn these comments are boring
        // loop through the rows, grab them all
        
$num $this->num_rows($res);
        for (
$i 0;$i $num;$i++)
            
$result[] = @pg_fetch_result($res$i$column);
        
        
// we warezish arent we? :P
        
return $result;
    }

    
/**
     * Method:            fetch_result
     * Description:    Fetch the specified result as a mixed!.
     * Arguments:        integer    - row number
     *                        string    - column name
     *                        resource - result resource (optional)
     * Returns:            mixed, or false
    **/ 
    
function fetch_result ($row$column$res=NULL) {

        if (empty(
$column) || !is_int($row)) 
            return 
false;

        
// juarez?
        
if (is_null($res))
            
$res $this->_resources["db_exec"];
        if (empty(
$res))
            return 
false;

        
// ouch, how did i ever come up with this function?
        
$result = @pg_fetch_result($res$row$column);

        
// we warezish arent we? :P
        
return $result;
    }
    
    
/**
     * Method:            num_rows
     * Description:    Get the number of rows returned in a select
     * Arguments:        resource    - result resource    (optional)
     * Returns:            integer, or false    
    **/ 
    
function num_rows ($res=NULL) {

        
// phwoar
        
if (is_null($res))
            
$res $this->_resources["db_exec"];
        if (empty(
$res))
            return 
false;

        
// I don't know how I do it, I'm just so amazing
        
$result pg_num_rows($res);

        return 
$result;
    }

    
/**
     * Method:            affected_rows
     * Description:    Get the number of rows affected by a query
     * Arguments:        resource    - result resource (optional)
     * Returns:            integer, or false
    **/ 
    
function affected_rows ($res=NULL) {

        
// phwoar || dude!
        
if (is_null($res))
            
$res $this->_resources["db_exec"];
        if (empty(
$res))
            return 
false;

        
// I don't know how I do it, I'm just so amazing || I fear j00.
        
$result = @pg_affected_rows($res);

        return 
$result;
    }

    
/**
     * Method:            spec
     * Description:    load a database spec file and hand it back
     * Arguments:        string    - table name
     *                        boolean    - connect to the database automatically or not (optional)
     * Returns:            object containing the table specs
    **/ 
    
function spec ($table$connect=true) {

        global 
$onyx_parent;

        
// load the juarez, d00d!
        
$spec $onyx_parent->load_plugin($table'dbspec');

        
// connect to the database if we're asked to
        
if ($connect && isset($spec->_database))
            
$this->select($spec->_database);

        return 
$spec;
    }

    
/**
     * Function:        get_error
     * Description:    Get last error from result resource
     * Arguments:        resource    - connection resource (optional)
     * Returns:            string or false
    **/
    
function get_error () {
    
        
// just return it
        
return $this->error;
    }

}

?>


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