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