<?
class dbsearch {
var $_Pattern;
var $_DbName;
var $_DbUser;
var $_DbPass;
var $_DbHost;
var $_PortServer;
var $_DbLink;
var $TablesName = array ();
var $NumResult;
var $FindStart;
var $_IdFindPerTable = array ();
Function dbsearch ($DbNameValue, $UserDbValue, $PassDbValue, $HostDbValue, $PortServerValue)
{
if ((!$DbNameValue)||(!$UserDbValue)||(!$PassDbValue)||(!$HostDbValue)){return FALSE;}
if (!(eregi ("[0-9]{1,}",$PortServerValue))){$PortServerValue=3306;}
$this->DbName ($DbNameValue);
$this->DbUser ($UserDbValue);
$this->DbPass ($PassDbValue);
$this->DbHost ($HostDbValue);
$this->PortServer ($PortServerValue);
$this->FindStart = 0;
$this->NumResult = 0;
$this->_Pattern = '';
return TRUE;
}
Function pg_list_tables($link)
{
$sql = "SELECT relname FROM pg_class WHERE relname !~'^pg_' and relname !~'seq' and relname !~'pkey';";
return (pg_exec($link, $sql));
}
Function OpenConnection()
{
$this->DbLink (pg_connect ("host=" .$this->DbHost('')." port=$this->PortServer('') dbname=".$this->DbName('')." user=".$this->DbUser('') ." password=".$this->DbPass('') ));
if (!$this->DbLink('')){return FALSE;}
$TablesQuery = $this->pg_list_tables ($this->DbLink(''));
for ($Counter = 0; $Counter < pg_numrows ($TablesQuery); $Counter++){
array_push ($this->TablesName, pg_result($TablesQuery, $Counter,"relname"));
}
unset ($Counter);
unset ($TablesQuery);
if ((count ($this->TablesName))<1){return FALSE;}
return TRUE;
}
Function CloseConnection()
{
if ($this->DbLink('')){pg_close ($this->DbLink(''));}
return TRUE;
}
Function EndSearch()
{
$this->CloseConnection();
unset ($this);
return TRUE;
}
Function FormatString ($PatternValue)
{
$PatternValue = ltrim ($PatternValue);
$PatternValue = rtrim ($PatternValue);
$PatternValue = htmlentities ($PatternValue);
$PatternValue = eregi_replace ("[ ]{1,}"," ",$PatternValue);
return $PatternValue;
}
Function NumResult()
{
if (!$this->FindStart){ return FALSE;}
if ($this->NumResult <1 ){ return "none";}
return $this->NumResult;
}
Function SetPattern ($NewPatternValue)
{
if (!$this->DbLink('')){return FALSE;}
$this->FindStart = 0;
$this->NumResult = 0;
if (!$NewPatternValue){ return FALSE;}
$NewPatternValue = $this->FormatString ($NewPatternValue);
if ((strlen ($NewPatternValue))<=1){ return FALSE;}
$this->Pattern ($NewPatternValue);
return TRUE;
}
Function FindInDb ($FullWord, $IsCase, $ExcludeTables)
{
$this->NumResult = 0;
$this->FindStart = 0;
if ((!$this->DbLink(''))||(strlen($this->Pattern(''))<1)||(count($this->TablesName)<1)){ return FALSE;}
$this->FindStart = 1;
$CheckExcludes=$NoInclude=0;
if (is_array ($ExcludeTables)){$CheckExcludes=1;}
$FieldsName = array ();
while (list ($null,$TableName) = each ($this->TablesName)){
if ($CheckExcludes==1){
while (list ($null,$Exclude) = each ($ExcludeTables)){
if ($Exclude==$TableName){$NoInclude=1;break;}
}
reset ($ExcludeTables);
}
if ($NoInclude==1){$NoInclude=0;continue;}
$FieldsName{$TableName}= array ();
$FieldsQuery = pg_exec ("select a.attname from pg_class c, pg_attribute a where c.relname = '".$TableName. "' and a.attnum > 0 and a.attrelid = c.oid");
while ($QueryResult = pg_fetch_array ($FieldsQuery)){
if ($QueryResult{'attname'} != 'id'){
array_push ($FieldsName{$TableName},$QueryResult{'attname'});
}
}
}
unset ($FieldsQuery);
unset ($QueryResult);
unset ($TableName);
$DataResults = array ();
while (list ($TableName, $ATTable) = each ($FieldsName)){
$DataResults{$TableName} = array ();
$SQLTable = "SELECT oid";
while (list ($null, $FieldName) = each ($ATTable)){
$SQLTable .= ",".$FieldName ;
}
$SQLTable .= " FROM $TableName ";
$TableQuery = pg_exec ($SQLTable);
if (!$TableQuery){
continue;
}
while ($QueryResult = pg_fetch_array ($TableQuery)){
while (list ($null,$FieldNombre) = each ($FieldsName{$TableName})){
if (strlen ($QueryResult{$FieldNombre})<1){ continue;}
if (isset ($NewFound)){unset ($NewFound);}
$EvalEreg = "if (ereg";
if (!$IsCase){ $EvalEreg .= 'i';}
$EvalEreg .= " (\"";
if ($FullWord){ $EvalEreg .= '^';}
$EvalEreg .= $this->Pattern ('');
if ($FullWord){ $EvalEreg .= '$';}
$EvalEreg .= "\", \$QueryResult{\$FieldNombre})){ array_push (\$DataResults{\$TableName}, \$QueryResult{'oid'});}";
eval ($EvalEreg);
}
reset ($FieldsName{$TableName});
}
}
reset ($this->TablesName);
reset ($DataResults);
$this->_IdFindPerTable = $DataResults;
$this->NoDuplis();
return TRUE;
}
Function NoDuplis()
{
$Is = $NoIs = 0;
$COPY = $this->_IdFindPerTable;
$VISTOS = array ();
while (list ($null, $tbname) = each ($this->TablesName)){
$VISTOS{$tbname}=array();
}
$NUEVO = $VISTOS;
while (list ($tabla, $array) = each ($COPY)){
while (list ($null, $id) = each ($array)){
$Is = 0;
if (count ($VISTOS{$tabla})<1){$NoIs=1;}else{
$NoIs = 0;}
while (list ($n, $idd) = each ($VISTOS{$tabla})){
if ($id==$idd){
$NoIs=0;
$Is=1;
break;
}
$NoIs=1;
$Is=0;
}
if ($NoIs==1){
array_push ($NUEVO{$tabla}, $id);
array_push ($VISTOS{$tabla}, $id);
$this->NumResult++;
}
reset ($VISTOS);
}
}
reset ($this->TablesName);
$this->_IdFindPerTable=$NUEVO;
}
Function GetDataId ($TableNameValue, $FieldsNameValue, $IdValue)
{
$SQLData = "SELECT $FieldsNameValue FROM $TableNameValue WHERE id='$IdValue'";
$QueryData = mysql_query ($SQLData, $this->DbLink(''));
return (pg_fetch_array ($QueryData));
}
Function DbName ($DbNameValue)
{
if (!$DbNameValue){ return $this->_DbName;}
return ($this->_DbName = $DbNameValue);
}
Function DbUser ($DbUserValue)
{
if (!$DbUserValue){ return $this->_DbUser;}
return ($this->_DbUser = $DbUserValue);
}
Function DbPass ($DbPassValue)
{
if (!$DbPassValue){ return $this->_DbPass;}
return ($this->_DbPass = $DbPassValue);
}
Function DbHost ($DbHostValue)
{
if (!$DbHostValue){ return $this->_DbHost;}
return ($this->_DbHost = $DbHostValue);
}
Function PortServer ($PortServerValue)
{
if (!$PortServerValue){ return $this->_PortServer;}
return ($this->_PortServer = $PortServerValue);
}
Function Pattern ($PatternValue)
{
if (!$PatternValue){ return $this->_Pattern;}
return ($this->_Pattern = $PatternValue);
}
Function DbLink ($DbLinkValue)
{
if (!$DbLinkValue){ return $this->_DbLink;}
return ($this->_DbLink = $DbLinkValue);
}
Function IdFindPerTable ($DataValue)
{
return $this->_IdFindPerTable;
}
}
?> |