<?


/**
 * Greg MacLellan's Database Abstraction Layer
 * 
 * Currently only supports MySQL
 *
 * @author  Greg MacLellan
 * @version 0.4.4
 * @since   Last Revised: Sept 8, 2003
 * @package Database
*/
class DatabaseAbstract {
/*

Change log:

0.4.4 - Added DatabaseAbstract::numAffected()
0.4.3 - Started change log
     - sqlExec() can now return the last insert ID
*/

    /** Database Type
     * @access  private 
    */
    
var $dbtype;
    
/** database host
     * @access  private 
    */
    
var $host
    
/** username
     * @access  private 
    */
    
var $username;
    
/** password
     * @access  private 
    */
    
var $password;
    
/** current database name
     * @access  private 
    */
    
var $db;
    
/** connection handle
     * @access  private 
    */
    
var $cid;
    
/** array of result handles
     *
     * Used for the different $sets
     * @access  public 
     * @see sqlResult, getRow
    */
    
var $result;
    
/**
     * The last error that occured
     * @return string
    */
    
var $lasterror;    
    
/** 
     * Whether or not to use "debug mode". When debug mode is on, errors will
     * terminate the current script and display the failed operation and error 
     * message. If debug is off, on an error nothing will be displayed.
     * @return bool
    */
    
var $debug false
    
    
/**
     * Creates a new database object.
     * @param  string  $dbtype      The type of database to connect to
     * @param  string  $dbhost      The hostname of the database server
     * @param  string  $user        The username to connect with
     * @param  string  $pass        The password to use
     * @param  string  $database    The database to use (optional)
    */
    
function DatabaseAbstract($dbtype$dbhost$user$pass$database "") {
        if (
$dbtype != "mysql") {
            
$error "Database type $dbtype not supported";
            return 
false;
        }
        
        
$this->dbtype $dbtype;
        
$this->host $dbhost;
        
$this->username $user;
        
$this->password $pass;
        
$this->db $database;
        
$this->dbtype $dbtype;

        switch (
$this->dbtype) {
            case 
"mysql":
                
$this->cid = @ mysql_connect($dbhost$user$pass);
                if (!
$this->cid) {
                    
$this->error("mysql_connect()""Could not connect to database server.");
                    return 
false;
                }

                if (
$database) {
                    return 
$this->changeDB($database);
                } else {
                    return 
true;
                }
            break;
        }
    }
    
    
/**
     * Terminate script if we're running debug, otherwise just save the error
    */
    
function error($query$error) {
        
$this->lasterror $error;
        if (
$this->debug) {
            exit(
"<b>Error running query:</b><br><br>$query<br><br><b>Server said:</b><br><br>$error<br>");
        }
    }
    
    
/**
     * Switched to the specified database name.
     * @param  string  $database  The name of the database to use
     * @return bool   True if the database was successfully selected
    */
    
function changeDB($database) {
        switch (
$this->dbtype) {
            case 
"mysql":
                if (@
mysql_select_db($database$this->cid)) {
                    
$this->db $database;
                    return 
true;
                } else {
                    
$this->error("mysql_select_db()","Error switching to database $database");
                    return 
false;
                }
            break;
        }
    }
    
    
/**
     * Disconnect from the database server
     *
    */
    
function close() { 
        switch (
$this->dbtype) {
            case 
"mysql":
                return 
mysql_close($this->cid);
            break;
        }
    }

    
/**
     * Executes an SQL query without storing the result. Useful for INSERT, UPDATE, etc
     * @param  string  $sql       The SQL code to execute
     * @param  bool    $returnid  If true, returns the last insert ID (only supported
     *                            on mysql,)
     * @return The result handle, or if $returnid is true, the last insertid
    */
    
function sqlExec($sql$returnid false) {
        switch (
$this->dbtype) {
            case 
"mysql":
                
$result = @mysql_query($sql$this->cid);
                if (!
$result) {
                    
$this->error($sqlmysql_error());
                }
                return (
$returnid mysql_insert_id($this->cid) : $result);
            break;
        }
    }
    
    
/**
     * Executes an SQL query and stores the result. 
     * @param  string  $sql  The SQL code to execute
     * @param  string  $set  The optional set name to store the results in. By
     *                       default, the name is "default". Only one result
     *                       set can be stored with the same name at a time.
    */
    
function sqlResult($sql$set "default") {
        switch (
$this->dbtype) {
            case 
"mysql":
                if (!(
$this->result[$set] = @mysql_query($sql$this->cid))) {
                    
$this->error($sqlmysql_error());
                }
                return 
$this->result[$set];
            break; 
        }
    }
    
    
/**
     * Executes an SQL query and gets only one row
     * @param  string  $sql  The SQL code to execute
    */
    
function sqlRow($sql$type MYSQL_BOTH) {
        switch (
$this->dbtype) {
            case 
"mysql":
                if (!(
$result = @ mysql_query($sql$this->cid))) {
                    
$this->error($sqlmysql_error());
                }
                return @ 
mysql_fetch_array($result);
            break; 
        }
    }

    
/**
     * Puts the next row into an associative array.
     * @param  mixed  $set  Optional result set identifier. The set "default"
     *                      is used if none is specified.
     * @return An array containing the next row, or False if there are no more rows
    */
    
function getRow($set "default") {
        switch (
$this->dbtype) {
            case 
"mysql":
                return @ 
mysql_fetch_array($this->result[$set]);
            break;
        }
    }

    
/**
     * Finds the number of rows in the result set
     * @param  mixed  $set  Optional result set identifier. The set "default"
     *                      is used if none is specified.
     * @return The number of rows
    */
    
function numRows($set "default") {
        switch (
$this->dbtype) {
            case 
"mysql":
                return @ 
mysql_num_rows($this->result[$set]);
            break;
        }
    }
    
    
/**
     * Finds the number of affected rows in the result set
     * @param  mixed  $set  Optional result set identifier. The set "default"
     *                      is used if none is specified.
     * @return The affected number of rows
    */
    
function numAffected($set "default") {
        switch (
$this->dbtype) {
            case 
"mysql":
                return @ 
mysql_affected_rows($this->cid);
            break;
        }
    }

    
/** raw mysql_query */
    
function sqlResultRaw($sql) {
        switch (
$this->dbtype) {
            case 
"mysql":
                return 
mysql_query($sql$this->cid);
            break;
        } 
    }
    
    
/** raw mysql_fetch_array */
    
function getRowRaw($result) {
        switch (
$this->dbtype) {
            case 
"mysql":
                return 
mysql_fetch_array($result);
            break;
        }
    }
}
?>