mirror of
				https://github.com/optim-enterprises-bv/control-pane.git
				synced 2025-10-31 01:47:52 +00:00 
			
		
		
		
	* tweaked db class
getting to know the code
This commit is contained in:
		
							
								
								
									
										59
									
								
								php/db.php
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								php/db.php
									
									
									
									
									
								
							| @@ -1,25 +1,21 @@ | ||||
| <?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': | ||||
| 				$file_name=$this->_workdir.'/var/db/'.$database.'.sqlite'; | ||||
| @@ -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; | ||||
| 				} | ||||
|  | ||||
| 				$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'; | ||||
| @@ -57,7 +52,7 @@ class Db { | ||||
| 				$connect='sqlite:'.$file_name; | ||||
| 				break; | ||||
| 		} | ||||
| 		 | ||||
|  | ||||
| 		/* | ||||
| 		$databases=array( | ||||
| 			'tasks'=>'cbsdtaskd', | ||||
| @@ -99,30 +94,32 @@ class Db { | ||||
| 				break; | ||||
| 		} | ||||
| 		*/ | ||||
| 		 | ||||
| 		$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(is_null($connect)) { | ||||
| 			$this->error=true; | ||||
| 			$this->error_message='DB file name not set or invalid'; | ||||
| 			return; | ||||
| 		} | ||||
| 		 | ||||
| 		if(empty($connect)) return false; // Return from __construct doesn't work! | ||||
|  | ||||
| 		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){ | ||||
| 		if($quer=$this->_pdo->query($query)){ | ||||
| 			$res=$quer->fetchAll(PDO::FETCH_ASSOC); | ||||
| @@ -138,7 +135,7 @@ class Db { | ||||
| 		} | ||||
| 		return array(); | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	function insert($query){ | ||||
| 		if($quer=$this->_pdo->query($query)){ | ||||
| 			$lastID=$this->_pdo->lastInsertId(); | ||||
| @@ -147,17 +144,17 @@ class Db { | ||||
| 		$error=array('error'=>true,'info'=>$this->_pdo->errorInfo()); | ||||
| 		return $error; | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	function update($query) { | ||||
| 		if($quer=$this->_pdo->query($query)){ | ||||
| 			$rowCount=$quer->rowCount(); | ||||
| 			return array('rowCount'=>$rowCount); | ||||
| 		} | ||||
| 		$error=$this->_pdo->errorInfo(); | ||||
| 		return $error;	 | ||||
| 		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
	 Momchil Bozhinov
					Momchil Bozhinov