From 3d3f8d3832921f99daf8ce1953304763c2e76c62 Mon Sep 17 00:00:00 2001 From: wei <> Date: Fri, 14 Apr 2006 06:22:09 +0000 Subject: Importing SQLMap + sample + docs. --- .../SQLMap/DataMapper/TDataMapperException.php | 65 ++++++++ .../DataAccess/SQLMap/DataMapper/TLazyLoadList.php | 87 +++++++++++ .../SQLMap/DataMapper/TPropertyAccess.php | 91 ++++++++++++ .../DataAccess/SQLMap/DataMapper/TSqlMapCache.php | 165 +++++++++++++++++++++ .../SQLMap/DataMapper/TSqlMapPagedList.php | 156 +++++++++++++++++++ .../SQLMap/DataMapper/TTypeHandlerFactory.php | 134 +++++++++++++++++ .../DataAccess/SQLMap/DataMapper/messages.txt | 61 ++++++++ 7 files changed, 759 insertions(+) create mode 100644 framework/DataAccess/SQLMap/DataMapper/TDataMapperException.php create mode 100644 framework/DataAccess/SQLMap/DataMapper/TLazyLoadList.php create mode 100644 framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php create mode 100644 framework/DataAccess/SQLMap/DataMapper/TSqlMapCache.php create mode 100644 framework/DataAccess/SQLMap/DataMapper/TSqlMapPagedList.php create mode 100644 framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php create mode 100644 framework/DataAccess/SQLMap/DataMapper/messages.txt (limited to 'framework/DataAccess/SQLMap/DataMapper') diff --git a/framework/DataAccess/SQLMap/DataMapper/TDataMapperException.php b/framework/DataAccess/SQLMap/DataMapper/TDataMapperException.php new file mode 100644 index 00000000..7cb74244 --- /dev/null +++ b/framework/DataAccess/SQLMap/DataMapper/TDataMapperException.php @@ -0,0 +1,65 @@ +asXml()),$file); + } +} + +class TSqlMapExecutionException extends TDataMapperException +{ +} + +class TSqlMapQueryExecutionException extends TSqlMapExecutionException +{ + protected $parent; + public function __construct($statement, $exception) + { + $this->parent = $exception; + parent::__construct('sqlmap_query_execution_error', + $statement->getID(), $exception->getMessage()); + } +} + +class TSqlMapUndefinedException extends TDataMapperException +{ + +} + +class TSqlMapDuplicateException extends TDataMapperException +{ +} + +class TSqlMapConnectionException extends TDataMapperException +{ +} + +class TInvalidPropertyException extends TDataMapperException +{ + +} +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/DataMapper/TLazyLoadList.php b/framework/DataAccess/SQLMap/DataMapper/TLazyLoadList.php new file mode 100644 index 00000000..465dcaac --- /dev/null +++ b/framework/DataAccess/SQLMap/DataMapper/TLazyLoadList.php @@ -0,0 +1,87 @@ +_param = $param; + $this->_target = $target; + $this->_statementName = $mappedStatement->getID(); + $this->_sqlMap = $mappedStatement->getSqlMap(); + $this->_propertyName = $propertyName; + } + + public static function newInstance($mappedStatement, $param, $target, $propertyName) + { + $handler = new self($mappedStatement, $param, $target, $propertyName); + $statement = $mappedStatement->getStatement(); + $list = $statement->createInstanceOfListClass(); + if(!is_object($list)) + throw new TSqlMapExecutionException('sqlmap_invalid_lazyload_list', + $statement->getID()); + return new TObjectProxy($handler, $list); + } + + public function intercept($method, $arguments) + { + return call_user_func_array(array($this->_innerList, $method), $arguments); + } + + protected function fetchListData() + { + + if($this->_loaded == false) + { + $this->_innerList = $this->_sqlMap->queryForList( + $this->_statementName, $this->_param); + $this->_loaded = true; + //replace the target property with real list + TPropertyAccess::set($this->_target, + $this->_propertyName, $this->_innerList); + } + } + + public function hasMethod($method) + { + $this->fetchListData(); + if(is_object($this->_innerList)) + return in_array($method, get_class_methods($this->_innerList)); + return false; + } +} + +interface IInterceptor +{ + public function intercept($method, $params); + public function hasMethod($method); +} + +class TObjectProxy +{ + private $_object; + private $_handler; + + public function __construct(IInterceptor $handler, $object) + { + $this->_handler = $handler; + $this->_object = $object; + } + + 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); + } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php b/framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php new file mode 100644 index 00000000..8680601e --- /dev/null +++ b/framework/DataAccess/SQLMap/DataMapper/TPropertyAccess.php @@ -0,0 +1,91 @@ +_obj = $obj; + $this->_performance=$performance; + } + + public function __get($name) + { + return self::get($this->_obj,$name,$this->_performance); + } + + public function __set($name,$value) + { + self::set($this->_obj,$name,$value,$this->_performance); + } + + /** + * Evaluates the data value at the specified field. + * - If the data is an array, then the field is treated as an array index + * and the corresponding element value is returned; + * - If the data is a TMap or TList object, then the field is treated as a key + * into the map or list, and the corresponding value is returned. + * - If the data is an object, the field is treated as a property or subproperty + * defined with getter methods. For example, if the object has a method called + * getMyValue(), then field 'MyValue' will retrive the result of this method call. + * If getMyValue() returns an object which contains a method getMySubValue(), + * then field 'MyValue.MySubValue' will return that method call result. + * @param mixed data containing the field value, can be an array, TMap, TList or object. + * @param mixed field value + * @return mixed value at the specified field + * @throw TInvalidDataValueException if field or data 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(isset($object[$prop])) + $object = $object[$prop]; + else + throw new TInvalidPropertyException('sqlmap_invalid_property',$path); + } + else if(is_object($object)) + { + $getter = 'get'.$prop; + if(is_callable(array($object,$getter))) + $object = $object->{$getter}(); + else if(in_array($prop, array_keys(get_object_vars($object)))) + $object = $object->{$prop}; + else + throw new TInvalidPropertyException('sqlmap_invalid_property',$path); + } + else + throw new TInvalidPropertyException('sqlmap_invalid_property',$path); + } + return $object; + } + + + public static function set($object, $path, $value) + { + $properties = explode('.', $path); + $prop = array_pop($properties); + if(count($properties) > 0) + $object = self::get($object, implode('.',$properties)); + if(is_object($object)) + { + $setter = 'set'.$prop; + if(is_callable(array($object, $setter))) + $object->{$setter}($value); + else + $object->{$prop} = $value; + } + else + throw new TInvalidPropertyException('sqlmap_invalid_property_type',$path); + } + +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/DataMapper/TSqlMapCache.php b/framework/DataAccess/SQLMap/DataMapper/TSqlMapCache.php new file mode 100644 index 00000000..8571d46d --- /dev/null +++ b/framework/DataAccess/SQLMap/DataMapper/TSqlMapCache.php @@ -0,0 +1,165 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Revision: $ $Date: $ + * @package System.DataAccess.SQLMap + */ + +interface ISqLMapCache +{ + public function remove($key); + + public function flush(); + + public function get($key); + + public function set($key, $value); + + public function configure($properties); +} + +/** + * Allow different implementation of caching strategy. See TSqlMapFifoCache + * for a first-in-first-out implementation. See TSqlMapLruCache for + * a least-recently-used cache implementation. + * + * @author Wei Zhuo + * @version $Revision: $ $Date: $ + * @package System.DataAccess.SQLMap + * @since 3.0 + */ +abstract class TSqlMapCache implements ISqlMapCache +{ + protected $_keyList; + protected $_cache; + protected $_cacheSize = 100; + + /** + * Create a new cache with limited cache size. + * @param integer maxium number of items to cache. + */ + public function __construct($cacheSize=100) + { + $this->_cache = new TMap; + $this->_cacheSize = intval($cacheSize); + $this->_keyList = new TList; + } + + /** + * Configures the Cache Size. + * @param array list of properties + */ + public function configure($properties) + { + if(isset($properties['size'])) + $this->_cacheSize = intval($properties['size']); + } + + /** + * @return object the object removed if exists, null otherwise. + */ + public function remove($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(); + } + +} + +/** + * First-in-First-out cache implementation, removes + * object that was first added when the cache is full. + * + * @author Wei Zhuo + * @version $Revision: $ $Date: $ + * @package System.DataAccess.SQLMap + * @since 3.0 + */ +class TSqlMapFifoCache extends TSqlMapCache +{ + /** + * @return mixed Gets a cached object with the specified key. + */ + public function get($key) + { + return $this->_cache->itemAt($key); + } + + /** + * Adds an item with the specified key and value into cached data. + * @param string cache key + * @param mixed value to cache. + */ + public function set($key, $value) + { + $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 + * @version $Revision: $ $Date: $ + * @package System.DataAccess.SQLMap + * @since 3.0 + */ +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); + } + else + return null; + } + + /** + * Adds an item with the specified key and value into cached data. + * @param string cache key + * @param mixed value to cache. + */ + public function set($key, $value) + { + $this->_cache->add($key, $value); + $this->_keyList->add($key); + if($this->_keyList->getCount() > $this->_cacheSize) + { + $oldestKey = $this->_keyList->removeAt(0); + $this->_cache->remove($oldestKey); + } + } +} + + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/DataMapper/TSqlMapPagedList.php b/framework/DataAccess/SQLMap/DataMapper/TSqlMapPagedList.php new file mode 100644 index 00000000..cded4b32 --- /dev/null +++ b/framework/DataAccess/SQLMap/DataMapper/TSqlMapPagedList.php @@ -0,0 +1,156 @@ + + * @version $Revision: $ $Date: $ + * @package System.Web.UI.WebControls + * @since 3.0 + */ +class TSqlMapPagedList extends TPagedList +{ + private $_statement; + private $_parameter; + private $_prevPageList; + private $_nextPageList; + private $_delegate=null; + + public function __construct(IMappedStatement $statement, + $parameter, $pageSize, $delegate=null) + { + parent::__construct(); + parent::setCustomPaging(true); + $this->initialize($statement,$parameter, $pageSize); + $this->_delegate=$delegate; + } + + protected function initialize($statement, $parameter, $pageSize) + { + $this->_statement = $statement; + $this->_parameter = $parameter; + $this->setPageSize($pageSize); + $this->attachEventHandler('OnFetchData', array($this, 'fetchDataFromStatement')); + $this->gotoPage(0); + } + + public function setCustomPaging($value) + { + throw new TDataMapperException('sqlmap_must_enable_custom_paging'); + } + + protected function fetchDataFromStatement($sender, $param) + { + $limit = $this->getOffsetAndLimit($param); + $connection = $this->_statement->getSqlMap()->openConnection(); + $data = $this->_statement->executeQueryForList($connection, + $this->_parameter, null, $limit[0], $limit[1], $this->_delegate); + $this->populateData($param, $data); + } + + public function nextPage() + { + if($this->getIsNextPageAvailable()) + return parent::nextPage(); + else + return false; + } + + public function previousPage() + { + if($this->getIsPreviousPageAvailable()) + return parent::previousPage(); + else + return false; + } + + 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($this->sublist($data, 0, $pageSize)); + $this->_nextPageList = $this->sublist($data, $pageSize,$total); + } + } + else + { + if($total <= $pageSize) + { + $this->_prevPageList = $this->sublist($data, 0, $total); + $param->setData(array()); + $this->_nextPageList = null; + } + else if($total <= $pageSize*2) + { + $this->_prevPageList = $this->sublist($data, 0, $pageSize); + $param->setData($this->sublist($data, $pageSize, $total)); + $this->_nextPageList = null; + } + else + { + $this->_prevPageList = $this->sublist($data, 0, $pageSize); + $param->setData($this->sublist($data, $pageSize, $pageSize*2)); + $this->_nextPageList = $this->sublist($data, $pageSize*2, $total); + } + } + } + + protected function sublist($data, $from, $to) + { + $array = array(); + for($i = $from; $i<$to; $i++) + $array[] = $data[$i]; + return $array; + } + + protected function getOffsetAndLimit($param) + { + $index = $param->getNewPageIndex(); + $pageSize = $this->getPageSize(); + if($index < 1) + return array($index, $pageSize*2); + else + return array(($index-1)*$pageSize, $pageSize*3); + } + + public function getIsNextPageAvailable() + { + return !is_null($this->_nextPageList); + } + + public function getIsPreviousPageAvailable() + { + return !is_null($this->_prevPageList); + } + + public function getIsLastPage() + { + return is_null($this->_nextPageList) + || $this->_nextPageList->getCount() < 1; + } + + public function getIsMiddlePage() + { + return !($this->getIsFirstPage() || $this->getIsLastPage()); + } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php b/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php new file mode 100644 index 00000000..fcadea28 --- /dev/null +++ b/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php @@ -0,0 +1,134 @@ +_typeHandlerMap = new TMap; + } + + public function getTypeHandler($type, $dbType=null) + { + $dbTypeHandlerMap = $this->_typeHandlerMap[$type]; + $handler = null; + if(!is_null($dbTypeHandlerMap)) + { + if(is_null($dbType)) + $handler = $dbTypeHandlerMap[self::NullDbType]; + else + { + $handler = $dbTypeHandlerMap[$dbType]; + if(is_null($handler)) + $handler = $dbTypeHandlerMap[self::NullDbType]; + } + } + return $handler; + } + + public function register($type, $handler, $dbType=null) + { + $map = $this->_typeHandlerMap[$type]; + if(is_null($map)) + { + $map = new TMap; + $this->_typeHandlerMap->add($type, $map); + } + if(is_null($dbType)) + $map->add(self::NullDbType, $handler); + else + $map->add($dbType, $handler); + } + + public static 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($type, false)) //NO auto loading + return new $type; + else + throw new TDataMapperException('sqlmap_unable_to_find_class', $type); + } + return null; + } + + public static 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. + */ +interface ITypeHandlerCallback +{ + /** + * 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 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 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 string nullValue. + * @return mixed + */ + public function createNewInstance(); +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/DataMapper/messages.txt b/framework/DataAccess/SQLMap/DataMapper/messages.txt new file mode 100644 index 00000000..79c80ad5 --- /dev/null +++ b/framework/DataAccess/SQLMap/DataMapper/messages.txt @@ -0,0 +1,61 @@ +component_property_undefined = Component property '{0}.{1}' is not defined. +component_property_readonly = Component property '{0}.{1}' is read-only. +component_event_undefined = Component event '{0}.{1}' is not defined. +component_eventhandler_invalid = Component event '{0}.{1}' is attached with an invalid event handler. +component_expression_invalid = Component '{0}' is evaluating an invalid expression '{1}' : {2}. +component_statements_invalid = Component '{0}' is evaluating invalid PHP statements '{1}' : {2}. + +propertyvalue_enumvalue_invalid = Value '{0}' is a not valid enumeration value ({1}). + +list_index_invalid = Index '{0}' is out of range. +list_item_inexistent = The item cannot be found in the list. +list_data_not_iterable = Data must be either an array or an object implementing Traversable interface. +list_readonly = {0} is read-only. + +map_addition_disallowed = The new item cannot be added to the map. +map_item_unremovable = The item cannot be removed from the map. +map_data_not_iterable = Data must be either an array or an object implementing Traversable interface. +map_readonly = {0} is read-only. + +sqlmap_contains_no_statement = Unable to find SQLMap statement '{0}'. +sqlmap_already_contains_statement = Duplicate SQLMap statement found, '{0}' already exists. +sqlmap_contains_no_result_map = Unable to find SQLMap result map '{0}'. +sqlmap_already_contains_result_map = Duplicate SQLMap result map found, '{0}' already exists. +sqlmap_contains_no_parameter_map = Unable to find SQLMap parameter map '{0}'. +sqlmap_already_contains_parameter_map = Duplicate SQLMap parameter map found, '{0}' already exists. +sqlmap_connection_already_exists = SqlMap could not invoke OpenConnection(). A connection is already started. Call CloseConnection first. +sqlmap_unable_to_close_null_connection = SqlMap could not invoke CloseConnection(). No connection was started. Call OpenConnection() first. +sqlmap_undefined_attribute = {0} attribute '{1}' is not defined for {2} in file {3}. +sqlmap_unable_find_provider_class = Unable to find a database provider in SQLMap configuration file {0}. +sqlmap_unable_to_find_parent_parameter_map = Unable to find parent parameter map extension '{0}' in file {1}. +sqlmap_unable_to_find_parent_sql = Unable to find parent sql statement extension '{0}' in file {1}. +sqlmap_unable_to_find_result_mapping = Unable to resolve SQLMap result mapping '{0}' in Result Map '{2}' using configuration file {1}. +sqlmap_unable_to_find_parent_result_map = Unable to find parent SQLMap result map '{0}' in file {1}. +sqlmap_undefined_property_inline_map = Invalid attribute '{0}' for inline parameter in statement '{1}' in file {2}. +sqlmap_index_must_be_string_or_int = Invalid index '{0}', must be an integes or string to get a SQLMap parameter map property. +sqlmap_undefined_input_property = Undefined array index '{0}' in retrieving property in SQLMap parameter map '{1}'. +sqlmap_unable_to_find_class = Unable to find result class '{0}' in TResultMap::createInstanceOfResult(). +sqlmap_can_not_instantiate = Type handler '{0}' can not create new objects. +sqlmap_cannot_execute_query_for_map = SQLMap statement class {0} can not query for map. +sqlmap_cannot_execute_update = SQLMap statement class {0} can not execute update query. +sqlmap_cannot_execute_insert = SQLMap statement class {0} can not execute insert. +sqlmap_cannot_execute_query_for_list = SQLMap statement class {0} can not query for list. +sqlmap_cannot_execute_query_for_object = SQLMap statement class {0} can not query for object +sqlmap_execution_error_no_record = No record set found in executing statement '{0}': '{1}'. +sqlmap_unable_to_create_new_instance = Unable to create a new instance of '{0}' using type hander '{1}' for SQLMap statement with ID '{2}'. +sqlmap_invalid_property = Invalid property getter path '{0}'. +sqlmap_invalid_property_type = Invalid object type, must be 'Object', unable to set property in path '{0}'. +sqlmap_unable_to_get_property_for_parameter = Unable to find property '{1}' in object '{2}' for parameter map '{0}' while executing statement '{4}': '{3}'. +sqlmap_error_in_parameter_from_handler = For parameter map '{0}', error in getting parameter from type handler '{2}' with value '{1}': '{3}'. +sqlmap_error_in_result_property_from_handler = For result map '{0}', error in getting result from type handler '{2}', with value '{1}': '{3}'.======= +sqlmap_unable_to_find_implemenation = Unable to find SQLMap cache implementation named '{0}'. +sqlmap_cache_model_already_exists = This SQLMap already contains cache model '{0}'. +sqlmap_unable_to_find_cache_model = Unable to find cache model '{0}' in this SQLMap. +sqlmap_unable_to_find_db_config = Unable to find database connection settings <provider> and <datasource> in configuration file '{0}'. +sqlmap_unable_to_find_config = Unable to find SQLMap configuration file '{0}'. +sqlmap_unable_to_find_groupby = Unable to find data in result set with column '{0}' in result map with ID '{1}'. +sqlmap_invalid_lazyload_list = Invalid type to lazy load, must specify a valid ListClass in statement '{0}'. +sqlmap_unable_to_find_resource = 'Unable to find SQLMap configuration file '{0}'. +sqlmap_query_execution_error = Error in executing SQLMap statement '{0}' : '{1}'. +sqlmap_undefined_discriminator = The discriminator is null, but somehow a subMap was reached in ResultMap '{0}' in file '{1}'. +sqlmap_invalid_delegate = Invalid callback row delegate '{1}' in mapped statement '{0}'. \ No newline at end of file -- cgit v1.2.3