From 90b5141367db5fcac9ba72042278556612b5dc3f Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Tue, 20 Jan 2015 22:02:33 +0100 Subject: One class per file: framework/Data --- .../TActiveRecordConfigurationException.php | 22 + .../Exceptions/TActiveRecordException.php | 15 +- .../Scaffold/IScaffoldEditRenderer.php | 31 + .../ActiveRecord/Scaffold/TScaffoldEditView.php | 24 +- framework/Data/ActiveRecord/TActiveRecord.php | 58 +- .../TActiveRecordChangeEventParameter.php | 44 ++ .../TActiveRecordInvalidFinderResult.php | 32 + framework/Data/DataGateway/TDataGatewayCommand.php | 540 -------------- .../DataGateway/TDataGatewayEventParameter.php | 319 ++++++++ .../TDataGatewayResultEventParameter.php | 57 ++ .../Data/SqlMap/Configuration/TDiscriminator.php | 54 +- .../Data/SqlMap/Configuration/TSqlMapCacheKey.php | 49 ++ .../SqlMap/Configuration/TSqlMapCacheModel.php | 58 +- .../SqlMap/Configuration/TSqlMapCacheTypes.php | 26 + .../Data/SqlMap/Configuration/TSqlMapDelete.php | 22 + .../Data/SqlMap/Configuration/TSqlMapInsert.php | 42 ++ .../Data/SqlMap/Configuration/TSqlMapSelect.php | 26 + .../Data/SqlMap/Configuration/TSqlMapSelectKey.php | 72 ++ .../Data/SqlMap/Configuration/TSqlMapStatement.php | 132 +--- .../Data/SqlMap/Configuration/TSqlMapUpdate.php | 22 + .../Configuration/TSqlMapXmlConfigBuilder.php | 111 +++ .../Configuration/TSqlMapXmlConfiguration.php | 801 --------------------- .../TSqlMapXmlMappingConfiguration.php | 517 +++++++++++++ framework/Data/SqlMap/Configuration/TSubMap.php | 61 ++ .../DataMapper/TInvalidPropertyException.php | 19 + framework/Data/SqlMap/DataMapper/TLazyLoadList.php | 42 +- framework/Data/SqlMap/DataMapper/TObjectProxy.php | 49 ++ .../SqlMap/DataMapper/TSqlMapApplicationCache.php | 139 ++++ framework/Data/SqlMap/DataMapper/TSqlMapCache.php | 209 +----- .../DataMapper/TSqlMapConfigurationException.php | 20 + .../DataMapper/TSqlMapDuplicateException.php | 19 + .../Data/SqlMap/DataMapper/TSqlMapException.php | 55 +- .../DataMapper/TSqlMapExecutionException.php | 12 + .../Data/SqlMap/DataMapper/TSqlMapFifoCache.php | 46 ++ .../Data/SqlMap/DataMapper/TSqlMapLruCache.php | 51 ++ .../Data/SqlMap/DataMapper/TSqlMapTypeHandler.php | 92 +++ .../DataMapper/TSqlMapTypeHandlerRegistry.php | 85 +-- .../DataMapper/TSqlMapUndefinedException.php | 20 + .../Data/SqlMap/Statements/TMappedStatement.php | 304 +------- .../Data/SqlMap/Statements/TPostSelectBinding.php | 41 ++ .../Statements/TResultSetListItemParameter.php | 46 ++ .../Statements/TResultSetMapItemParameter.php | 53 ++ .../Statements/TSqlMapObjectCollectionTree.php | 201 ++++++ framework/Data/TDbColumnCaseMode.php | 33 + framework/Data/TDbConnection.php | 49 +- framework/Data/TDbNullConversionMode.php | 33 + 46 files changed, 2340 insertions(+), 2413 deletions(-) create mode 100644 framework/Data/ActiveRecord/Exceptions/TActiveRecordConfigurationException.php create mode 100644 framework/Data/ActiveRecord/Scaffold/IScaffoldEditRenderer.php create mode 100644 framework/Data/ActiveRecord/TActiveRecordChangeEventParameter.php create mode 100644 framework/Data/ActiveRecord/TActiveRecordInvalidFinderResult.php delete mode 100644 framework/Data/DataGateway/TDataGatewayCommand.php create mode 100644 framework/Data/DataGateway/TDataGatewayEventParameter.php create mode 100644 framework/Data/DataGateway/TDataGatewayResultEventParameter.php create mode 100644 framework/Data/SqlMap/Configuration/TSqlMapCacheKey.php create mode 100644 framework/Data/SqlMap/Configuration/TSqlMapCacheTypes.php create mode 100644 framework/Data/SqlMap/Configuration/TSqlMapDelete.php create mode 100644 framework/Data/SqlMap/Configuration/TSqlMapInsert.php create mode 100644 framework/Data/SqlMap/Configuration/TSqlMapSelect.php create mode 100644 framework/Data/SqlMap/Configuration/TSqlMapSelectKey.php create mode 100644 framework/Data/SqlMap/Configuration/TSqlMapUpdate.php create mode 100644 framework/Data/SqlMap/Configuration/TSqlMapXmlConfigBuilder.php delete mode 100644 framework/Data/SqlMap/Configuration/TSqlMapXmlConfiguration.php create mode 100644 framework/Data/SqlMap/Configuration/TSqlMapXmlMappingConfiguration.php create mode 100644 framework/Data/SqlMap/Configuration/TSubMap.php create mode 100644 framework/Data/SqlMap/DataMapper/TInvalidPropertyException.php create mode 100644 framework/Data/SqlMap/DataMapper/TObjectProxy.php create mode 100644 framework/Data/SqlMap/DataMapper/TSqlMapApplicationCache.php create mode 100644 framework/Data/SqlMap/DataMapper/TSqlMapConfigurationException.php create mode 100644 framework/Data/SqlMap/DataMapper/TSqlMapDuplicateException.php create mode 100644 framework/Data/SqlMap/DataMapper/TSqlMapExecutionException.php create mode 100644 framework/Data/SqlMap/DataMapper/TSqlMapFifoCache.php create mode 100644 framework/Data/SqlMap/DataMapper/TSqlMapLruCache.php create mode 100644 framework/Data/SqlMap/DataMapper/TSqlMapTypeHandler.php create mode 100644 framework/Data/SqlMap/DataMapper/TSqlMapUndefinedException.php create mode 100644 framework/Data/SqlMap/Statements/TPostSelectBinding.php create mode 100644 framework/Data/SqlMap/Statements/TResultSetListItemParameter.php create mode 100644 framework/Data/SqlMap/Statements/TResultSetMapItemParameter.php create mode 100644 framework/Data/SqlMap/Statements/TSqlMapObjectCollectionTree.php create mode 100644 framework/Data/TDbColumnCaseMode.php create mode 100644 framework/Data/TDbNullConversionMode.php diff --git a/framework/Data/ActiveRecord/Exceptions/TActiveRecordConfigurationException.php b/framework/Data/ActiveRecord/Exceptions/TActiveRecordConfigurationException.php new file mode 100644 index 00000000..a1c07b8c --- /dev/null +++ b/framework/Data/ActiveRecord/Exceptions/TActiveRecordConfigurationException.php @@ -0,0 +1,22 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.ActiveRecord + */ + +/** + * TActiveRecordConfigurationException class. + * + * @author Wei Zhuo + * @package System.Data.ActiveRecord + * @since 3.1 + */ +class TActiveRecordConfigurationException extends TActiveRecordException +{ + +} \ No newline at end of file diff --git a/framework/Data/ActiveRecord/Exceptions/TActiveRecordException.php b/framework/Data/ActiveRecord/Exceptions/TActiveRecordException.php index 11fb796b..a252631c 100644 --- a/framework/Data/ActiveRecord/Exceptions/TActiveRecordException.php +++ b/framework/Data/ActiveRecord/Exceptions/TActiveRecordException.php @@ -30,17 +30,4 @@ class TActiveRecordException extends TDbException $msgFile=$path.'/messages.txt'; return $msgFile; } -} - -/** - * TActiveRecordConfigurationException class. - * - * @author Wei Zhuo - * @package System.Data.ActiveRecord - * @since 3.1 - */ -class TActiveRecordConfigurationException extends TActiveRecordException -{ - -} - +} \ No newline at end of file diff --git a/framework/Data/ActiveRecord/Scaffold/IScaffoldEditRenderer.php b/framework/Data/ActiveRecord/Scaffold/IScaffoldEditRenderer.php new file mode 100644 index 00000000..e7f85065 --- /dev/null +++ b/framework/Data/ActiveRecord/Scaffold/IScaffoldEditRenderer.php @@ -0,0 +1,31 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.ActiveRecord.Scaffold + */ + +/** + * IScaffoldEditRenderer interface. + * + * IScaffoldEditRenderer defines the interface that an edit renderer + * needs to implement. Besides the {@link getData Data} property, an edit + * renderer also needs to provide {@link updateRecord updateRecord} method + * that is called before the save() method is called on the TActiveRecord. + * + * @author Wei Zhuo + * @package System.Data.ActiveRecord.Scaffold + * @since 3.1 + */ +interface IScaffoldEditRenderer extends IDataRenderer +{ + /** + * This method should update the record with the user input data. + * @param TActiveRecord record to be saved. + */ + public function updateRecord($record); +} \ No newline at end of file diff --git a/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php b/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php index 74e79eaa..592c958a 100644 --- a/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php +++ b/framework/Data/ActiveRecord/Scaffold/TScaffoldEditView.php @@ -281,26 +281,4 @@ class TScaffoldEditView extends TScaffoldBase { return 'group_'.$this->getUniqueID(); } -} - -/** - * IScaffoldEditRenderer interface. - * - * IScaffoldEditRenderer defines the interface that an edit renderer - * needs to implement. Besides the {@link getData Data} property, an edit - * renderer also needs to provide {@link updateRecord updateRecord} method - * that is called before the save() method is called on the TActiveRecord. - * - * @author Wei Zhuo - * @package System.Data.ActiveRecord.Scaffold - * @since 3.1 - */ -interface IScaffoldEditRenderer extends IDataRenderer -{ - /** - * This method should update the record with the user input data. - * @param TActiveRecord record to be saved. - */ - public function updateRecord($record); -} - +} \ No newline at end of file diff --git a/framework/Data/ActiveRecord/TActiveRecord.php b/framework/Data/ActiveRecord/TActiveRecord.php index 735579cd..5e4755d6 100644 --- a/framework/Data/ActiveRecord/TActiveRecord.php +++ b/framework/Data/ActiveRecord/TActiveRecord.php @@ -1046,60 +1046,4 @@ abstract class TActiveRecord extends TComponent public function toJSON(){ return json_encode($this->toArray()); } -} - -/** - * TActiveRecordChangeEventParameter class - * - * TActiveRecordChangeEventParameter encapsulates the parameter data for - * ActiveRecord change commit events that are broadcasted. The following change events - * may be raise: {@link TActiveRecord::OnInsert}, {@link TActiveRecord::OnUpdate} and - * {@link TActiveRecord::OnDelete}. The {@link setIsValid IsValid} parameter can - * be set to false to prevent the requested change event to be performed. - * - * @author Wei Zhuo - * @package System.Data.ActiveRecord - * @since 3.1.2 - */ -class TActiveRecordChangeEventParameter extends TEventParameter -{ - private $_isValid=true; - - /** - * @return boolean whether the event should be performed. - */ - public function getIsValid() - { - return $this->_isValid; - } - - /** - * @param boolean set to false to prevent the event. - */ - public function setIsValid($value) - { - $this->_isValid = TPropertyValue::ensureBoolean($value); - } -} - -/** - * TActiveRecordInvalidFinderResult class. - * TActiveRecordInvalidFinderResult defines the enumerable type for possible results - * if an invalid {@link TActiveRecord::__call magic-finder} invoked. - * - * The following enumerable values are defined: - * - Null: return null (default) - * - Exception: throws a TActiveRecordException - * - * @author Yves Berkholz - * @package System.Data.ActiveRecord - * @see TActiveRecordManager::setInvalidFinderResult - * @see TActiveRecordConfig::setInvalidFinderResult - * @see TActiveRecord::setInvalidFinderResult - * @since 3.1.5 - */ -class TActiveRecordInvalidFinderResult extends TEnumerable -{ - const Null = 'Null'; - const Exception = 'Exception'; -} +} \ No newline at end of file diff --git a/framework/Data/ActiveRecord/TActiveRecordChangeEventParameter.php b/framework/Data/ActiveRecord/TActiveRecordChangeEventParameter.php new file mode 100644 index 00000000..f1307529 --- /dev/null +++ b/framework/Data/ActiveRecord/TActiveRecordChangeEventParameter.php @@ -0,0 +1,44 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.ActiveRecord + */ + +/** + * TActiveRecordChangeEventParameter class + * + * TActiveRecordChangeEventParameter encapsulates the parameter data for + * ActiveRecord change commit events that are broadcasted. The following change events + * may be raise: {@link TActiveRecord::OnInsert}, {@link TActiveRecord::OnUpdate} and + * {@link TActiveRecord::OnDelete}. The {@link setIsValid IsValid} parameter can + * be set to false to prevent the requested change event to be performed. + * + * @author Wei Zhuo + * @package System.Data.ActiveRecord + * @since 3.1.2 + */ +class TActiveRecordChangeEventParameter extends TEventParameter +{ + private $_isValid=true; + + /** + * @return boolean whether the event should be performed. + */ + public function getIsValid() + { + return $this->_isValid; + } + + /** + * @param boolean set to false to prevent the event. + */ + public function setIsValid($value) + { + $this->_isValid = TPropertyValue::ensureBoolean($value); + } +} \ No newline at end of file diff --git a/framework/Data/ActiveRecord/TActiveRecordInvalidFinderResult.php b/framework/Data/ActiveRecord/TActiveRecordInvalidFinderResult.php new file mode 100644 index 00000000..1d02f4c8 --- /dev/null +++ b/framework/Data/ActiveRecord/TActiveRecordInvalidFinderResult.php @@ -0,0 +1,32 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.ActiveRecord + */ + +/** + * TActiveRecordInvalidFinderResult class. + * TActiveRecordInvalidFinderResult defines the enumerable type for possible results + * if an invalid {@link TActiveRecord::__call magic-finder} invoked. + * + * The following enumerable values are defined: + * - Null: return null (default) + * - Exception: throws a TActiveRecordException + * + * @author Yves Berkholz + * @package System.Data.ActiveRecord + * @see TActiveRecordManager::setInvalidFinderResult + * @see TActiveRecordConfig::setInvalidFinderResult + * @see TActiveRecord::setInvalidFinderResult + * @since 3.1.5 + */ +class TActiveRecordInvalidFinderResult extends TEnumerable +{ + const Null = 'Null'; + const Exception = 'Exception'; +} \ No newline at end of file diff --git a/framework/Data/DataGateway/TDataGatewayCommand.php b/framework/Data/DataGateway/TDataGatewayCommand.php deleted file mode 100644 index fb0f5df0..00000000 --- a/framework/Data/DataGateway/TDataGatewayCommand.php +++ /dev/null @@ -1,540 +0,0 @@ - - * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005-2014 PradoSoft - * @license http://www.pradosoft.com/license/ - * @version $Id$ - * @package System.Data.DataGateway - */ - -/** - * TDataGatewayCommand is command builder and executor class for - * TTableGateway and TActiveRecordGateway. - * - * TDataGatewayCommand builds the TDbCommand for TTableGateway - * and TActiveRecordGateway commands such as find(), update(), insert(), etc, - * using the TDbCommandBuilder classes (database specific TDbCommandBuilder - * classes are used). - * - * Once the command is built and the query parameters are binded, the - * {@link OnCreateCommand} event is raised. Event handlers for the OnCreateCommand - * event should not alter the Command property nor the Criteria property of the - * TDataGatewayEventParameter. - * - * TDataGatewayCommand excutes the TDbCommands and returns the result obtained from the - * database (returned value depends on the method executed). The - * {@link OnExecuteCommand} event is raised after the command is executed and resulting - * data is set in the TDataGatewayResultEventParameter object's Result property. - * - * @author Wei Zhuo - * @version $Id$ - * @package System.Data.DataGateway - * @since 3.1 - */ -class TDataGatewayCommand extends TComponent -{ - private $_builder; - - /** - * @param TDbCommandBuilder database specific database command builder. - */ - public function __construct($builder) - { - $this->_builder = $builder; - } - - /** - * @return TDbTableInfo - */ - public function getTableInfo() - { - return $this->_builder->getTableInfo(); - } - - /** - * @return TDbConnection - */ - public function getDbConnection() - { - return $this->_builder->getDbConnection(); - } - - /** - * @return TDbCommandBuilder - */ - public function getBuilder() - { - return $this->_builder; - } - - /** - * Executes a delete command. - * @param TSqlCriteria delete conditions and parameters. - * @return integer number of records affected. - */ - public function delete($criteria) - { - $where = $criteria->getCondition(); - $parameters = $criteria->getParameters()->toArray(); - $command = $this->getBuilder()->createDeleteCommand($where, $parameters); - $this->onCreateCommand($command,$criteria); - $command->prepare(); - return $command->execute(); - } - - /** - * Updates the table with new data. - * @param array date for update. - * @param TSqlCriteria update conditions and parameters. - * @return integer number of records affected. - */ - public function update($data, $criteria) - { - $where = $criteria->getCondition(); - $parameters = $criteria->getParameters()->toArray(); - $command = $this->getBuilder()->createUpdateCommand($data,$where, $parameters); - $this->onCreateCommand($command,$criteria); - $command->prepare(); - return $this->onExecuteCommand($command, $command->execute()); - } - - /** - * @param array update for update - * @param array primary key-value name pairs. - * @return integer number of records affected. - */ - public function updateByPk($data, $keys) - { - list($where, $parameters) = $this->getPrimaryKeyCondition((array)$keys); - return $this->update($data, new TSqlCriteria($where, $parameters)); - } - - /** - * Find one record matching the critera. - * @param TSqlCriteria find conditions and parameters. - * @return array matching record. - */ - public function find($criteria) - { - $command = $this->getFindCommand($criteria); - return $this->onExecuteCommand($command, $command->queryRow()); - } - - /** - * Find one or more matching records. - * @param TSqlCriteria $criteria - * @return TDbDataReader record reader. - */ - public function findAll($criteria) - { - $command = $this->getFindCommand($criteria); - return $this->onExecuteCommand($command, $command->query()); - } - - /** - * Build the find command from the criteria. Limit, Offset and Ordering are applied if applicable. - * @param TSqlCriteria $criteria - * @return TDbCommand. - */ - protected function getFindCommand($criteria) - { - if($criteria===null) - return $this->getBuilder()->createFindCommand(); - $where = $criteria->getCondition(); - $parameters = $criteria->getParameters()->toArray(); - $ordering = $criteria->getOrdersBy(); - $limit = $criteria->getLimit(); - $offset = $criteria->getOffset(); - $select = $criteria->getSelect(); - $command = $this->getBuilder()->createFindCommand($where,$parameters,$ordering,$limit,$offset,$select); - $this->onCreateCommand($command, $criteria); - return $command; - } - - /** - * @param mixed primary key value, or composite key values as array. - * @return array matching record. - */ - public function findByPk($keys) - { - list($where, $parameters) = $this->getPrimaryKeyCondition((array)$keys); - $command = $this->getBuilder()->createFindCommand($where, $parameters); - $this->onCreateCommand($command, new TSqlCriteria($where,$parameters)); - return $this->onExecuteCommand($command, $command->queryRow()); - } - - /** - * @param array multiple primary key values or composite value arrays - * @return TDbDataReader record reader. - */ - public function findAllByPk($keys) - { - $where = $this->getCompositeKeyCondition((array)$keys); - $command = $this->getBuilder()->createFindCommand($where); - $this->onCreateCommand($command, new TSqlCriteria($where,$keys)); - return $this->onExecuteCommand($command,$command->query()); - } - - public function findAllByIndex($criteria,$fields,$values) - { - $index = $this->getIndexKeyCondition($this->getTableInfo(),$fields,$values); - if(strlen($where = $criteria->getCondition())>0) - $criteria->setCondition("({$index}) AND ({$where})"); - else - $criteria->setCondition($index); - $command = $this->getFindCommand($criteria); - $this->onCreateCommand($command, $criteria); - return $this->onExecuteCommand($command,$command->query()); - } - - /** - * @param array multiple primary key values or composite value arrays - * @return integer number of rows affected. - */ - public function deleteByPk($keys) - { - $where = $this->getCompositeKeyCondition((array)$keys); - $command = $this->getBuilder()->createDeleteCommand($where); - $this->onCreateCommand($command, new TSqlCriteria($where,$keys)); - $command->prepare(); - return $this->onExecuteCommand($command,$command->execute()); - } - - public function getIndexKeyCondition($table,$fields,$values) - { - if (!count($values)) - return 'FALSE'; - $columns = array(); - $tableName = $table->getTableFullName(); - foreach($fields as $field) - $columns[] = $tableName.'.'.$table->getColumn($field)->getColumnName(); - return '('.implode(', ',$columns).') IN '.$this->quoteTuple($values); - } - - /** - * Construct a "pk IN ('key1', 'key2', ...)" criteria. - * @param array values for IN predicate - * @param string SQL string for primary keys IN a list. - */ - protected function getCompositeKeyCondition($values) - { - $primary = $this->getTableInfo()->getPrimaryKeys(); - $count = count($primary); - if($count===0) - { - throw new TDbException('dbtablegateway_no_primary_key_found', - $this->getTableInfo()->getTableFullName()); - } - if(!is_array($values) || count($values) === 0) - { - throw new TDbException('dbtablegateway_missing_pk_values', - $this->getTableInfo()->getTableFullName()); - } - if($count>1 && (!isset($values[0]) || !is_array($values[0]))) - $values = array($values); - if($count > 1 && count($values[0]) !== $count) - { - throw new TDbException('dbtablegateway_pk_value_count_mismatch', - $this->getTableInfo()->getTableFullName()); - } - return $this->getIndexKeyCondition($this->getTableInfo(),$primary, $values); - } - - /** - * @param TDbConnection database connection. - * @param array values - * @return string quoted recursive tuple values, e.g. "('val1', 'val2')". - */ - protected function quoteTuple($array) - { - $conn = $this->getDbConnection(); - $data = array(); - foreach($array as $k=>$v) - $data[] = is_array($v) ? $this->quoteTuple($v) : $conn->quoteString($v); - return '('.implode(', ', $data).')'; - } - - /** - * Create the condition and parameters for find by primary. - * @param array primary key values - * @return array tuple($where, $parameters) - */ - protected function getPrimaryKeyCondition($values) - { - $primary = $this->getTableInfo()->getPrimaryKeys(); - if(count($primary)===0) - { - throw new TDbException('dbtablegateway_no_primary_key_found', - $this->getTableInfo()->getTableFullName()); - } - $criteria=array(); - $bindings=array(); - $i = 0; - foreach($primary as $key) - { - $column = $this->getTableInfo()->getColumn($key)->getColumnName(); - $criteria[] = $column.' = :'.$key; - $bindings[$key] = isset($values[$key])?$values[$key]:$values[$i++]; - } - return array(implode(' AND ', $criteria), $bindings); - } - - /** - * Find one matching records for arbituary SQL. - * @param TSqlCriteria $criteria - * @return TDbDataReader record reader. - */ - public function findBySql($criteria) - { - $command = $this->getSqlCommand($criteria); - return $this->onExecuteCommand($command, $command->queryRow()); - } - - /** - * Find zero or more matching records for arbituary SQL. - * @param TSqlCriteria $criteria - * @return TDbDataReader record reader. - */ - public function findAllBySql($criteria) - { - $command = $this->getSqlCommand($criteria); - return $this->onExecuteCommand($command, $command->query()); - } - - /** - * Build sql command from the criteria. Limit, Offset and Ordering are applied if applicable. - * @param TSqlCriteria $criteria - * @return TDbCommand command corresponding to the criteria. - */ - protected function getSqlCommand($criteria) - { - $sql = $criteria->getCondition(); - $ordering = $criteria->getOrdersBy(); - $limit = $criteria->getLimit(); - $offset = $criteria->getOffset(); - if(count($ordering) > 0) - $sql = $this->getBuilder()->applyOrdering($sql, $ordering); - if($limit>=0 || $offset>=0) - $sql = $this->getBuilder()->applyLimitOffset($sql, $limit, $offset); - $command = $this->getBuilder()->createCommand($sql); - $this->getBuilder()->bindArrayValues($command, $criteria->getParameters()->toArray()); - $this->onCreateCommand($command, $criteria); - return $command; - } - - /** - * @param TSqlCriteria $criteria - * @return integer number of records. - */ - public function count($criteria) - { - if($criteria===null) - return (int)$this->getBuilder()->createCountCommand()->queryScalar(); - $where = $criteria->getCondition(); - $parameters = $criteria->getParameters()->toArray(); - $ordering = $criteria->getOrdersBy(); - $limit = $criteria->getLimit(); - $offset = $criteria->getOffset(); - $command = $this->getBuilder()->createCountCommand($where,$parameters,$ordering,$limit,$offset); - $this->onCreateCommand($command, $criteria); - return $this->onExecuteCommand($command, (int)$command->queryScalar()); - } - - /** - * Inserts a new record into the table. Each array key must - * correspond to a column name in the table unless a null value is permitted. - * @param array new record data. - * @return mixed last insert id if one column contains a serial or sequence, - * otherwise true if command executes successfully and affected 1 or more rows. - */ - public function insert($data) - { - $command=$this->getBuilder()->createInsertCommand($data); - $this->onCreateCommand($command, new TSqlCriteria(null,$data)); - $command->prepare(); - if($this->onExecuteCommand($command, $command->execute()) > 0) - { - $value = $this->getLastInsertId(); - return $value !== null ? $value : true; - } - return false; - } - - /** - * Iterate through all the columns and returns the last insert id of the - * first column that has a sequence or serial. - * @return mixed last insert id, null if none is found. - */ - public function getLastInsertID() - { - return $this->getBuilder()->getLastInsertID(); - } - - /** - * @param string __call method name - * @param string criteria conditions - * @param array method arguments - * @return TActiveRecordCriteria criteria created from the method name and its arguments. - */ - public function createCriteriaFromString($method, $condition, $args) - { - $fields = $this->extractMatchingConditions($method, $condition); - $args=count($args) === 1 && is_array($args[0]) ? $args[0] : $args; - if(count($fields)>count($args)) - { - throw new TDbException('dbtablegateway_mismatch_args_exception', - $method,count($fields),count($args)); - } - return new TSqlCriteria(implode(' ',$fields), $args); - } - - /** - * Calculates the AND/OR condition from dynamic method substrings using - * table meta data, allows for any AND-OR combinations. - * @param string dynamic method name - * @param string dynamic method search criteria - * @return array search condition substrings - */ - protected function extractMatchingConditions($method, $condition) - { - $table = $this->getTableInfo(); - $columns = $table->getLowerCaseColumnNames(); - $regexp = '/('.implode('|', array_keys($columns)).')(and|_and_|or|_or_)?/i'; - $matches = array(); - if(!preg_match_all($regexp, strtolower($condition), $matches,PREG_SET_ORDER)) - { - throw new TDbException('dbtablegateway_mismatch_column_name', - $method, implode(', ', $columns), $table->getTableFullName()); - } - - $fields = array(); - foreach($matches as $match) - { - $key = $columns[$match[1]]; - $column = $table->getColumn($key)->getColumnName(); - $sql = $column . ' = ? '; - if(count($match) > 2) - $sql .= strtoupper(str_replace('_', '', $match[2])); - $fields[] = $sql; - } - return $fields; - } - - /** - * Raised when a command is prepared and parameter binding is completed. - * The parameter object is TDataGatewayEventParameter of which the - * {@link TDataGatewayEventParameter::getCommand Command} property can be - * inspected to obtain the sql query to be executed. - * @param TDataGatewayCommand originator $sender - * @param TDataGatewayEventParameter - */ - public function onCreateCommand($command, $criteria) - { - $this->raiseEvent('OnCreateCommand', $this, new TDataGatewayEventParameter($command,$criteria)); - } - - /** - * Raised when a command is executed and the result from the database was returned. - * The parameter object is TDataGatewayResultEventParameter of which the - * {@link TDataGatewayEventParameter::getResult Result} property contains - * the data return from the database. The data returned can be changed - * by setting the {@link TDataGatewayEventParameter::setResult Result} property. - * @param TDataGatewayCommand originator $sender - * @param TDataGatewayResultEventParameter - */ - public function onExecuteCommand($command, $result) - { - $parameter = new TDataGatewayResultEventParameter($command, $result); - $this->raiseEvent('OnExecuteCommand', $this, $parameter); - return $parameter->getResult(); - } -} - -/** - * TDataGatewayEventParameter class contains the TDbCommand to be executed as - * well as the criteria object. - * - * @author Wei Zhuo - * @version $Id$ - * @package System.Data.DataGateway - * @since 3.1 - */ -class TDataGatewayEventParameter extends TEventParameter -{ - private $_command; - private $_criteria; - - public function __construct($command,$criteria) - { - $this->_command=$command; - $this->_criteria=$criteria; - } - - /** - * The database command to be executed. Do not rebind the parameters or change - * the sql query string. - * @return TDbCommand command to be executed. - */ - public function getCommand() - { - return $this->_command; - } - - /** - * @return TSqlCriteria criteria used to bind the sql query parameters. - */ - public function getCriteria() - { - return $this->_criteria; - } -} - -/** - * TDataGatewayResultEventParameter contains the TDbCommand executed and the resulting - * data returned from the database. The data can be changed by changing the - * {@link setResult Result} property. - * - * @author Wei Zhuo - * @version $Id$ - * @package System.Data.DataGateway - * @since 3.1 - */ -class TDataGatewayResultEventParameter extends TEventParameter -{ - private $_command; - private $_result; - - public function __construct($command,$result) - { - $this->_command=$command; - $this->_result=$result; - } - - /** - * @return TDbCommand database command executed. - */ - public function getCommand() - { - return $this->_command; - } - - /** - * @return mixed result returned from executing the command. - */ - public function getResult() - { - return $this->_result; - } - - /** - * @param mixed change the result returned by the gateway. - */ - public function setResult($value) - { - $this->_result=$value; - } -} - diff --git a/framework/Data/DataGateway/TDataGatewayEventParameter.php b/framework/Data/DataGateway/TDataGatewayEventParameter.php new file mode 100644 index 00000000..f6fcc3f2 --- /dev/null +++ b/framework/Data/DataGateway/TDataGatewayEventParameter.php @@ -0,0 +1,319 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.DataGateway + */)"); + else + $criteria->setCondition($index); + $command = $this->getFindCommand($criteria); + $this->onCreateCommand($command, $criteria); + return $this->onExecuteCommand($command,$command->query()); + } + + /** + * @param array multiple primary key values or composite value arrays + * @return integer number of rows affected. + */ + public function deleteByPk($keys) + { + $where = $this->getCompositeKeyCondition((array)$keys); + $command = $this->getBuilder()->createDeleteCommand($where); + $this->onCreateCommand($command, new TSqlCriteria($where,$keys)); + $command->prepare(); + return $this->onExecuteCommand($command,$command->execute()); + } + + public function getIndexKeyCondition($table,$fields,$values) + { + if (!count($values)) + return 'FALSE'; + $columns = array(); + $tableName = $table->getTableFullName(); + foreach($fields as $field) + $columns[] = $tableName.'.'.$table->getColumn($field)->getColumnName(); + return '('.implode(', ',$columns).') IN '.$this->quoteTuple($values); + } + + /** + * Construct a "pk IN ('key1', 'key2', ...)" criteria. + * @param array values for IN predicate + * @param string SQL string for primary keys IN a list. + */ + protected function getCompositeKeyCondition($values) + { + $primary = $this->getTableInfo()->getPrimaryKeys(); + $count = count($primary); + if($count===0) + { + throw new TDbException('dbtablegateway_no_primary_key_found', + $this->getTableInfo()->getTableFullName()); + } + if(!is_array($values) || count($values) === 0) + { + throw new TDbException('dbtablegateway_missing_pk_values', + $this->getTableInfo()->getTableFullName()); + } + if($count>1 && (!isset($values[0]) || !is_array($values[0]))) + $values = array($values); + if($count > 1 && count($values[0]) !== $count) + { + throw new TDbException('dbtablegateway_pk_value_count_mismatch', + $this->getTableInfo()->getTableFullName()); + } + return $this->getIndexKeyCondition($this->getTableInfo(),$primary, $values); + } + + /** + * @param TDbConnection database connection. + * @param array values + * @return string quoted recursive tuple values, e.g. "('val1', 'val2')". + */ + protected function quoteTuple($array) + { + $conn = $this->getDbConnection(); + $data = array(); + foreach($array as $k=>$v) + $data[] = is_array($v) ? $this->quoteTuple($v) : $conn->quoteString($v); + return '('.implode(', ', $data).')'; + } + + /** + * Create the condition and parameters for find by primary. + * @param array primary key values + * @return array tuple($where, $parameters) + */ + protected function getPrimaryKeyCondition($values) + { + $primary = $this->getTableInfo()->getPrimaryKeys(); + if(count($primary)===0) + { + throw new TDbException('dbtablegateway_no_primary_key_found', + $this->getTableInfo()->getTableFullName()); + } + $criteria=array(); + $bindings=array(); + $i = 0; + foreach($primary as $key) + { + $column = $this->getTableInfo()->getColumn($key)->getColumnName(); + $criteria[] = $column.' = :'.$key; + $bindings[$key] = isset($values[$key])?$values[$key]:$values[$i++]; + } + return array(implode(' AND ', $criteria), $bindings); + } + + /** + * Find one matching records for arbituary SQL. + * @param TSqlCriteria $criteria + * @return TDbDataReader record reader. + */ + public function findBySql($criteria) + { + $command = $this->getSqlCommand($criteria); + return $this->onExecuteCommand($command, $command->queryRow()); + } + + /** + * Find zero or more matching records for arbituary SQL. + * @param TSqlCriteria $criteria + * @return TDbDataReader record reader. + */ + public function findAllBySql($criteria) + { + $command = $this->getSqlCommand($criteria); + return $this->onExecuteCommand($command, $command->query()); + } + + /** + * Build sql command from the criteria. Limit, Offset and Ordering are applied if applicable. + * @param TSqlCriteria $criteria + * @return TDbCommand command corresponding to the criteria. + */ + protected function getSqlCommand($criteria) + { + $sql = $criteria->getCondition(); + $ordering = $criteria->getOrdersBy(); + $limit = $criteria->getLimit(); + $offset = $criteria->getOffset(); + if(count($ordering) > 0) + $sql = $this->getBuilder()->applyOrdering($sql, $ordering); + if($limit>=0 || $offset>=0) + $sql = $this->getBuilder()->applyLimitOffset($sql, $limit, $offset); + $command = $this->getBuilder()->createCommand($sql); + $this->getBuilder()->bindArrayValues($command, $criteria->getParameters()->toArray()); + $this->onCreateCommand($command, $criteria); + return $command; + } + + /** + * @param TSqlCriteria $criteria + * @return integer number of records. + */ + public function count($criteria) + { + if($criteria===null) + return (int)$this->getBuilder()->createCountCommand()->queryScalar(); + $where = $criteria->getCondition(); + $parameters = $criteria->getParameters()->toArray(); + $ordering = $criteria->getOrdersBy(); + $limit = $criteria->getLimit(); + $offset = $criteria->getOffset(); + $command = $this->getBuilder()->createCountCommand($where,$parameters,$ordering,$limit,$offset); + $this->onCreateCommand($command, $criteria); + return $this->onExecuteCommand($command, (int)$command->queryScalar()); + } + + /** + * Inserts a new record into the table. Each array key must + * correspond to a column name in the table unless a null value is permitted. + * @param array new record data. + * @return mixed last insert id if one column contains a serial or sequence, + * otherwise true if command executes successfully and affected 1 or more rows. + */ + public function insert($data) + { + $command=$this->getBuilder()->createInsertCommand($data); + $this->onCreateCommand($command, new TSqlCriteria(null,$data)); + $command->prepare(); + if($this->onExecuteCommand($command, $command->execute()) > 0) + { + $value = $this->getLastInsertId(); + return $value !== null ? $value : true; + } + return false; + } + + /** + * Iterate through all the columns and returns the last insert id of the + * first column that has a sequence or serial. + * @return mixed last insert id, null if none is found. + */ + public function getLastInsertID() + { + return $this->getBuilder()->getLastInsertID(); + } + + /** + * @param string __call method name + * @param string criteria conditions + * @param array method arguments + * @return TActiveRecordCriteria criteria created from the method name and its arguments. + */ + public function createCriteriaFromString($method, $condition, $args) + { + $fields = $this->extractMatchingConditions($method, $condition); + $args=count($args) === 1 && is_array($args[0]) ? $args[0] : $args; + if(count($fields)>count($args)) + { + throw new TDbException('dbtablegateway_mismatch_args_exception', + $method,count($fields),count($args)); + } + return new TSqlCriteria(implode(' ',$fields), $args); + } + + /** + * Calculates the AND/OR condition from dynamic method substrings using + * table meta data, allows for any AND-OR combinations. + * @param string dynamic method name + * @param string dynamic method search criteria + * @return array search condition substrings + */ + protected function extractMatchingConditions($method, $condition) + { + $table = $this->getTableInfo(); + $columns = $table->getLowerCaseColumnNames(); + $regexp = '/('.implode('|', array_keys($columns)).')(and|_and_|or|_or_)?/i'; + $matches = array(); + if(!preg_match_all($regexp, strtolower($condition), $matches,PREG_SET_ORDER)) + { + throw new TDbException('dbtablegateway_mismatch_column_name', + $method, implode(', ', $columns), $table->getTableFullName()); + } + + $fields = array(); + foreach($matches as $match) + { + $key = $columns[$match[1]]; + $column = $table->getColumn($key)->getColumnName(); + $sql = $column . ' = ? '; + if(count($match) > 2) + $sql .= strtoupper(str_replace('_', '', $match[2])); + $fields[] = $sql; + } + return $fields; + } + + /** + * Raised when a command is prepared and parameter binding is completed. + * The parameter object is TDataGatewayEventParameter of which the + * {@link TDataGatewayEventParameter::getCommand Command} property can be + * inspected to obtain the sql query to be executed. + * @param TDataGatewayCommand originator $sender + * @param TDataGatewayEventParameter + */ + public function onCreateCommand($command, $criteria) + { + $this->raiseEvent('OnCreateCommand', $this, new TDataGatewayEventParameter($command,$criteria)); + } + + /** + * Raised when a command is executed and the result from the database was returned. + * The parameter object is TDataGatewayResultEventParameter of which the + * {@link TDataGatewayEventParameter::getResult Result} property contains + * the data return from the database. The data returned can be changed + * by setting the {@link TDataGatewayEventParameter::setResult Result} property. + * @param TDataGatewayCommand originator $sender + * @param TDataGatewayResultEventParameter + */ + public function onExecuteCommand($command, $result) + { + $parameter = new TDataGatewayResultEventParameter($command, $result); + $this->raiseEvent('OnExecuteCommand', $this, $parameter); + return $parameter->getResult(); + } +} + +/** + * TDataGatewayEventParameter class contains the TDbCommand to be executed as + * well as the criteria object. + * + * @author Wei Zhuo + * @version $Id$ + * @package System.Data.DataGateway + * @since 3.1 + */ +class TDataGatewayEventParameter extends TEventParameter +{ + private $_command; + private $_criteria; + + public function __construct($command,$criteria) + { + $this->_command=$command; + $this->_criteria=$criteria; + } + + /** + * The database command to be executed. Do not rebind the parameters or change + * the sql query string. + * @return TDbCommand command to be executed. + */ + public function getCommand() + { + return $this->_command; + } + + /** + * @return TSqlCriteria criteria used to bind the sql query parameters. + */ + public function getCriteria() + { + return $this->_criteria; + } +} \ No newline at end of file diff --git a/framework/Data/DataGateway/TDataGatewayResultEventParameter.php b/framework/Data/DataGateway/TDataGatewayResultEventParameter.php new file mode 100644 index 00000000..004ab2d8 --- /dev/null +++ b/framework/Data/DataGateway/TDataGatewayResultEventParameter.php @@ -0,0 +1,57 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Id$ + * @package System.Data.DataGateway + */ + +/** + * TDataGatewayResultEventParameter contains the TDbCommand executed and the resulting + * data returned from the database. The data can be changed by changing the + * {@link setResult Result} property. + * + * @author Wei Zhuo + * @version $Id$ + * @package System.Data.DataGateway + * @since 3.1 + */ +class TDataGatewayResultEventParameter extends TEventParameter +{ + private $_command; + private $_result; + + public function __construct($command,$result) + { + $this->_command=$command; + $this->_result=$result; + } + + /** + * @return TDbCommand database command executed. + */ + public function getCommand() + { + return $this->_command; + } + + /** + * @return mixed result returned from executing the command. + */ + public function getResult() + { + return $this->_result; + } + + /** + * @param mixed change the result returned by the gateway. + */ + public function setResult($value) + { + $this->_result=$value; + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TDiscriminator.php b/framework/Data/SqlMap/Configuration/TDiscriminator.php index 1f7347ae..1c7f679f 100644 --- a/framework/Data/SqlMap/Configuration/TDiscriminator.php +++ b/framework/Data/SqlMap/Configuration/TDiscriminator.php @@ -174,56 +174,4 @@ class TDiscriminator extends TComponent $manager->getResultMap($subMap->getResultMapping()); } } -} - -/** - * TSubMap class defines a submapping value and the corresponding - * - * The {@link Value setValue()} property is used for comparison with the - * discriminator column value. When the {@link Value setValue()} matches - * that of the discriminator column value, the corresponding {@link ResultMapping setResultMapping} - * is used inplace of the current result map. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Configuration - * @since 3.1 - */ -class TSubMap extends TComponent -{ - private $_value; - private $_resultMapping; - - /** - * @return string value for comparison with discriminator column value. - */ - public function getValue() - { - return $this->_value; - } - - /** - * @param string value for comparison with discriminator column value. - */ - public function setValue($value) - { - $this->_value = $value; - } - - /** - * The result map to use when the Value matches the discriminator column value. - * @return string ID of a result map - */ - public function getResultMapping() - { - return $this->_resultMapping; - } - - /** - * @param string ID of a result map - */ - public function setResultMapping($value) - { - $this->_resultMapping = $value; - } -} - +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapCacheKey.php b/framework/Data/SqlMap/Configuration/TSqlMapCacheKey.php new file mode 100644 index 00000000..095f24d4 --- /dev/null +++ b/framework/Data/SqlMap/Configuration/TSqlMapCacheKey.php @@ -0,0 +1,49 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Configuration + */ + +/** + * TSqlMapCacheKey class. + * + * Provides a hash of the object to be cached. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Configuration + * @since 3.1 + */ +class TSqlMapCacheKey +{ + private $_key; + + /** + * @param mixed object to be cached. + */ + public function __construct($object) + { + $this->_key = $this->generateKey(serialize($object)); + } + + /** + * @param string serialized object + * @return string crc32 hash of the serialized object. + */ + protected function generateKey($string) + { + return sprintf('%x',crc32($string)); + } + + /** + * @return string object hash. + */ + public function getHash() + { + return $this->_key; + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapCacheModel.php b/framework/Data/SqlMap/Configuration/TSqlMapCacheModel.php index 271db675..0021cad8 100644 --- a/framework/Data/SqlMap/Configuration/TSqlMapCacheModel.php +++ b/framework/Data/SqlMap/Configuration/TSqlMapCacheModel.php @@ -183,60 +183,4 @@ class TSqlMapCacheModel extends TComponent else return 0; } -} - -/** - * TSqlMapCacheTypes enumerable class. - * - * Implemented cache are 'Basic', 'FIFO' and 'LRU'. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Configuration - * @since 3.1 - */ -class TSqlMapCacheTypes extends TEnumerable -{ - const Basic='Basic'; - const FIFO='FIFO'; - const LRU='LRU'; -} - -/** - * TSqlMapCacheKey class. - * - * Provides a hash of the object to be cached. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Configuration - * @since 3.1 - */ -class TSqlMapCacheKey -{ - private $_key; - - /** - * @param mixed object to be cached. - */ - public function __construct($object) - { - $this->_key = $this->generateKey(serialize($object)); - } - - /** - * @param string serialized object - * @return string crc32 hash of the serialized object. - */ - protected function generateKey($string) - { - return sprintf('%x',crc32($string)); - } - - /** - * @return string object hash. - */ - public function getHash() - { - return $this->_key; - } -} - +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapCacheTypes.php b/framework/Data/SqlMap/Configuration/TSqlMapCacheTypes.php new file mode 100644 index 00000000..c252151c --- /dev/null +++ b/framework/Data/SqlMap/Configuration/TSqlMapCacheTypes.php @@ -0,0 +1,26 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Configuration + */ + +/** + * TSqlMapCacheTypes enumerable class. + * + * Implemented cache are 'Basic', 'FIFO' and 'LRU'. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Configuration + * @since 3.1 + */ +class TSqlMapCacheTypes extends TEnumerable +{ + const Basic='Basic'; + const FIFO='FIFO'; + const LRU='LRU'; +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapDelete.php b/framework/Data/SqlMap/Configuration/TSqlMapDelete.php new file mode 100644 index 00000000..90e1ab21 --- /dev/null +++ b/framework/Data/SqlMap/Configuration/TSqlMapDelete.php @@ -0,0 +1,22 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Configuration + */ + +/** + * TSqlMapDelete class corresponds to the element. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Configuration + * @since 3.1 + */ +class TSqlMapDelete extends TSqlMapUpdate +{ +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapInsert.php b/framework/Data/SqlMap/Configuration/TSqlMapInsert.php new file mode 100644 index 00000000..f269386f --- /dev/null +++ b/framework/Data/SqlMap/Configuration/TSqlMapInsert.php @@ -0,0 +1,42 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Configuration + */ + +/** + * TSqlMapInsert class corresponds to the element. + * + * The element allows child elements that can be used + * to generate a key to be used for the insert command. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Configuration + * @since 3.1 + */ +class TSqlMapInsert extends TSqlMapStatement +{ + private $_selectKey=null; + + /** + * @return TSqlMapSelectKey select key element. + */ + public function getSelectKey() + { + return $this->_selectKey; + } + + /** + * @param TSqlMapSelectKey select key. + */ + public function setSelectKey($value) + { + $this->_selectKey = $value; + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapSelect.php b/framework/Data/SqlMap/Configuration/TSqlMapSelect.php new file mode 100644 index 00000000..098be9fb --- /dev/null +++ b/framework/Data/SqlMap/Configuration/TSqlMapSelect.php @@ -0,0 +1,26 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Configuration + */ + +/** + * TSqlMapSelect class file. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Statements + * @since 3.1 + */ +class TSqlMapSelect extends TSqlMapStatement +{ + private $_generate; + + public function getGenerate(){ return $this->_generate; } + public function setGenerate($value){ $this->_generate = $value; } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapSelectKey.php b/framework/Data/SqlMap/Configuration/TSqlMapSelectKey.php new file mode 100644 index 00000000..51299097 --- /dev/null +++ b/framework/Data/SqlMap/Configuration/TSqlMapSelectKey.php @@ -0,0 +1,72 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Configuration + */ + +/** + * TSqlMapSelect corresponds to the element. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Configuration + * @since 3.1 + */ +class TSqlMapSelectKey extends TSqlMapStatement +{ + private $_type = 'post'; + private $_property; + + /** + * @return string select generated key type, 'post' or 'pre'. + */ + public function getType() + { + return $this->_type; + } + + /** + * @param string select generated key type, 'post' or 'pre'. + */ + public function setType($value) + { + $this->_type = strtolower($value) == 'post' ? 'post' : 'pre'; + } + + /** + * @return string property name for the generated key. + */ + public function getProperty() + { + return $this->_property; + } + + /** + * @param string property name for the generated key. + */ + public function setProperty($value) + { + $this->_property = $value; + } + + /** + * @throws TSqlMapConfigurationException extends is unsupported. + */ + public function setExtends($value) + { + throw new TSqlMapConfigurationException('sqlmap_can_not_extend_select_key'); + } + + /** + * @return boolean true if key is generated after insert command, false otherwise. + */ + public function getIsAfter() + { + return $this->_type == 'post'; + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapStatement.php b/framework/Data/SqlMap/Configuration/TSqlMapStatement.php index 0efcc5b6..3477e9cf 100644 --- a/framework/Data/SqlMap/Configuration/TSqlMapStatement.php +++ b/framework/Data/SqlMap/Configuration/TSqlMapStatement.php @@ -311,134 +311,4 @@ class TSqlMapStatement extends TComponent return array_diff(parent::__sleep(),$exprops); } -} - -/** - * TSqlMapSelect class file. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Statements - * @since 3.1 - */ -class TSqlMapSelect extends TSqlMapStatement -{ - private $_generate; - - public function getGenerate(){ return $this->_generate; } - public function setGenerate($value){ $this->_generate = $value; } -} - -/** - * TSqlMapInsert class corresponds to the element. - * - * The element allows child elements that can be used - * to generate a key to be used for the insert command. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Configuration - * @since 3.1 - */ -class TSqlMapInsert extends TSqlMapStatement -{ - private $_selectKey=null; - - /** - * @return TSqlMapSelectKey select key element. - */ - public function getSelectKey() - { - return $this->_selectKey; - } - - /** - * @param TSqlMapSelectKey select key. - */ - public function setSelectKey($value) - { - $this->_selectKey = $value; - } -} - -/** - * TSqlMapUpdate class corresponds to element. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Configuration - * @since 3.1 - */ -class TSqlMapUpdate extends TSqlMapStatement -{ -} - -/** - * TSqlMapDelete class corresponds to the element. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Configuration - * @since 3.1 - */ -class TSqlMapDelete extends TSqlMapUpdate -{ -} - -/** - * TSqlMapSelect corresponds to the element. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Configuration - * @since 3.1 - */ -class TSqlMapSelectKey extends TSqlMapStatement -{ - private $_type = 'post'; - private $_property; - - /** - * @return string select generated key type, 'post' or 'pre'. - */ - public function getType() - { - return $this->_type; - } - - /** - * @param string select generated key type, 'post' or 'pre'. - */ - public function setType($value) - { - $this->_type = strtolower($value) == 'post' ? 'post' : 'pre'; - } - - /** - * @return string property name for the generated key. - */ - public function getProperty() - { - return $this->_property; - } - - /** - * @param string property name for the generated key. - */ - public function setProperty($value) - { - $this->_property = $value; - } - - /** - * @throws TSqlMapConfigurationException extends is unsupported. - */ - public function setExtends($value) - { - throw new TSqlMapConfigurationException('sqlmap_can_not_extend_select_key'); - } - - /** - * @return boolean true if key is generated after insert command, false otherwise. - */ - public function getIsAfter() - { - return $this->_type == 'post'; - } -} - +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapUpdate.php b/framework/Data/SqlMap/Configuration/TSqlMapUpdate.php new file mode 100644 index 00000000..853e7e2a --- /dev/null +++ b/framework/Data/SqlMap/Configuration/TSqlMapUpdate.php @@ -0,0 +1,22 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Configuration + */ + +/** + * TSqlMapUpdate class corresponds to element. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Configuration + * @since 3.1 + */ +class TSqlMapUpdate extends TSqlMapStatement +{ +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapXmlConfigBuilder.php b/framework/Data/SqlMap/Configuration/TSqlMapXmlConfigBuilder.php new file mode 100644 index 00000000..47982fca --- /dev/null +++ b/framework/Data/SqlMap/Configuration/TSqlMapXmlConfigBuilder.php @@ -0,0 +1,111 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Configuration + */ + +Prado::using('System.Data.SqlMap.Configuration.TSqlMapStatement'); + +/** + * TSqlMapXmlConfig class file. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Configuration + */ +abstract class TSqlMapXmlConfigBuilder +{ + /** + * Create an instance of an object give by the attribute named 'class' in the + * node and set the properties on the object given by attribute names and values. + * @param SimpleXmlNode property node + * @return Object new instance of class with class name given by 'class' attribute value. + */ + protected function createObjectFromNode($node) + { + if(isset($node['class'])) + { + $obj = Prado::createComponent((string)$node['class']); + $this->setObjectPropFromNode($obj,$node,array('class')); + return $obj; + } + throw new TSqlMapConfigurationException( + 'sqlmap_node_class_undef', $node, $this->getConfigFile()); + } + + /** + * For each attributes (excluding attribute named in $except) set the + * property of the $obj given by the name of the attribute with the value + * of the attribute. + * @param Object object instance + * @param SimpleXmlNode property node + * @param array exception property name + */ + protected function setObjectPropFromNode($obj,$node,$except=array()) + { + foreach($node->attributes() as $name=>$value) + { + if(!in_array($name,$except)) + { + if($obj->canSetProperty($name)) + $obj->{$name} = (string)$value; + else + throw new TSqlMapConfigurationException( + 'sqlmap_invalid_property', $name, get_class($obj), + $node, $this->getConfigFile()); + } + } + } + + /** + * Gets the filename relative to the basefile. + * @param string base filename + * @param string relative filename + * @return string absolute filename. + */ + protected function getAbsoluteFilePath($basefile,$resource) + { + $basedir = dirname($basefile); + $file = realpath($basedir.DIRECTORY_SEPARATOR.$resource); + if(!is_string($file) || !is_file($file)) + $file = realpath($resource); + if(is_string($file) && is_file($file)) + return $file; + else + throw new TSqlMapConfigurationException( + 'sqlmap_unable_to_find_resource', $resource); + } + + /** + * Load document using simple xml. + * @param string filename. + * @return SimpleXmlElement xml document. + */ + protected function loadXmlDocument($filename,TSqlMapXmlConfiguration $config) + { + if( strpos($filename, '${') !== false) + $filename = $config->replaceProperties($filename); + + if(!is_file($filename)) + throw new TSqlMapConfigurationException( + 'sqlmap_unable_to_find_config', $filename); + return simplexml_load_string($config->replaceProperties(file_get_contents($filename))); + } + + /** + * Get element node by ID value (try for attribute name ID as case insensitive). + * @param SimpleXmlDocument $document + * @param string tag name. + * @param string id value. + * @return SimpleXmlElement node if found, null otherwise. + */ + protected function getElementByIdValue($document, $tag, $value) + { + //hack to allow upper case and lower case attribute names. + foreach(array('id','ID','Id', 'iD') as $id) + { + $xpath = "//{$tag}[@{$id}='{$value} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSqlMapXmlConfiguration.php b/framework/Data/SqlMap/Configuration/TSqlMapXmlConfiguration.php deleted file mode 100644 index db178f70..00000000 --- a/framework/Data/SqlMap/Configuration/TSqlMapXmlConfiguration.php +++ /dev/null @@ -1,801 +0,0 @@ - - * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005-2014 PradoSoft - * @license http://www.pradosoft.com/license/ - * @package System.Data.SqlMap.Configuration - */ - -Prado::using('System.Data.SqlMap.Configuration.TSqlMapStatement'); - -/** - * TSqlMapXmlConfig class file. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Configuration - */ -abstract class TSqlMapXmlConfigBuilder -{ - /** - * Create an instance of an object give by the attribute named 'class' in the - * node and set the properties on the object given by attribute names and values. - * @param SimpleXmlNode property node - * @return Object new instance of class with class name given by 'class' attribute value. - */ - protected function createObjectFromNode($node) - { - if(isset($node['class'])) - { - $obj = Prado::createComponent((string)$node['class']); - $this->setObjectPropFromNode($obj,$node,array('class')); - return $obj; - } - throw new TSqlMapConfigurationException( - 'sqlmap_node_class_undef', $node, $this->getConfigFile()); - } - - /** - * For each attributes (excluding attribute named in $except) set the - * property of the $obj given by the name of the attribute with the value - * of the attribute. - * @param Object object instance - * @param SimpleXmlNode property node - * @param array exception property name - */ - protected function setObjectPropFromNode($obj,$node,$except=array()) - { - foreach($node->attributes() as $name=>$value) - { - if(!in_array($name,$except)) - { - if($obj->canSetProperty($name)) - $obj->{$name} = (string)$value; - else - throw new TSqlMapConfigurationException( - 'sqlmap_invalid_property', $name, get_class($obj), - $node, $this->getConfigFile()); - } - } - } - - /** - * Gets the filename relative to the basefile. - * @param string base filename - * @param string relative filename - * @return string absolute filename. - */ - protected function getAbsoluteFilePath($basefile,$resource) - { - $basedir = dirname($basefile); - $file = realpath($basedir.DIRECTORY_SEPARATOR.$resource); - if(!is_string($file) || !is_file($file)) - $file = realpath($resource); - if(is_string($file) && is_file($file)) - return $file; - else - throw new TSqlMapConfigurationException( - 'sqlmap_unable_to_find_resource', $resource); - } - - /** - * Load document using simple xml. - * @param string filename. - * @return SimpleXmlElement xml document. - */ - protected function loadXmlDocument($filename,TSqlMapXmlConfiguration $config) - { - if( strpos($filename, '${') !== false) - $filename = $config->replaceProperties($filename); - - if(!is_file($filename)) - throw new TSqlMapConfigurationException( - 'sqlmap_unable_to_find_config', $filename); - return simplexml_load_string($config->replaceProperties(file_get_contents($filename))); - } - - /** - * Get element node by ID value (try for attribute name ID as case insensitive). - * @param SimpleXmlDocument $document - * @param string tag name. - * @param string id value. - * @return SimpleXmlElement node if found, null otherwise. - */ - protected function getElementByIdValue($document, $tag, $value) - { - //hack to allow upper case and lower case attribute names. - foreach(array('id','ID','Id', 'iD') as $id) - { - $xpath = "//{$tag}[@{$id}='{$value}']"; - foreach($document->xpath($xpath) as $node) - return $node; - } - } - - /** - * @return string configuration file. - */ - protected abstract function getConfigFile(); -} - -/** - * TSqlMapXmlConfig class. - * - * Configures the TSqlMapManager using xml configuration file. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Configuration - * @since 3.1 - */ -class TSqlMapXmlConfiguration extends TSqlMapXmlConfigBuilder -{ - /** - * @var TSqlMapManager manager - */ - private $_manager; - /** - * @var string configuration file. - */ - private $_configFile; - /** - * @var array global properties. - */ - private $_properties=array(); - - /** - * @param TSqlMapManager manager instance. - */ - public function __construct($manager) - { - $this->_manager=$manager; - } - - public function getManager() - { - return $this->_manager; - } - - protected function getConfigFile() - { - return $this->_configFile; - } - - /** - * Configure the TSqlMapManager using the given xml file. - * @param string SqlMap configuration xml file. - */ - public function configure($filename=null) - { - $this->_configFile=$filename; - $document = $this->loadXmlDocument($filename,$this); - - foreach($document->xpath('//property') as $property) - $this->loadGlobalProperty($property); - - foreach($document->xpath('//typeHandler') as $handler) - $this->loadTypeHandler($handler); - - foreach($document->xpath('//connection[last()]') as $conn) - $this->loadDatabaseConnection($conn); - - //try to load configuration in the current config file. - $mapping = new TSqlMapXmlMappingConfiguration($this); - $mapping->configure($filename); - - foreach($document->xpath('//sqlMap') as $sqlmap) - $this->loadSqlMappingFiles($sqlmap); - - $this->resolveResultMapping(); - $this->attachCacheModels(); - } - - /** - * Load global replacement property. - * @param SimpleXmlElement property node. - */ - protected function loadGlobalProperty($node) - { - $this->_properties[(string)$node['name']] = (string)$node['value']; - } - - /** - * Load the type handler configurations. - * @param SimpleXmlElement type handler node - */ - protected function loadTypeHandler($node) - { - $handler = $this->createObjectFromNode($node); - $this->_manager->getTypeHandlers()->registerTypeHandler($handler); - } - - /** - * Load the database connection tag. - * @param SimpleXmlElement connection node. - */ - protected function loadDatabaseConnection($node) - { - $conn = $this->createObjectFromNode($node); - $this->_manager->setDbConnection($conn); - } - - /** - * Load SqlMap mapping configuration. - * @param unknown_type $node - */ - protected function loadSqlMappingFiles($node) - { - if(strlen($resource = (string)$node['resource']) > 0) - { - if( strpos($resource, '${') !== false) - $resource = $this->replaceProperties($resource); - - $mapping = new TSqlMapXmlMappingConfiguration($this); - $filename = $this->getAbsoluteFilePath($this->_configFile, $resource); - $mapping->configure($filename); - } - } - - /** - * Resolve nest result mappings. - */ - protected function resolveResultMapping() - { - $maps = $this->_manager->getResultMaps(); - foreach($maps as $entry) - { - foreach($entry->getColumns() as $item) - { - $resultMap = $item->getResultMapping(); - if(strlen($resultMap) > 0) - { - if($maps->contains($resultMap)) - $item->setNestedResultMap($maps[$resultMap]); - else - throw new TSqlMapConfigurationException( - 'sqlmap_unable_to_find_result_mapping', - $resultMap, $this->_configFile, $entry->getID()); - } - } - if($entry->getDiscriminator()!==null) - $entry->getDiscriminator()->initialize($this->_manager); - } - } - - /** - * Set the cache for each statement having a cache model property. - */ - protected function attachCacheModels() - { - foreach($this->_manager->getMappedStatements() as $mappedStatement) - { - if(strlen($model = $mappedStatement->getStatement()->getCacheModel()) > 0) - { - $cache = $this->_manager->getCacheModel($model); - $mappedStatement->getStatement()->setCache($cache); - } - } - } - - /** - * Replace the place holders ${name} in text with properties the - * corresponding global property value. - * @param string original string. - * @return string string with global property replacement. - */ - public function replaceProperties($string) - { - foreach($this->_properties as $find => $replace) - $string = str_replace('${'.$find.'}', $replace, $string); - return $string; - } -} - -/** - * Loads the statements, result maps, parameters maps from xml configuration. - * - * description - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Configuration - * @since 3.1 - */ -class TSqlMapXmlMappingConfiguration extends TSqlMapXmlConfigBuilder -{ - private $_xmlConfig; - private $_configFile; - private $_manager; - - private $_document; - - private $_FlushOnExecuteStatements=array(); - - /** - * Regular expressions for escaping simple/inline parameter symbols - */ - const SIMPLE_MARK='$'; - const INLINE_SYMBOL='#'; - const ESCAPED_SIMPLE_MARK_REGEXP='/\$\$/'; - const ESCAPED_INLINE_SYMBOL_REGEXP='/\#\#/'; - const SIMPLE_PLACEHOLDER='`!!`'; - const INLINE_PLACEHOLDER='`!!!`'; - - /** - * @param TSqlMapXmlConfiguration parent xml configuration. - */ - public function __construct(TSqlMapXmlConfiguration $xmlConfig) - { - $this->_xmlConfig=$xmlConfig; - $this->_manager=$xmlConfig->getManager(); - } - - protected function getConfigFile() - { - return $this->_configFile; - } - - /** - * Configure an XML mapping. - * @param string xml mapping filename. - */ - public function configure($filename) - { - $this->_configFile=$filename; - $document = $this->loadXmlDocument($filename,$this->_xmlConfig); - $this->_document=$document; - - static $bCacheDependencies; - if($bCacheDependencies === null) - $bCacheDependencies = Prado::getApplication()->getMode() !== TApplicationMode::Performance; - - if($bCacheDependencies) - $this->_manager->getCacheDependencies() - ->getDependencies() - ->add(new TFileCacheDependency($filename)); - - foreach($document->xpath('//resultMap') as $node) - $this->loadResultMap($node); - - foreach($document->xpath('//parameterMap') as $node) - $this->loadParameterMap($node); - - foreach($document->xpath('//statement') as $node) - $this->loadStatementTag($node); - - foreach($document->xpath('//select') as $node) - $this->loadSelectTag($node); - - foreach($document->xpath('//insert') as $node) - $this->loadInsertTag($node); - - foreach($document->xpath('//update') as $node) - $this->loadUpdateTag($node); - - foreach($document->xpath('//delete') as $node) - $this->loadDeleteTag($node); - - foreach($document->xpath('//procedure') as $node) - $this->loadProcedureTag($node); - - foreach($document->xpath('//cacheModel') as $node) - $this->loadCacheModel($node); - - $this->registerCacheTriggers(); - } - - /** - * Load the result maps. - * @param SimpleXmlElement result map node. - */ - protected function loadResultMap($node) - { - $resultMap = $this->createResultMap($node); - - //find extended result map. - if(strlen($extendMap = $resultMap->getExtends()) > 0) - { - if(!$this->_manager->getResultMaps()->contains($extendMap)) - { - $extendNode=$this->getElementByIdValue($this->_document,'resultMap',$extendMap); - if($extendNode!==null) - $this->loadResultMap($extendNode); - } - - if(!$this->_manager->getResultMaps()->contains($extendMap)) - throw new TSqlMapConfigurationException( - 'sqlmap_unable_to_find_parent_result_map', $node, $this->_configFile, $extendMap); - - $superMap = $this->_manager->getResultMap($extendMap); - $resultMap->getColumns()->mergeWith($superMap->getColumns()); - } - - //add the result map - if(!$this->_manager->getResultMaps()->contains($resultMap->getID())) - $this->_manager->addResultMap($resultMap); - } - - /** - * Create a new result map and its associated result properties, - * disciminiator and sub maps. - * @param SimpleXmlElement result map node - * @return TResultMap SqlMap result mapping. - */ - protected function createResultMap($node) - { - $resultMap = new TResultMap(); - $this->setObjectPropFromNode($resultMap,$node); - - //result nodes - foreach($node->result as $result) - { - $property = new TResultProperty($resultMap); - $this->setObjectPropFromNode($property,$result); - $resultMap->addResultProperty($property); - } - - //create the discriminator - $discriminator = null; - if(isset($node->discriminator)) - { - $discriminator = new TDiscriminator(); - $this->setObjectPropFromNode($discriminator, $node->discriminator); - $discriminator->initMapping($resultMap); - } - - foreach($node->xpath('subMap') as $subMapNode) - { - if($discriminator===null) - throw new TSqlMapConfigurationException( - 'sqlmap_undefined_discriminator', $node, $this->_configFile,$subMapNode); - $subMap = new TSubMap; - $this->setObjectPropFromNode($subMap,$subMapNode); - $discriminator->addSubMap($subMap); - } - - if($discriminator!==null) - $resultMap->setDiscriminator($discriminator); - - return $resultMap; - } - - /** - * Load parameter map from xml. - * - * @param SimpleXmlElement parameter map node. - */ - protected function loadParameterMap($node) - { - $parameterMap = $this->createParameterMap($node); - - if(strlen($extendMap = $parameterMap->getExtends()) > 0) - { - if(!$this->_manager->getParameterMaps()->contains($extendMap)) - { - $extendNode=$this->getElementByIdValue($this->_document,'parameterMap',$extendMap); - if($extendNode!==null) - $this->loadParameterMap($extendNode); - } - - if(!$this->_manager->getParameterMaps()->contains($extendMap)) - throw new TSqlMapConfigurationException( - 'sqlmap_unable_to_find_parent_parameter_map', $node, $this->_configFile,$extendMap); - $superMap = $this->_manager->getParameterMap($extendMap); - $index = 0; - foreach($superMap->getPropertyNames() as $propertyName) - $parameterMap->insertProperty($index++,$superMap->getProperty($propertyName)); - } - $this->_manager->addParameterMap($parameterMap); - } - - /** - * Create a new parameter map from xml node. - * @param SimpleXmlElement parameter map node. - * @return TParameterMap new parameter mapping. - */ - protected function createParameterMap($node) - { - $parameterMap = new TParameterMap(); - $this->setObjectPropFromNode($parameterMap,$node); - foreach($node->parameter as $parameter) - { - $property = new TParameterProperty(); - $this->setObjectPropFromNode($property,$parameter); - $parameterMap->addProperty($property); - } - return $parameterMap; - } - - /** - * Load statement mapping from xml configuration file. - * @param SimpleXmlElement statement node. - */ - protected function loadStatementTag($node) - { - $statement = new TSqlMapStatement(); - $this->setObjectPropFromNode($statement,$node); - $this->processSqlStatement($statement, $node); - $mappedStatement = new TMappedStatement($this->_manager, $statement); - $this->_manager->addMappedStatement($mappedStatement); - } - - /** - * Load extended SQL statements if application. Replaces global properties - * in the sql text. Extracts inline parameter maps. - * @param TSqlMapStatement mapped statement. - * @param SimpleXmlElement statement node. - */ - protected function processSqlStatement($statement, $node) - { - $commandText = (string)$node; - if(strlen($extend = $statement->getExtends()) > 0) - { - $superNode = $this->getElementByIdValue($this->_document,'*',$extend); - if($superNode!==null) - $commandText = (string)$superNode . $commandText; - else - throw new TSqlMapConfigurationException( - 'sqlmap_unable_to_find_parent_sql', $extend, $this->_configFile,$node); - } - //$commandText = $this->_xmlConfig->replaceProperties($commandText); - $statement->initialize($this->_manager); - $this->applyInlineParameterMap($statement, $commandText, $node); - } - - /** - * Extract inline parameter maps. - * @param TSqlMapStatement statement object. - * @param string sql text - * @param SimpleXmlElement statement node. - */ - protected function applyInlineParameterMap($statement, $sqlStatement, $node) - { - $scope['file'] = $this->_configFile; - $scope['node'] = $node; - - $sqlStatement=preg_replace(self::ESCAPED_INLINE_SYMBOL_REGEXP,self::INLINE_PLACEHOLDER,$sqlStatement); - if($statement->parameterMap() === null) - { - // Build a Parametermap with the inline parameters. - // if they exist. Then delete inline infos from sqltext. - $parameterParser = new TInlineParameterMapParser; - $sqlText = $parameterParser->parse($sqlStatement, $scope); - if(count($sqlText['parameters']) > 0) - { - $map = new TParameterMap(); - $map->setID($statement->getID().'-InLineParameterMap'); - $statement->setInlineParameterMap($map); - foreach($sqlText['parameters'] as $property) - $map->addProperty($property); - } - $sqlStatement = $sqlText['sql']; - } - $sqlStatement=preg_replace('/'.self::INLINE_PLACEHOLDER.'/',self::INLINE_SYMBOL,$sqlStatement); - - $this->prepareSql($statement, $sqlStatement, $node); - } - - /** - * Prepare the sql text (may extend to dynamic sql). - * @param TSqlMapStatement mapped statement. - * @param string sql text. - * @param SimpleXmlElement statement node. - * @todo Extend to dynamic sql. - */ - protected function prepareSql($statement,$sqlStatement, $node) - { - $simpleDynamic = new TSimpleDynamicParser; - $sqlStatement=preg_replace(self::ESCAPED_SIMPLE_MARK_REGEXP,self::SIMPLE_PLACEHOLDER,$sqlStatement); - $dynamics = $simpleDynamic->parse($sqlStatement); - if(count($dynamics['parameters']) > 0) - { - $sql = new TSimpleDynamicSql($dynamics['parameters']); - $sqlStatement = $dynamics['sql']; - } - else - $sql = new TStaticSql(); - $sqlStatement=preg_replace('/'.self::SIMPLE_PLACEHOLDER.'/',self::SIMPLE_MARK,$sqlStatement); - $sql->buildPreparedStatement($statement, $sqlStatement); - $statement->setSqlText($sql); - } - - /** - * Load select statement from xml mapping. - * @param SimpleXmlElement select node. - */ - protected function loadSelectTag($node) - { - $select = new TSqlMapSelect; - $this->setObjectPropFromNode($select,$node); - $this->processSqlStatement($select,$node); - $mappedStatement = new TMappedStatement($this->_manager, $select); - if(strlen($select->getCacheModel()) > 0) - $mappedStatement = new TCachingStatement($mappedStatement); - - $this->_manager->addMappedStatement($mappedStatement); - } - - /** - * Load insert statement from xml mapping. - * @param SimpleXmlElement insert node. - */ - protected function loadInsertTag($node) - { - $insert = $this->createInsertStatement($node); - $this->processSqlStatement($insert, $node); - $mappedStatement = new TInsertMappedStatement($this->_manager, $insert); - $this->_manager->addMappedStatement($mappedStatement); - } - - /** - * Create new insert statement from xml node. - * @param SimpleXmlElement insert node. - * @return TSqlMapInsert insert statement. - */ - protected function createInsertStatement($node) - { - $insert = new TSqlMapInsert; - $this->setObjectPropFromNode($insert,$node); - if(isset($node->selectKey)) - $this->loadSelectKeyTag($insert,$node->selectKey); - return $insert; - } - - /** - * Load the selectKey statement from xml mapping. - * @param SimpleXmlElement selectkey node - */ - protected function loadSelectKeyTag($insert, $node) - { - $selectKey = new TSqlMapSelectKey; - $this->setObjectPropFromNode($selectKey,$node); - $selectKey->setID($insert->getID()); - $selectKey->setID($insert->getID().'.SelectKey'); - $this->processSqlStatement($selectKey,$node); - $insert->setSelectKey($selectKey); - $mappedStatement = new TMappedStatement($this->_manager, $selectKey); - $this->_manager->addMappedStatement($mappedStatement); - } - - /** - * Load update statement from xml mapping. - * @param SimpleXmlElement update node. - */ - protected function loadUpdateTag($node) - { - $update = new TSqlMapUpdate; - $this->setObjectPropFromNode($update,$node); - $this->processSqlStatement($update, $node); - $mappedStatement = new TUpdateMappedStatement($this->_manager, $update); - $this->_manager->addMappedStatement($mappedStatement); - } - - /** - * Load delete statement from xml mapping. - * @param SimpleXmlElement delete node. - */ - protected function loadDeleteTag($node) - { - $delete = new TSqlMapDelete; - $this->setObjectPropFromNode($delete,$node); - $this->processSqlStatement($delete, $node); - $mappedStatement = new TDeleteMappedStatement($this->_manager, $delete); - $this->_manager->addMappedStatement($mappedStatement); - } - - /** - * Load procedure statement from xml mapping. - * @todo Implement loading procedure - * @param SimpleXmlElement procedure node - */ - protected function loadProcedureTag($node) - { - //var_dump('todo: add load procedure'); - } - - /** - * Load cache models from xml mapping. - * @param SimpleXmlElement cache node. - */ - protected function loadCacheModel($node) - { - $cacheModel = new TSqlMapCacheModel; - $properties = array('id','implementation'); - foreach($node->attributes() as $name=>$value) - { - if(in_array(strtolower($name), $properties)) - $cacheModel->{'set'.$name}((string)$value); - } - $cache = Prado::createComponent($cacheModel->getImplementationClass(), $cacheModel); - $this->setObjectPropFromNode($cache,$node,$properties); - - foreach($node->xpath('property') as $propertyNode) - { - $name = $propertyNode->attributes()->name; - if($name===null || $name==='') continue; - - $value = $propertyNode->attributes()->value; - if($value===null || $value==='') continue; - - if( !TPropertyAccess::has($cache, $name) ) continue; - - TPropertyAccess::set($cache, $name, $value); - } - - $this->loadFlushInterval($cacheModel,$node); - - $cacheModel->initialize($cache); - $this->_manager->addCacheModel($cacheModel); - foreach($node->xpath('flushOnExecute') as $flush) - $this->loadFlushOnCache($cacheModel,$node,$flush); - } - - /** - * Load the flush interval - * @param TSqlMapCacheModel cache model - * @param SimpleXmlElement cache node - */ - protected function loadFlushInterval($cacheModel, $node) - { - $flushInterval = $node->xpath('flushInterval'); - if($flushInterval === null || count($flushInterval) === 0) return; - $duration = 0; - foreach($flushInterval[0]->attributes() as $name=>$value) - { - switch(strToLower($name)) - { - case 'seconds': - $duration += (integer)$value; - break; - case 'minutes': - $duration += 60 * (integer)$value; - break; - case 'hours': - $duration += 3600 * (integer)$value; - break; - case 'days': - $duration += 86400 * (integer)$value; - break; - case 'duration': - $duration = (integer)$value; - break 2; // switch, foreach - } - } - $cacheModel->setFlushInterval($duration); - } - - /** - * Load the flush on cache properties. - * @param TSqlMapCacheModel cache model - * @param SimpleXmlElement parent node. - * @param SimpleXmlElement flush node. - */ - protected function loadFlushOnCache($cacheModel,$parent,$node) - { - $id = $cacheModel->getID(); - if(!isset($this->_FlushOnExecuteStatements[$id])) - $this->_FlushOnExecuteStatements[$id] = array(); - foreach($node->attributes() as $name=>$value) - { - if(strtolower($name)==='statement') - $this->_FlushOnExecuteStatements[$id][] = (string)$value; - } - } - - /** - * Attach CacheModel to statement and register trigger statements for cache models - */ - protected function registerCacheTriggers() - { - foreach($this->_FlushOnExecuteStatements as $cacheID => $statementIDs) - { - $cacheModel = $this->_manager->getCacheModel($cacheID); - foreach($statementIDs as $statementID) - { - $statement = $this->_manager->getMappedStatement($statementID); - $cacheModel->registerTriggerStatement($statement); - } - } - } -} - diff --git a/framework/Data/SqlMap/Configuration/TSqlMapXmlMappingConfiguration.php b/framework/Data/SqlMap/Configuration/TSqlMapXmlMappingConfiguration.php new file mode 100644 index 00000000..d677f6cd --- /dev/null +++ b/framework/Data/SqlMap/Configuration/TSqlMapXmlMappingConfiguration.php @@ -0,0 +1,517 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Configuration + */ + +/** + * Loads the statements, result maps, parameters maps from xml configuration. + * + * description + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Configuration + * @since 3.1 + */ +class TSqlMapXmlMappingConfiguration extends TSqlMapXmlConfigBuilder +{ + private $_xmlConfig; + private $_configFile; + private $_manager; + + private $_document; + + private $_FlushOnExecuteStatements=array(); + + /** + * Regular expressions for escaping simple/inline parameter symbols + */ + const SIMPLE_MARK='$'; + const INLINE_SYMBOL='#'; + const ESCAPED_SIMPLE_MARK_REGEXP='/\$\$/'; + const ESCAPED_INLINE_SYMBOL_REGEXP='/\#\#/'; + const SIMPLE_PLACEHOLDER='`!!`'; + const INLINE_PLACEHOLDER='`!!!`'; + + /** + * @param TSqlMapXmlConfiguration parent xml configuration. + */ + public function __construct(TSqlMapXmlConfiguration $xmlConfig) + { + $this->_xmlConfig=$xmlConfig; + $this->_manager=$xmlConfig->getManager(); + } + + protected function getConfigFile() + { + return $this->_configFile; + } + + /** + * Configure an XML mapping. + * @param string xml mapping filename. + */ + public function configure($filename) + { + $this->_configFile=$filename; + $document = $this->loadXmlDocument($filename,$this->_xmlConfig); + $this->_document=$document; + + static $bCacheDependencies; + if($bCacheDependencies === null) + $bCacheDependencies = Prado::getApplication()->getMode() !== TApplicationMode::Performance; + + if($bCacheDependencies) + $this->_manager->getCacheDependencies() + ->getDependencies() + ->add(new TFileCacheDependency($filename)); + + foreach($document->xpath('//resultMap') as $node) + $this->loadResultMap($node); + + foreach($document->xpath('//parameterMap') as $node) + $this->loadParameterMap($node); + + foreach($document->xpath('//statement') as $node) + $this->loadStatementTag($node); + + foreach($document->xpath('//select') as $node) + $this->loadSelectTag($node); + + foreach($document->xpath('//insert') as $node) + $this->loadInsertTag($node); + + foreach($document->xpath('//update') as $node) + $this->loadUpdateTag($node); + + foreach($document->xpath('//delete') as $node) + $this->loadDeleteTag($node); + + foreach($document->xpath('//procedure') as $node) + $this->loadProcedureTag($node); + + foreach($document->xpath('//cacheModel') as $node) + $this->loadCacheModel($node); + + $this->registerCacheTriggers(); + } + + /** + * Load the result maps. + * @param SimpleXmlElement result map node. + */ + protected function loadResultMap($node) + { + $resultMap = $this->createResultMap($node); + + //find extended result map. + if(strlen($extendMap = $resultMap->getExtends()) > 0) + { + if(!$this->_manager->getResultMaps()->contains($extendMap)) + { + $extendNode=$this->getElementByIdValue($this->_document,'resultMap',$extendMap); + if($extendNode!==null) + $this->loadResultMap($extendNode); + } + + if(!$this->_manager->getResultMaps()->contains($extendMap)) + throw new TSqlMapConfigurationException( + 'sqlmap_unable_to_find_parent_result_map', $node, $this->_configFile, $extendMap); + + $superMap = $this->_manager->getResultMap($extendMap); + $resultMap->getColumns()->mergeWith($superMap->getColumns()); + } + + //add the result map + if(!$this->_manager->getResultMaps()->contains($resultMap->getID())) + $this->_manager->addResultMap($resultMap); + } + + /** + * Create a new result map and its associated result properties, + * disciminiator and sub maps. + * @param SimpleXmlElement result map node + * @return TResultMap SqlMap result mapping. + */ + protected function createResultMap($node) + { + $resultMap = new TResultMap(); + $this->setObjectPropFromNode($resultMap,$node); + + //result nodes + foreach($node->result as $result) + { + $property = new TResultProperty($resultMap); + $this->setObjectPropFromNode($property,$result); + $resultMap->addResultProperty($property); + } + + //create the discriminator + $discriminator = null; + if(isset($node->discriminator)) + { + $discriminator = new TDiscriminator(); + $this->setObjectPropFromNode($discriminator, $node->discriminator); + $discriminator->initMapping($resultMap); + } + + foreach($node->xpath('subMap') as $subMapNode) + { + if($discriminator===null) + throw new TSqlMapConfigurationException( + 'sqlmap_undefined_discriminator', $node, $this->_configFile,$subMapNode); + $subMap = new TSubMap; + $this->setObjectPropFromNode($subMap,$subMapNode); + $discriminator->addSubMap($subMap); + } + + if($discriminator!==null) + $resultMap->setDiscriminator($discriminator); + + return $resultMap; + } + + /** + * Load parameter map from xml. + * + * @param SimpleXmlElement parameter map node. + */ + protected function loadParameterMap($node) + { + $parameterMap = $this->createParameterMap($node); + + if(strlen($extendMap = $parameterMap->getExtends()) > 0) + { + if(!$this->_manager->getParameterMaps()->contains($extendMap)) + { + $extendNode=$this->getElementByIdValue($this->_document,'parameterMap',$extendMap); + if($extendNode!==null) + $this->loadParameterMap($extendNode); + } + + if(!$this->_manager->getParameterMaps()->contains($extendMap)) + throw new TSqlMapConfigurationException( + 'sqlmap_unable_to_find_parent_parameter_map', $node, $this->_configFile,$extendMap); + $superMap = $this->_manager->getParameterMap($extendMap); + $index = 0; + foreach($superMap->getPropertyNames() as $propertyName) + $parameterMap->insertProperty($index++,$superMap->getProperty($propertyName)); + } + $this->_manager->addParameterMap($parameterMap); + } + + /** + * Create a new parameter map from xml node. + * @param SimpleXmlElement parameter map node. + * @return TParameterMap new parameter mapping. + */ + protected function createParameterMap($node) + { + $parameterMap = new TParameterMap(); + $this->setObjectPropFromNode($parameterMap,$node); + foreach($node->parameter as $parameter) + { + $property = new TParameterProperty(); + $this->setObjectPropFromNode($property,$parameter); + $parameterMap->addProperty($property); + } + return $parameterMap; + } + + /** + * Load statement mapping from xml configuration file. + * @param SimpleXmlElement statement node. + */ + protected function loadStatementTag($node) + { + $statement = new TSqlMapStatement(); + $this->setObjectPropFromNode($statement,$node); + $this->processSqlStatement($statement, $node); + $mappedStatement = new TMappedStatement($this->_manager, $statement); + $this->_manager->addMappedStatement($mappedStatement); + } + + /** + * Load extended SQL statements if application. Replaces global properties + * in the sql text. Extracts inline parameter maps. + * @param TSqlMapStatement mapped statement. + * @param SimpleXmlElement statement node. + */ + protected function processSqlStatement($statement, $node) + { + $commandText = (string)$node; + if(strlen($extend = $statement->getExtends()) > 0) + { + $superNode = $this->getElementByIdValue($this->_document,'*',$extend); + if($superNode!==null) + $commandText = (string)$superNode . $commandText; + else + throw new TSqlMapConfigurationException( + 'sqlmap_unable_to_find_parent_sql', $extend, $this->_configFile,$node); + } + //$commandText = $this->_xmlConfig->replaceProperties($commandText); + $statement->initialize($this->_manager); + $this->applyInlineParameterMap($statement, $commandText, $node); + } + + /** + * Extract inline parameter maps. + * @param TSqlMapStatement statement object. + * @param string sql text + * @param SimpleXmlElement statement node. + */ + protected function applyInlineParameterMap($statement, $sqlStatement, $node) + { + $scope['file'] = $this->_configFile; + $scope['node'] = $node; + + $sqlStatement=preg_replace(self::ESCAPED_INLINE_SYMBOL_REGEXP,self::INLINE_PLACEHOLDER,$sqlStatement); + if($statement->parameterMap() === null) + { + // Build a Parametermap with the inline parameters. + // if they exist. Then delete inline infos from sqltext. + $parameterParser = new TInlineParameterMapParser; + $sqlText = $parameterParser->parse($sqlStatement, $scope); + if(count($sqlText['parameters']) > 0) + { + $map = new TParameterMap(); + $map->setID($statement->getID().'-InLineParameterMap'); + $statement->setInlineParameterMap($map); + foreach($sqlText['parameters'] as $property) + $map->addProperty($property); + } + $sqlStatement = $sqlText['sql']; + } + $sqlStatement=preg_replace('/'.self::INLINE_PLACEHOLDER.'/',self::INLINE_SYMBOL,$sqlStatement); + + $this->prepareSql($statement, $sqlStatement, $node); + } + + /** + * Prepare the sql text (may extend to dynamic sql). + * @param TSqlMapStatement mapped statement. + * @param string sql text. + * @param SimpleXmlElement statement node. + * @todo Extend to dynamic sql. + */ + protected function prepareSql($statement,$sqlStatement, $node) + { + $simpleDynamic = new TSimpleDynamicParser; + $sqlStatement=preg_replace(self::ESCAPED_SIMPLE_MARK_REGEXP,self::SIMPLE_PLACEHOLDER,$sqlStatement); + $dynamics = $simpleDynamic->parse($sqlStatement); + if(count($dynamics['parameters']) > 0) + { + $sql = new TSimpleDynamicSql($dynamics['parameters']); + $sqlStatement = $dynamics['sql']; + } + else + $sql = new TStaticSql(); + $sqlStatement=preg_replace('/'.self::SIMPLE_PLACEHOLDER.'/',self::SIMPLE_MARK,$sqlStatement); + $sql->buildPreparedStatement($statement, $sqlStatement); + $statement->setSqlText($sql); + } + + /** + * Load select statement from xml mapping. + * @param SimpleXmlElement select node. + */ + protected function loadSelectTag($node) + { + $select = new TSqlMapSelect; + $this->setObjectPropFromNode($select,$node); + $this->processSqlStatement($select,$node); + $mappedStatement = new TMappedStatement($this->_manager, $select); + if(strlen($select->getCacheModel()) > 0) + $mappedStatement = new TCachingStatement($mappedStatement); + + $this->_manager->addMappedStatement($mappedStatement); + } + + /** + * Load insert statement from xml mapping. + * @param SimpleXmlElement insert node. + */ + protected function loadInsertTag($node) + { + $insert = $this->createInsertStatement($node); + $this->processSqlStatement($insert, $node); + $mappedStatement = new TInsertMappedStatement($this->_manager, $insert); + $this->_manager->addMappedStatement($mappedStatement); + } + + /** + * Create new insert statement from xml node. + * @param SimpleXmlElement insert node. + * @return TSqlMapInsert insert statement. + */ + protected function createInsertStatement($node) + { + $insert = new TSqlMapInsert; + $this->setObjectPropFromNode($insert,$node); + if(isset($node->selectKey)) + $this->loadSelectKeyTag($insert,$node->selectKey); + return $insert; + } + + /** + * Load the selectKey statement from xml mapping. + * @param SimpleXmlElement selectkey node + */ + protected function loadSelectKeyTag($insert, $node) + { + $selectKey = new TSqlMapSelectKey; + $this->setObjectPropFromNode($selectKey,$node); + $selectKey->setID($insert->getID()); + $selectKey->setID($insert->getID().'.SelectKey'); + $this->processSqlStatement($selectKey,$node); + $insert->setSelectKey($selectKey); + $mappedStatement = new TMappedStatement($this->_manager, $selectKey); + $this->_manager->addMappedStatement($mappedStatement); + } + + /** + * Load update statement from xml mapping. + * @param SimpleXmlElement update node. + */ + protected function loadUpdateTag($node) + { + $update = new TSqlMapUpdate; + $this->setObjectPropFromNode($update,$node); + $this->processSqlStatement($update, $node); + $mappedStatement = new TUpdateMappedStatement($this->_manager, $update); + $this->_manager->addMappedStatement($mappedStatement); + } + + /** + * Load delete statement from xml mapping. + * @param SimpleXmlElement delete node. + */ + protected function loadDeleteTag($node) + { + $delete = new TSqlMapDelete; + $this->setObjectPropFromNode($delete,$node); + $this->processSqlStatement($delete, $node); + $mappedStatement = new TDeleteMappedStatement($this->_manager, $delete); + $this->_manager->addMappedStatement($mappedStatement); + } + + /** + * Load procedure statement from xml mapping. + * @todo Implement loading procedure + * @param SimpleXmlElement procedure node + */ + protected function loadProcedureTag($node) + { + //var_dump('todo: add load procedure'); + } + + /** + * Load cache models from xml mapping. + * @param SimpleXmlElement cache node. + */ + protected function loadCacheModel($node) + { + $cacheModel = new TSqlMapCacheModel; + $properties = array('id','implementation'); + foreach($node->attributes() as $name=>$value) + { + if(in_array(strtolower($name), $properties)) + $cacheModel->{'set'.$name}((string)$value); + } + $cache = Prado::createComponent($cacheModel->getImplementationClass(), $cacheModel); + $this->setObjectPropFromNode($cache,$node,$properties); + + foreach($node->xpath('property') as $propertyNode) + { + $name = $propertyNode->attributes()->name; + if($name===null || $name==='') continue; + + $value = $propertyNode->attributes()->value; + if($value===null || $value==='') continue; + + if( !TPropertyAccess::has($cache, $name) ) continue; + + TPropertyAccess::set($cache, $name, $value); + } + + $this->loadFlushInterval($cacheModel,$node); + + $cacheModel->initialize($cache); + $this->_manager->addCacheModel($cacheModel); + foreach($node->xpath('flushOnExecute') as $flush) + $this->loadFlushOnCache($cacheModel,$node,$flush); + } + + /** + * Load the flush interval + * @param TSqlMapCacheModel cache model + * @param SimpleXmlElement cache node + */ + protected function loadFlushInterval($cacheModel, $node) + { + $flushInterval = $node->xpath('flushInterval'); + if($flushInterval === null || count($flushInterval) === 0) return; + $duration = 0; + foreach($flushInterval[0]->attributes() as $name=>$value) + { + switch(strToLower($name)) + { + case 'seconds': + $duration += (integer)$value; + break; + case 'minutes': + $duration += 60 * (integer)$value; + break; + case 'hours': + $duration += 3600 * (integer)$value; + break; + case 'days': + $duration += 86400 * (integer)$value; + break; + case 'duration': + $duration = (integer)$value; + break 2; // switch, foreach + } + } + $cacheModel->setFlushInterval($duration); + } + + /** + * Load the flush on cache properties. + * @param TSqlMapCacheModel cache model + * @param SimpleXmlElement parent node. + * @param SimpleXmlElement flush node. + */ + protected function loadFlushOnCache($cacheModel,$parent,$node) + { + $id = $cacheModel->getID(); + if(!isset($this->_FlushOnExecuteStatements[$id])) + $this->_FlushOnExecuteStatements[$id] = array(); + foreach($node->attributes() as $name=>$value) + { + if(strtolower($name)==='statement') + $this->_FlushOnExecuteStatements[$id][] = (string)$value; + } + } + + /** + * Attach CacheModel to statement and register trigger statements for cache models + */ + protected function registerCacheTriggers() + { + foreach($this->_FlushOnExecuteStatements as $cacheID => $statementIDs) + { + $cacheModel = $this->_manager->getCacheModel($cacheID); + foreach($statementIDs as $statementID) + { + $statement = $this->_manager->getMappedStatement($statementID); + $cacheModel->registerTriggerStatement($statement); + } + } + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Configuration/TSubMap.php b/framework/Data/SqlMap/Configuration/TSubMap.php new file mode 100644 index 00000000..0a2e7614 --- /dev/null +++ b/framework/Data/SqlMap/Configuration/TSubMap.php @@ -0,0 +1,61 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Configuration + */ + +/** + * TSubMap class defines a submapping value and the corresponding + * + * The {@link Value setValue()} property is used for comparison with the + * discriminator column value. When the {@link Value setValue()} matches + * that of the discriminator column value, the corresponding {@link ResultMapping setResultMapping} + * is used inplace of the current result map. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Configuration + * @since 3.1 + */ +class TSubMap extends TComponent +{ + private $_value; + private $_resultMapping; + + /** + * @return string value for comparison with discriminator column value. + */ + public function getValue() + { + return $this->_value; + } + + /** + * @param string value for comparison with discriminator column value. + */ + public function setValue($value) + { + $this->_value = $value; + } + + /** + * The result map to use when the Value matches the discriminator column value. + * @return string ID of a result map + */ + public function getResultMapping() + { + return $this->_resultMapping; + } + + /** + * @param string ID of a result map + */ + public function setResultMapping($value) + { + $this->_resultMapping = $value; + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TInvalidPropertyException.php b/framework/Data/SqlMap/DataMapper/TInvalidPropertyException.php new file mode 100644 index 00000000..2defaf0f --- /dev/null +++ b/framework/Data/SqlMap/DataMapper/TInvalidPropertyException.php @@ -0,0 +1,19 @@ + + * @package System.Data.SqlMap + * @since 3.1 + */ + +/** + * TInvalidPropertyException, raised when setting or getting an invalid property. + * + * @author Wei Zhuo + * @package System.Data.SqlMap + * @since 3.1 + */ +class TInvalidPropertyException extends TSqlMapException +{ +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TLazyLoadList.php b/framework/Data/SqlMap/DataMapper/TLazyLoadList.php index d50c1b84..e0601e44 100644 --- a/framework/Data/SqlMap/DataMapper/TLazyLoadList.php +++ b/framework/Data/SqlMap/DataMapper/TLazyLoadList.php @@ -98,44 +98,4 @@ class TLazyLoadList 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 - * @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); - } -} - +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TObjectProxy.php b/framework/Data/SqlMap/DataMapper/TObjectProxy.php new file mode 100644 index 00000000..957c8a8f --- /dev/null +++ b/framework/Data/SqlMap/DataMapper/TObjectProxy.php @@ -0,0 +1,49 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap + */ + +/** + * TObjectProxy sets up a simple object that intercepts method calls to a + * particular object and relays the call to handler object. + * + * @author Wei Zhuo + * @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); + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapApplicationCache.php b/framework/Data/SqlMap/DataMapper/TSqlMapApplicationCache.php new file mode 100644 index 00000000..bb3cb6c0 --- /dev/null +++ b/framework/Data/SqlMap/DataMapper/TSqlMapApplicationCache.php @@ -0,0 +1,139 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap + */ + +/** + * TSqlMapApplicationCache uses the default Prado application cache for + * caching SqlMap results. + * + * @author Wei Zhuo + * @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'); + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapCache.php b/framework/Data/SqlMap/DataMapper/TSqlMapCache.php index aa853b6c..33a270cd 100644 --- a/framework/Data/SqlMap/DataMapper/TSqlMapCache.php +++ b/framework/Data/SqlMap/DataMapper/TSqlMapCache.php @@ -80,211 +80,4 @@ abstract class TSqlMapCache implements ICache { 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 - * @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 - * @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 - * @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'); - } -} - +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapConfigurationException.php b/framework/Data/SqlMap/DataMapper/TSqlMapConfigurationException.php new file mode 100644 index 00000000..8d5556c8 --- /dev/null +++ b/framework/Data/SqlMap/DataMapper/TSqlMapConfigurationException.php @@ -0,0 +1,20 @@ + + * @package System.Data.SqlMap + * @since 3.1 + */ + +/** + * TSqlMapConfigurationException, raised during configuration file parsing. + * + * @author Wei Zhuo + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapConfigurationException extends TSqlMapException +{ + +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapDuplicateException.php b/framework/Data/SqlMap/DataMapper/TSqlMapDuplicateException.php new file mode 100644 index 00000000..008d8f08 --- /dev/null +++ b/framework/Data/SqlMap/DataMapper/TSqlMapDuplicateException.php @@ -0,0 +1,19 @@ + + * @package System.Data.SqlMap + * @since 3.1 + */ + +/** + * TSqlMapDuplicateException, raised when a duplicate mapped statement is found. + * + * @author Wei Zhuo + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapDuplicateException extends TSqlMapException +{ +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapException.php b/framework/Data/SqlMap/DataMapper/TSqlMapException.php index bce03a5c..720ee7ee 100644 --- a/framework/Data/SqlMap/DataMapper/TSqlMapException.php +++ b/framework/Data/SqlMap/DataMapper/TSqlMapException.php @@ -1,5 +1,4 @@ - * @package System.Data.SqlMap - * @since 3.1 - */ -class TSqlMapConfigurationException extends TSqlMapException -{ - -} - -/** - * TSqlMapUndefinedException, raised when mapped statemented are undefined. - * - * @author Wei Zhuo - * @package System.Data.SqlMap - * @since 3.1 - */ -class TSqlMapUndefinedException extends TSqlMapException -{ - -} - -/** - * TSqlMapDuplicateException, raised when a duplicate mapped statement is found. - * - * @author Wei Zhuo - * @package System.Data.SqlMap - * @since 3.1 - */ -class TSqlMapDuplicateException extends TSqlMapException -{ -} - -/** - * TInvalidPropertyException, raised when setting or getting an invalid property. - * - * @author Wei Zhuo - * @package System.Data.SqlMap - * @since 3.1 - */ -class TInvalidPropertyException extends TSqlMapException -{ -} - -class TSqlMapExecutionException extends TSqlMapException -{ -} - +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapExecutionException.php b/framework/Data/SqlMap/DataMapper/TSqlMapExecutionException.php new file mode 100644 index 00000000..90896c35 --- /dev/null +++ b/framework/Data/SqlMap/DataMapper/TSqlMapExecutionException.php @@ -0,0 +1,12 @@ + + * @package System.Data.SqlMap + * @since 3.1 + */ + +class TSqlMapExecutionException extends TSqlMapException +{ +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapFifoCache.php b/framework/Data/SqlMap/DataMapper/TSqlMapFifoCache.php new file mode 100644 index 00000000..3c036a97 --- /dev/null +++ b/framework/Data/SqlMap/DataMapper/TSqlMapFifoCache.php @@ -0,0 +1,46 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap + */ + +/** + * First-in-First-out cache implementation, removes + * object that was first added when the cache is full. + * + * @author Wei Zhuo + * @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); + } + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapLruCache.php b/framework/Data/SqlMap/DataMapper/TSqlMapLruCache.php new file mode 100644 index 00000000..e34cad84 --- /dev/null +++ b/framework/Data/SqlMap/DataMapper/TSqlMapLruCache.php @@ -0,0 +1,51 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap + */ + +/** + * Least recently used cache implementation, removes + * object that was accessed last when the cache is full. + * + * @author Wei Zhuo + * @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); + } + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandler.php b/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandler.php new file mode 100644 index 00000000..a5d3f39c --- /dev/null +++ b/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandler.php @@ -0,0 +1,92 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap + */ + +/** + * 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 + * @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); +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php b/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php index 18b8ef99..f6a22ee1 100644 --- a/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php +++ b/framework/Data/SqlMap/DataMapper/TSqlMapTypeHandlerRegistry.php @@ -103,87 +103,4 @@ class TSqlMapTypeHandlerRegistry 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 - * @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); -} - +} \ No newline at end of file diff --git a/framework/Data/SqlMap/DataMapper/TSqlMapUndefinedException.php b/framework/Data/SqlMap/DataMapper/TSqlMapUndefinedException.php new file mode 100644 index 00000000..44e968ec --- /dev/null +++ b/framework/Data/SqlMap/DataMapper/TSqlMapUndefinedException.php @@ -0,0 +1,20 @@ + + * @package System.Data.SqlMap + * @since 3.1 + */ + +/** + * TSqlMapUndefinedException, raised when mapped statemented are undefined. + * + * @author Wei Zhuo + * @package System.Data.SqlMap + * @since 3.1 + */ +class TSqlMapUndefinedException extends TSqlMapException +{ + +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Statements/TMappedStatement.php b/framework/Data/SqlMap/Statements/TMappedStatement.php index 4d3b6355..517127d3 100644 --- a/framework/Data/SqlMap/Statements/TMappedStatement.php +++ b/framework/Data/SqlMap/Statements/TMappedStatement.php @@ -931,306 +931,4 @@ class TMappedStatement extends TComponent implements IMappedStatement if (!$this->_IsRowDataFound) $exprops[] = "\0$cn\0_IsRowDataFound"; return array_diff(parent::__sleep(),$exprops); } -} - -/** - * TPostSelectBinding class. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Statements - * @since 3.1 - */ -class TPostSelectBinding -{ - private $_statement=null; - private $_property=null; - private $_resultObject=null; - private $_keys=null; - private $_method=TMappedStatement::QUERY_FOR_LIST; - - public function getStatement(){ return $this->_statement; } - public function setStatement($value){ $this->_statement = $value; } - - public function getResultProperty(){ return $this->_property; } - public function setResultProperty($value){ $this->_property = $value; } - - public function getResultObject(){ return $this->_resultObject; } - public function setResultObject($value){ $this->_resultObject = $value; } - - public function getKeys(){ return $this->_keys; } - public function setKeys($value){ $this->_keys = $value; } - - public function getMethod(){ return $this->_method; } - public function setMethod($value){ $this->_method = $value; } -} - -/** - * TSQLMapObjectCollectionTree class. - * - * Maps object collection graphs as trees. Nodes in the collection can - * be {@link add} using parent relationships. The object collections can be - * build using the {@link collect} method. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Statements - * @since 3.1 - */ -class TSqlMapObjectCollectionTree extends TComponent -{ - /** - * @var array object graph as tree - */ - private $_tree = array(); - /** - * @var array tree node values - */ - private $_entries = array(); - /** - * @var array resulting object collection - */ - private $_list = array(); - - /** - * @return boolean true if the graph is empty - */ - public function isEmpty() - { - return count($this->_entries) == 0; - } - - /** - * Add a new node to the object tree graph. - * @param string parent node id - * @param string new node id - * @param mixed node value - */ - public function add($parent, $node, $object='') - { - if(isset($this->_entries[$parent]) && ($this->_entries[$parent]!==null) - && isset($this->_entries[$node]) && ($this->_entries[$node]!==null)) - { - $this->_entries[$node] = $object; - return; - } - $this->_entries[$node] = $object; - if(empty($parent)) - { - if(isset($this->_entries[$node])) - return; - $this->_tree[$node] = array(); - } - $found = $this->addNode($this->_tree, $parent, $node); - if(!$found && !empty($parent)) - { - $this->_tree[$parent] = array(); - if(!isset($this->_entries[$parent]) || $object !== '') - $this->_entries[$parent] = $object; - $this->addNode($this->_tree, $parent, $node); - } - } - - /** - * Find the parent node and add the new node as its child. - * @param array list of nodes to check - * @param string parent node id - * @param string new node id - * @return boolean true if parent node is found. - */ - protected function addNode(&$childs, $parent, $node) - { - $found = false; - reset($childs); - for($i = 0, $k = count($childs); $i < $k; $i++) - { - $key = key($childs); - next($childs); - if($key == $parent) - { - $found = true; - $childs[$key][$node] = array(); - } - else - { - $found = $found || $this->addNode($childs[$key], $parent, $node); - } - } - return $found; - } - - /** - * @return array object collection - */ - public function collect() - { - while(count($this->_tree) > 0) - $this->collectChildren(null, $this->_tree); - return $this->getCollection(); - } - - /** - * @param array list of nodes to check - * @return boolean true if all nodes are leaf nodes, false otherwise - */ - protected function hasChildren(&$nodes) - { - $hasChildren = false; - foreach($nodes as $node) - if(count($node) != 0) - return true; - return $hasChildren; - } - - /** - * Visit all the child nodes and collect them by removing. - * @param string parent node id - * @param array list of child nodes. - */ - protected function collectChildren($parent, &$nodes) - { - $noChildren = !$this->hasChildren($nodes); - $childs = array(); - for(reset($nodes); $key = key($nodes);) - { - next($nodes); - if($noChildren) - { - $childs[] = $key; - unset($nodes[$key]); - } - else - $this->collectChildren($key, $nodes[$key]); - } - if(count($childs) > 0) - $this->onChildNodesVisited($parent, $childs); - } - - /** - * Set the object properties for all the child nodes visited. - * @param string parent node id - * @param array list of child nodes visited. - */ - protected function onChildNodesVisited($parent, $nodes) - { - if(empty($parent) || empty($this->_entries[$parent])) - return; - - $parentObject = $this->_entries[$parent]['object']; - $property = $this->_entries[$nodes[0]]['property']; - - $list = TPropertyAccess::get($parentObject, $property); - - foreach($nodes as $node) - { - if($list instanceof TList) - $parentObject->{$property}[] = $this->_entries[$node]['object']; - else if(is_array($list)) - $list[] = $this->_entries[$node]['object']; - else - throw new TSqlMapExecutionException( - 'sqlmap_property_must_be_list'); - } - - if(is_array($list)) - TPropertyAccess::set($parentObject, $property, $list); - - if($this->_entries[$parent]['property'] === null) - $this->_list[] = $parentObject; - } - - /** - * @return array object collection. - */ - protected function getCollection() - { - return $this->_list; - } - - public function __sleep() - { - $exprops = array(); $cn = __CLASS__; - if (!count($this->_tree)) $exprops[] = "\0$cn\0_tree"; - if (!count($this->_entries)) $exprops[] = "\0$cn\0_entries"; - if (!count($this->_list)) $exprops[] = "\0$cn\0_list"; - return array_diff(parent::__sleep(),$exprops); - } -} - -/** - * TResultSetListItemParameter class - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Statements - * @since 3.1 - */ -class TResultSetListItemParameter extends TComponent -{ - private $_resultObject; - private $_parameterObject; - private $_list; - - public function __construct($result, $parameter, &$list) - { - $this->_resultObject = $result; - $this->_parameterObject = $parameter; - $this->_list = &$list; - } - - public function getResult() - { - return $this->_resultObject; - } - - public function getParameter() - { - return $this->_parameterObject; - } - - public function &getList() - { - return $this->_list; - } -} - -/** - * TResultSetMapItemParameter class. - * - * @author Wei Zhuo - * @package System.Data.SqlMap.Statements - * @since 3.1 - */ -class TResultSetMapItemParameter extends TComponent -{ - private $_key; - private $_value; - private $_parameterObject; - private $_map; - - public function __construct($key, $value, $parameter, &$map) - { - $this->_key = $key; - $this->_value = $value; - $this->_parameterObject = $parameter; - $this->_map = &$map; - } - - public function getKey() - { - return $this->_key; - } - - public function getValue() - { - return $this->_value; - } - - public function getParameter() - { - return $this->_parameterObject; - } - - public function &getMap() - { - return $this->_map; - } -} - +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Statements/TPostSelectBinding.php b/framework/Data/SqlMap/Statements/TPostSelectBinding.php new file mode 100644 index 00000000..8180e73d --- /dev/null +++ b/framework/Data/SqlMap/Statements/TPostSelectBinding.php @@ -0,0 +1,41 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Statements + */ + +/** + * TPostSelectBinding class. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Statements + * @since 3.1 + */ +class TPostSelectBinding +{ + private $_statement=null; + private $_property=null; + private $_resultObject=null; + private $_keys=null; + private $_method=TMappedStatement::QUERY_FOR_LIST; + + public function getStatement(){ return $this->_statement; } + public function setStatement($value){ $this->_statement = $value; } + + public function getResultProperty(){ return $this->_property; } + public function setResultProperty($value){ $this->_property = $value; } + + public function getResultObject(){ return $this->_resultObject; } + public function setResultObject($value){ $this->_resultObject = $value; } + + public function getKeys(){ return $this->_keys; } + public function setKeys($value){ $this->_keys = $value; } + + public function getMethod(){ return $this->_method; } + public function setMethod($value){ $this->_method = $value; } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Statements/TResultSetListItemParameter.php b/framework/Data/SqlMap/Statements/TResultSetListItemParameter.php new file mode 100644 index 00000000..e330a5ae --- /dev/null +++ b/framework/Data/SqlMap/Statements/TResultSetListItemParameter.php @@ -0,0 +1,46 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Statements + */ + +/** + * TResultSetListItemParameter class + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Statements + * @since 3.1 + */ +class TResultSetListItemParameter extends TComponent +{ + private $_resultObject; + private $_parameterObject; + private $_list; + + public function __construct($result, $parameter, &$list) + { + $this->_resultObject = $result; + $this->_parameterObject = $parameter; + $this->_list = &$list; + } + + public function getResult() + { + return $this->_resultObject; + } + + public function getParameter() + { + return $this->_parameterObject; + } + + public function &getList() + { + return $this->_list; + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Statements/TResultSetMapItemParameter.php b/framework/Data/SqlMap/Statements/TResultSetMapItemParameter.php new file mode 100644 index 00000000..363edb55 --- /dev/null +++ b/framework/Data/SqlMap/Statements/TResultSetMapItemParameter.php @@ -0,0 +1,53 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Statements + */ + +/** + * TResultSetMapItemParameter class. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Statements + * @since 3.1 + */ +class TResultSetMapItemParameter extends TComponent +{ + private $_key; + private $_value; + private $_parameterObject; + private $_map; + + public function __construct($key, $value, $parameter, &$map) + { + $this->_key = $key; + $this->_value = $value; + $this->_parameterObject = $parameter; + $this->_map = &$map; + } + + public function getKey() + { + return $this->_key; + } + + public function getValue() + { + return $this->_value; + } + + public function getParameter() + { + return $this->_parameterObject; + } + + public function &getMap() + { + return $this->_map; + } +} \ No newline at end of file diff --git a/framework/Data/SqlMap/Statements/TSqlMapObjectCollectionTree.php b/framework/Data/SqlMap/Statements/TSqlMapObjectCollectionTree.php new file mode 100644 index 00000000..91fe3882 --- /dev/null +++ b/framework/Data/SqlMap/Statements/TSqlMapObjectCollectionTree.php @@ -0,0 +1,201 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data.SqlMap.Statements + */ + +/** + * TSQLMapObjectCollectionTree class. + * + * Maps object collection graphs as trees. Nodes in the collection can + * be {@link add} using parent relationships. The object collections can be + * build using the {@link collect} method. + * + * @author Wei Zhuo + * @package System.Data.SqlMap.Statements + * @since 3.1 + */ +class TSqlMapObjectCollectionTree extends TComponent +{ + /** + * @var array object graph as tree + */ + private $_tree = array(); + /** + * @var array tree node values + */ + private $_entries = array(); + /** + * @var array resulting object collection + */ + private $_list = array(); + + /** + * @return boolean true if the graph is empty + */ + public function isEmpty() + { + return count($this->_entries) == 0; + } + + /** + * Add a new node to the object tree graph. + * @param string parent node id + * @param string new node id + * @param mixed node value + */ + public function add($parent, $node, $object='') + { + if(isset($this->_entries[$parent]) && ($this->_entries[$parent]!==null) + && isset($this->_entries[$node]) && ($this->_entries[$node]!==null)) + { + $this->_entries[$node] = $object; + return; + } + $this->_entries[$node] = $object; + if(empty($parent)) + { + if(isset($this->_entries[$node])) + return; + $this->_tree[$node] = array(); + } + $found = $this->addNode($this->_tree, $parent, $node); + if(!$found && !empty($parent)) + { + $this->_tree[$parent] = array(); + if(!isset($this->_entries[$parent]) || $object !== '') + $this->_entries[$parent] = $object; + $this->addNode($this->_tree, $parent, $node); + } + } + + /** + * Find the parent node and add the new node as its child. + * @param array list of nodes to check + * @param string parent node id + * @param string new node id + * @return boolean true if parent node is found. + */ + protected function addNode(&$childs, $parent, $node) + { + $found = false; + reset($childs); + for($i = 0, $k = count($childs); $i < $k; $i++) + { + $key = key($childs); + next($childs); + if($key == $parent) + { + $found = true; + $childs[$key][$node] = array(); + } + else + { + $found = $found || $this->addNode($childs[$key], $parent, $node); + } + } + return $found; + } + + /** + * @return array object collection + */ + public function collect() + { + while(count($this->_tree) > 0) + $this->collectChildren(null, $this->_tree); + return $this->getCollection(); + } + + /** + * @param array list of nodes to check + * @return boolean true if all nodes are leaf nodes, false otherwise + */ + protected function hasChildren(&$nodes) + { + $hasChildren = false; + foreach($nodes as $node) + if(count($node) != 0) + return true; + return $hasChildren; + } + + /** + * Visit all the child nodes and collect them by removing. + * @param string parent node id + * @param array list of child nodes. + */ + protected function collectChildren($parent, &$nodes) + { + $noChildren = !$this->hasChildren($nodes); + $childs = array(); + for(reset($nodes); $key = key($nodes);) + { + next($nodes); + if($noChildren) + { + $childs[] = $key; + unset($nodes[$key]); + } + else + $this->collectChildren($key, $nodes[$key]); + } + if(count($childs) > 0) + $this->onChildNodesVisited($parent, $childs); + } + + /** + * Set the object properties for all the child nodes visited. + * @param string parent node id + * @param array list of child nodes visited. + */ + protected function onChildNodesVisited($parent, $nodes) + { + if(empty($parent) || empty($this->_entries[$parent])) + return; + + $parentObject = $this->_entries[$parent]['object']; + $property = $this->_entries[$nodes[0]]['property']; + + $list = TPropertyAccess::get($parentObject, $property); + + foreach($nodes as $node) + { + if($list instanceof TList) + $parentObject->{$property}[] = $this->_entries[$node]['object']; + else if(is_array($list)) + $list[] = $this->_entries[$node]['object']; + else + throw new TSqlMapExecutionException( + 'sqlmap_property_must_be_list'); + } + + if(is_array($list)) + TPropertyAccess::set($parentObject, $property, $list); + + if($this->_entries[$parent]['property'] === null) + $this->_list[] = $parentObject; + } + + /** + * @return array object collection. + */ + protected function getCollection() + { + return $this->_list; + } + + public function __sleep() + { + $exprops = array(); $cn = __CLASS__; + if (!count($this->_tree)) $exprops[] = "\0$cn\0_tree"; + if (!count($this->_entries)) $exprops[] = "\0$cn\0_entries"; + if (!count($this->_list)) $exprops[] = "\0$cn\0_list"; + return array_diff(parent::__sleep(),$exprops); + } +} \ No newline at end of file diff --git a/framework/Data/TDbColumnCaseMode.php b/framework/Data/TDbColumnCaseMode.php new file mode 100644 index 00000000..553407ed --- /dev/null +++ b/framework/Data/TDbColumnCaseMode.php @@ -0,0 +1,33 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data + */ + +/** + * TDbColumnCaseMode + * + * @author Qiang Xue + * @package System.Data + * @since 3.0 + */ +class TDbColumnCaseMode extends TEnumerable +{ + /** + * Column name cases are kept as is from the database + */ + const Preserved='Preserved'; + /** + * Column names are converted to lower case + */ + const LowerCase='LowerCase'; + /** + * Column names are converted to upper case + */ + const UpperCase='UpperCase'; +} \ No newline at end of file diff --git a/framework/Data/TDbConnection.php b/framework/Data/TDbConnection.php index b475c059..6ff0174e 100644 --- a/framework/Data/TDbConnection.php +++ b/framework/Data/TDbConnection.php @@ -630,51 +630,4 @@ class TDbConnection extends TComponent else $this->_attributes[$name]=$value; } -} - -/** - * TDbColumnCaseMode - * - * @author Qiang Xue - * @package System.Data - * @since 3.0 - */ -class TDbColumnCaseMode extends TEnumerable -{ - /** - * Column name cases are kept as is from the database - */ - const Preserved='Preserved'; - /** - * Column names are converted to lower case - */ - const LowerCase='LowerCase'; - /** - * Column names are converted to upper case - */ - const UpperCase='UpperCase'; -} - -/** - * TDbNullConversionMode - * - * @author Qiang Xue - * @package System.Data - * @since 3.0 - */ -class TDbNullConversionMode extends TEnumerable -{ - /** - * No conversion is performed for null and empty values. - */ - const Preserved='Preserved'; - /** - * NULL is converted to empty string - */ - const NullToEmptyString='NullToEmptyString'; - /** - * Empty string is converted to NULL - */ - const EmptyStringToNull='EmptyStringToNull'; -} - +} \ No newline at end of file diff --git a/framework/Data/TDbNullConversionMode.php b/framework/Data/TDbNullConversionMode.php new file mode 100644 index 00000000..1171861a --- /dev/null +++ b/framework/Data/TDbNullConversionMode.php @@ -0,0 +1,33 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Data + */ + +/** + * TDbNullConversionMode + * + * @author Qiang Xue + * @package System.Data + * @since 3.0 + */ +class TDbNullConversionMode extends TEnumerable +{ + /** + * No conversion is performed for null and empty values. + */ + const Preserved='Preserved'; + /** + * NULL is converted to empty string + */ + const NullToEmptyString='NullToEmptyString'; + /** + * Empty string is converted to NULL + */ + const EmptyStringToNull='EmptyStringToNull'; +} \ No newline at end of file -- cgit v1.2.3