Location: PHPKode > scripts > PHP DataCache > php-datacachea1/DataCacheDaoPgDb.php
<?php
/**
 *	DataCacheDaoPgDb class definiton
 *
 *	concrete postgres database based DAO for the DataCache class
 *
 *	A PHP Class to implement server side data caching
 *	Copyright (C) 2003 Jason E. Sweat
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation; either
 *	version 2.1 of the License, or (at your option) any later version.
 *
 *	This library 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
 *	Lesser General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public
 *	License along with this library; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *	Modification History: <pre>
 *		$Log: DataCacheDaoPgDb.php,v $
 *		Revision 1.1.1.1  2003/12/11 05:22:10  jsweat
 *		Initial check-in of PHP DataCache library.
 *		
 *	</pre>
 *	@author			Jason E. Sweat
 *	@since			2003-11-21
 *	@package		DataCache
 *	@subpackage		DAO
 *	@version		$Id: DataCacheDaoPgDb.php,v 1.1.1.1 2003/12/11 05:22:10 jsweat Exp $
 */

/**
 *	stored proceedure to return the application ID for the specified application
 *
 *	will insert a new row if the name does not currently exist
 */
define('DATACACHE_DAOPGDB_SEL_APPL_ID', 'get_appl_id');
/**
 *	stored proceedure to check if a paricular data ID is cached for the specified application
 */
define('DATACACHE_DAOPGDB_CHK_DATA', 'is_cached');
/**
 *
 */
define('DATACACHE_DAOPGDB_SET_DATA', 'set_data');
/**
 *
 */
define('DATACACHE_DAOPGDB_GET_DATA', 'get_data');
/**
 *
 */
define('DATACACHE_DAOPGDB_DEL_DATA', 'del_data');
/**
 *
 */
define('DATACACHE_DAOPGDB_GET_DATA_DATE', 'get_cache_time');
/**
 *
 */
define('DATACACHE_DAOPGDB_IS_ZIP', 'is_compressed');

/**
 * DataCacheDaoPgDb implements a Data Access Object using {@link http://www.postgres.org/ Postgres} as the underlying data source
 *
 * @package		DataCache
 * @subpackage	DAO
 */
class DataCacheDaoPgDb
{
	/**
	 * @private
	 * @var	integer	$_miAppl		the row_id of the application
	 */
	var $_miAppl;

	/**
	 * constructor
	 *
	 * @param	string	$psAppl						the application identifier
	 * @param	object ADODbConnection	$poConn		an ADOdb connection object
	 * @return	void
	 */
	function DataCacheDaoPgDb($psAppl, &$poConn)
	{
		$this->_moConn =& $poConn;
		$this->_miAppl = $this->_GetApplId($psAppl);
	}
	
	/**
	 * return the application id from the text application name
	 *
	 * @access	private
	 * @param	string	$psAppl	the application identifier
	 * @return	integer			the application id
	 */
	function _GetApplId($psAppl)
	{
		$o_rs =& $this->_CallDbFunct(DATACACHE_DAOPGDB_SEL_APPL_ID, array($psAppl));
		if ($o_rs) {
			$a_row = $o_rs->FetchRow();
			return (int)$a_row[DATACACHE_DAOPGDB_SEL_APPL_ID];
		} else {
			trigger_error('Database Error: '.$this->_moConn->ErrorMsg());
			return false;
		}
	}
	
	/**
	 * call a postgres sql function and return the result set
	 *
	 * @access	private
	 * @param	string	$psFunct		the name of the function to call
	 * @param	array	$paBind			optional, the array of values to bind
	 * @return	object	ADORecordSet	the ADORecordSet returned from the call
	 */
	function &_CallDbFunct($psFunct, $paBind=array())
	{	
		$s_args = '';
		$i_binds = count($paBind);
		if ($i_binds) {
			$s_args = '(';
			foreach ($paBind as $m_bind) {
				$s_args .= '?, ';
			}
			$s_args = substr($s_args, 0, -2).')';
		}
		$o_rs =& $this->_moConn->Execute("select $psFunct$s_args", $paBind);
		return $o_rs;
	}
	
	
	/**
	 * cache a particular piece of data
	 *
	 * @param	string	$psId		identifier for the data being cached
	 * @param	mixed	$pmValue	value to cache
	 * @param	boolean	$pbZip		optional - compress the data, defaults to true
	 * @return	boolean				sucess
	 */
	function SetCache($psId, $pmValue, $pbZip=true)
	{
		if ($pbZip) {
			$s_data_flag = 'Y';
			$s_data = base64_encode(gzcompress(serialize($pmValue)));
		} else {
			$s_data_flag = 'N';
			$s_data = addslashes(serialize($pmValue));
		}
		$o_rs =& $this->_CallDbFunct(DATACACHE_DAOPGDB_SET_DATA,
										array($this->_miAppl
												,$psId
												,$s_data_flag 
												,$s_data));
		if ($o_rs) {
			$a_row = $o_rs->FetchRow();
			return ('1' == $a_row[DATACACHE_DAOPGDB_SET_DATA]) ? true : false;
		} else {
			trigger_error('Database Error: '.$this->_moConn->ErrorMsg());
			return false;
		}
	}
	
