Location: PHPKode > scripts > Abstract SQL Table to Object > abstract-sql-table-to-object/coredb.class.php
<?php

include dirname(__FILE__)."/adodb/adodb.inc.php";

/*
    Common core class.
    This class provide generic access to databases
*/

class CoreDB
{

    var $db;

    
    /**
     * CoreDB class constructor
     *
     * @param string $driver
     *          Example:
     *                   "mysql://<db_user>:<db_pass>@<db_server>/<db_database>"
     */
    function CoreDB($driver)
    {
        $this->db = &ADONewConnection($driver);
    }


    /**
     * Check if there was DB error and if so than die with displaying SQL error
     *
     */
    function ErrorDie($sql = null,$notdie = false)
    {
        if($this->db->ErrorNo())
        {
            $errtxt = "SQL error: ".$this->db->ErrorMsg().("" != $sql ? "\n<hr />\n{$sql}" : "");
            if($notdie != true){
            #echo '<!--';
            echo($errtxt);
            #echo '-->';
                die();
            }else{
                return 1;
            }
        }
    }


    function getTableFields($table = false,$noincr_id_fld = true){
        $r = $this->getAll("SHOW FIELDS FROM `$table`");

        $this->ErrorDie($sql);

        if($noincr_id_fld == true){
            $tmp = array();
            if(is_array($r)){
                foreach($r as $k => $v){
                    if($v['Extra'] != 'auto_increment'){
                        $tmp[] = $v;
                    }
                }
        
            }
            $r = $tmp;
        }

        if($noparentid == true){
            $tmp = array();
            if(is_array($r)){
                foreach($r as $k => $v){
                    if($v['Field'] != 'parent_id'){
                        $tmp[] = $v;
                    }
                }
        
            }
            $r = $tmp;
        }



        return $r;
    }



    /**
     * getOne returns one value result from SQL command execution
     *
     * @param string $sql
     * @return mixed value
     */
    function getOne($sql)
    {
        $r = $this->db->getOne($sql);
        $this->ErrorDie($sql);
        return $r;
    }


    /**
     * getRow return DB row as named, indexed or both type array
     *
     * @param string  $sql - SQL command to execute
     * @param integet $mode 0 - Show named columns (default);
                            1 - Show indexes columns
                            2 - Show indexes & named columns
     * @return result array
     */
    function getRow($sql, $mode = 0)
    {
        $this->db->SetFetchMode(0 == $mode ? ADODB_FETCH_ASSOC : (1 == $mode ? ADODB_FETCH_NUM : ADODB_FETCH_BOTH));
        $r = $this->db->getRow($sql);
        $this->ErrorDie($sql);
        return $r;
    }
    
    /**
     * Return two dimentional array of the result from the sql command
     *
     * @param string  $sql - SQL command to execute
     * @param integet $mode 0 - Show named columns (default);
                            1 - Show indexes columns
                            2 - Show indexes & named columns
     * @param array $arr - array values (?, ?)
     * @param integer $numrows - return $numrows records
     * @param integer $offset - start from $offset record

     * @return two dimentional result array 
     */
    function getAll($sql, $mode = 0, $arr = false, $numrows=-1, $offset=-1)
    {
        $this->db->SetFetchMode(0 == $mode ? ADODB_FETCH_ASSOC : (1 == $mode ? ADODB_FETCH_NUM : ADODB_FETCH_BOTH));

        if(-1 == $numrows  &&  -1 == $offset)
        {
            $r = $this->db->getAll($sql);
        }
        else 
        {
            $w = $this->db->SelectLimit($sql, $numrows, $offset, $arr);
            $this->ErrorDie($sql);
            $r = array();
            while($arr = $w->FetchRow())
            {
                $r[] = $arr;
            }
        }
        $this->ErrorDie("general getAll error in : $sql");
        return $r;
    }
    
    
    /**
     * Return last inserted ID
     *
     * @return last record ID
     */
    function getInsertID()
    {
        return $this->db->Insert_ID();
    }


    /**
     * Function do common SQL request. Available commands are INSERT/DELETE/UPDATE/SELECT
     *
     * @param string $sql
     * @param integer $mode
     * @param array $prm
     * @return return 0 if it was UPDATE/DELETE
               return ID of last inserted record if there was INSERT command executed
               return result array if SELECT command was given
     */
    function Exec($sql, $mode = 0, $prm = null)
    {
        $w = split(" ", $sql);
        $fl = 0;
        $result = "";
        if("insert" == trim(strtolower($w[0])))
        {
            $fl = 1;
        } else 
        if("select" == trim(strtolower($w[0])))
        {
            $fl = 2;
        } 

        
        $this->db->SetFetchMode(0 == $mode ? ADODB_FETCH_ASSOC : (1 == $mode ? ADODB_FETCH_NUM : ADODB_FETCH_BOTH));

        if(null != $prm)
        {
            $csql = $this->db->Prepare($sql);
            $result = array();
            foreach ($prm as $p)
            {
                if(2 == $fl)
                {
                    $res = $this->db->Execute($csql, $p);
                    $result[] = $res->FetchRow();
                }
                else
                {
                    $result[] = $this->db->Execute($csql, $p);
                }
            }
            return $result;
        }
        else
        {
            $result = $this->db->Execute($sql);
        }
        
        $this->ErrorDie($sql);

        // -- Check if there was SELECT command
        if("select" == trim(strtolower($w[0])))
        {
            echo "<b>".$result->RecordCount()."</B>";
            $res = array();
            while($arr = $result->FetchRow())
            {
                $res[] = $arr;
                $result->MoveNext();
            }
            $result = $res;
        }
        else
            $result = 0;

        return (1 == $fl) ? $this->getInsertID() : $result;
    }

    function testExec($sql, $mode = 0, $prm = null)
    {
        $w = split(" ", $sql);
        $fl = 0;
        $result = "";
        if("insert" == trim(strtolower($w[0])))
        {
            $fl = 1;
        } else
        if("select" == trim(strtolower($w[0])))
        {
            $fl = 2;
        }


        $this->db->SetFetchMode(0 == $mode ? ADODB_FETCH_ASSOC : (1 == $mode ? ADODB_FETCH_NUM : ADODB_FETCH_BOTH));

        if(null != $prm)
        {
            $csql = $this->db->Prepare($sql);
            $result = array();
            foreach ($prm as $p)
            {
                if(2 == $fl)
                {
                    $res = $this->db->Execute($csql, $p);
                    $result[] = $res->FetchRow();
                }
                else
                {
                    $result[] = $this->db->Execute($csql, $p);
                }
            }
            return $result;
        }
        else
        {
            $result = $this->db->Execute($sql);
        }

        return $this->ErrorDie($sql,true);

    }


};


?>
Return current item: Abstract SQL Table to Object