Location: PHPKode > scripts > Persist Class > persist-class/persistclass/PersistClass.php
<?php

require_once(dirname(__FILE__).'/dbconnection/exception/DbException.php');
require_once(dirname(__FILE__).'/dbconnection/exception/ConnectionException.php');
require_once(dirname(__FILE__).'/dbconnection/exception/NoDataException.php');
require_once(dirname(__FILE__).'/dbconnection/exception/NoResultException.php');
require_once(dirname(__FILE__).'/dbconnection/exception/QueryException.php');
require_once(dirname(__FILE__).'/dbconnection/DbConnectionPool.php');
require_once(dirname(__FILE__).'/dbconnection/DbConnection.php');
require_once(dirname(__FILE__).'/dbconnection/DbConnectionMysql.php');
require_once(dirname(__FILE__).'/dbconnection/DbConnectionOracle.php');



class PersistClass {

	protected $sqlPrimaryKey;
	protected $sqlTableName;
	protected $sqlConnectionName = null;

	protected $sqlData=array();
	protected $sqlChangedFields = null;
	protected $sqlNotEscapedFields = null;

	public function __construct(&$indata = null) {
		$this->loadObject($indata);
	}

	private function loadObject(&$indata = null) {
		if(is_array($indata)) {
			$this->sqlData = $indata;
			$this->init();
		}
		else if(is_null($indata)) {

		}
		else {
			$this->initId($indata);
		}
	}

	public function reInit() {
		loadObject($this->getId());
	}

	public function initId($id) {
		$con = DbConnectionPool::instance()->getConnection($this->sqlConnectionName);
		$row = PersistClass::execSelectSql($con, $this->sqlTableName, $this->sqlPrimaryKey, $id);
		$this->loadObject($row);
	}

	protected function init() {

	}

	public function getAllItems($orderColumn = null) {
		$q = 'select ' . $this->sqlPrimaryKey . ' from ' . $this->sqlTableName;
		if($orderColumn != null) $q .= ' order by '.$orderColumn;
		$con = DbConnectionPool::instance()->getConnection($this->sqlConnectionName);
		$ids = $con->queryFirstColumnSet($q);
		return $this->getItemsWithIds($ids);
	}

	public function getItemsWithIds(&$ids) {
		$items = array();
		$class = get_class($this);
		foreach($ids as $id) {
			$item = new $class;
			$item->initId($id);
			$items[$id] = $item;
		}
		return $items;
	}

	public function getDataArr() {
		return $this->sqlData;
	}

	public function isData($key) {
		if(array_key_exists($key, $this->sqlData)) return true;
		else return false;
	}

	public function getData($key) {
		if(!array_key_exists($key, $this->sqlData)) throw new NoDataException($key.' not found in '.$this->sqlTableName);
		return $this->sqlData[$key];
	}

	public function getHtmlData($key) {
		return Escaper::html($this->getData($key));
	}

	public function setData($key, $value, $escape = true)
	{
		if(!array_key_exists($key, $this->sqlData)) throw new NoDataException($key.' cant be set. Doesnt exist in '.$this->sqlTableName);
		$this->setNewData($key, $value, $escape);
	}

	public function setNewData($key, $value, $escape = true) {
		$markNotEscaped = $this->sqlNotEscapedFields === null ? false : array_key_exists($key, $this->sqlChangedFields);
		if((!array_key_exists($key, $this->sqlData) || $this->sqlData[$key] != $value) || $escape == $markNotEscaped) {
			$this->sqlData[$key] = $value;
			$this->initSqlChangedFields();
			if(!array_key_exists($key, $this->sqlChangedFields)) $this->sqlChangedFields[$key] = $key;
			if(!$escape) {
				$this->initSqlNotEscapedFields();
				if(!array_key_exists($key, $this->sqlNotEscapedFields)) $this->sqlNotEscapedFields[$key] = $key;
			} else {
				if($this->sqlNotEscapedFields !== null) unset($this->sqlNotEscapedFields[$key]);
			}
		}
	}

	private function initSqlNotEscapedFields() {
		if($this->sqlNotEscapedFields === null) $this->sqlNotEscapedFields = array();
	}

	private function initSqlChangedFields() {
		if($this->sqlChangedFields === null) $this->sqlChangedFields = array();
	}

	public function getId() {
		return $this->sqlData[$this->sqlPrimaryKey];
	}

	public function setId($primkeyval) {
		$this->initSqlChangedFields();
		$this->sqlChangedFields[] = $this->sqlPrimaryKey;
		$this->sqlData[$this->sqlPrimaryKey]=$primkeyval;
	}


	public function insertDb($primkeyval = null) {
		$con = DbConnectionPool::instance()->getConnection($this->sqlConnectionName);
		$id=PersistClass::execInsertSql($con, $this->sqlTableName, $this->sqlPrimaryKey, $this->sqlData, $primkeyval, $this->sqlNotEscapedFields);
		$this->sqlData[$this->sqlPrimaryKey] = $id;
		$this->sqlChangedFields = null;
		$this->sqlNotEscapedFields = null;
		return $id;
	}

