mysqlシミュレーションaspのデータベース操作クラス
14005 ワード
nErr=0;
$this->NewEdit=0;
$this->nResult=-1;
$this->nCols=0;
$this->nRows=0;
$this->nOffset=0;
$this->EOF=true;
$this->sName="";
$this->sValue="#@!";
$this->sEdit="#@!";
unset($this->aFName);
unset($this->aNew);
}
function MySqlDB($TableName="",$database="slt") //
{
$this->Initialize();
$this->sTName=$TableName;
$this->linkid=mysql_connect($host,$user,$password);
if(!$this->linkid)
{
$this->nErr=1;
$this->sErr="MySqlDB: , !";
return;
}
$this->dbid=mysql_select_db($database);
if(!$this->dbid)
{
$this->nErr=1;
$this->sErr="MySqlDB: ".$database." !";
return;
}
}
function IsEmpty($Value)
{
if(is_string($Value)&&empty($Value))
return true;
return false;
}
function Destroy() //
{
mysql_query("commit");
mysql_close();
}
function PrintErr()
{
if($this->nErr==1)
{
echo($this->sErr."
");
}
else
{
echo("
");
}
}
function Execute($SQL) // SQL
{
if(empty($SQL))
{
$this->nErr=1;
$this->sErr="Execute: !";
return false;
}
$this->sSQL=$SQL;
if(!mysql_query($SQL))
{
$this->nErr=1;
$this->sErr="Execute:SQL :".$SQL."
MySql :".mysql_error();
return false;
}
return true;
}
function Query($TableName="",$SQL="*",$Condition="",$Order="",$Sequenc="") //
{
$this->Initialize();
if(!empty($TableName))
$this->sTName=$TableName;
$strSQL="select ".$SQL." from ".$this->sTName;
if(!empty($Condition))
$strSQL=$strSQL." where ".$Condition;
if(!empty($Order))
$strSQL=$strSQL." order by ".$Order;
if(!empty($Sequenc))
$strSQL=$strSQL." ".$Sequenc;
$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Query:SQL :".$strSQL."
MySql :".mysql_error()."
";
return;
}
$this->nOffset=0;
$this->nRows=mysql_num_rows($this->nResult);
$this->nCols=mysql_num_fields($this->nResult);
if($this->nRows>0)
$this->EOF=false;
else
$this->EOF=true;
unset($this->aFName);
$this->aFName=array();
for($i=0;$inCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}
function MoveNext()
{
if($this->EOF)
{
$this->nErr=1;
$this->sErr="MoveNext: !";
return;
}
$this->nOffset++;
if($this->nOffset>=$this->nRows)
$this->EOF=true;
}
function MoveTo($Offset)
{
if(empty($Offset))
{
$this->nErr=1;
$this->sErr="MoveTo: ! ";
return;
}
if(!$this->nResult)
{
$this->nErr=1;
$this->sErr="MoveTo: :Query";
return;
}
$this->nOffset=$Offset;
}
// ,
// Offset
// nFields ,
function GetValue($nFields=-1,$Offset=-1)
{
if($this->nResult==-1)
{
$this->nErr=1;
$this->sErr="GetValue: Query() !";
return;
}
if($Offset>-1)
{
$this->nOffset=$Offset;
if($this->nOffset>=$this->nRows)
{
$this->nErr=1;
$this->sErr="GetValue: , !";
return;
}
}
if(!@mysql_data_seek($this->nResult,$this->nOffset))
{
$this->nErr=1;
$this->sErr="GetValue: !";
return;
}
$aResult=mysql_fetch_row($this->nResult);
if(is_int($nFields)&&$nFields>-1)
{
if($nFileds>$this->nCols)
{
$this->nErr=1;
$this->sErr="GetValue: !";
return;
}
return $aResult[$nFields];
}
if(is_string($nFields))
{
$nFields=strtolower($nFields);
for($i=0;$inCols;$i++)
{
if($this->aFName[$i]==$nFields)
break;
}
if($i==$this->nCols)
{
$this->nErr=1;
$this->sErr="GetValue: , !";
return;
}
return $aResult[$i];
}
return $aResult;
}
function AddNew($TableName="") //
{
$this->Initialize();
if(!empty($TableName))
$this->sTName=$TableName;
if($this->NewEdit>0)
{
$this->nErr=1;
$this->sErr="AddNew: !";
return;
}
if(empty($this->sTName))
{
$this->nErr=1;
$this->sErr="AddNew: , , AddNew() !";
return;
}
unset($this->aNew);
$this->aNew=array();
$this->NewEdit=1;
$strSQL="select * from ".$this->sTName;
$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="AddNew:SQL :".strSQL."
MySql :".mysql_error();
return;
}
$this->nCols=mysql_num_fields($this->nResult);
unset($this->aFName);
$this->aFName=array();
for($i=0;$inCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}
function Edit($Condition="",$TableName="") //
{
$this->Initialize();
if(!empty($TableName))
$this->sTName=$TableName;
$this->sEditCon=$Condition;
if(empty($this->sTName))
{
$this->nErr=1;
$this->sErr="Edit: !";
return;
}
unset($this->aNew);
$this->aNew=array();
$this->NewEdit=2;
$strSQL="select * from ".$this->sTName;
$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Edit:SQL :".strSQL."
MySql :".mysql_error();
return;
}
$this->nCols=mysql_num_fields($this->nResult);
unset($this->aFName);
$this->aFName=array();
for($i=0;$inCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}
function SetValue($Index,$Value) // , AddNew ;
{
if($this->NewEdit==0)
{
$this->nErr=1;
$this->sErr="SetValue: AddNew() Edit()!";
return;
}
if(is_int($Index))
{
if($Index<0||$Index>$this->nCols)
{
$this->nErr=1;
$this->sErr="SetValue: !";
return;
}
$this->aNew[$Index]=$Value;
$tmpIn=$Index;
}
elseif(is_string($Index))
{
$Index=strtolower($Index);
for($i=0;$inCols;$i++)
{
if($this->aFName[$i]==$Index)
break;
}
if($i==$this->nCols)
{
$this->nErr=1;
$this->sErr="SetValue: !";
return;
}
$this->aNew[$i]=$Value;
$tmpIn=$i;
}
if(!empty($this->sName))
$this->sName.=",";
$this->sName.=$this->aFName[$tmpIn];
//
if($this->sValue!="#@!")
$this->sValue.=",";
else
$this->sValue="";
$ftype=@mysql_field_type($this->nResult,$i);
//echo($ftype.",".$this->aNew[$i].",".$i.":".$sValue."
");
switch($ftype)
{
case "string":
case "date":
case "datetime":
$this->sValue.=""".$this->aNew[$tmpIn].""";
$this->sEdit=""".$this->aNew[$tmpIn].""";
break;
case "int":
case "unknown":
$this->sValue.=$this->aNew[$tmpIn];
$this->sEdit=$this->aNew[$tmpIn];
break;
default:
$this->nErr=1;
$this->sErr="Update: ".$this->aFName[$tmpIn]." ".$ftype." , !";
return;
}
if($this->NewEdit==2)
$this->sName.="=".$this->sEdit;
}
function Update() //
{
$strSQL="";
if($this->NewEdit==0)
{
$this->nErr=1;
$this->sErr="Update: AddNew() Edit(), SetValue() !";
return;
}
if(empty($this->sValue))
{
$this->nErr=1;
$this->sErr="Update: , !";
return;
}
switch($this->NewEdit)
{
case 1: //
$strSQL="insert into ";
$strSQL.=$this->sTName;
$strSQL.=" (".$this->sName.") ";
$strSQL.="values (".$this->sValue.")";
break;
case 2: //
$strSQL="update ";
$strSQL.=$this->sTName;
$strSQL.=" set ";
$strSQL.=$this->sName;
if(!empty($this->sEditCon))
$strSQL.=" where ".$this->sEditCon;
break;
default:
$this->nErr=1;
$this->sErr="Update:Update() SQL , !";
return;
}
$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Update:SQL :".$strSQL."
MySql :".mysql_error();
return;
}
//echo($this->sSQL."
");
//
$this->NewEdit=0;
unset($this->aNew);
mysql_query("commit");
}
}
?>