You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
4.2 KiB
139 lines
4.2 KiB
<?php
|
|
class SQLite_DB{
|
|
var $DBFILE;
|
|
var $DBFLAGS;
|
|
var $DBENCRYPT;
|
|
var $DBLink;
|
|
var $CachePath;
|
|
var $isConnected=false;
|
|
var $isCached=false;
|
|
var $DebugLog = '';
|
|
function __construct( $DBFILE, $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $encryptionKey = "" ){
|
|
$this->DBFILE=$DBFILE;
|
|
$this->DBFLAGS=$flags;
|
|
$this->DBENCRYPT=$encryptionKey;
|
|
try{
|
|
$this->DBLink = new SQLite3( $this->DBFILE, $this->DBFLAGS, $this->DBENCRYPT );
|
|
$this->DBLink->busyTimeout(5000);
|
|
// WAL mode has better control over concurrency.
|
|
// Source: https://www.sqlite.org/wal.html
|
|
$this->DBLink->exec('PRAGMA journal_mode = wal;');
|
|
$this->isConnected = true;
|
|
}catch( Exception $e ){
|
|
echo $e->getMessage();
|
|
};
|
|
}
|
|
function CleanUpCache(){
|
|
if ($DD=@opendir($this->CachePath)){
|
|
while($FN=readdir($DD)){
|
|
$CF=$this->CachePath.'/'.$FN;
|
|
if (is_file($CF)){
|
|
if ( strstr($FN,'dbchache_') && (time()-filemtime($CF))>360){
|
|
@unlink($CF);
|
|
};
|
|
};
|
|
};
|
|
closedir($DD);
|
|
} else $this->isCached=false;
|
|
}
|
|
function ResetCache(){
|
|
if ($DD=@opendir($this->CachePath)){
|
|
while($FN=readdir($DD)){
|
|
$CF=$this->CachePath.'/'.$FN;
|
|
if (is_file($CF)){
|
|
if ( strstr($FN,'dbchache_')){
|
|
@unlink($CF);
|
|
};
|
|
};
|
|
};
|
|
closedir($DD);
|
|
} else $this->isCached=false;
|
|
}
|
|
function SetCacheDir($Path){
|
|
$this->CachePath = $Path;
|
|
$this->isCached = is_dir($this->CachePath) && is_writable($this->CachePath);
|
|
if ($this->isCached) $this->CleanUpCache();
|
|
}
|
|
function escape( $Str ){
|
|
if( $this->isConnected ){
|
|
return $this->DBLink->escapeString( $Str );
|
|
};
|
|
return '';
|
|
}
|
|
function Query($QS,$ChacheIfAveable=true){
|
|
if($this->isConnected){
|
|
$QS = trim( $QS );
|
|
if( $this->isCached && $ChacheIfAveable ){
|
|
$QMD5=md5($QS);
|
|
$Prefetch=$this->CachePath.'/dbchache_'.$QMD5;
|
|
|
|
if ( is_file($Prefetch)){
|
|
|
|
if ( (time()- filemtime($Prefetch))<360){
|
|
$FData=file_get_contents($Prefetch);
|
|
$this->DebugLog .="[{$QS}] as (Cahed Data),\n";
|
|
return unserialize($FData);
|
|
} else {
|
|
|
|
$result = $this->DBLink->query( $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
|
|
$RetVal=array();
|
|
if( ( stripos( $QS, 'insert' ) === false ) && ( stripos( $QS, 'update' ) === false ) && $result !== false && is_a( $result, 'SQLite3Result' ) ){
|
|
while( $array = $result->fetchArray( SQLITE3_ASSOC )) $RetVal[]=$array;
|
|
$result->finalize();
|
|
};
|
|
$FData=serialize($RetVal);
|
|
$FD=fopen($Prefetch,'w');
|
|
fwrite($FD,$FData);
|
|
fclose($FD);
|
|
$this->DebugLog .="[{$QS}] as (".$this->DBLink->lastInsertRowID()."),\n";
|
|
return $RetVal;
|
|
}
|
|
} else {
|
|
$result = $this->DBLink->query( $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
|
|
$RetVal=array();
|
|
if( ( stripos( $QS, 'insert' ) === false ) && ( stripos( $QS, 'update' ) === false ) && $result !== false && is_a( $result, 'SQLite3Result' ) ){
|
|
while( $array = $result->fetchArray( SQLITE3_ASSOC )) $RetVal[]=$array;
|
|
$result->finalize();
|
|
};
|
|
$FData = serialize($RetVal);
|
|
$FD=fopen($Prefetch,'w');
|
|
fwrite($FD,$FData);
|
|
fclose($FD);
|
|
$this->DebugLog .="[{$QS}] as (".$this->DBLink->lastInsertRowID()."),\n";
|
|
return $RetVal;
|
|
}
|
|
} else {
|
|
$result = $this->DBLink->query( $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink));
|
|
$RetVal=array();
|
|
if( ( stripos( $QS, 'insert' ) === false ) && ( stripos( $QS, 'update' ) === false ) && $result !== false && is_a( $result, 'SQLite3Result' ) ){
|
|
while( $array = $result->fetchArray( SQLITE3_ASSOC )) $RetVal[]=$array;
|
|
$result->finalize();
|
|
};
|
|
$this->DebugLog .="[{$QS}] as (".$this->DBLink->lastInsertRowID()."),\n";
|
|
return $RetVal;
|
|
};
|
|
} else {
|
|
echo "No Connection to DB<br>";
|
|
};
|
|
}
|
|
function getLastInsertID(){
|
|
if($this->isConnected){
|
|
return $this->DBLink->lastInsertRowID();
|
|
};
|
|
return -1;
|
|
}
|
|
function getDebugLog(){
|
|
return $this->DebugLog;
|
|
}
|
|
function __destruct(){
|
|
if( $this->isConnected ){
|
|
$this->DBLink->close();
|
|
unset( $this->DBLink );
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|