Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/db/dbobject.class.php
<?php
/**
 * 
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */

class dbobject extends db_abstract {
	
	private $cnf, $query;
	
	public function __construct($cnf){
		$this->cnf = $cnf;
		unset($cnf);
		$DBRef = configs::get('db', 'relations', $this->cnf);
		if ( FALSE === $DBRef ){
			errors::raise('Missing CNF option : DBRef, not set for CNF "'.$this->cnf.'", abording!', CORE_LOG_ERROR, 'DBOBJ');
			return;
		}
		parent::__construct($DBRef);
		unset($DBRef);
		$this->query = configs::get($this->cnf, $this->getvendor(), NULL, 'cnf');
		if ( FALSE === $this->query ){
			errors::raise('No SQL queries could be loaded for CNF "'.$this->cnf.'" and vendor "'.$this->getvendor().'" abording!', CORE_LOG_ERROR, 'DBOBJ');
			return;
		}
		if ( FALSE === $this->connect() ){ return; }
		$table = configs::get($this->cnf, 'table', NULL, 'cnf');
		$tables = configs::get($this->cnf, 'tables', NULL, 'cnf');
		if ( FALSE !== $table ){
			$this->auto_table_create($table, $table);
		} elseif ( FALSE !== $tables ){
			foreach($tables as $k => &$table){
				$this->auto_table_create($table, $k);
			}
		}
	}
	
	private function auto_table_create($table, $ref){
		if ( TRUE === $this->table_exists($table) ){ return TRUE; }
		$qs = configs::get($this->cnf, 'create', Array($this->getvendor() ), 'cnf');
		if ( FALSE === $qs ){ 
			errors::raise('Auto creation of table '.$ref.' for CNF '.$this->cnf. ' failed :  no creation query found in cnf.', CORE_LOG_ERROR, 'DBOBJ');
			return FALSE; 
		}
		if ( FALSE === is_array($qs) ){ 
			$q = str_replace('%table%', $table, $qs);
			$struct = configs::get($this->cnf, 'struct', NULL, 'cnf');
		} elseif ( isset($qs[$ref]) ){
			$q = str_replace('%table%', $table, $qs[$ref]);
			$struct = configs::get($this->cnf, 'structs', $ref, 'cnf');
		} else {
			errors::raise('Auto creation of table '.$ref.' for CNF '.$this->cnf.' failed : missing creation query', CORE_LOG_ERROR, 'DBOBJ');
			return FALSE;			
		}
		if ( FALSE === $struct ){
			errors::raise('Auto creation of table '.$ref.' for CNF '.$this->cnf. ' failed :  no structure found in cnf.', CORE_LOG_ERROR, 'DBOBJ'); 
			return FALSE; 
		}
		if ( is_array($struct) ){
			foreach($struct as $int => &$field ){ $q = str_replace('%struct_'.$int.'%', $field, $q); }
			unset($int, $field);
		}
		errors::raise('Auto creation of table '.$ref.' for CNF '.$this->cnf. ' : '.$q, CORE_LOG_INFO, 'DBOBJ');
		return $this->create_table($q);
	}
	
	public function dbquery($args, $qref, $field=NULL){
		if ( ! $this->isconnected() ){ return FALSE; }
	 	$q = $this->gen_query($args, $qref);
	 	if ( FALSE === $q ){ return FALSE; }
	 	//errors::raise('SQL query : '.$q, CORE_LOG_DEBUG, 'SQL');
	 	$method = 'db_'.$this->query[$qref][0];
	 	if ( $method == 'db_fetchfield' OR $method == 'db_getonefield' ){
	 		if ( NULL === $field OR empty($field) ){
	 			errors::raise('Missing or Empty argument : field for query ref : '.$method.' do not expect any results other than FALSE', CORE_LOG_WARNING, 'DBOBJ');
	 		}
	 		return $this->$method($q, $field);
	 	} elseif (method_exists($this, $method) ){
		 	return $this->$method($q);
	 	} else {
	 		errors::raise('Unknow dbobject method defined in CNF for qref '.$qref, CORE_LOG_ERROR, 'DBOBJ');
	 		return FALSE;
	 	}
	}
	