	/**
	 * check to see if a cached data is valid
	 *
	 * @param	string	$psId		identifier for the data being cached
	 * @return	boolean				result of check
	 */
	function IsCached($psId)
	{
		$o_rs =& $this->_CallDbFunct(DATACACHE_DAOPGDB_CHK_DATA,
										array($this->_miAppl, $psId));
		if ($o_rs) {
			$a_row = $o_rs->FetchRow();
			return ('1' == $a_row[DATACACHE_DAOPGDB_CHK_DATA]) ? true : false;
		} else {
			trigger_error('Database Error: '.$this->_moConn->ErrorMsg());
			return false;
		}
	}

	
	/**
	 * retrieve some cached data
	 *
	 * @param	string	$psId		identifier for the data being cached
	 * @return	mixed				the data cached
	 */
	function GetCache($psId)
	{
		if($this->IsCached($psId)) {
			$o_rs =& $this->_CallDbFunct(DATACACHE_DAOPGDB_GET_DATA,
											array($this->_miAppl, $psId));
			if ($o_rs) {
				$a_row = $o_rs->FetchRow();
				$s_ret = $a_row[DATACACHE_DAOPGDB_GET_DATA];
				$a_data = unpack('A1zip/A*data', $s_ret);
				switch ($a_data['zip']) {
				case 'Y':
					return unserialize(gzuncompress(base64_decode($a_data['data'])));
				case 'N':
					return unserialize(stripslashes($a_data['data']));
				default:
					trigger_error('Bad compressed indicator');
					return NULL;
				}
			} else {
				trigger_error('Database Error: '.$this->_moConn->ErrorMsg());
				return false;
			}
		} else {
			trigger_error("Data Identified '$psId' is not cached");
			return NULL;
		}
	}
	
	/**
	 * retrieve time when data was cached
	 *
	 * @param	string	$psId		identifier for the data being cached
	 * @return	mixed				the data cached
	 */
	function GetCacheTime($psId)
	{
		if($this->IsCached($psId)) {
			$o_rs =& $this->_CallDbFunct(DATACACHE_DAOPGDB_GET_DATA_DATE,
											array($this->_miAppl, $psId));
			if ($o_rs) {
				$a_row = $o_rs->FetchRow();
				return strtotime($a_row[DATACACHE_DAOPGDB_GET_DATA_DATE]);
			} else {
				trigger_error('Database Error: '.$this->_moConn->ErrorMsg());
				return false;
			}
		} else {
			trigger_error("Data Identified '$psId' is not cached");
			return false;
		}
	}
	
	/**
	 * remove cached data if valid
	 *
	 * @param	string	$psId		identifier for the cached data
	 * @return	boolean				result of removal
	 */
	function RemoveCache($psId)
	{
		if($this->IsCached($psId)) {
			$o_rs =& $this->_CallDbFunct(DATACACHE_DAOPGDB_DEL_DATA,
											array($this->_miAppl, $psId));
			if ($o_rs) {
				$a_row = $o_rs->FetchRow();
				return ('1' == $a_row[DATACACHE_DAOPGDB_DEL_DATA]) ? true : false;
			} else {
				trigger_error('Database Error: '.$this->_moConn->ErrorMsg());
				return false;
			}
		} else {
			trigger_error("Data Identified '$psId' is not cached");
			return false;
		}
	}
	
	/**
	 * remove a set cached data with identifiers matich a patter, if valid
	 *
	 * @todo	implement this - will require pulling of a list of valid cache ids for the
	 *			current application, then iterating over them with the passed regex
	 * @todo	investigate the possible use of Postgres regex query to avoid iteration in php
	 * @param	string	$psPattern		the identifier pattern for the cached data
	 * @return	boolean					result of removal
	 */
	function RemovePatternCache($psPattern)
	{
		return false;
	}
	

	
}


#?>
Return current item: PHP DataCache