Location: PHPKode > scripts > Class Database > class-database/class_database.php
<?php
/****************************************************************
*****************************************************************

Copyright (C) 2003  Matthieu MARY http://www.phplibrairies.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

You can found more information about GPL licence at:
http://www.gnu.org/licenses/gpl.html

for contact me: http://www.phplibrairies.com
****************************************************************
****************************************************************/
/**
 * @shortdesc help in managing DB acces with pear package DB
 * latest version can be download at http://www.phplibrairies.com
 * under GPL licence
 *
 * @author      Matthieu MARY
 * @version     1.1.0
 * @date      december 11th 2003
 **/
 
require_once 'DB.php';

class database
{
    /**
     * @shortdesc array of errors
     * @type array
     **/
    var $aErr;
    
    /**
     * @shortdesc PEAR DB object
     * @type mixed
     **/
    var $oDB;

    /**
     * @shortdesc does we are connected to db?
     * @type boolean
     **/
    var $bConnect;
    
    /**
     * @shortdesc result object
     * @type mixed
     **/
    var $oR;
    
    /**
     * @shortdesc values to connect
     * @type array
     **/
    var $dDatas;

    /**
     * @shortdesc builder
     * builder
     * @param array db : array of the following structure : 'type' : kind of server (eg. Mysql, Mssql,...)
     *                                                      'login' : login to acces to database
     *                                                      'mdp' : password for this login
     *                                                      'server' : name or ip of this server (eg. Localhost,...)
     *                                                      'db' : database name to connect
     * @type void
     * @public
     **/
    function database($db)
    {
        $this->aErr = array();
        $this->oDB = false;
        $this->bConnect = false;
        $this->oR = false;
        $this->dDatas = $db;

        $sDSN = $db['type'].'://'.$db['login'].':'.$db['mdp'].'@'.$db['server'].'/'.$db['db'];
		$this->_connect($sDSN);
    }

    /**
     * @shortdesc connect to database
     * connect to database
     * @param string sDSN : DSN sequence to connect
     * @private
     * @type void
     **/
    function _connect($sDSN)
    {
        $this->oDB = DB::connect($sDSN,TRUE);
		if (DB::iserror($this->oDB)){
		 return $this->_ERR_add('connection to ['.$this->dDatas['db'].'] => '.$this->oDB->getMessage());	
		}
		$this->bConnect = true;
    }

	function getID($table_name)
	{
		return $this->oDB->nextId($table_name);
	}
	
    /**
     * @shortdesc disconnect to database
     * disconnect to database
     * @private
     * @type void
     **/
    function _disconnect()
    {
        if (!$this->bConnect)return;
        $this->oDB->disconnect();
        $this->bConnect = FALSE;
    }

    /**
     * @shortdesc disconnect from database and close all sequence data
     * disconnect from database and close all sequence data
     * @public
     * @type void
     **/
    function QUIT()
    {
    //	$this->oR->free();
		$this->_disconnect();
    }
    
    /**
     * @shortdesc does a SQL query
     * does a SQL query
     * @param string sSQL : the SQL data to do
     * @private
     * @type void
     **/
    function execute($sSQL)
    {
        if (!$this->bConnect) return $this->_ERR_add('not connect to database');
        $this->oR = $this->oDB->query($sSQL);
        if(DB::iserror($this->oR)) return $this->_ERR_add(" [$sSQL] => [".$this->oR->getMessage()."]");
    }
    
    /**
     * @shortdesc initialise database error array
     * initialise database error array
     * @private
     * @type void
     **/
    function _ERR_ini()
    {
        $this->aErr = array();
    }

    /**
     * @shortdesc add an error in the database array error
     * add an error in the database array error
     * @param string sLabel : the label of error
     * @private
     * @type boolean (FALSE)
     **/
    function _ERR_add($sLabel)
    {
        $this->aErr[] = $sLabel;
        return FALSE;
    }

    /**
     * @shortdesc is the array of error empty?
     * is the array of error empty?
     * @public
     * @type boolean
     **/
    function ERR_exist()
    {
        return (!(count($this->aErr)==0));
    }

    /**
     * @shortdesc return the array of error
     * return the array of error
     * @public
     * @type array
     **/
    function ERR_get()
    {
        return $this->aErr;
    }

    /**
     * @shortdesc return the number of enr found
     * return the number of enr found
     * @public
     * @type int
     **/
    function ENR_found()
    {
        return intval($this->oR->numRows());
    }

    /**
     * @shortdesc return the number of enr affected by the last SQL data
     * return the number of enr affected by the last SQL data
     * @public
     * @type int
     **/
    function ENR_affected()
    {
        return intval($this->oDB->affectedRows());
    }
}
?>
Return current item: Class Database