	public function updateDb() {
		$this->initSqlChangedFields();
		$changed = $this->sqlChangedFields;
		if(count($changed)>0) {
			$con = DbConnectionPool::instance()->getConnection($this->sqlConnectionName);
			PersistClass::execUpdateSql($con, $this->sqlTableName, $this->sqlPrimaryKey, $this->sqlData, $changed, $this->sqlNotEscapedFields);
			if($con->getRowsNum()<1)
				throw new NoResultException('Update DB failed, id: ' . $this->getId());
			$this->sqlChangedFields = null;
			$this->sqlNotEscapedFields = null;
		}
	}

	public function deleteDb($id = null) {
		$affectedRows;
		if($id === null) {
			return $this->deleteDbThis();
		} else {
			return $this->deleteDbOther($id);
		}
		return $affectedRows;
	}

	private function deleteDbThis() {
		$con = DbConnectionPool::instance()->getConnection($this->sqlConnectionName);
		$affectedRows = PersistClass::execDeleteSql($con, $this->sqlTableName, $this->sqlPrimaryKey, $this->getId());
		if($con->getRowsNum()<1) throw new NoResultException('Delete DB failed, id: ' . $this->getId());
		$this->sqlChangedFields = null;
		$this->sqlNotEscapedFields = null;
		return $affectedRows;
	}

	private function deleteDbOther($id) {
		$con = DbConnectionPool::instance()->getConnection($this->sqlConnectionName);
		return PersistClass::execDeleteSql($con, $this->sqlTableName, $this->sqlPrimaryKey, $id);
	}

	public static function execInsertSql(&$con, &$sqlTableName, &$sqlPrimaryKey, &$sqlData, $primkeyval=null, &$sqlNotEscapedFields) {
		if($primkeyval != null)
			$sqlData[$sqlPrimaryKey] = $primkeyval;
		$colcomma = false;
		$valcomma = false;
		$columns='(';
		$values=' values(';
		foreach($sqlData as $datakey => $dataval) {
			if($colcomma) $columns .= ', ';
			else $colcomma=true;
			$columns .= $datakey;

			if($valcomma) $values .= ', ';
			else $valcomma = true;

			if($dataval !== null ) {
				$sqlVal = ($sqlNotEscapedFields !== null && array_key_exists($datakey, $sqlNotEscapedFields)) ? $dataval : $con->escape($dataval);
				$values .= $sqlVal;
			}
			else $values .= 'NULL';
		}

		$columns.=')';
		$values.=')';
		$q = 'insert into '.$sqlTableName.$columns.$values;
		$con->query($q);
		if($primkeyval != null) 
			return $primkeyval;
		else if(array_key_exists($sqlPrimaryKey, $sqlData))
			return $sqlData[$sqlPrimaryKey];
		else return $con->getLastId();
	}

	public static function execUpdateSql(&$con, &$sqlTableName, &$sqlPrimaryKey, &$sqlData, &$sqlChangedFields=null, &$sqlNotEscapedFields=null) {
		if(!(is_array($sqlChangedFields) && count($sqlChangedFields)==0)) {

			$q='update '.$sqlTableName.' set ';
			$comma=false;

			$forcechange = $sqlChangedFields === null ? true : false;
			foreach($sqlData as $key => $value) {
				if($forcechange || array_key_exists($key, $sqlChangedFields)) {
					if($comma)
					$q .= ', ';
					else
					$comma = true;
					if($value !== null) {
						$sqlVal = ($sqlNotEscapedFields !== null && array_key_exists($key, $sqlNotEscapedFields)) ? $value : $con->escape($value);
						$q.=$key.'='.$sqlVal;
					}
					else $q.=$key.'=NULL';
				}
			}

			$q.=' where '.$sqlPrimaryKey.'='.$con->escape($sqlData[$sqlPrimaryKey]);
			$con->query($q);
		}
	}

	private static function execDeleteSql(&$con, &$sqlTableName, &$sqlPrimaryKey, $sqlPrimaryKeyVal) {
		$q='delete from '.$sqlTableName.' where '.$sqlPrimaryKey.'='.$con->escape($sqlPrimaryKeyVal);
		return $con->query($q);
	}

	public static function execSelectSql(&$con, &$sqlTableName, &$sqlPrimaryKey, $primKeyVal) {
		try {
			$q='select * from '.$sqlTableName.' where '.$sqlPrimaryKey.'='.$con->escape($primKeyVal);
			$row = $con->queryFirstRow($q);
			return $row;
		} catch(NoResultException $e) {
			throw new NoResultException('Id '.$primKeyVal.' does not exist in '.$sqlTableName . ' in column: ' . $sqlPrimaryKey);
		}
	}

	public function setSqlTableName($string) {
		$this->sqlTableName = $string;
	}

	public function setSqlPrimaryKey($string) {
		$this->sqlPrimaryKey = $string;
	}
}

?>
Return current item: Persist Class