<?
/****************************************************************************************/
/* CLASS NAME : MetaScanner */
/* DEVELOPER : Fryderyk Benigni */
/* LICENSE : GPL(Generic Public License) */
/* DATE : 05 November 2004 */
/* OBJECTIVE : Retrieve and show Metadata from a MySql DataBase Server */
/* DESCRIPTION : This class is intended to retrieve MetaData from any MySql Server. It */
/* many different methods. It also offer a simple method called showAll that sows a */
/* simple table style representation of the MySql server. Anothe method very useful is */
/* scanAll a simple method that scann the all MySql Server and store server information */
/* about the strocture on determined variables called: $dblists(Lists of all */
/* Databases), $tblists(List of all tables indexed by the name of the relative */
/* database) and $fllists(List of all field of all the tabels indexed by the name of */
/* the tables. */
/*
/* Edit History: */
/* */
/* Dick Munroe hide@address.com 09-Nov-2004 */
/* Back port to php 4, add username/password to constructor. */
/* Make sure all mysql statements use the connection resource, in an active */
/* environment, no guarantees of "last opened" will work. */
/* Added functions fetchDatabases, fetchTables, and fetchFields that return */
/* arrays of the appropriate metadata to the caller. */
/****************************************************************************************/
class MetaScanner
{
var $connection; //holds the connection to MySql Server
var $username; //holds the username (if any) for the connection.
var $password; //holds the password (if any) for the connection.
var $dblists; //Lists of all databases in a particular MySql Server
var $tblists; //Lists of all tabels in a particular MySql Server indexed by database name
var $fllists; //Lists of all fields in a particular MySql Server indexed by table name
/****************************************/
/* Class Constructor */
/****************************************/
function MetaScanner($__Connection, $__Username = null, $__Password = null)
{
$this->connection = $__Connection; //Specication and attribution os a connection
$this->username = $__Username ;
$this->password = $__Password ;
}
function __Construct($__Connection, $__Username = null, $__Password = null)
{
$this->connection = $__Connection; //Specication and attribution os a connection
$this->username = $__Username ;
$this->password = $__Password ;
}
/****************************************/
/* Connection function */
/****************************************/
function connect()
{
$this->connection = mysql_connect($this->connection,
$this->username,
$this->password); //Connects to the specified connection
}
/****************************************/
/* Desconnection function */
/****************************************/
function disconnect()
{
mysql_close($this->connection);
}
/****************************************/
/* Return DB list */
/****************************************/
function returndblist()
{
return $this->dblists;
}
/****************************************/
/* Return Table List */
/****************************************/
function returntblist()
{
return $this->tblists;
}
/****************************************/
/* Return Fields List */
/****************************************/
function returnfllist()
{
return $this->fllists;
}
/****************************************/
/* Retrieve DataBases */
/****************************************/
function getDataBases()
{
$this->dblists = mysql_query("SHOW DATABASES", $this->connection); //Gets all DataBases of a specific connection
return $this->dblists; //Return the DataBases list
}
function fetchDataBases()
{
$theReturnValue = array() ;
$theResult = $this->getDataBases() ;
while ($theRow = mysql_fetch_row($theResult))
{
$theReturnValue[] = $theRow[0] ;
}
return $theReturnValue ;
}
/****************************************/
/* Retrieve Tables */
/****************************************/
function getTables($__Database) //Calling getTables passing a specific DataBase as a parameter
{
mysql_select_db($__Database, $this->connection); //Selecting the specific DataBase
$this->tblists["$__Database"] = mysql_query("SHOW TABLES", $this->connection); //Retrieving Table list of a specific DataBase
return $this->tblists; //Return List of Tables of a specific Database
}
function fetchTables($__Database)
{
$theReturnValue = array() ;
$theResult = $this->getTables($__Database) ;
while ($theRow = mysql_fetch_row($theResult[$__Database]))
{
$theReturnValue[] = $theRow[0] ;
}
return $theReturnValue ;
}
/****************************************/
/* Retrieve Fields */
/****************************************/
function getFields($__Database, $__Table) //Retrieve Fields passing a Database and Table name
{
$this->fllists["$__Table"] = mysql_list_fields($__Database, $__Table, $this->connection); //Retrieve Fields
return $this->fllists; //Return Fields List
}
function fetchFields($__Database, $__Table)
{
$theReturnValue = array() ;
$theResult = $this->getFields($__Database, $__Table) ;
$theNumberOfFields = mysql_num_fields($theResult[$__Table]) ;
for ($i = 0; $i < $theNumberOfFields; $i++)
{
$theReturnValue[] = mysql_field_name($theResult[$__Table], $i) ;
}
return $theReturnValue ;
}
/****************************************/
/* Retrieve all MetaData */
/****************************************/
function scanAll()
{
$this->dblists = $this->getDataBases(); //Gets Lists of DataBases in the specified MySql Server
while ($linha = mysql_fetch_row($this->dblists)) //Read true all the DB result
{
$this->tblists = $this->getTables($linha[0]); //Gets table for each specified DataBase
$temp = $linha[0]; //Temporary attribution of DB names
while ($linha2 = mysql_fetch_row($this->tblists[$temp])) //Read true all Tables
{
$this->fllists = $this->getFields($linha[0],$linha2[0]);//Gets all fields for the specific DB and Table
}
}
}
/****************************************/
/* Retrieve all MetaData */
/****************************************/
function showAll()
{
$this->dblists = $this->getDataBases(); //Gets Lists of DataBases in the specified MySql Server
while ($linha = mysql_fetch_row($this->dblists)) //Read true all the DB result
{
print("<p><b><font size=\"4\" face=\"Arial\">Database : $linha[0]</font></b></p><br>"); //Print DB Name
$this->tblists = $this->getTables($linha[0]); //Gets table for each specified DataBase
$temp = $linha[0]; //Temporary attribution of DB names
while ($linha2 = mysql_fetch_row($this->tblists[$temp])) //Read true all Tables and shows Name and FIeld Title
{
print("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" width=\"100%\" \"><tr>");
print("<td width=\"100%\" colspan=\"3\" bgcolor=\"#EFEFEF\"><font face=\"Arial\"><b>Table: $linha2[0]</b></font></td>");
print("</tr>
<tr>
<td width=\"33%\" bgcolor=\"#D7D7D7\" align=\"center\"><b>
<font face=\"Arial\" size=\"2\">Field Name</font></b></td>
<td width=\"33%\" bgcolor=\"#D7D7D7\" align=\"center\"><b>
<font face=\"Arial\" size=\"2\">Field Type</font></b></td>
<td width=\"34%\" bgcolor=\"#D7D7D7\" align=\"center\"><b>
<font face=\"Arial\" size=\"2\">Field Size</font></b></td>
</tr>
");
$this->fllists = $this->getFields($linha[0],$linha2[0]); //Gets all fields for the specific DB and Table
$temp2 = $linha2[0]; //Temporary attribution of Tables names
for($i=0;$i<mysql_num_fields($this->fllists[$temp2]);$i++) //Shows all Fields name,type and lenght for all fields of all tables
{
print("<tr>
<td width=\"33%\" align=\"center\">".mysql_field_name($this->fllists[$temp2],$i)."</td>
<td width=\"33%\" align=\"center\">".mysql_field_type($this->fllists[$temp2],$i)."</td>
<td width=\"34%\" align=\"center\">".mysql_field_len($this->fllists[$temp2],$i)."</td>
</tr>");
}
}
print("<br>");
}
}
}
?>