diff options
Diffstat (limited to 'framework/Data/SqlMap/DataMapper')
-rw-r--r-- | framework/Data/SqlMap/DataMapper/TFastSqlMapApplicationCache.php | 178 | ||||
-rw-r--r-- | framework/Data/SqlMap/DataMapper/TLazyLoadList.php | 286 | ||||
-rw-r--r-- | framework/Data/SqlMap/DataMapper/TPropertyAccess.php | 312 | ||||
-rw-r--r-- | framework/Data/SqlMap/DataMapper/TSqlMapCache.php | 590 | ||||
-rw-r--r-- | framework/Data/SqlMap/DataMapper/TSqlMapException.php | 230 | ||||
-rw-r--r-- | framework/Data/SqlMap/DataMapper/TSqlMapPagedList.php | 416 | ||||
-rw-r--r-- | framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php | 382 |
7 files changed, 1197 insertions, 1197 deletions
diff --git a/framework/Data/SqlMap/DataMapper/TFastSqlMapApplicationCache.php b/framework/Data/SqlMap/DataMapper/TFastSqlMapApplicationCache.php index d780b413..e85c54cf 100644 --- a/framework/Data/SqlMap/DataMapper/TFastSqlMapApplicationCache.php +++ b/framework/Data/SqlMap/DataMapper/TFastSqlMapApplicationCache.php @@ -1,89 +1,89 @@ -<?php
-/**
- * TFastSqlMapApplicationCache class file contains Fast SqlMap cache implementation.
- *
- * @author Berczi Gabor <gabor.berczi@devworx.hu>
- * @link http://www.pradosoft.com/
- * @copyright Copyright © 2005-2012 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id: TFastSqlMapApplicationCache.php 2996 2011-06-20 15:24:57Z ctrlaltca@gmail.com $
- * @package System.Data.SqlMap
- */
-
-/**
- * TFastSqlMapApplicationCache class file
- *
- * Fast SqlMap result cache class with minimal-concurrency get/set and atomic flush operations
- *
- * @author Berczi Gabor <gabor.berczi@devworx.hu>
- * @version $Id: TFastSqlMapApplicationCache.php 2996 2011-06-20 15:24:57Z ctrlaltca@gmail.com $
- * @package System.Data.SqlMap
- * @since 3.2
- */
-
-class TFastSqlMapApplicationCache implements ICache
-{
- protected $_cacheModel=null;
- protected $_cache=null;
-
- public function __construct($cacheModel=null)
- {
- $this->_cacheModel = $cacheModel;
- }
-
- protected function getBaseKeyKeyName()
- {
- return 'SqlMapCacheBaseKey::'.$this->_cacheModel->getId();
- }
-
- protected function getBaseKey()
- {
- $cache = $this->getCache();
- $keyname = $this->getBaseKeyKeyName();
- $basekey = $cache->get($keyname);
- if (!$basekey)
- {
- $basekey = DxUtil::generateRandomHash(8);
- $cache->set($keyname,$basekey);
- }
- return $basekey;
- }
-
- protected function getCacheKey($key)
- {
- return $this->getBaseKey().'###'.$key;
- }
-
- public function delete($key)
- {
- $this->getCache()->delete($this->getCacheKey($key));
- }
-
- public function flush()
- {
- $this->getCache()->delete($this->getBaseKeyKeyName());
- }
-
- public function get($key)
- {
- $result = $this->getCache()->get($this->getCacheKey($key));
- return $result === false ? null : $result;
- }
-
- public function set($key, $value,$expire=0,$dependency=null)
- {
- $this->getCache()->set($this->getCacheKey($key), $value, $expire,$dependency);
- }
-
- protected function getCache()
- {
- if (!$this->_cache)
- $this->_cache = Prado::getApplication()->getCache();
- return $this->_cache;
- }
-
- public function add($id,$value,$expire=0,$dependency=null)
- {
- throw new TSqlMapException('sqlmap_use_set_to_store_cache');
- }
-}
+<?php +/** + * TFastSqlMapApplicationCache class file contains Fast SqlMap cache implementation. + * + * @author Berczi Gabor <gabor.berczi@devworx.hu> + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2012 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id: TFastSqlMapApplicationCache.php 2996 2011-06-20 15:24:57Z ctrlaltca@gmail.com $ + * @package System.Data.SqlMap + */ + +/** + * TFastSqlMapApplicationCache class file + * + * Fast SqlMap result cache class with minimal-concurrency get/set and atomic flush operations + * + * @author Berczi Gabor <gabor.berczi@devworx.hu> + * @version $Id: TFastSqlMapApplicationCache.php 2996 2011-06-20 15:24:57Z ctrlaltca@gmail.com $ + * @package System.Data.SqlMap + * @since 3.2 + */ + +class TFastSqlMapApplicationCache implements ICache +{ + protected $_cacheModel=null; + protected $_cache=null; + + public function __construct($cacheModel=null) + { + $this->_cacheModel = $cacheModel; + } + + protected function getBaseKeyKeyName() + { + return 'SqlMapCacheBaseKey::'.$this->_cacheModel->getId(); + } + + protected function getBaseKey() + { + $cache = $this->getCache(); + $keyname = $this->getBaseKeyKeyName(); + $basekey = $cache->get($keyname); + if (!$basekey) + { + $basekey = DxUtil::generateRandomHash(8); + $cache->set($keyname,$basekey); + } + return $basekey; + } + + protected function getCacheKey($key) + { + return $this->getBaseKey().'###'.$key; + } + + public function delete($key) + { + $this->getCache()->delete($this->getCacheKey($key)); + } + + public function flush() + { + $this->getCache()->delete($this->getBaseKeyKeyName()); + } + + public function get($key) + { + $result = $this->getCache()->get($this->getCacheKey($key)); + return $result === false ? null : $result; + } + + public function set($key, $value,$expire=0,$dependency=null) + { + $this->getCache()->set($this->getCacheKey($key), $value, $expire,$dependency); + } + + protected function getCache() + { + if (!$this->_cache) + $this->_cache = Prado::getApplication()->getCache(); + return $this->_cache; + } + + public function add($id,$value,$expire=0,$dependency=null) + { + throw new TSqlMapException('sqlmap_use_set_to_store_cache'); + } +} diff --git a/framework/Data/SqlMap/DataMapper/TLazyLoadList.php b/framework/Data/SqlMap/DataMapper/TLazyLoadList.php index 9b163960..8e5d8d85 100644 --- a/framework/Data/SqlMap/DataMapper/TLazyLoadList.php +++ b/framework/Data/SqlMap/DataMapper/TLazyLoadList.php @@ -1,144 +1,144 @@ -<?php
-/**
- * TLazyLoadList, TObjectProxy classes file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
+<?php +/** + * TLazyLoadList, TObjectProxy classes file. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link http://www.pradosoft.com/ * @copyright Copyright © 2005-2012 PradoSoft - * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.SqlMap
- */
-
-/**
- * TLazyLoadList executes mapped statements when the proxy collection is first accessed.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TLazyLoadList
-{
- private $_param;
- private $_target;
- private $_propertyName='';
- private $_statement='';
- private $_loaded=false;
- private $_innerList;
- private $_connection;
-
- /**
- * Create a new proxy list that will execute the mapped statement when any
- * of the list's method are accessed for the first time.
- * @param TMappedStatement statement to be executed to load the data.
- * @param mixed parameter value for the statement.
- * @param object result object that contains the lazy collection.
- * @param string property of the result object to set the loaded collection.
- */
- protected function __construct($mappedStatement, $param, $target, $propertyName)
- {
- $this->_param = $param;
- $this->_target = $target;
- $this->_statement = $mappedStatement;
- $this->_connection=$mappedStatement->getManager()->getDbConnection();
- $this->_propertyName = $propertyName;
- }
-
- /**
- * Create a new instance of a lazy collection.
- * @param TMappedStatement statement to be executed to load the data.
- * @param mixed parameter value for the statement.
- * @param object result object that contains the lazy collection.
- * @param string property of the result object to set the loaded collection.
- * @return TObjectProxy proxied collection object.
- */
- public static function newInstance($mappedStatement, $param, $target, $propertyName)
- {
- $handler = new self($mappedStatement, $param, $target, $propertyName);
- $statement = $mappedStatement->getStatement();
- $registry=$mappedStatement->getManager()->getTypeHandlers();
- $list = $statement->createInstanceOfListClass($registry);
- if(!is_object($list))
- throw new TSqlMapExecutionException('sqlmap_invalid_lazyload_list',$statement->getID());
- return new TObjectProxy($handler, $list);
- }
-
- /**
- * Relay the method call to the underlying collection.
- * @param string method name.
- * @param array method parameters.
- */
- public function intercept($method, $arguments)
- {
- return call_user_func_array(array($this->_innerList, $method), $arguments);
- }
-
- /**
- * Load the data by executing the mapped statement.
- */
- protected function fetchListData()
- {
- if($this->_loaded == false)
- {
- $this->_innerList = $this->_statement->executeQueryForList($this->_connection,$this->_param);
- $this->_loaded = true;
- //replace the target property with real list
- TPropertyAccess::set($this->_target, $this->_propertyName, $this->_innerList);
- }
- }
-
- /**
- * Try to fetch the data when any of the proxy collection method is called.
- * @param string method name.
- * @return boolean true if the underlying collection has the corresponding method name.
- */
- public function hasMethod($method)
- {
- $this->fetchListData();
- if(is_object($this->_innerList))
- return in_array($method, get_class_methods($this->_innerList));
- return false;
- }
-}
-
-/**
- * TObjectProxy sets up a simple object that intercepts method calls to a
- * particular object and relays the call to handler object.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TObjectProxy
-{
- private $_object;
- private $_handler;
-
- /**
- * @param object handler to method calls.
- * @param object the object to by proxied.
- */
- public function __construct($handler, $object)
- {
- $this->_handler = $handler;
- $this->_object = $object;
- }
-
- /**
- * Relay the method call to the handler object (if able to be handled), otherwise
- * it calls the proxied object's method.
- * @param string method name called
- * @param array method arguments
- * @return mixed method return value.
- */
- public function __call($method,$params)
- {
- if($this->_handler->hasMethod($method))
- return $this->_handler->intercept($method, $params);
- else
- return call_user_func_array(array($this->_object, $method), $params);
- }
-}
-
+ * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.SqlMap + */ + +/** + * TLazyLoadList executes mapped statements when the proxy collection is first accessed. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TLazyLoadList +{ + private $_param; + private $_target; + private $_propertyName=''; + private $_statement=''; + private $_loaded=false; + private $_innerList; + private $_connection; + + /** + * Create a new proxy list that will execute the mapped statement when any + * of the list's method are accessed for the first time. + * @param TMappedStatement statement to be executed to load the data. + * @param mixed parameter value for the statement. + * @param object result object that contains the lazy collection. + * @param string property of the result object to set the loaded collection. + */ + protected function __construct($mappedStatement, $param, $target, $propertyName) + { + $this->_param = $param; + $this->_target = $target; + $this->_statement = $mappedStatement; + $this->_connection=$mappedStatement->getManager()->getDbConnection(); + $this->_propertyName = $propertyName; + } + + /** + * Create a new instance of a lazy collection. + * @param TMappedStatement statement to be executed to load the data. + * @param mixed parameter value for the statement. + * @param object result object that contains the lazy collection. + * @param string property of the result object to set the loaded collection. + * @return TObjectProxy proxied collection object. + */ + public static function newInstance($mappedStatement, $param, $target, $propertyName) + { + $handler = new self($mappedStatement, $param, $target, $propertyName); + $statement = $mappedStatement->getStatement(); + $registry=$mappedStatement->getManager()->getTypeHandlers(); + $list = $statement->createInstanceOfListClass($registry); + if(!is_object($list)) + throw new TSqlMapExecutionException('sqlmap_invalid_lazyload_list',$statement->getID()); + return new TObjectProxy($handler, $list); + } + + /** + * Relay the method call to the underlying collection. + * @param string method name. + * @param array method parameters. + */ + public function intercept($method, $arguments) + { + return call_user_func_array(array($this->_innerList, $method), $arguments); + } + + /** + * Load the data by executing the mapped statement. + */ + protected function fetchListData() + { + if($this->_loaded == false) + { + $this->_innerList = $this->_statement->executeQueryForList($this->_connection,$this->_param); + $this->_loaded = true; + //replace the target property with real list + TPropertyAccess::set($this->_target, $this->_propertyName, $this->_innerList); + } + } + + /** + * Try to fetch the data when any of the proxy collection method is called. + * @param string method name. + * @return boolean true if the underlying collection has the corresponding method name. + */ + public function hasMethod($method) + { + $this->fetchListData(); + if(is_object($this->_innerList)) + return in_array($method, get_class_methods($this->_innerList)); + return false; + } +} + +/** + * TObjectProxy sets up a simple object that intercepts method calls to a + * particular object and relays the call to handler object. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TObjectProxy +{ + private $_object; + private $_handler; + + /** + * @param object handler to method calls. + * @param object the object to by proxied. + */ + public function __construct($handler, $object) + { + $this->_handler = $handler; + $this->_object = $object; + } + + /** + * Relay the method call to the handler object (if able to be handled), otherwise + * it calls the proxied object's method. + * @param string method name called + * @param array method arguments + * @return mixed method return value. + */ + public function __call($method,$params) + { + if($this->_handler->hasMethod($method)) + return $this->_handler->intercept($method, $params); + else + return call_user_func_array(array($this->_object, $method), $params); + } +} + diff --git a/framework/Data/SqlMap/DataMapper/TPropertyAccess.php b/framework/Data/SqlMap/DataMapper/TPropertyAccess.php index 7445e9d8..a6a2c451 100644 --- a/framework/Data/SqlMap/DataMapper/TPropertyAccess.php +++ b/framework/Data/SqlMap/DataMapper/TPropertyAccess.php @@ -1,156 +1,156 @@ -<?php
-/**
- * TPropertyAccess class file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright © 2005-2012 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.SqlMap
- */
-
-/**
- * TPropertyAccess class provides dot notation stype property access and setting.
- *
- * Access object's properties (and subproperties) using dot path notation.
- * The following are equivalent.
- * <code>
- * echo $obj->property1;
- * echo $obj->getProperty1();
- * echo $obj['property1']; //$obj may be an array or object
- * echo TPropertyAccess($obj, 'property1');
- * </code>
- *
- * Setting a property value.
- * <code>
- * $obj1->propert1 = 'hello';
- * $obj->setProperty('hello');
- * $obj['property1'] = 'hello'; //$obj may be an array or object
- * TPropertyAccess($obj, 'property1', 'hello');
- * </code>
- *
- * Subproperties are supported using the dot notation. E.g.
- * <code>
- * echo $obj->property1->property2->property3
- * echo TPropertyAccess::get($obj, 'property1.property2.property3');
- * </code>
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TPropertyAccess
-{
- /**
- * Gets the property value.
- * @param mixed object or path.
- * @param string property path.
- * @return mixed property value.
- * @throws TInvalidDataValueException if property path is invalid.
- */
- public static function get($object,$path)
- {
- if(!is_array($object) && !is_object($object))
- return $object;
- $properties = explode('.', $path);
- foreach($properties as $prop)
- {
- if(is_array($object) || $object instanceof ArrayAccess)
- {
- if(array_key_exists($prop, $object))
- $object = $object[$prop];
- else
- throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
- }
- else if(is_object($object))
- {
- $getter = 'get'.$prop;
- if(method_exists($object, $getter) && is_callable(array($object, $getter)))
- $object = $object->{$getter}();
- else if(in_array($prop, array_keys(get_object_vars($object))))
- $object = $object->{$prop};
- elseif(method_exists($object, '__get') && is_callable(array($object, '__get')))
- $object = $object->{$prop};
- else
- throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
- }
- else
- throw new TInvalidPropertyException('sqlmap_invalid_property',$path);
- }
- return $object;
- }
-
- /**
- * @param mixed object or array
- * @param string property path.
- * @return boolean true if property path is valid
- */
- public static function has($object, $path)
- {
- if(!is_array($object) && !is_object($object))
- return false;
- $properties = explode('.', $path);
- foreach($properties as $prop)
- {
- if(is_array($object) || $object instanceof ArrayAccess)
- {
- if(array_key_exists($prop, $object))
- $object = $object[$prop];
- else
- return false;
- }
- else if(is_object($object))
- {
- $getter = 'get'.$prop;
- if(method_exists($object, $getter) && is_callable(array($object, $getter)))
- $object = $object->{$getter}();
- else if(in_array($prop, array_keys(get_object_vars($object))))
- $object = $object->{$prop};
- elseif(method_exists($object, '__get') && is_callable(array($object, '__get')))
- $object = $object->{$prop};
- else
- return false;
- }
- else
- return false;
- }
- return true;
- }
-
- /**
- * Sets the property value.
- * @param mixed object or array
- * @param string property path.
- * @param mixed new property value.
- * @throws TInvalidDataValueException if property path is invalid.
- */
- public static function set(&$originalObject, $path, $value)
- {
- $properties = explode('.', $path);
- $prop = array_pop($properties);
- if(count($properties) > 0)
- $object = self::get($originalObject, implode('.',$properties));
- else
- $object = &$originalObject;
-
- if(is_array($object) || $object instanceof ArrayAccess)
- {
- $object[$prop] = $value;
- }
- else if(is_object($object))
- {
- $setter = 'set'.$prop;
- if (method_exists($object, $setter) && is_callable(array($object, $setter)))
- $object->{$setter}($value);
- else
- $object->{$prop} = $value;
- }
- else
- throw new TInvalidPropertyException('sqlmap_invalid_property_type',$path);
- }
-
-}
-
-?>
+<?php +/** + * TPropertyAccess class file. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2012 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.SqlMap + */ + +/** + * TPropertyAccess class provides dot notation stype property access and setting. + * + * Access object's properties (and subproperties) using dot path notation. + * The following are equivalent. + * <code> + * echo $obj->property1; + * echo $obj->getProperty1(); + * echo $obj['property1']; //$obj may be an array or object + * echo TPropertyAccess($obj, 'property1'); + * </code> + * + * Setting a property value. + * <code> + * $obj1->propert1 = 'hello'; + * $obj->setProperty('hello'); + * $obj['property1'] = 'hello'; //$obj may be an array or object + * TPropertyAccess($obj, 'property1', 'hello'); + * </code> + * + * Subproperties are supported using the dot notation. E.g. + * <code> + * echo $obj->property1->property2->property3 + * echo TPropertyAccess::get($obj, 'property1.property2.property3'); + * </code> + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TPropertyAccess +{ + /** + * Gets the property value. + * @param mixed object or path. + * @param string property path. + * @return mixed property value. + * @throws TInvalidDataValueException if property path is invalid. + */ + public static function get($object,$path) + { + if(!is_array($object) && !is_object($object)) + return $object; + $properties = explode('.', $path); + foreach($properties as $prop) + { + if(is_array($object) || $object instanceof ArrayAccess) + { + if(array_key_exists($prop, $object)) + $object = $object[$prop]; + else + throw new TInvalidPropertyException('sqlmap_invalid_property',$path); + } + else if(is_object($object)) + { + $getter = 'get'.$prop; + if(method_exists($object, $getter) && is_callable(array($object, $getter))) + $object = $object->{$getter}(); + else if(in_array($prop, array_keys(get_object_vars($object)))) + $object = $object->{$prop}; + elseif(method_exists($object, '__get') && is_callable(array($object, '__get'))) + $object = $object->{$prop}; + else + throw new TInvalidPropertyException('sqlmap_invalid_property',$path); + } + else + throw new TInvalidPropertyException('sqlmap_invalid_property',$path); + } + return $object; + } + + /** + * @param mixed object or array + * @param string property path. + * @return boolean true if property path is valid + */ + public static function has($object, $path) + { + if(!is_array($object) && !is_object($object)) + return false; + $properties = explode('.', $path); + foreach($properties as $prop) + { + if(is_array($object) || $object instanceof ArrayAccess) + { + if(array_key_exists($prop, $object)) + $object = $object[$prop]; + else + return false; + } + else if(is_object($object)) + { + $getter = 'get'.$prop; + if(method_exists($object, $getter) && is_callable(array($object, $getter))) + $object = $object->{$getter}(); + else if(in_array($prop, array_keys(get_object_vars($object)))) + $object = $object->{$prop}; + elseif(method_exists($object, '__get') && is_callable(array($object, '__get'))) + $object = $object->{$prop}; + else + return false; + } + else + return false; + } + return true; + } + + /** + * Sets the property value. + * @param mixed object or array + * @param string property path. + * @param mixed new property value. + * @throws TInvalidDataValueException if property path is invalid. + */ + public static function set(&$originalObject, $path, $value) + { + $properties = explode('.', $path); + $prop = array_pop($properties); + if(count($properties) > 0) + $object = self::get($originalObject, implode('.',$properties)); + else + $object = &$originalObject; + + if(is_array($object) || $object instanceof ArrayAccess) + { + $object[$prop] = $value; + } + else if(is_object($object)) + { + $setter = 'set'.$prop; + if (method_exists($object, $setter) && is_callable(array($object, $setter))) + $object->{$setter}($value); + else + $object->{$prop} = $value; + } + else + throw new TInvalidPropertyException('sqlmap_invalid_property_type',$path); + } + +} + +?> diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapCache.php b/framework/Data/SqlMap/DataMapper/TSqlMapCache.php index db027013..4e713fea 100644 --- a/framework/Data/SqlMap/DataMapper/TSqlMapCache.php +++ b/framework/Data/SqlMap/DataMapper/TSqlMapCache.php @@ -1,295 +1,295 @@ -<?php
-/**
- * TSqlMapCache class file contains FIFO, LRU, and GLOBAL cache implementations.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright © 2005-2012 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.SqlMap
- */
-
-/**
- * Allow different implementation of caching strategy. See <tt>TSqlMapFifoCache</tt>
- * for a first-in-first-out implementation. See <tt>TSqlMapLruCache</tt> for
- * a least-recently-used cache implementation.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-abstract class TSqlMapCache implements ICache
-{
- protected $_keyList;
- protected $_cache;
- protected $_cacheSize = 100;
- protected $_cacheModel = null;
-
- /**
- * Create a new cache with limited cache size.
- * @param TSqlMapCacheModel $cacheModel.
- */
- public function __construct($cacheModel=null)
- {
- $this->_cache = new TMap;
- $this->_keyList = new TList;
- $this->_cacheModel=$cacheModel;
- }
-
- /**
- * Maximum number of items to cache. Default size is 100.
- * @param int cache size.
- */
- public function setCacheSize($value)
- {
- $this->_cacheSize=TPropertyValue::ensureInteger($value,100);
- }
-
- /**
- * @return int cache size.
- */
- public function getCacheSize()
- {
- return $this->_cacheSize;
- }
-
- /**
- * @return object the object removed if exists, null otherwise.
- */
- public function delete($key)
- {
- $object = $this->get($key);
- $this->_cache->remove($key);
- $this->_keyList->remove($key);
- return $object;
- }
-
- /**
- * Clears the cache.
- */
- public function flush()
- {
- $this->_keyList->clear();
- $this->_cache->clear();
- }
-
- /**
- * @throws TSqlMapException not implemented.
- */
- public function add($id,$value,$expire=0,$dependency=null)
- {
- throw new TSqlMapException('sqlmap_use_set_to_store_cache');
- }
-}
-
-/**
- * First-in-First-out cache implementation, removes
- * object that was first added when the cache is full.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TSqlMapFifoCache extends TSqlMapCache
-{
- /**
- * @return mixed Gets a cached object with the specified key.
- */
- public function get($key)
- {
- return $this->_cache->itemAt($key);
- }
-
- /**
- * Stores a value identified by a key into cache.
- * The expire and dependency parameters are ignored.
- * @param string cache key
- * @param mixed value to cache.
- */
- public function set($key, $value,$expire=0,$dependency=null)
- {
- $this->_cache->add($key, $value);
- $this->_keyList->add($key);
- if($this->_keyList->getCount() > $this->_cacheSize)
- {
- $oldestKey = $this->_keyList->removeAt(0);
- $this->_cache->remove($oldestKey);
- }
- }
-}
-
-/**
- * Least recently used cache implementation, removes
- * object that was accessed last when the cache is full.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TSqlMapLruCache extends TSqlMapCache
-{
- /**
- * @return mixed Gets a cached object with the specified key.
- */
- public function get($key)
- {
- if($this->_keyList->contains($key))
- {
- $this->_keyList->remove($key);
- $this->_keyList->add($key);
- return $this->_cache->itemAt($key);
- }
- }
-
- /**
- * Stores a value identified by a key into cache.
- * The expire and dependency parameters are ignored.
- * @param string the key identifying the value to be cached
- * @param mixed the value to be cached
- */
- public function set($key, $value,$expire=0,$dependency=null)
- {
- $this->_cache->add($key, $value);
- $this->_keyList->add($key);
- if($this->_keyList->getCount() > $this->_cacheSize)
- {
- $oldestKey = $this->_keyList->removeAt(0);
- $this->_cache->remove($oldestKey);
- }
- }
-}
-
-/**
- * TSqlMapApplicationCache uses the default Prado application cache for
- * caching SqlMap results.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TSqlMapApplicationCache implements ICache
-{
- protected $_cacheModel=null;
-
- /**
- * Create a new cache with limited cache size.
- * @param TSqlMapCacheModel $cacheModel.
- */
- public function __construct($cacheModel=null)
- {
- $this->_cacheModel=$cacheModel;
- }
-
- /**
- *
- * @return string a KeyListID for the cache model.
- */
- protected function getKeyListId()
- {
- $id='keyList';
- if ($this->_cacheModel instanceof TSqlMapCacheModel)
- $id.='_'.$this->_cacheModel->getId();
- return $id;
- }
- /**
- * Retreive keylist from cache or create it if it doesn't exists
- * @return TList
- */
- protected function getKeyList()
- {
- if (($keyList=$this->getCache()->get($this->getKeyListId()))===false)
- {
- $keyList=new TList();
- $this->getCache()->set($this->getKeyListId(), $keyList);
- }
- return $keyList;
- }
-
- protected function setKeyList($keyList)
- {
- $this->getCache()->set($this->getKeyListId(), $keyList);
- }
-
- /**
- * @param string item to be deleted.
- */
- public function delete($key)
- {
- $keyList=$this->getKeyList();
- $keyList->remove($key);
- $this->getCache()->delete($key);
- $this->setKeyList($keyList);
- }
-
- /**
- * Deletes all items in the cache, only for data cached by sqlmap cachemodel
- */
- public function flush()
- {
- $keyList=$this->getKeyList();
- $cache=$this->getCache();
- foreach ($keyList as $key)
- {
- $cache->delete($key);
- }
- // Remove the old keylist
- $cache->delete($this->getKeyListId());
- }
-
- /**
- * @return mixed Gets a cached object with the specified key.
- */
- public function get($key)
- {
- $result = $this->getCache()->get($key);
- if ($result === false)
- {
- // if the key has not been found in cache (e.g expired), remove from keylist
- $keyList=$this->getKeyList();
- if ($keyList->contains($key))
- {
- $keyList->remove($key);
- $this->setKeyList($keyList);
- }
- }
- return $result === false ? null : $result;
- }
-
- /**
- * Stores a value identified by a key into cache.
- * @param string the key identifying the value to be cached
- * @param mixed the value to be cached
- */
- public function set($key, $value,$expire=0,$dependency=null)
- {
- $this->getCache()->set($key, $value, $expire,$dependency);
- $keyList=$this->getKeyList();
- if (!$keyList->contains($key))
- {
- $keyList->add($key);
- $this->setKeyList($keyList);
- }
- }
-
- /**
- * @return ICache Application cache instance.
- */
- protected function getCache()
- {
- return Prado::getApplication()->getCache();
- }
-
- /**
- * @throws TSqlMapException not implemented.
- */
- public function add($id,$value,$expire=0,$dependency=null)
- {
- throw new TSqlMapException('sqlmap_use_set_to_store_cache');
- }
-}
-
+<?php +/** + * TSqlMapCache class file contains FIFO, LRU, and GLOBAL cache implementations. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2012 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.SqlMap + */ + +/** + * Allow different implementation of caching strategy. See <tt>TSqlMapFifoCache</tt> + * for a first-in-first-out implementation. See <tt>TSqlMapLruCache</tt> for + * a least-recently-used cache implementation. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +abstract class TSqlMapCache implements ICache +{ + protected $_keyList; + protected $_cache; + protected $_cacheSize = 100; + protected $_cacheModel = null; + + /** + * Create a new cache with limited cache size. + * @param TSqlMapCacheModel $cacheModel. + */ + public function __construct($cacheModel=null) + { + $this->_cache = new TMap; + $this->_keyList = new TList; + $this->_cacheModel=$cacheModel; + } + + /** + * Maximum number of items to cache. Default size is 100. + * @param int cache size. + */ + public function setCacheSize($value) + { + $this->_cacheSize=TPropertyValue::ensureInteger($value,100); + } + + /** + * @return int cache size. + */ + public function getCacheSize() + { + return $this->_cacheSize; + } + + /** + * @return object the object removed if exists, null otherwise. + */ + public function delete($key) + { + $object = $this->get($key); + $this->_cache->remove($key); + $this->_keyList->remove($key); + return $object; + } + + /** + * Clears the cache. + */ + public function flush() + { + $this->_keyList->clear(); + $this->_cache->clear(); + } + + /** + * @throws TSqlMapException not implemented. + */ + public function add($id,$value,$expire=0,$dependency=null) + { + throw new TSqlMapException('sqlmap_use_set_to_store_cache'); + } +} + +/** + * First-in-First-out cache implementation, removes + * object that was first added when the cache is full. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapFifoCache extends TSqlMapCache +{ + /** + * @return mixed Gets a cached object with the specified key. + */ + public function get($key) + { + return $this->_cache->itemAt($key); + } + + /** + * Stores a value identified by a key into cache. + * The expire and dependency parameters are ignored. + * @param string cache key + * @param mixed value to cache. + */ + public function set($key, $value,$expire=0,$dependency=null) + { + $this->_cache->add($key, $value); + $this->_keyList->add($key); + if($this->_keyList->getCount() > $this->_cacheSize) + { + $oldestKey = $this->_keyList->removeAt(0); + $this->_cache->remove($oldestKey); + } + } +} + +/** + * Least recently used cache implementation, removes + * object that was accessed last when the cache is full. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapLruCache extends TSqlMapCache +{ + /** + * @return mixed Gets a cached object with the specified key. + */ + public function get($key) + { + if($this->_keyList->contains($key)) + { + $this->_keyList->remove($key); + $this->_keyList->add($key); + return $this->_cache->itemAt($key); + } + } + + /** + * Stores a value identified by a key into cache. + * The expire and dependency parameters are ignored. + * @param string the key identifying the value to be cached + * @param mixed the value to be cached + */ + public function set($key, $value,$expire=0,$dependency=null) + { + $this->_cache->add($key, $value); + $this->_keyList->add($key); + if($this->_keyList->getCount() > $this->_cacheSize) + { + $oldestKey = $this->_keyList->removeAt(0); + $this->_cache->remove($oldestKey); + } + } +} + +/** + * TSqlMapApplicationCache uses the default Prado application cache for + * caching SqlMap results. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapApplicationCache implements ICache +{ + protected $_cacheModel=null; + + /** + * Create a new cache with limited cache size. + * @param TSqlMapCacheModel $cacheModel. + */ + public function __construct($cacheModel=null) + { + $this->_cacheModel=$cacheModel; + } + + /** + * + * @return string a KeyListID for the cache model. + */ + protected function getKeyListId() + { + $id='keyList'; + if ($this->_cacheModel instanceof TSqlMapCacheModel) + $id.='_'.$this->_cacheModel->getId(); + return $id; + } + /** + * Retreive keylist from cache or create it if it doesn't exists + * @return TList + */ + protected function getKeyList() + { + if (($keyList=$this->getCache()->get($this->getKeyListId()))===false) + { + $keyList=new TList(); + $this->getCache()->set($this->getKeyListId(), $keyList); + } + return $keyList; + } + + protected function setKeyList($keyList) + { + $this->getCache()->set($this->getKeyListId(), $keyList); + } + + /** + * @param string item to be deleted. + */ + public function delete($key) + { + $keyList=$this->getKeyList(); + $keyList->remove($key); + $this->getCache()->delete($key); + $this->setKeyList($keyList); + } + + /** + * Deletes all items in the cache, only for data cached by sqlmap cachemodel + */ + public function flush() + { + $keyList=$this->getKeyList(); + $cache=$this->getCache(); + foreach ($keyList as $key) + { + $cache->delete($key); + } + // Remove the old keylist + $cache->delete($this->getKeyListId()); + } + + /** + * @return mixed Gets a cached object with the specified key. + */ + public function get($key) + { + $result = $this->getCache()->get($key); + if ($result === false) + { + // if the key has not been found in cache (e.g expired), remove from keylist + $keyList=$this->getKeyList(); + if ($keyList->contains($key)) + { + $keyList->remove($key); + $this->setKeyList($keyList); + } + } + return $result === false ? null : $result; + } + + /** + * Stores a value identified by a key into cache. + * @param string the key identifying the value to be cached + * @param mixed the value to be cached + */ + public function set($key, $value,$expire=0,$dependency=null) + { + $this->getCache()->set($key, $value, $expire,$dependency); + $keyList=$this->getKeyList(); + if (!$keyList->contains($key)) + { + $keyList->add($key); + $this->setKeyList($keyList); + } + } + + /** + * @return ICache Application cache instance. + */ + protected function getCache() + { + return Prado::getApplication()->getCache(); + } + + /** + * @throws TSqlMapException not implemented. + */ + public function add($id,$value,$expire=0,$dependency=null) + { + throw new TSqlMapException('sqlmap_use_set_to_store_cache'); + } +} + diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapException.php b/framework/Data/SqlMap/DataMapper/TSqlMapException.php index 0bf0ac32..694774d8 100644 --- a/framework/Data/SqlMap/DataMapper/TSqlMapException.php +++ b/framework/Data/SqlMap/DataMapper/TSqlMapException.php @@ -1,115 +1,115 @@ -<?php
-
-/**
- * TSqlMapException is the base exception class for all SqlMap exceptions.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TSqlMapException extends TException
-{
- /**
- * Constructor, similar to the parent constructor. For parameters that
- * are of SimpleXmlElement, the tag name and its attribute names and values
- * are expanded into a string.
- */
- public function __construct($errorMessage)
- {
- $this->setErrorCode($errorMessage);
- $errorMessage=$this->translateErrorMessage($errorMessage);
- $args=func_get_args();
- array_shift($args);
- $n=count($args);
- $tokens=array();
- for($i=0;$i<$n;++$i)
- {
- if($args[$i] instanceof SimpleXmlElement)
- $tokens['{'.$i.'}']=$this->implodeNode($args[$i]);
- else
- $tokens['{'.$i.'}']=TPropertyValue::ensureString($args[$i]);
- }
- parent::__construct(strtr($errorMessage,$tokens));
- }
-
- /**
- * @param SimpleXmlElement node
- * @return string tag name and attribute names and values.
- */
- protected function implodeNode($node)
- {
- $attributes=array();
- foreach($node->attributes() as $k=>$v)
- $attributes[]=$k.'="'.(string)$v.'"';
- return '<'.$node->getName().' '.implode(' ',$attributes).'>';
- }
-
- /**
- * @return string path to the error message file
- */
- protected function getErrorMessageFile()
- {
- $lang=Prado::getPreferredLanguage();
- $dir=dirname(__FILE__);
- $msgFile=$dir.'/messages-'.$lang.'.txt';
- if(!is_file($msgFile))
- $msgFile=$dir.'/messages.txt';
- return $msgFile;
- }
-}
-
-/**
- * TSqlMapConfigurationException, raised during configuration file parsing.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TSqlMapConfigurationException extends TSqlMapException
-{
-
-}
-
-/**
- * TSqlMapUndefinedException, raised when mapped statemented are undefined.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TSqlMapUndefinedException extends TSqlMapException
-{
-
-}
-
-/**
- * TSqlMapDuplicateException, raised when a duplicate mapped statement is found.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TSqlMapDuplicateException extends TSqlMapException
-{
-}
-
-/**
- * TInvalidPropertyException, raised when setting or getting an invalid property.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TInvalidPropertyException extends TSqlMapException
-{
-}
-
-class TSqlMapExecutionException extends TSqlMapException
-{
-}
-
+<?php + +/** + * TSqlMapException is the base exception class for all SqlMap exceptions. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapException extends TException +{ + /** + * Constructor, similar to the parent constructor. For parameters that + * are of SimpleXmlElement, the tag name and its attribute names and values + * are expanded into a string. + */ + public function __construct($errorMessage) + { + $this->setErrorCode($errorMessage); + $errorMessage=$this->translateErrorMessage($errorMessage); + $args=func_get_args(); + array_shift($args); + $n=count($args); + $tokens=array(); + for($i=0;$i<$n;++$i) + { + if($args[$i] instanceof SimpleXmlElement) + $tokens['{'.$i.'}']=$this->implodeNode($args[$i]); + else + $tokens['{'.$i.'}']=TPropertyValue::ensureString($args[$i]); + } + parent::__construct(strtr($errorMessage,$tokens)); + } + + /** + * @param SimpleXmlElement node + * @return string tag name and attribute names and values. + */ + protected function implodeNode($node) + { + $attributes=array(); + foreach($node->attributes() as $k=>$v) + $attributes[]=$k.'="'.(string)$v.'"'; + return '<'.$node->getName().' '.implode(' ',$attributes).'>'; + } + + /** + * @return string path to the error message file + */ + protected function getErrorMessageFile() + { + $lang=Prado::getPreferredLanguage(); + $dir=dirname(__FILE__); + $msgFile=$dir.'/messages-'.$lang.'.txt'; + if(!is_file($msgFile)) + $msgFile=$dir.'/messages.txt'; + return $msgFile; + } +} + +/** + * TSqlMapConfigurationException, raised during configuration file parsing. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapConfigurationException extends TSqlMapException +{ + +} + +/** + * TSqlMapUndefinedException, raised when mapped statemented are undefined. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapUndefinedException extends TSqlMapException +{ + +} + +/** + * TSqlMapDuplicateException, raised when a duplicate mapped statement is found. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapDuplicateException extends TSqlMapException +{ +} + +/** + * TInvalidPropertyException, raised when setting or getting an invalid property. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TInvalidPropertyException extends TSqlMapException +{ +} + +class TSqlMapExecutionException extends TSqlMapException +{ +} + diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapPagedList.php b/framework/Data/SqlMap/DataMapper/TSqlMapPagedList.php index 7c6a8e87..57949561 100644 --- a/framework/Data/SqlMap/DataMapper/TSqlMapPagedList.php +++ b/framework/Data/SqlMap/DataMapper/TSqlMapPagedList.php @@ -1,208 +1,208 @@ -<?php
-/**
- * TSqlMapPagedList class file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
- * @copyright Copyright © 2005-2012 PradoSoft
- * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.SqlMap
- */
-
-Prado::using('System.Collections.TPagedList');
-
-/**
- * TSqlMapPagedList implements a list with paging functionality that retrieves
- * data from a SqlMap statement.
- *
- * The maximum number of records fetched is 3 times the page size. It fetches
- * the current, the previous and the next page at a time. This allows the paged
- * list to determine if the page is a the begin, the middle or the end of the list.
- *
- * The paged list does not need to know about the total number of records.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TSqlMapPagedList extends TPagedList
-{
- private $_statement;
- private $_parameter;
- private $_prevPageList;
- private $_nextPageList;
- private $_delegate=null;
-
- /**
- * Create a new SqlMap paged list.
- * @param IMappedStatement SqlMap statement.
- * @param mixed query parameters
- * @param int page size
- * @param mixed delegate for each data row retrieved.
- * @param int number of page to fetch on initialization
- */
- public function __construct(IMappedStatement $statement,$parameter, $pageSize, $delegate=null, $page=0)
- {
- parent::__construct();
- parent::setCustomPaging(true);
- $this->initialize($statement,$parameter, $pageSize, $page);
- $this->_delegate=$delegate;
- }
-
- /**
- * Initialize the paged list.
- * @param IMappedStatement SqlMap statement.
- * @param mixed query parameters
- * @param int page size.
- * @param int number of page.
- */
- protected function initialize($statement, $parameter, $pageSize, $page)
- {
- $this->_statement = $statement;
- $this->_parameter = $parameter;
- $this->setPageSize($pageSize);
- $this->attachEventHandler('OnFetchData', array($this, 'fetchDataFromStatement'));
- $this->gotoPage($page);
- }
-
- /**
- * @throws TSqlMapException custom paging must be enabled.
- */
- public function setCustomPaging($value)
- {
- throw new TSqlMapException('sqlmap_must_enable_custom_paging');
- }
-
- /**
- * Fetch data by executing the SqlMap statement.
- * @param TPageList current object.
- * @param TPagedListFetchDataEventParameter fetch parameters
- */
- protected function fetchDataFromStatement($sender, $param)
- {
- $limit = $this->getOffsetAndLimit($param);
- $connection = $this->_statement->getManager()->getDbConnection();
- $data = $this->_statement->executeQueryForList($connection,
- $this->_parameter, null, $limit[0], $limit[1], $this->_delegate);
- $this->populateData($param, $data);
- }
-
- /**
- * Switches to the next page.
- * @return integer|boolean the new page index, false if next page is not availabe.
- */
- public function nextPage()
- {
- return $this->getIsNextPageAvailable() ? parent::nextPage() : false;
- }
-
- /**
- * Switches to the previous page.
- * @return integer|boolean the new page index, false if previous page is not availabe.
- */
- public function previousPage()
- {
- return $this->getIsPreviousPageAvailable() ? parent::previousPage() : false;
- }
-
- /**
- * Populate the list with the fetched data.
- * @param TPagedListFetchDataEventParameter fetch parameters
- * @param array fetched data.
- */
- protected function populateData($param, $data)
- {
- $total = $data instanceof TList ? $data->getCount() : count($data);
- $pageSize = $this->getPageSize();
- if($total < 1)
- {
- $param->setData($data);
- $this->_prevPageList = null;
- $this->_nextPageList = null;
- return;
- }
-
- if($param->getNewPageIndex() < 1)
- {
- $this->_prevPageList = null;
- if($total <= $pageSize)
- {
- $param->setData($data);
- $this->_nextPageList = null;
- }
- else
- {
- $param->setData(array_slice($data, 0, $pageSize));
- $this->_nextPageList = array_slice($data, $pageSize-1,$total);
- }
- }
- else
- {
- if($total <= $pageSize)
- {
- $this->_prevPageList = array_slice($data, 0, $total);
- $param->setData(array());
- $this->_nextPageList = null;
- }
- else if($total <= $pageSize*2)
- {
- $this->_prevPageList = array_slice($data, 0, $pageSize);
- $param->setData(array_slice($data, $pageSize, $total));
- $this->_nextPageList = null;
- }
- else
- {
- $this->_prevPageList = array_slice($data, 0, $pageSize);
- $param->setData(array_slice($data, $pageSize, $pageSize));
- $this->_nextPageList = array_slice($data, $pageSize*2, $total-$pageSize*2);
- }
- }
- }
-
- /**
- * Calculate the data fetch offsets and limits.
- * @param TPagedListFetchDataEventParameter fetch parameters
- * @return array 1st element is the offset, 2nd element is the limit.
- */
- protected function getOffsetAndLimit($param)
- {
- $index = $param->getNewPageIndex();
- $pageSize = $this->getPageSize();
- return $index < 1 ? array($index, $pageSize*2) : array(($index-1)*$pageSize, $pageSize*3);
- }
-
- /**
- * @return boolean true if the next page is available, false otherwise.
- */
- public function getIsNextPageAvailable()
- {
- return $this->_nextPageList!==null;
- }
-
- /**
- * @return boolean true if the previous page is available, false otherwise.
- */
- public function getIsPreviousPageAvailable()
- {
- return $this->_prevPageList!==null;
- }
-
- /**
- * @return boolean true if is the very last page, false otherwise.
- */
- public function getIsLastPage()
- {
- return ($this->_nextPageList===null) || $this->_nextPageList->getCount() < 1;
- }
-
- /**
- * @return boolean true if is not first nor last page, false otherwise.
- */
- public function getIsMiddlePage()
- {
- return !($this->getIsFirstPage() || $this->getIsLastPage());
- }
-}
-
+<?php +/** + * TSqlMapPagedList class file. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2012 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.SqlMap + */ + +Prado::using('System.Collections.TPagedList'); + +/** + * TSqlMapPagedList implements a list with paging functionality that retrieves + * data from a SqlMap statement. + * + * The maximum number of records fetched is 3 times the page size. It fetches + * the current, the previous and the next page at a time. This allows the paged + * list to determine if the page is a the begin, the middle or the end of the list. + * + * The paged list does not need to know about the total number of records. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapPagedList extends TPagedList +{ + private $_statement; + private $_parameter; + private $_prevPageList; + private $_nextPageList; + private $_delegate=null; + + /** + * Create a new SqlMap paged list. + * @param IMappedStatement SqlMap statement. + * @param mixed query parameters + * @param int page size + * @param mixed delegate for each data row retrieved. + * @param int number of page to fetch on initialization + */ + public function __construct(IMappedStatement $statement,$parameter, $pageSize, $delegate=null, $page=0) + { + parent::__construct(); + parent::setCustomPaging(true); + $this->initialize($statement,$parameter, $pageSize, $page); + $this->_delegate=$delegate; + } + + /** + * Initialize the paged list. + * @param IMappedStatement SqlMap statement. + * @param mixed query parameters + * @param int page size. + * @param int number of page. + */ + protected function initialize($statement, $parameter, $pageSize, $page) + { + $this->_statement = $statement; + $this->_parameter = $parameter; + $this->setPageSize($pageSize); + $this->attachEventHandler('OnFetchData', array($this, 'fetchDataFromStatement')); + $this->gotoPage($page); + } + + /** + * @throws TSqlMapException custom paging must be enabled. + */ + public function setCustomPaging($value) + { + throw new TSqlMapException('sqlmap_must_enable_custom_paging'); + } + + /** + * Fetch data by executing the SqlMap statement. + * @param TPageList current object. + * @param TPagedListFetchDataEventParameter fetch parameters + */ + protected function fetchDataFromStatement($sender, $param) + { + $limit = $this->getOffsetAndLimit($param); + $connection = $this->_statement->getManager()->getDbConnection(); + $data = $this->_statement->executeQueryForList($connection, + $this->_parameter, null, $limit[0], $limit[1], $this->_delegate); + $this->populateData($param, $data); + } + + /** + * Switches to the next page. + * @return integer|boolean the new page index, false if next page is not availabe. + */ + public function nextPage() + { + return $this->getIsNextPageAvailable() ? parent::nextPage() : false; + } + + /** + * Switches to the previous page. + * @return integer|boolean the new page index, false if previous page is not availabe. + */ + public function previousPage() + { + return $this->getIsPreviousPageAvailable() ? parent::previousPage() : false; + } + + /** + * Populate the list with the fetched data. + * @param TPagedListFetchDataEventParameter fetch parameters + * @param array fetched data. + */ + protected function populateData($param, $data) + { + $total = $data instanceof TList ? $data->getCount() : count($data); + $pageSize = $this->getPageSize(); + if($total < 1) + { + $param->setData($data); + $this->_prevPageList = null; + $this->_nextPageList = null; + return; + } + + if($param->getNewPageIndex() < 1) + { + $this->_prevPageList = null; + if($total <= $pageSize) + { + $param->setData($data); + $this->_nextPageList = null; + } + else + { + $param->setData(array_slice($data, 0, $pageSize)); + $this->_nextPageList = array_slice($data, $pageSize-1,$total); + } + } + else + { + if($total <= $pageSize) + { + $this->_prevPageList = array_slice($data, 0, $total); + $param->setData(array()); + $this->_nextPageList = null; + } + else if($total <= $pageSize*2) + { + $this->_prevPageList = array_slice($data, 0, $pageSize); + $param->setData(array_slice($data, $pageSize, $total)); + $this->_nextPageList = null; + } + else + { + $this->_prevPageList = array_slice($data, 0, $pageSize); + $param->setData(array_slice($data, $pageSize, $pageSize)); + $this->_nextPageList = array_slice($data, $pageSize*2, $total-$pageSize*2); + } + } + } + + /** + * Calculate the data fetch offsets and limits. + * @param TPagedListFetchDataEventParameter fetch parameters + * @return array 1st element is the offset, 2nd element is the limit. + */ + protected function getOffsetAndLimit($param) + { + $index = $param->getNewPageIndex(); + $pageSize = $this->getPageSize(); + return $index < 1 ? array($index, $pageSize*2) : array(($index-1)*$pageSize, $pageSize*3); + } + + /** + * @return boolean true if the next page is available, false otherwise. + */ + public function getIsNextPageAvailable() + { + return $this->_nextPageList!==null; + } + + /** + * @return boolean true if the previous page is available, false otherwise. + */ + public function getIsPreviousPageAvailable() + { + return $this->_prevPageList!==null; + } + + /** + * @return boolean true if is the very last page, false otherwise. + */ + public function getIsLastPage() + { + return ($this->_nextPageList===null) || $this->_nextPageList->getCount() < 1; + } + + /** + * @return boolean true if is not first nor last page, false otherwise. + */ + public function getIsMiddlePage() + { + return !($this->getIsFirstPage() || $this->getIsLastPage()); + } +} + diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php b/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php index 7a54e347..61c97245 100644 --- a/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php +++ b/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php @@ -1,192 +1,192 @@ -<?php
-/**
- * TSqlMapTypeHandlerRegistry, and abstract TSqlMapTypeHandler classes file.
- *
- * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
- * @link http://www.pradosoft.com/
+<?php +/** + * TSqlMapTypeHandlerRegistry, and abstract TSqlMapTypeHandler classes file. + * + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> + * @link http://www.pradosoft.com/ * @copyright Copyright © 2005-2012 PradoSoft - * @license http://www.pradosoft.com/license/
- * @version $Id$
- * @package System.Data.SqlMap
- */
-
-/**
- * TTypeHandlerFactory provides type handler classes to convert database field type
- * to PHP types and vice versa.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-class TSqlMapTypeHandlerRegistry
-{
- private $_typeHandlers=array();
-
- /**
- * @param string database field type
- * @return TSqlMapTypeHandler type handler for give database field type.
- */
- public function getDbTypeHandler($dbType='NULL')
- {
- foreach($this->_typeHandlers as $handler)
- if($handler->getDbType()===$dbType)
- return $handler;
- }
-
- /**
- * @param string type handler class name
- * @return TSqlMapTypeHandler type handler
- */
- public function getTypeHandler($class)
- {
- if(isset($this->_typeHandlers[$class]))
- return $this->_typeHandlers[$class];
- }
-
- /**
- * @param TSqlMapTypeHandler registers a new type handler
- */
- public function registerTypeHandler(TSqlMapTypeHandler $handler)
- {
- $this->_typeHandlers[$handler->getType()] = $handler;
- }
-
- /**
- * Creates a new instance of a particular class (for PHP primative types,
- * their corresponding default value for given type is used).
- * @param string PHP type name
- * @return mixed default type value, if no type is specified null is returned.
- * @throws TSqlMapException if class name is not found.
- */
- public function createInstanceOf($type='')
- {
- if(strlen($type) > 0)
- {
- switch(strtolower($type))
- {
- case 'string': return '';
- case 'array': return array();
- case 'float': case 'double': case 'decimal': return 0.0;
- case 'integer': case 'int': return 0;
- case 'bool': case 'boolean': return false;
- }
-
- if(class_exists('Prado', false))
- return Prado::createComponent($type);
- else if(class_exists($type, false)) //NO auto loading
- return new $type;
- else
- throw new TSqlMapException('sqlmap_unable_to_find_class', $type);
- }
- }
-
- /**
- * Converts the value to given type using PHP's settype() function.
- * @param string PHP primative type.
- * @param mixed value to be casted
- * @return mixed type casted value.
- */
- public function convertToType($type, $value)
- {
- switch(strtolower($type))
- {
- case 'integer': case 'int':
- $type = 'integer'; break;
- case 'float': case 'double': case 'decimal':
- $type = 'float'; break;
- case 'boolean': case 'bool':
- $type = 'boolean'; break;
- case 'string' :
- $type = 'string'; break;
- default:
- return $value;
- }
- settype($value, $type);
- return $value;
- }
-}
-
-/**
- * A simple interface for implementing custom type handlers.
- *
- * Using this interface, you can implement a type handler that
- * will perform customized processing before parameters are set
- * on and after values are retrieved from the database.
- * Using a custom type handler you can extend
- * the framework to handle types that are not supported, or
- * handle supported types in a different way. For example,
- * you might use a custom type handler to implement proprietary
- * BLOB support (e.g. Oracle), or you might use it to handle
- * booleans using "Y" and "N" instead of the more typical 0/1.
- *
- * @author Wei Zhuo <weizho[at]gmail[dot]com>
- * @version $Id$
- * @package System.Data.SqlMap
- * @since 3.1
- */
-abstract class TSqlMapTypeHandler extends TComponent
-{
- private $_dbType='NULL';
- private $_type;
- /**
- * @param string database field type.
- */
- public function setDbType($value)
- {
- $this->_dbType=$value;
- }
-
- /**
- * @return string database field type.
- */
- public function getDbType()
- {
- return $this->_dbType;
- }
-
- public function getType()
- {
- if($this->_type===null)
- return get_class($this);
- else
- return $this->_type;
- }
-
- public function setType($value)
- {
- $this->_type=$value;
- }
-
- /**
- * Performs processing on a value before it is used to set
- * the parameter of a IDbCommand.
- * @param object The interface for setting the value.
- * @param object The value to be set.
- */
- public abstract function getParameter($object);
-
-
- /**
- * Performs processing on a value before after it has been retrieved
- * from a database
- * @param object The interface for getting the value.
- * @return mixed The processed value.
- */
- public abstract function getResult($string);
-
-
- /**
- * Casts the string representation of a value into a type recognized by
- * this type handler. This method is used to translate nullValue values
- * into types that can be appropriately compared. If your custom type handler
- * cannot support nullValues, or if there is no reasonable string representation
- * for this type (e.g. File type), you can simply return the String representation
- * as it was passed in. It is not recommended to return null, unless null was passed
- * in.
- * @param array result row.
- * @return mixed
- */
- public abstract function createNewInstance($row=null);
-}
-
+ * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.SqlMap + */ + +/** + * TTypeHandlerFactory provides type handler classes to convert database field type + * to PHP types and vice versa. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapTypeHandlerRegistry +{ + private $_typeHandlers=array(); + + /** + * @param string database field type + * @return TSqlMapTypeHandler type handler for give database field type. + */ + public function getDbTypeHandler($dbType='NULL') + { + foreach($this->_typeHandlers as $handler) + if($handler->getDbType()===$dbType) + return $handler; + } + + /** + * @param string type handler class name + * @return TSqlMapTypeHandler type handler + */ + public function getTypeHandler($class) + { + if(isset($this->_typeHandlers[$class])) + return $this->_typeHandlers[$class]; + } + + /** + * @param TSqlMapTypeHandler registers a new type handler + */ + public function registerTypeHandler(TSqlMapTypeHandler $handler) + { + $this->_typeHandlers[$handler->getType()] = $handler; + } + + /** + * Creates a new instance of a particular class (for PHP primative types, + * their corresponding default value for given type is used). + * @param string PHP type name + * @return mixed default type value, if no type is specified null is returned. + * @throws TSqlMapException if class name is not found. + */ + public function createInstanceOf($type='') + { + if(strlen($type) > 0) + { + switch(strtolower($type)) + { + case 'string': return ''; + case 'array': return array(); + case 'float': case 'double': case 'decimal': return 0.0; + case 'integer': case 'int': return 0; + case 'bool': case 'boolean': return false; + } + + if(class_exists('Prado', false)) + return Prado::createComponent($type); + else if(class_exists($type, false)) //NO auto loading + return new $type; + else + throw new TSqlMapException('sqlmap_unable_to_find_class', $type); + } + } + + /** + * Converts the value to given type using PHP's settype() function. + * @param string PHP primative type. + * @param mixed value to be casted + * @return mixed type casted value. + */ + public function convertToType($type, $value) + { + switch(strtolower($type)) + { + case 'integer': case 'int': + $type = 'integer'; break; + case 'float': case 'double': case 'decimal': + $type = 'float'; break; + case 'boolean': case 'bool': + $type = 'boolean'; break; + case 'string' : + $type = 'string'; break; + default: + return $value; + } + settype($value, $type); + return $value; + } +} + +/** + * A simple interface for implementing custom type handlers. + * + * Using this interface, you can implement a type handler that + * will perform customized processing before parameters are set + * on and after values are retrieved from the database. + * Using a custom type handler you can extend + * the framework to handle types that are not supported, or + * handle supported types in a different way. For example, + * you might use a custom type handler to implement proprietary + * BLOB support (e.g. Oracle), or you might use it to handle + * booleans using "Y" and "N" instead of the more typical 0/1. + * + * @author Wei Zhuo <weizho[at]gmail[dot]com> + * @version $Id$ + * @package System.Data.SqlMap + * @since 3.1 + */ +abstract class TSqlMapTypeHandler extends TComponent +{ + private $_dbType='NULL'; + private $_type; + /** + * @param string database field type. + */ + public function setDbType($value) + { + $this->_dbType=$value; + } + + /** + * @return string database field type. + */ + public function getDbType() + { + return $this->_dbType; + } + + public function getType() + { + if($this->_type===null) + return get_class($this); + else + return $this->_type; + } + + public function setType($value) + { + $this->_type=$value; + } + + /** + * Performs processing on a value before it is used to set + * the parameter of a IDbCommand. + * @param object The interface for setting the value. + * @param object The value to be set. + */ + public abstract function getParameter($object); + + + /** + * Performs processing on a value before after it has been retrieved + * from a database + * @param object The interface for getting the value. + * @return mixed The processed value. + */ + public abstract function getResult($string); + + + /** + * Casts the string representation of a value into a type recognized by + * this type handler. This method is used to translate nullValue values + * into types that can be appropriately compared. If your custom type handler + * cannot support nullValues, or if there is no reasonable string representation + * for this type (e.g. File type), you can simply return the String representation + * as it was passed in. It is not recommended to return null, unless null was passed + * in. + * @param array result row. + * @return mixed + */ + public abstract function createNewInstance($row=null); +} + |