Location: PHPKode > scripts > Abstract SQL Table to Object > abstract-sql-table-to-object/j-coredb.class.php
<?php
/*
	Database interface wrapper for Joomla 1.12 .
	By Sergey Shilko 28.03.2007
	Rev.: 0.1
*/


class j__db{
	var $db;

	
	function j__db($joomladbobj = false){
		if($joomladbobj == false){
			global $database;
			$joomladbobj =& $database;
		}

		if(!is_object($joomladbobj)){die('Must pass joomla`s database object to db wrapper');}
		$this->db =& $joomladbobj;
	}
	
	
    function ErrorDie($sql)
    {
        if($this->db->getErrorNum())
        {
            $dberrmsg = $this->db->stderr();
            $errmsg = ereg_replace("[^a-zA-Z_0-9,() ]",'',$sql);
            echo '<center>';
            $newmsg2 = "<h2 style='color:red;'>$dberrmsg</h2>\r\n
                        <div style='border:1px solid;'>$sql</div>";
        echo "<div style=\"border:1px solid #000000; background-color: #CDCDCD;\">".("" == $comments ? "" : "<b>$comments</b>\n");
        echo($newmsg2);
        echo "\n<br /></div></center>";
        return false;
        }
    }
	
	
	/**
	* Load a list of database objects
	* @param string The field name of a primary key
	* @return array If <var>key</var> is empty as sequential list of returned records.
	* If <var>key</var> is not empty then the returned array is indexed by the value
	* the database key.  Returns <var>null</var> if the query fails.
	*/
	function loadObjectList($sql,$offset = 0,$limit = 0,$key = ''){
		$this->db->setQuery( $sql, $offset, $limit );
		$r = $this->db->loadObjectList($key);
        $this->ErrorDie($sql);
		return $r;
	}
	
	function getObjectList($sql,$offset = 0,$limit = 0,$key = ''){
		$r = $this->loadObjectList($sql,$offset,$limit,$key);
		return $r;
	}
	

    function getTableFields($table = array()){
    	if(!is_array($table)){return false;}
		$r = $this->db->getTableFields($table);
        $this->ErrorDie($sql);
        return $r;
    }



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


    /**
     * getRow return DB row as named, indexed or both type array
     *
     * @return result array
     */
    function getRow($sql, $offset = 0,$key = null,$limit = 1,$nozerparray = true)
    {
    	
    	$this->db->setQuery($sql,$offset,$limit);
		$r = $this->getAll($sql,$key,$offset,$limit);
    	if($nozerparray == true){$r = $r[0];}
        $this->ErrorDie($sql);
    	return $r;
    }
    
    /**
     * Return two dimentional array of the result from the sql command
     *
     * @param string  $sql - SQL command to execute
     * @return two dimentional result array 
     */
    function getAll($sql,$key = '',$offset = 0,$limit = 0)
    {
    	$this->db->setQuery($sql,$offset,$limit);
		$r = $this->db->loadAssocList($key);
        $this->ErrorDie($sql);
    	return $r;
    }
    
    
    /**
     * Return last inserted ID
     *
     * @return last record ID
     */
    function getInsertID()
    {
        return $this->db->insertid();
    }


    /**
     * 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)
    {
	  		  $this->db->setQuery($sql);
	  		  $r = $this->db->query();
	          $this->ErrorDie($sql);
    		  return $r;
    	
    }
	
}

?>
Return current item: Abstract SQL Table to Object