sp=new SqlParser(); } /* * All public functions named like the ADODB package syntax * *********************************************************************************************/ /* PConnect make a new connection to db dummy; set the database name here */ function PConnect($lm_db_host = '', $lm_db_user = '', $lm_db_pwd = '', $lm_db_name = "./data/") { global $QueryCache; $this->db_name = $lm_db_name; if(!is_dir($lm_db_name))return; //initalize cache system if($this->qcache) { if(is_file($this->db_name."cache/QueryCache.php")) { include_once($this->db_name."cache/QueryCache.php"); $QueryCache=$sQueryCache; } } $this->connect=true; } /* general function to execute sql-code */ function &Execute($sqlstring) { global $query_count; $sqlstring=str_replace("#__",$this->prefix,$sqlstring); $sqlstring = preg_replace (array("/\s+/"), array(" "), stripslashes((trim($sqlstring)))); $tmp = explode (" ", $sqlstring, 5); $this->rowset=false; $this->ErrorMsg[0]=""; $query_count++; switch ($tmp[0]) { case "SELECT": if($this->cache)if($this->getcache($tmp[3],$sqlstring))break; include_once('select.php'); $rs = _select($this, $sqlstring); $this->dec_rs(); if($this->cache)$this->newcache($tmp[3],$sqlstring); break; case "INSERT": include_once('insert.php'); $rs = _insert($this, $sqlstring); if($this->cache)$this->delcache($tmp[2],$sqlstring); break; case "UPDATE": include_once('update.php'); $rs = _update($this, $sqlstring); if($this->cache)$this->delcache($tmp[1],$sqlstring); break; case "DELETE": include_once('delete.php'); $rs = _delete($this, $sqlstring); if($this->cache)$this->delcache($tmp[2],$sqlstring); break; case "CREATE": if($tmp[1]=="DATABASE")break; case "CREATE": include_once('create.php'); $rs = _create($this, $sqlstring); break; case "DROP": include_once('drop.php'); $rs = _drop($this, $sqlstring); if($this->cache)$this->delcache($tmp[1],$sqlstring); break; default: $this->ErrorMsg[0] = DB_ERRORMSG_101; $rs = false; } if($this->error && trim ($this->ErrorMsg[0])!='' )echo "[ERRsql:$sqlstring:".$this->ErrorMsg[0]."]
"; /*there was an error only then return false*/ if(strlen($this->ErrorMsg[0])>0){return false;} return $this; } function dec_rs() { $trowset=false; if(count($this->rowset)>0 && is_array($this->rowset)) { foreach($this->rowset as $row) { $tr=false; if(is_array($row)){ foreach($row as $var=>$val) { $tr[$var]=$this->dbcoding?dbdecode($val):$val; } $trowset[]=$tr; } } } $this->rowset=$trowset; } /* return recordset array */ function GetArray($num = 0) { if ($num > 0) { return array_slice ($this->rowset, 0, $num); } else { return $this->rowset; } } /*return a single row */ function GetRow($sql) { $this->Execute($sql); return $this->rowset[0]; } /* return recordset array */ function SelectLimit($sql,$numrows=-1,$lm_offset=-1) { $this->Execute($sql); if( !is_array($this->rowset)) return false; if ($numrows > 0 && $lm_offset==-1) { return $this->rowset = array_slice ($this->rowset, 0, $numrows); } else if ($numrows > 0 && $lm_offset >= 0) { return $this->rowset = array_slice ($this->rowset , $lm_offset, $numrows); } else { return $this; } } /* return autoincremented id */ function Insert_ID() { return $this->nid; } /*return last error message */ function ErrorMsg() { return $this->ErrorMsg[0]; } /* return affected rows from update or delete */ function Affected_Rows() { return $this->aff_rows; } /* return number of rows in recordset */ function RecordCount() { if (is_array ($this->rowset)) { return count ($this->rowset); } else { return 0; } } function RowCount() { } /* return number of fileds (columns) in recordset */ function FieldCount() { if (is_array ($this->rowset)) { return count (array_keys ($this->rowset[0])); } else { return 0; } } /* return array of table names in database */ function MetaTables() { include_once('db_func.php'); return meta_tables($this); } /* return table sizes */ function ReturnTableSizes() { if (empty ($this->db_size)) { $this->MetaTables(); } return $this->db_size; } /* return array of ADOFieldObjects, one object per table column */ function MetaColumns($table, $upper = true) { include_once('db_func.php'); return meta_columns($this, $table); } /* return array of column names i table */ function MetaColumnNames($table) { if (!@include_once ($this->db_name.$table.'/schema.php')) { $this->ErrorMsg[0] = DB_ERRORMSG_102; return false; } else { foreach ($columns as $key=>$col) { $rs[] = $key; } return $rs; } } /* * Cache handler ( other the db becomes very slow when it get big ) * *******************************************************************************************/ function newcache($table,$sql_string) { if(!is_dir($this->db_name."cache"))mkdir($this->db_name."cache",0777); $fp=fopen($this->db_name."cache/".$table.md5($sql_string),"w"); $val=serialize($this->rowset); fwrite($fp,$val,strlen($val)); fclose($fp); } function delcache($table,$sql_string) { if ($dir = @opendir($this->db_name."cache/")) { while($file = readdir($dir)) { if(strstr($file,$table)) unlink($this->db_name."cache/".$file); } closedir($dir); } } function delallcache() { if ($dir = @opendir($this->db_name."cache/")) { while($file = readdir($dir)) { if($file=='.' || $file=='..' ) continue; unlink($this->db_name."cache/".$file); } closedir($dir); } } function getcache($table,$sql_string) { if(!is_dir($this->db_name."cache"))return false; if( is_file($this->db_name."cache/".$table.md5($sql_string)) ) { $fp=fopen($this->db_name."cache/".$table.md5($sql_string),"r"); $val=fread($fp,filesize($this->db_name."cache/".$table.md5($sql_string))); $this->rowset=unserialize($val); fclose($fp); return true; } return false; } /* * Query Cache handler ( other the db becomes very slow when it get big ) * *******************************************************************************************/ function newqcache($sql,$object) { if(!$this->qcache)return; global $qcdel; $qcfile=$this->db_name."cache/QueryCache.php"; if(!isset($qcdel)){ if(filesize($qcfile)>60000) unlink($qcfile); $this->delallcache(); $qcdel=true; } $fp=fopen($qcfile,"a"); if(!$fp) { if(!is_file($qcfile)) { touch($qcfile); } } $val=addslashes(serialize($object)); $str=sprintf("\r\n",md5($sql),$val); fwrite($fp,$str,strlen($str)); fclose($fp); } function delqcache($table,$sql_string) { if(is_file($this->db_name."cache/QueryCache.php"))unlink($this->db_name."cache/".$file); } } class ADOFieldObject { var $lm_name = ''; var $max_length = 'n/a'; var $type = "char"; var $not_null = false; var $has_default = false; var $default_value = ""; } ?>