mirror of
https://github.com/optim-enterprises-bv/control-pane.git
synced 2025-11-02 19:07:59 +00:00
* tweaked db class
getting to know the code
This commit is contained in:
39
php/db.php
39
php/db.php
@@ -1,24 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class Db {
|
class Db {
|
||||||
private $_pdo=null, $_connected;
|
private $_pdo=null;
|
||||||
private $_workdir='';
|
private $_workdir='';
|
||||||
private $_filename='';
|
private $_filename='';
|
||||||
public $error=false;
|
public $error=false;
|
||||||
public $error_message='';
|
public $error_message='';
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
$place = base (This is a basic set of databases: local, nodes, etc)
|
$place = base (This is a basic set of databases: local, nodes, etc)
|
||||||
$place = file (specify a specific database for the full pathth)
|
$place = file (specify a specific database for the full pathth)
|
||||||
*/
|
*/
|
||||||
function __destruct(){
|
|
||||||
//if($this->_pdo) $this->_pdo->close();
|
|
||||||
}
|
|
||||||
|
|
||||||
function __construct($place='base',$database=''){
|
function __construct($place='base',$database=''){
|
||||||
$this->_connected=false;
|
|
||||||
$this->_workdir=getenv('WORKDIR'); // /usr/jails/
|
$this->_workdir=getenv('WORKDIR'); // /usr/jails/
|
||||||
|
$connect = null;
|
||||||
|
$file_name = null;
|
||||||
|
|
||||||
switch($place){
|
switch($place){
|
||||||
case 'base':
|
case 'base':
|
||||||
@@ -34,11 +30,10 @@ class Db {
|
|||||||
///usr/jails/jails-system/cbsdpuppet1/helpers/redis.sqlite
|
///usr/jails/jails-system/cbsdpuppet1/helpers/redis.sqlite
|
||||||
$file_name=$this->_workdir.'/jails-system/'.$database['jname'].'/helpers/'.$database['helper'].".sqlite";
|
$file_name=$this->_workdir.'/jails-system/'.$database['jname'].'/helpers/'.$database['helper'].".sqlite";
|
||||||
$connect='sqlite:'.$file_name;
|
$connect='sqlite:'.$file_name;
|
||||||
break;
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
$file_name=$this->_workdir.'/formfile/'.$database.".sqlite";
|
$file_name=$this->_workdir.'/formfile/'.$database.".sqlite";
|
||||||
$connect='sqlite:'.$file_name;
|
$connect='sqlite:'.$file_name;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'cbsd-settings':
|
case 'cbsd-settings':
|
||||||
$file_name=$this->_workdir.'/jails-system/CBSDSYS/helpers/cbsd.sqlite';
|
$file_name=$this->_workdir.'/jails-system/CBSDSYS/helpers/cbsd.sqlite';
|
||||||
@@ -100,27 +95,29 @@ class Db {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$this->_filename=$file_name;
|
if(is_null($file_name) || !file_exists($file_name)){
|
||||||
//echo $file_name,PHP_EOL,PHP_EOL;
|
|
||||||
|
|
||||||
if(!isset($file_name) || empty($file_name) || !file_exists($file_name)){
|
|
||||||
$this->error=true;
|
$this->error=true;
|
||||||
$this->error_message='DB file not found!';
|
$this->error_message='DB file name not set or not found!';
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(empty($connect)) return false; // Return from __construct doesn't work!
|
if(is_null($connect)) {
|
||||||
|
$this->error=true;
|
||||||
|
$this->error_message='DB file name not set or invalid';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->_pdo = new PDO($connect);
|
$this->_pdo = new PDO($connect);
|
||||||
$this->_pdo->setAttribute(PDO::ATTR_TIMEOUT,5000);
|
$this->_pdo->setAttribute(PDO::ATTR_TIMEOUT,5000);
|
||||||
$this->_connected=true;
|
|
||||||
|
|
||||||
}catch (PDOException $e){
|
}catch (PDOException $e){
|
||||||
$this->error=true;
|
$this->error=true;
|
||||||
$this->error_message=$e->getMessage(); //'DB Error';
|
$this->error_message=$e->getMessage(); //'DB Error';
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->_filename=$file_name;
|
||||||
|
//echo $file_name,PHP_EOL,PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
function select($query){
|
function select($query){
|
||||||
@@ -157,7 +154,7 @@ class Db {
|
|||||||
return $error;
|
return $error;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isConnected(){ return($this->_connected); }
|
function isConnected(){ return( !is_null($this->_pdo); }
|
||||||
function getWorkdir(){ return $this->_workdir; }
|
function getWorkdir(){ return $this->_workdir; }
|
||||||
function getFileName(){ return $this->_filename; }
|
function getFileName(){ return $this->_filename; }
|
||||||
function escape($str){ return SQLite3::escapeString($str); } // For now sqlite only!
|
function escape($str){ return SQLite3::escapeString($str); } // For now sqlite only!
|
||||||
|
|||||||
Reference in New Issue
Block a user