	private function gen_query($args, $qref){
		$q = configs::get($this->cnf, $this->getvendor(), Array($qref, 1), 'cnf');
		if ( FALSE === $q ){
			errors::raise('Query reference : "'.$qref.'" was not set for cnf : "'.$this->cnf.'", vendor : '.$this->getvendor(), CORE_LOG_ERROR, 'DBOBJ');
			return FALSE;
		}
		if ( is_array($args) ){
			foreach($args as $k => $val ){
				$q = str_replace('%arg_'.$k.'%', $this->checkval($val), $q);
			}
			unset($k, $val);
		}
		$struct = configs::get($this->cnf, 'struct', NULL, 'cnf');
		if ( is_array($struct) ){
			foreach($struct as $int => $field ){
				$q = str_replace('%struct_'.$int.'%', $field, $q);
			}
			unset($int, $field);
		}
		unset($struct);
		$tables = configs::get($this->cnf, 'tables', NULL, 'cnf');
		if ( is_array($tables) ){
			$structs = configs::get($this->cnf, 'structs', NULL, 'cnf');
			foreach( $tables as $k => $tbl ){
				$q = str_replace('%table_'.$k.'%', $tbl, $q);
				if ( isset($structs[$k]) ){
					foreach( $structs[$k] as $int => $field ){
						$q = str_replace('%struct_'.$k.'_'.$int.'%', $tbl.'.'.$field, $q);
					}
					unset($int, $field);
				}
			}
			unset($k, $tbl, $tables, $structs);
		} else {
			$table = configs::get($this->cnf, 'table', NULL, 'cnf');
			$q = str_replace('%table%', $table, $q);
		}
		if (empty($q) ){
			errors::raise('An empty query has been generated for reference : "'.$qref.'" in cnf : "'.$this->cnf.'"', CORE_LOG_ERROR, 'DBOBJ');
			return FALSE;
		}
		return $q;
	}
	
	private function db_getcount($q){ 
		$r = $this->getonerow($q);
		if ( CORE::isError($r) ){ errors::broadcast($r);return FALSE; }
		if ( isset($r['COUNT(*)']) ){ return $r['COUNT(*)']; }
	}
	
	private function db_ispresent($q){	
		$r = $this->db_getcount($q);
		if ( FALSE === $r ){ return $r; }
		return ($r != 0);
	}
	
	private function db_getonefield($q, $field){ 
		$r = $this->db_getonerow($q);
		if ( CORE::isError($r) ){ errors::broadcast($r);echo $r->getmsg();return FALSE; }
		if ( isset($r[$field]) ){ return $r[$field]; }
		return FALSE;
	}	
	
	private function db_getonerow($q){	
		$r = parent::getonerow($q);
		if ( CORE::isError($r) ){ errors::broadcast($r);return FALSE; }
		return $this->replace_keys($r);
	}	
	
	private function db_fetchfield($q, $field){ 
		$r = $this->db_fetchrow($q);
		if ( isset($r[$field]) ){ return $r[$field]; }
		return FALSE;
	}	
	
	private function db_fetchrow($q){ 
		$r = parent::fetchrow($q);
		if ( CORE::isError($r) ){ errors::broadcast($r);return FALSE; }
		return $this->replace_keys($r);
	}	
	
	private function db_insert($q){
		$r = parent::insert($q);
		if ( CORE::isError($r) ){ errors::broadcast($r);return FALSE; }
		return TRUE;
	}	
	
	private function db_delete($q){ 
		$r = parent::delete($q);
		if ( CORE::isError($r) ){ errors::broadcast($r);return FALSE; }
		return TRUE;
	}	
	
	private function db_update($q){ 
		$r = parent::update($q);
		if ( CORE::isError($r) ){ errors::broadcast($r);return FALSE; }
		return TRUE;
	}	
	
	private function replace_keys($ar){
		if ( ! is_array($ar) ){ return $ar; }
		$tables = configs::get($this->cnf, 'tables', NULL, 'cnf');
		if ( FALSE === $tables ){
			$struct = configs::get($this->cnf, 'struct', NULL, 'cnf');
			foreach($ar as $k => &$val ){
				foreach( $struct as $int => &$field ){ 
					if ( $k == $field ){ $o[$int] = $val; } 
				}
				unset($ar[$k]);
			}
		} else {
			$structs = configs::get($this->cnf, 'structs', NULL, 'cnf');
			foreach($ar as $k => $val ){
				foreach( $tables as $tref => $tbl ){
					if ( isset($structs[$tref]) ){
						foreach( $structs[$tref] as $int => $field ){
							if ( $k == $field ){ $o[$int] = $val;break; }
						}
					}
				}
			}
		}
		if ( isset($o) ){ return $o; }
		return $ar;
	}
}

return TRUE;
Return current item: Simple Way to Usenet