PHP Classes
Icontem

File: activeDBLib.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Giorgos  >  ActiveDBLib  >  activeDBLib.php  
File: activeDBLib.php
Role: Class source
Content type: text/plain
Description: The activeDBLib class source
Class: ActiveDBLib
Database abstraction library independent wrapper
 

Contents

Class file image Download
<?php
// +----------------------------------------------------------------------+
// | activeDBLib 0.1                                                      |
// +----------------------------------------------------------------------+
// | Date: 16 Feb 2005                                                    |
// +----------------------------------------------------------------------+
// | License: LGPL                                                        |
// +----------------------------------------------------------------------+
// | PHP class to access MySQL (default) or optionally other Databases,   |
// | by using a Database Abstraction Library (ADODB,PEAR::DB or METABASE).|
// | It's primary intention is to hide the differences                    |
// | between the different Database Abstraction Libraries                 |
// | and to provide standard methods for the                              |
// | database connection, query and result operations.                    |
// +----------------------------------------------------------------------+
// | Author: Giorgos Tsiledakis <gt [at] corissia [dot] com>              |
// +----------------------------------------------------------------------+
class activeDBLib{
//==============================================================================================
// PUBLIC (Constructor) -> checks the included libraries (if any)
// param $driver: sets the database type; default: mysql
//==============================================================================================
function activeDBLib($driver="mysql"){
$this->driver=$driver;
	if (class_exists("ADOConnection")){
		$this->actDBLib="ADODB";
		$this->extDBLib=&ADONewConnection($driver);
	}
	elseif (class_exists("DB")) $this->actDBLib="PEARDB";
	elseif (function_exists("MetabaseSetupDatabase")) $this->actDBLib="METABASE";
}
//==============================================================================================
// PUBLIC debug() -> if there is an error, it prints the error message and exits
//==============================================================================================
function debug($bool=true){
$this->debug=$bool;
}
//==============================================================================================
// PUBLIC error() -> returns false, if there is no error or the error message, if there is an error
//==============================================================================================
function error(){
return $this->error;
}
//==============================================================================================
// PUBLIC connect() -> opens a connection to a database server
//==============================================================================================
function connect($host=false,$user=false,$pass=false,$dbase=false,$opt=false){
	if ($this->actDBLib=="ADODB"){
		@$this->conn=$this->extDBLib->Connect($host,$user,$pass,$dbase);
		if (!$this->conn) $this->activeDBLib_error($this->extDBLib->ErrorMsg());
	}
	elseif ($this->actDBLib=="PEARDB"){
		$dns=array("phptype"=>$this->driver,"hostspec"=>$host,"username"=>$user,"password"=>$pass,"database"=>$dbase);
		$this->conn=&DB::connect($dns,$opt);
		if (DB::isError($this->conn)) $this->activeDBLib_error($this->conn->getMessage());
	}
	elseif ($this->actDBLib=="METABASE"){
		$dns=array("Type"=>$this->driver,"Host"=>$host,"User"=>$user,"Password"=>$pass,"Database"=>$dbase,"Persistent"=>0,"Options"=>$opt);
		$error=MetabaseSetupDatabase($dns,$this->conn);
		if ($error!="") $this->activeDBLib_error($error);
	}
	elseif ($this->actDBLib==false){
		$this->driver="mysql";
		$this->conn=@mysql_connect($host,$user,$pass);
		@mysql_select_db($dbase,$this->conn);
			if (!$this->conn) $this->activeDBLib_error();
	}
}
//==============================================================================================
// PUBLIC pconnect() -> opens a persistent connection to a database server
//==============================================================================================
function pconnect($host=false,$user=false,$pass=false,$dbase=false,$opt=false){
	if ($this->actDBLib=="ADODB"){
		@$this->conn=$this->extDBLib->PConnect($host,$user,$pass,$dbase);
		if (!$this->conn) $this->activeDBLib_error($this->extDBLib->ErrorMsg());
	}
	elseif ($this->actDBLib=="PEARDB"){
		$dns=array("phptype"=>$this->driver,"hostspec"=>$host,"username"=>$user,"password"=>$pass,"database"=>$dbase);
		$this->conn=&DB::connect($dns,$opt);
		$this->conn->setOption('persistent', true);
		if (DB::isError($this->conn)) $this->activeDBLib_error($this->conn->getMessage());
	}
	elseif ($this->actDBLib=="METABASE"){
		$dns=array("Type"=>$this->driver,"Host"=>$host,"User"=>$user,"Password"=>$pass,"Database"=>$dbase,"Persistent"=>1,"Options"=>$opt);
		$error=MetabaseSetupDatabase($dns,$this->conn);
		if ($error!="") $this->activeDBLib_error($error);
	}
	elseif ($this->actDBLib==false){
		$this->driver="mysql";
		$this->conn=@mysql_pconnect($host,$user,$pass);
		@mysql_select_db($dbase,$this->conn);
			if (!$this->conn) $this->activeDBLib_error();
	}
}
//==============================================================================================
// PUBLIC execute() -> sends a SQL query to Database
// param $sql: SQL string
//==============================================================================================
function execute($sql=false){
	if ($this->conn){
		if ($this->actDBLib=="ADODB"){
			@$this->sqlRs=$this->extDBLib->Execute($sql);
			if (!$this->sqlRs) $this->activeDBLib_error($this->extDBLib->ErrorMsg());
			else return $this->sqlRs;
		}
		elseif ($this->actDBLib=="PEARDB"){
			@$this->sqlRs=&$this->conn->query($sql);
			if (DB::isError($this->sqlRs)) $this->activeDBLib_error($this->sqlRs->getUserInfo());
			else return $this->sqlRs;
		}
		elseif ($this->actDBLib=="METABASE"){
			$this->sqlRs=MetabaseQuery($this->conn,$sql);
			$this->rowInit=0;
			if ($this->sqlRs==0) $this->activeDBLib_error(MetabaseError($this->conn));
			else return $this->sqlRs;
		}
		elseif ($this->actDBLib==false){
			if ($this->sqlRs) @mysql_free_result($this->sqlRs);
		$this->sqlRs=@mysql_query($sql,$this->conn);
			if (!$this->sqlRs) $this->activeDBLib_error();
			else return $this->sqlRs;
		}
	}
	else return false;
}
//==============================================================================================
// PUBLIC rowCount() -> gets the number of rows in previous operation
//==============================================================================================
function rowCount(){
	if ($this->sqlRs){
		if ($this->actDBLib=="ADODB") return $this->sqlRs->RowCount();
		elseif ($this->actDBLib=="PEARDB") return @$this->sqlRs->numRows();
		elseif ($this->actDBLib=="METABASE") return MetabaseNumberOfRows($this->conn,$this->sqlRs);
		elseif ($this->actDBLib==false) return @mysql_num_rows($this->sqlRs);
	}
	else return false;
}
//==============================================================================================
// PUBLIC fieldCount() -> gets the number of columns in previous operation
//==============================================================================================
function fieldCount(){
	if ($this->sqlRs){
		if ($this->actDBLib=="ADODB") return $this->sqlRs->FieldCount();
		elseif ($this->actDBLib=="PEARDB") return @$this->sqlRs->numCols();
		elseif ($this->actDBLib=="METABASE") return MetabaseNumberOfColumns($this->conn,$this->sqlRs);
		elseif ($this->actDBLib==false) return @mysql_num_fields($this->sqlRs);
	}
	else return false;
}
//==============================================================================================
// PUBLIC getArray() -> fetches a result row as an array
//==============================================================================================
function getArray(){
$data=false;
	if ($this->sqlRs){
		if ($this->actDBLib=="ADODB") $data=$this->sqlRs->FetchRow();
		elseif ($this->actDBLib=="PEARDB") $data=&$this->sqlRs->fetchRow();
		elseif ($this->actDBLib=="METABASE"){
			if ($this->rowInit<$this->rowCount()){
				@MetabaseFetchResultArray($this->conn,$this->sqlRs,&$data,$this->rowInit);
				$this->rowInit++;
			}
		}
		elseif ($this->actDBLib==false) $data=@mysql_fetch_array($this->sqlRs, MYSQL_BOTH);
	}
return $data;
}
//==============================================================================================
// PUBLIC affectedRows() ->  gets number of affected rows in previous operation
//==============================================================================================
function affectedRows(){
	if ($this->sqlRs){
		if ($this->actDBLib=="ADODB") return $this->extDBLib->Affected_Rows();
		elseif ($this->actDBLib=="PEARDB") return $this->conn->affectedRows();
		elseif ($this->actDBLib=="METABASE") return MetabaseAffectedRows($this->conn, &$affected_rows);
		elseif ($this->actDBLib==false) return mysql_affected_rows();
	}
	else return false;
}
//==============================================================================================
// PUBLIC getField() -> gets information of the specified field in a result
// param $column: number of the field
// param $mode:
// name -> gets the name of the field;
// type -> gets the type of the field; (not implemented for Metabase yet)
// max_Length -> gets the maximal length of the field; (not implemented for Metabase yet)
//==============================================================================================
function getField($column=0,$mode="name"){
	if ($this->sqlRs){
		if ($this->actDBLib=="ADODB"){
			$fld=$this->sqlRs->FetchField($column);
			switch($mode){
				case "name": return $fld->name;
				case "type": return $fld->type;
				case "max_length": return $fld->max_length;
			}
		}
		elseif ($this->actDBLib=="PEARDB"){
			$fld=$this->sqlRs->tableInfo($this->sqlRs,0);
			switch($mode){
				case "name": return $fld[$column]['name'];
				case "type": return $fld[$column]['type'];
				case "max_length": return $fld[$column]['len'];
			}
		}
		elseif ($this->actDBLib=="METABASE"){
			switch($mode){
			case "name": MetabaseGetColumnNames($this->conn,$this->sqlRs,$fld);
				foreach ($fld as $name => $col){
					if ($col==$column) return $name;
				}
			// other cases not implemented yet
			}
		}
		elseif ($this->actDBLib==false){
			switch($mode){
				case "name": return @mysql_field_name($this->sqlRs,$column);
				case "type": return @mysql_field_type($this->sqlRs,$column);
				case "max_length": return @mysql_field_len($this->sqlRs,$column);
			}
		}
	}
	else return false;
}
//==============================================================================================
// PUBLIC disconnect() ->  closes the connection
//==============================================================================================
function disconnect(){
	if ($this->conn){
		if ($this->actDBLib=="ADODB"){
			if ($this->sqlRs) $this->sqlRs->Close();
			$this->conn->Close();
		}
		elseif ($this->actDBLib=="PEARDB") $this->conn->disconnect();
		elseif ($this->actDBLib=="METABASE") MetabaseCloseSetup($this->conn);
		elseif ($this->actDBLib==false) mysql_close($this->conn);
	}
	else return false;
}
//==============================================================================================
// PUBLIC getExtLibrary() ->  returns the name of loaded Database Abstraction Library (if any)
//==============================================================================================
function getDBLibrary(){
	if ($this->actDBLib==false) $lib="activeDBLib";
	else $lib=$this->actDBLib;
return $lib;
}
//==============================================================================================
// PUBLIC getDriver() ->  returns the Database type used (default mysql)
//==============================================================================================
function getDriver(){
return $this->driver;
}
//==============================================================================================
// PUBLIC htmlTable() ->  returns a html table string with the field info and rows of a SELECT (test function)
//==============================================================================================
function htmlTable(){
$out="<table>\n";
$out.="<tr>";
	for ($i=0;$i<$this->fieldCount();$i++) $out.="<th>".$this->getField($i,"name")."</th>";
$out.="</tr>\n";
$out.="<tr>";
	for ($i=0;$i<$this->fieldCount();$i++) $out.="<th>".$this->getField($i,"type")."</th>";
$out.="</tr>\n";
$out.="<tr>";
	for ($i=0;$i<$this->fieldCount();$i++) $out.="<th>".$this->getField($i,"max_length")."</th>";
$out.="</tr>\n";
	while ($rows=$this->GetArray()){
		$out.="<tr>\n";
		for ($x=0;$x<$this->fieldCount();$x++) {
			$out.="<td>".htmlentities($rows[$x])."</td>\n";
		}
		$out.="</tr>\n";
	}
$out.="</table>\n";
return $out;
}
//==============================================================================================
// PRIVATE VARIABLES
//==============================================================================================
var $debug=false; // true: display errors and die
var $error=false; // the error description
var $actDBLib=false; // name of loaded library; possible values: false (none), ADODB, PEARDB, METABASE
var $extDBLib=false; // object of the ADODB Connection
var $conn=false; // the connection ID
var $sqlRs=false; // the SQL query ID
var $rowInit=0; // help var for getArray if metabase
//==============================================================================================
// PRIVATE activeDBLib_error -> (if debug==true) prints the error message and exits the application
//==============================================================================================
function activeDBLib_error($errorDes=""){
	if ($errorDes==""){
		if (mysql_errno()) $errorDes="MySQL said: Error #" .mysql_errno()." : ". mysql_error();
	}
$this->error=$errorDes;
	if ($this->debug) die($errorDes);
}
}
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products