From 3d3f8d3832921f99daf8ce1953304763c2e76c62 Mon Sep 17 00:00:00 2001 From: wei <> Date: Fri, 14 Apr 2006 06:22:09 +0000 Subject: Importing SQLMap + sample + docs. --- .../SQLMap/Configuration/TConfigDeserialize.php | 171 ++++++++ .../SQLMap/Configuration/TDiscriminator.php | 86 ++++ .../SQLMap/Configuration/TDomSqlMapBuilder.php | 481 +++++++++++++++++++++ .../Configuration/TInlineParameterMapParser.php | 50 +++ .../SQLMap/Configuration/TParameterMap.php | 95 ++++ .../SQLMap/Configuration/TParameterProperty.php | 68 +++ .../DataAccess/SQLMap/Configuration/TResultMap.php | 63 +++ .../SQLMap/Configuration/TResultProperty.php | 164 +++++++ .../SQLMap/Configuration/TSqlMapCacheModel.php | 131 ++++++ .../SQLMap/Configuration/TSqlMapDelete.php | 7 + .../SQLMap/Configuration/TSqlMapInsert.php | 15 + .../SQLMap/Configuration/TSqlMapSelect.php | 11 + .../SQLMap/Configuration/TSqlMapSelectKey.php | 26 ++ .../SQLMap/Configuration/TSqlMapStatement.php | 109 +++++ .../SQLMap/Configuration/TSqlMapUpdate.php | 11 + 15 files changed, 1488 insertions(+) create mode 100644 framework/DataAccess/SQLMap/Configuration/TConfigDeserialize.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TDiscriminator.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TDomSqlMapBuilder.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TInlineParameterMapParser.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TParameterMap.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TParameterProperty.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TResultMap.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TResultProperty.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TSqlMapCacheModel.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TSqlMapDelete.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TSqlMapInsert.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TSqlMapSelect.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TSqlMapSelectKey.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TSqlMapStatement.php create mode 100644 framework/DataAccess/SQLMap/Configuration/TSqlMapUpdate.php (limited to 'framework/DataAccess/SQLMap/Configuration') diff --git a/framework/DataAccess/SQLMap/Configuration/TConfigDeserialize.php b/framework/DataAccess/SQLMap/Configuration/TConfigDeserialize.php new file mode 100644 index 00000000..250133b9 --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TConfigDeserialize.php @@ -0,0 +1,171 @@ +_properties = $properties; + } + + public function loadConfiguration($object, $node, $file) + { + foreach($node->attributes() as $k=>$v) + { + if($object->canSetProperty($k)) + $object->{'set'.$k}($this->replaceProperties((string)($v))); + else + throw new TUndefinedAttributeException($k,$node,$object,$file); + } + } + + public function replaceProperties($string) + { + foreach($this->_properties as $find => $replace) + $string = str_replace('${'.$find.'}', $replace, $string); + return $string; + } + + public function parameterMap($node, $sqlMap, $file) + { + $parameterMap = new TParameterMap; + $this->loadConfiguration($parameterMap, $node, $file); + foreach($node->parameter as $parameter) + { + $property = $this->parameterProperty($parameter, $sqlMap, $file); + $property->initialize($sqlMap); + $parameterMap->addParameterProperty($property); + } + return $parameterMap; + } + + public function parameterProperty($node, $sqlMap, $file) + { + $property = new TParameterProperty; + $this->loadConfiguration($property, $node, $file); + return $property; + } + + public function select($node, $sqlMap, $file) + { + $select = new TSqlMapSelect; + $this->loadConfiguration($select, $node, $file); + $select->initialize($sqlMap); + return $select; + } + + public function update($node, $sqlMap, $file) + { + $update = new TSqlMapUpdate; + $this->loadConfiguration($update, $node, $file); + $update->initialize($sqlMap); + return $update; + } + + public function delete($node, $sqlMap, $file) + { + $delete = new TSqlMapDelete; + $this->loadConfiguration($delete, $node, $file); + $delete->initialize($sqlMap); + return $delete; + } + + + public function insert($node, $sqlMap, $file) + { + $insert = new TSqlMapInsert; + $this->loadConfiguration($insert, $node, $file); + if(isset($node->selectKey)) + { + $selectKey = new TSqlMapSelectKey; + $this->loadConfiguration($selectKey, $node->selectKey, $file); + $type = $selectKey->getType(); + $selectKey->setType(strtolower($type) == 'post' ? 'post' : 'pre'); + $insert->setSelectKey($selectKey); + } + if(isset($node->generate)) + { + var_dump("add generate"); + } + + $insert->initialize($sqlMap); + return $insert; + } + + public function statement($node, $sqlMap, $file) + { + $statement = new TSqlMapStatement; + $this->loadConfiguration($statement, $node, $file); + $statement->initialize($sqlMap); + return $statement; + } + + public function resultMap($node, $sqlMap, $file) + { + $resultMap = new TResultMap; + $this->loadConfiguration($resultMap, $node, $file); + foreach($node->result as $result) + { + $property = $this->resultProperty($result, $sqlMap, $file); + $property->initialize($sqlMap, $resultMap); + $resultMap->addResultProperty($property); + } + $discriminator = null; + if(isset($node->discriminator)) + { + $discriminator = new TDiscriminator; + $this->loadConfiguration($discriminator, $node->discriminator, $file); + $discriminator->initMapping($sqlMap, $resultMap); + } + foreach($node->subMap as $subMapNode) + { + if(is_null($discriminator)) + throw new TSqlMapConfigurationException( + 'sqlmap_undefined_discriminator', $resultMap->getID(), $file); + $subMap = new TSubMap; + $this->loadConfiguration($subMap, $subMapNode, $file); + $discriminator->add($subMap); + } + if(!is_null($discriminator)) + $resultMap->setDiscriminator($discriminator); + + return $resultMap; + } + + public function resultProperty($node, $sqlMap, $file) + { + $resultProperty = new TResultProperty; + $this->loadConfiguration($resultProperty, $node, $file); + return $resultProperty; + } + + public function cacheModel($node, $sqlMap, $file) + { + $cacheModel = new TSqlMapCacheModel; + $this->loadConfiguration($cacheModel, $node, $file); + if(isset($node->flushInterval)) + { + $interval = $node->flushInterval; + $span = 0; //span in seconds + if(isset($interval['hours'])) + $span += intval($interval['hours'])*60*60; + if(isset($interval['minutes'])) + $span += intval($interval['minutes'])*60; + if(isset($interval['seconds'])) + $span += intval($interval['seconds']); + if($span > 0) + $cacheModel->setFlushInterval($span); + } + if(isset($node->property)) + { + foreach($node->property as $property) + $cacheModel->addProperty((string)$property['name'], + (string)$property['value']); + } + return $cacheModel; + } +} + + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TDiscriminator.php b/framework/DataAccess/SQLMap/Configuration/TDiscriminator.php new file mode 100644 index 00000000..0ee34e3e --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TDiscriminator.php @@ -0,0 +1,86 @@ +_column; } + public function setColumn($value){ $this->_column = $value; } + + public function getType(){ return $this->_type; } + public function setType($value){ $this->_type = $value; } + + public function getTypeHandler(){ return $this->_typeHandler; } + public function setTypeHandler($value){ $this->_typeHandler = $value; } + + public function getDbType(){ return $this->_dbType; } + public function setDbType($value){ $this->_dbType = $value; } + + public function getColumnIndex(){ return $this->_columnIndex; } + public function setColumnIndex($value){ $this->_columnIndex = $value; } + + public function getNullValue(){ return $this->_nullValue; } + public function setNullValue($value){ $this->_nullValue = $value; } + + public function getMapping(){ return $this->_mapping; } + + public function getResultMaps(){ return $this->_resultMaps; } + public function setResultMaps($value){ $this->_resultMaps = $value; } + + public function add($subMap) + { + $this->_subMaps[] = $subMap; + } + + public function getSubMap($value) + { + if(isset($this->_resultMaps[$value])) + return $this->_resultMaps[$value]; + else + return null; + } + + public function initMapping($sqlMap, $resultMap) + { + $this->_mapping = new TResultProperty; + $this->_mapping->setColumn($this->getColumn()); + $this->_mapping->setColumnIndex($this->getColumnIndex()); + $this->_mapping->setType($this->getType()); + $this->_mapping->setTypeHandler($this->getTypeHandler()); + $this->_mapping->setDbType($this->getDbType()); + $this->_mapping->setNullValue($this->getNullValue()); + $this->_mapping->initialize($sqlMap, $resultMap); + } + + public function initialize($sqlMap) + { + foreach($this->_subMaps as $subMap) + { + $this->_resultMaps[$subMap->getValue()] = + $sqlMap->getResultMap($subMap->getResultMapping()); + } + } +} + + +class TSubMap extends TComponent +{ + private $_value=''; + private $_resultMapping=''; + + public function getValue(){ return $this->_value; } + public function setValue($value){ $this->_value = $value; } + + public function getResultMapping(){ return $this->_resultMapping; } + public function setResultMapping($value){ $this->_resultMapping = $value; } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TDomSqlMapBuilder.php b/framework/DataAccess/SQLMap/Configuration/TDomSqlMapBuilder.php new file mode 100644 index 00000000..d3059dd7 --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TDomSqlMapBuilder.php @@ -0,0 +1,481 @@ +_properties = new TMap; + $this->_deserialize = new TConfigDeserialize($this->_properties); + } + + public function configure($resource=null) + { + if($resource instanceof SimpleXMLElement) + return $this->build($resource); + + if(!is_string($resource)) + $resource = self::DEFAULT_CONFIG_FILE; + + $this->_configFile = $resource; + if(!is_file($resource)) + throw new TSqlMapConfigurationException( + 'sqlmap_unable_to_find_config', $resource); + + return $this->build($this->getConfigAsXmlDocument($resource)); + } + + protected function getConfigAsXmlDocument($file) + { + return simplexml_load_file($file); + } + + public function build(SimpleXMLElement $document) + { + $this->_document = $document; + $this->initialize($document); + return $this->_sqlMapper; + } + + protected function initialize($document) + { + $this->_sqlMapper = new TSqlMapper(new TTypeHandlerFactory); + + if(isset($document->properties)) + { + $this->loadGlobalProperties($document->properties); + } + + if(isset($document->settings) && isset($document->settings->setting)) + $this->configureSettings($document->settings); + + //load database provider + if(isset($document->provider) && isset($document->provider->datasource)) + { + $this->loadProvider($document->provider, + $document->provider->datasource, $document, $this->_configFile); + } + else + { + throw new TSqlMapConfigurationException( + 'sqlmap_unable_to_find_db_config', $this->_configFile); + } + + if(isset($document->sqlMaps) && isset($document->sqlMaps->sqlMap)) + $this->loadSqlMappingFiles($document->sqlMaps); + + if($this->_sqlMapper->getIsCacheModelsEnabled()) + $this->attachCacheModel(); + } + + protected function configureSettings($node) + { + foreach($node->setting as $setting) + { + if(isset($setting['useStatementNamespaces'])) + { + $this->_useNamespaces = + TPropertyValue::ensureBoolean( + (string)$setting['useStatementNamespaces']); + } + + if(isset($setting['cacheModelsEnabled'])) + { + $this->_sqlMapper->setCacheModelsEnabled( + TPropertyValue::ensureBoolean( + (string)$setting['cacheModelsEnabled'])); + } + } + } + + /** + * Attach CacheModel to statement and register trigger statements for + * cache models + */ + protected function attachCacheModel() + { + foreach($this->_sqlMapper->getStatements() as $mappedStatement) + { + if(strlen($model = $mappedStatement->getStatement()->getCacheModel()) > 0) + { + $cache = $this->_sqlMapper->getCache($model); + //var_dump($model); + $mappedStatement->getStatement()->setCache($cache); + } + } + + foreach($this->_FlushOnExecuteStatements as $cacheID => $statementIDs) + { + if(count($statementIDs) > 0) + { + foreach($statementIDs as $statementID) + { + $cacheModel = $this->_sqlMapper->getCache($cacheID); + $statement = $this->_sqlMapper->getMappedStatement($statementID); + $cacheModel->registerTriggerStatement($statement); + } + } + } + } + + protected function loadGlobalProperties($node) + { + if(isset($node['resource'])) + $this->loadPropertyResource($node); + + foreach($node->children() as $child) + { + if(isset($child['resource'])) + $this->loadPropertyResource($child); + $this->_properties[(string)$child['key']] = (string)$child['value']; + } + } + + protected function loadPropertyResource($node) + { + $resource = $this->getResourceFromPath((string)$node['resource']); + $properties = $this->getConfigAsXmlDocument($resource); + $this->loadGlobalProperties($properties); + } + + protected function loadProvider($providerNode, $node, $document, $file) + { + //$id = (string)$node['id']; + $class = (string)$providerNode['class']; + if(strlen($class) > 0 && class_exists($class,false)) + { + $provider = new $class; + $this->_deserialize->loadConfiguration($provider, $node,$file); + $this->_sqlMapper->setDataProvider($provider); + } + else + { + throw new TSqlMapConfigurationException( + 'sqlmap_unable_find_provider_class', $file); + } + //var_dump($node); + } +/* + protected function loadTypeHandlers() + { + } +*/ + protected function loadSqlMappingFiles($sqlmappings) + { + foreach($sqlmappings->sqlMap as $node) + { + $resource = $this->getResourceFromPath((string)$node['resource']); + $sqlmap = $this->getConfigAsXmlDocument($resource); + $this->configureSqlMap($sqlmap,$resource); + } + + $this->resolveResultMapping(); + } + + protected function getResourceFromPath($resource) + { + $basedir = dirname($this->_configFile); + $file = realpath($basedir.'/'.$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); + } + + protected function configureSqlMap($document,$file) + { + // if(isset($document->typeAlias)) + // foreach($document->typeAlias as $node) + // TTypeAliasDeSerializer::Deserialize($node, $this->_sqlMapper); + if(isset($document->resultMap)) + foreach($document->resultMap as $node) + $this->loadResultMap($node,$document,$file); + if(isset($document->parameterMap)) + foreach($document->parameterMap as $node) + $this->loadParameterMap($node, $document, $file); + if(isset($document->statement)) + foreach($document->statement as $node) + $this->loadStatementTag($node, $document,$file); + if(isset($document->select)) + foreach($document->select as $node) + $this->loadSelectTag($node, $document, $file); + if(isset($document->insert)) + foreach($document->insert as $node) + $this->loadInsertTag($node, $document, $file); + if(isset($document->update)) + foreach($document->update as $node) + $this->loadUpdateTag($node, $document, $file); + if(isset($document->delete)) + foreach($document->delete as $node) + $this->loadDeleteTag($node, $document, $file); +/* if(isset($document->procedure)) + foreach($document->procedure as $node) + $this->loadProcedureTag($node); +*/ + if($this->_sqlMapper->getIsCacheModelsEnabled()) + { + if(isset($document->cacheModel)) + foreach($document->cacheModel as $node) + $this->loadCacheModel($node, $document, $file); + } + } + + protected function loadCacheModel($node, $document, $file) + { + $cacheModel = $this->_deserialize->cacheModel($node, $this->_sqlMapper, $file); + if(isset($node->flushOnExecute)) + { + foreach($node->flushOnExecute as $flush) + { + $id = $cacheModel->getID(); + if(!isset($this->_FlushOnExecuteStatements[$id])) + $this->_FlushOnExecuteStatements[$id] = array(); + + $this->_FlushOnExecuteStatements[$id][] = (string)$flush['statement']; + } + } + //var_dump($cacheModel); + $cacheModel->initialize($this->_sqlMapper); + $this->_sqlMapper->addCache($cacheModel); + } + + protected function loadUpdateTag($node, $document, $file) + { + $update = $this->_deserialize->insert($node, $this->_sqlMapper, $file); + if(!is_null($update->getGenerate())) + { + var_dump('generate update'); + } + else + { + $this->processSqlStatement($update, $document, $node, $file); + } + $mappedStatement = new TUpdateMappedStatement($this->_sqlMapper, $update); + $this->_sqlMapper->addMappedStatement($mappedStatement); + } + + protected function loadDeleteTag($node, $document, $file) + { + $delete = $this->_deserialize->delete($node, $this->_sqlMapper, $file); + if(!is_null($delete->getGenerate())) + { + var_dump('generate delete'); + } + else + { + $this->processSqlStatement($delete, $document, $node, $file); + } + $mappedStatement = new TDeleteMappedStatement($this->_sqlMapper, $delete); + $this->_sqlMapper->addMappedStatement($mappedStatement); + } + + + protected function loadParameterMap($node, $document, $file) + { + $id = (string)$node['id']; + if($this->_sqlMapper->getParameterMaps()->contains($id)) + return; + $parameterMap = $this->_deserialize->parameterMap($node, $this->_sqlMapper, $file); + $extendMap = $parameterMap->getExtends(); + if(strlen($extendMap) > 0) + { + if($this->_sqlMapper->getParameterMaps()->contains($extendMap) == false) + { + $nodes = $document->xpath("//parameterMap[@id='{$extendMap}']"); + if(isset($nodes[0])) + $this->loadParameterMap($nodes[0],$document,$file); + else + throw new TSqlMapConfigurationException( + 'sqlmap_unable_to_find_parent_parameter_map', $extendMap, $file); + } + $superMap = $this->_sqlMapper->getParameterMap($extendMap); + $index = 0; + foreach($superMap->getPropertyNames() as $propertyName) + { + $parameterMap->insertParameterProperty($index++, + $superMap->getProperty($propertyName)); + } + } + $this->_sqlMapper->addParameterMap($parameterMap); + } + + protected function loadInsertTag($node, $document, $file) + { + $insert = $this->_deserialize->insert($node, $this->_sqlMapper, $file); + if(!is_null($insert->getGenerate())) + { + var_dump("generate insert"); + } + else + { + $this->processSqlStatement($insert, $document, $node, $file); + } + + $mappedStatement = new TInsertMappedStatement($this->_sqlMapper, $insert); + $this->_sqlMapper->addMappedStatement($mappedStatement); + if(!is_null($insert->getSelectKey())) + { + $selectKey = $insert->getSelectKey(); + $selectKey->setID($insert->getID()); + $selectKey->initialize($this->_sqlMapper); + $selectKey->setID($insert->getID().'.SelectKey'); + $this->processSqlStatement($selectKey, + $document, $node->selectKey, $file); + $mappedStatement = new TMappedStatement($this->_sqlMapper, $selectKey); + $this->_sqlMapper->addMappedStatement($mappedStatement); + } + } + + protected function processSqlStatement($statement, $document, $node, $file) + { + $commandText = (string)$node; + if(strlen($extend = $statement->getExtends()) > 0) + { + $superNodes = $document->xpath("//*[@id='{$extend}']"); + if(isset($superNodes[0])) + $commandText = (string)$superNodes[0] . $commandText; + else + throw TSqlMapConfigurationException( + 'sqlmap_unable_to_find_parent_sql', $extend, $file); + } + + //$sql = new TStaticSql(); + //$sql->buildPreparedStatement($statement, (string)$node); + $commandText = $this->_deserialize->replaceProperties($commandText); + $this->applyInlineParameterMap($statement, $commandText, $node, $file); + //$statement->setSql($sql); + } + + protected function applyInlineParameterMap($statement, $sqlStatement, $node, $file) + { + if($statement->parameterMap() == null) + { + $scope['statement'] = $statement->getID(); + $scope['file'] = $file; + + // Build a Parametermap with the inline parameters. + // if they exist. Then delete inline infos from sqltext. + $parameterParser = new TInlineParameterMapParser; + $sqlText = $parameterParser->parseInlineParameterMap( + $this->_sqlMapper, $statement, $sqlStatement, $scope); + if(count($sqlText['parameters']) > 0) + { + $map = new TParameterMap(); + $map->setID($statement->getID().'-InLineParameterMap'); + $statement->setInlineParameterMap($map); + foreach($sqlText['parameters'] as $property) + $map->addParameterProperty($property); + } + $sqlStatement = $sqlText['sql']; + } + $sql = new TStaticSql(); + $sql->buildPreparedStatement($statement, $sqlStatement); + $statement->setSql($sql); + } + + protected function resolveResultMapping() + { + $maps = $this->_sqlMapper->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(!is_null($entry->getDiscriminator())) + { + $entry->getDiscriminator()->initialize($this->_sqlMapper); + } + } + } + + protected function loadSelectTag($node, $document, $file) + { + $select = $this->_deserialize->select($node, $this->_sqlMapper, $file); + if(!is_null($select->getGenerate())) + { + var_dump("generate select"); + } + else + { + $this->processSqlStatement($select, $document, $node, $file); + /*$sql = new TStaticSql(); + $sql->buildPreparedStatement($select, (string)$node); + $select->setSql($sql);*/ + } + + $mappedStatement = new TMappedStatement($this->_sqlMapper, $select); + if($this->_sqlMapper->getIsCacheModelsEnabled() && + strlen($select->getCacheModel()) > 0) + { + $mappedStatement = new TCachingStatement($mappedStatement); + } + + $this->_sqlMapper->addMappedStatement($mappedStatement); + } + + protected function loadResultMap($node,$document,$file) + { + $resultMap = $this->_deserialize->resultMap($node, $this->_sqlMapper,$file); + $extendMap = $resultMap->getExtends(); + if(strlen($extendMap) > 0) + { + if(!$this->_sqlMapper->getResultMaps()->contains($extendMap)) + { + $nodes = $document->xpath("//resultMap[@id='{$extendMap}']"); + if(isset($nodes[0])) + $this->loadResultMap($nodes[0],$document,$file); + else + throw new TSqlMapConfigurationException( + 'sqlmap_unable_to_find_parent_result_map', $extendMap, $file); + } + $superMap = $this->_sqlMapper->getResultMap($extendMap); + $resultMap->getColumns()->mergeWith($superMap->getColumns()); + } + if(!$this->_sqlMapper->getResultMaps()->contains($resultMap->getID())) + $this->_sqlMapper->addResultMap($resultMap); + } + + + protected function loadStatementTag($node, $document, $file) + { + $statement = $this->_deserialize->statement($node, $this->_sqlMapper, $file); + + /*$sql = new TStaticSql(); + $sql->buildPreparedStatement($statement, (string)$node); + $statement->setSql($sql);*/ + $this->processSqlStatement($statement, $document, $node, $file); + + $mappedStatement = new TMappedStatement($this->_sqlMapper, $statement); + $this->_sqlMapper->addMappedStatement($mappedStatement); + } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TInlineParameterMapParser.php b/framework/DataAccess/SQLMap/Configuration/TInlineParameterMapParser.php new file mode 100644 index 00000000..8b1ecf04 --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TInlineParameterMapParser.php @@ -0,0 +1,50 @@ +getParameterClass() : null; + $matches = array(); + $mappings = array(); + preg_match_all($this->PARAMETER_TOKEN_REGEXP, $sqlText, $matches); + + for($i = 0, $k=count($matches[1]); $i<$k; $i++) + { + $mappings[] = $this->parseMapping($matches[1][$i], + $parameterClass, $sqlMap, $scope); + $sqlText = str_replace($matches[0][$i], '?', $sqlText); + } + return array('sql'=>$sqlText, 'parameters'=>$mappings); + } + + /** + * Parse inline parameter with syntax as + * #propertyName,type=string,dbype=Varchar,nullValue=N/A,handler=string# + */ + protected function parseMapping($token, $parameterClass, $sqlMap, $scope) + { + $mapping = new TParameterProperty; + $properties = explode(',', $token); + $mapping->setProperty(trim(array_shift($properties))); + //var_dump($properties); + foreach($properties as $property) + { + $prop = explode('=',$property); + $name = trim($prop[0]); $value=trim($prop[1]); + if($mapping->canSetProperty($name)) + $mapping->{'set'.$name}($value); + else + throw new TSqlMapUndefinedException( + 'sqlmap_undefined_property_inline_map', + $name, $scope['statement'], $scope['file']); + } + $mapping->initialize($sqlMap); + return $mapping; + } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TParameterMap.php b/framework/DataAccess/SQLMap/Configuration/TParameterMap.php new file mode 100644 index 00000000..8e1c757d --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TParameterMap.php @@ -0,0 +1,95 @@ +_properties = new TList; + $this->_propertyMap = new TMap; + } + + public function getProperties(){ return $this->_properties; } + + public function getID(){ return $this->_ID; } + public function setID($value){ $this->_ID = $value; } + + public function getExtends(){ return $this->_extend; } + public function setExtends($value){ $this->_extend = $value; } + + public function getProperty($index) + { + if(is_string($index)) + return $this->_propertyMap->itemAt($index); + else if(is_int($index)) + return $this->_properties->itemAt($index); + else + throw new TDataMapperException( + 'sqlmap_index_must_be_string_or_int', $index); + } + + public function addParameterProperty(TParameterProperty $property) + { + $this->_propertyMap->add($property->getProperty(), $property); + $this->_properties->add($property); + } + + public function insertParameterProperty($index, TParameterProperty $property) + { + $this->_propertyMap->add($property->getProperty(), $property); + $this->_properties->insertAt($index, $property); + } + + public function getPropertyNames() + { + return $this->_propertyMap->getKeys(); + } + + public function getParameter($mapping, $parameterValue, $statement) + { + $value = $parameterValue; + $typeHandler = $mapping->getTypeHandler(); + try + { + $value = TPropertyAccess::get($parameterValue, $mapping->getProperty()); + } + catch (TInvalidPropertyException $e) + { + throw new TSqlMapExecutionException( + 'sqlmap_unable_to_get_property_for_parameter',$this->getID(), + $mapping->getProperty(), get_class($parameterValue), + $e->getMessage(), $statement->getID()); + } + + if(!is_null($typeHandler)) + { + try + { + $value = $typeHandler->getParameter($value); + } + catch (Exception $e) + { + throw new TSqlMapExecutionException( + 'sqlmap_error_in_parameter_from_handler',$this->getID(), + $value, get_class($typeHandler), $e->getMessage()); + } + } + + if(!is_null($nullValue = $mapping->getNullValue())) + { + if($nullValue === $value) + $value = null; + } + + if(!is_null($type = $mapping->getType())) + $value = TTypeHandlerFactory::convertToType($type, $value); + + return $value; + } +} +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TParameterProperty.php b/framework/DataAccess/SQLMap/Configuration/TParameterProperty.php new file mode 100644 index 00000000..2ff55f73 --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TParameterProperty.php @@ -0,0 +1,68 @@ +_typeHandlerFactory)) return null; + if(!is_null($this->_typeHandler)) + return $this->_typeHandlerFactory->getTypeHandler($this->_typeHandler); + else if(!is_null($this->getType())) + return $this->_typeHandlerFactory->getTypeHandler($this->getType()); + else + return null; + } + public function setTypeHandler($value){ $this->_typeHandler = $value; } + + public function getType(){ return $this->_type; } + public function setType($value){ $this->_type = $value; } + + public function getColumn(){ return $this->_column; } + public function setColumn($value){ $this->_column = $value; } + + public function getDbType(){ return $this->_dbType; } + public function setDbType($value){ $this->_dbType = $value; } + + public function getProperty(){ return $this->_property; } + public function setProperty($value){ $this->_property = $value; } + + public function getNullValue(){ return $this->_nullValue; } + public function setNullValue($value){ $this->_nullValue = $value; } + + public function getSize(){ return $this->_size; } + public function setSize($value){ $this->_size = $value; } + + public function getPrecision(){ return $this->_precision; } + public function setPrecision($value){ $this->_precision = $value; } + + public function getScale(){ return $this->_scale; } + public function setScale($value){ $this->_scale = $value; } + + + public function getDirection(){ return $this->_direction; } + public function setDirection($value){ $this->_direction = $value; } + + public function initialize($sqlMap) + { + $this->_typeHandlerFactory = $sqlMap->getTypeHandlerFactory(); + // $type = !is_null($this->_typeHandler) ? $this->_typeHandler: $this->_type; + // $this->setTypeHandler($sqlMap->getTypeHandlerFactory()->getTypeHandler($type)); + // if(!is_null($type)) + // var_dump($sqlMap->getTypeHandlerFactory()); + } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TResultMap.php b/framework/DataAccess/SQLMap/Configuration/TResultMap.php new file mode 100644 index 00000000..0f09a1ba --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TResultMap.php @@ -0,0 +1,63 @@ +_columns = new TMap; + } + + public function getID(){ return $this->_ID; } + public function setID($value){ $this->_ID = $value; } + + public function getClass(){ return $this->_className; } + public function setClass($value){ $this->_className = $value; } + + public function getColumns(){ return $this->_columns; } + public function setColumns($value){ $this->_columns = $value; } + + public function getExtends(){ return $this->_extendMap; } + public function setExtends($value){ $this->_extendMap = $value; } + + public function getGroupBy(){ return $this->_groupBy; } + public function setGroupBy($value){ $this->_groupBy = $value; } + + public function getDiscriminator(){ return $this->_discriminator; } + public function setDiscriminator($value){ $this->_discriminator = $value; } + + public function addResultProperty(TResultProperty $property) + { + $this->_columns->add($property->getProperty(), $property); + } + + public function createInstanceOfResult() + { + return TTypeHandlerFactory::createInstanceOf($this->getClass()); + } + + public function resolveSubMap($row) + { + $subMap = $this; + if(!is_null($disc = $this->getDiscriminator())) + { + $mapping = $disc->getMapping(); + $dataValue = $mapping->getDatabaseValue($row); + $subMap = $disc->getSubMap((string)$dataValue); + + if(is_null($subMap)) + $subMap = $this; + else if($subMap !== $this) + $subMap = $subMap->resolveSubMap($row); + } + return $subMap; + } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TResultProperty.php b/framework/DataAccess/SQLMap/Configuration/TResultProperty.php new file mode 100644 index 00000000..9cc0df3a --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TResultProperty.php @@ -0,0 +1,164 @@ +_nullValue; } + public function setNullValue($value){ $this->_nullValue = $value; } + + public function getProperty(){ return $this->_propertyName; } + public function setProperty($value){ $this->_propertyName = $value; } + + public function getColumn(){ return $this->_columnName; } + public function setColumn($value){ $this->_columnName = $value; } + + public function getColumnIndex(){ return $this->_columnIndex; } + public function setColumnIndex($value){ $this->_columnIndex = TPropertyValue::ensureInteger($value,-1); } + + public function getResultMapping(){ return $this->_nestedResultMapName; } + public function setResultMapping($value){ $this->_nestedResultMapName = $value; } + + public function getNestedResultMap(){ return $this->_nestedResultMap; } + public function setNestedResultMap($value){ $this->_nestedResultMap = $value; } + + public function getType(){ return $this->_valueType; } + public function setType($value) { $this->_valueType = $value; } + + public function getTypeHandler() + { + if(is_null($this->_typeHandlerFactory)) return null; + if(!is_null($this->_typeHandler)) + return $this->_typeHandlerFactory->getTypeHandler($this->_typeHandler); + else if(!is_null($this->getType())) + return $this->_typeHandlerFactory->getTypeHandler($this->getType()); + else + return null; + } + public function setTypeHandler($value) { $this->_typeHandler = $value; } + + public function getSelect(){ return $this->_select; } + public function setSelect($value){ $this->_select = $value; } + + public function getLazyLoad(){ return $this->_isLazyLoad; } + public function setLazyLoad($value){ $this->_isLazyLoad = TPropertyValue::ensureBoolean($value,false); } + + public function getDbType(){ return $this->_dbType; } + public function setDbType($value){ $this->_dbType = $value; } + + public function initialize($sqlMap, $resultMap=null) + { + $this->_typeHandlerFactory = $sqlMap->getTypeHandlerFactory(); + if(!is_null($resultMap)) + $this->_hostResultMapID = $resultMap->getID(); +// $type = !is_null($this->_typeHandler) ? $this->_typeHandler: $this->_valueType; +// $this->setTypeHandler($sqlMap->getTypeHandlerFactory()->getTypeHandler($type)); + } + + public function getDatabaseValue($row,$forceType=true) + { + $value = null; + if($this->_columnIndex > 0 && isset($row[$this->_columnIndex])) + $value = $this->getTypedValue($row[$this->_columnIndex], $forceType); + else if(isset($row[$this->_columnName])) + $value = $this->getTypedValue($row[$this->_columnName],$forceType); + if(is_null($value) && !is_null($this->_nullValue)) + $value = $this->getTypedValue($this->_nullValue,$forceType); + return $value; + } + + public function getOrdinalValue($row) + { + return $this->getDatabaseValue($row,false); + } + + private function getTypedValue($value, $forceType=true) + { + if(!$forceType) return $value; + if(is_null($this->getTypeHandler())) + { + return TTypeHandlerFactory::convertToType($this->_valueType, $value); + } + else + { + try + { + return $this->getTypeHandler()->getResult($value); + } + catch (Exception $e) + { + throw new TSqlMapExecutionException( + 'sqlmap_error_in_result_property_from_handler',$this->_hostResultMapID, + $value, get_class($this->getTypeHandler()), $e->getMessage()); + } + } + } + + + public function getPropertyType($type=null) + { + if(is_null($type)) + $type = $this->getType(); + if(class_exists($type, false)) //NO force autoloading + { + $class = new ReflectionClass($type); + if($class->isSubclassOf('TList')) + return self::LIST_TYPE; + if($class->inmplementsInterface('ArrayAccess')) + return self::ARRAY_TYPE; + } + if(strtolower($type) == 'array') + return self::ARRAY_TYPE; + return self::OBJECT_TYPE; + } + + public function isListType($target) + { + if(is_null($this->getType())) + { + $prop = TPropertyAccess::get($target,$this->getProperty()); + return $prop instanceof TList; + } + return $this->getPropertyType() == self::LIST_TYPE; + } + + public function isArrayType($target) + { + if(is_null($this->getType())) + { + $prop = TPropertyAccess::get($target,$this->getProperty()); + if(is_object($prop)) + return $prop instanceof ArrayAccess; + return is_array($prop); + } + return $this->getPropertyType() == self::ARRAY_TYPE; + } + + public function isObjectType($target) + { + if(is_null($this->getType())) + { + $prop = TPropertyAccess::get($target,$this->getProperty()); + return is_object($prop); + } + return $this->getPropertyType() == self::OBJECT_TYPE; + } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TSqlMapCacheModel.php b/framework/DataAccess/SQLMap/Configuration/TSqlMapCacheModel.php new file mode 100644 index 00000000..6c945155 --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TSqlMapCacheModel.php @@ -0,0 +1,131 @@ +_id; } + public function setID($value){ $this->_id = $value; } + + public function getImplementation(){ return $this->_implementation; } + public function setImplementation($value){ $this->_implementation = $value; } + + public function getFlushInterval(){ return $this->_flushInterval; } + public function setFlushInterval($value){ $this->_flushInterval = $value; } + + public function initialize($sqlMap) + { + $implementation = $this->getImplementationClass( + $sqlMap->getTypeHandlerFactory()); + $this->_cache = new $implementation; + $this->_cache->configure($this->_properties); + } + + protected function getImplementationClass($typeFactory) + { + switch(strtolower($this->_implementation)) + { + case 'fifo': return 'TSqlMapFifoCache'; + case 'lru' : return 'TSqlMapLruCache'; + } + + if(class_exists($this->_implementation, false)) + $class = $this->_implementation; + else + $class = $typeFactory->getTypeHandler($this->_implementation); + if(!is_null($class)) + return $class; + else + throw new TSqlMapConfigurationException( + 'sqlmap_unable_to_find_implemenation', $this->_implementation); + } + + public function addProperty($name, $value) + { + $this->_properties[strtolower($name)] = $value; + } + + public function registerTriggerStatement($mappedStatement) + { + $mappedStatement->attachEventHandler('OnExecuteQuery', + array($this, 'flushHandler')); + } + + protected function flushHandler($sender, $param) + { + $this->flush(); + } + + public function flush() + { + var_dump("flush!"); + $this->_cache->flush(); + } + + public function get($key) + { + if($key instanceof TSqlMapCacheKey) + $key = $key->getHash(); + + //if flush ? + $value = $this->_cache->get($key); + $this->_requests++; + if(!is_null($value)) + $this->_hits++; + return $value; + } + + public function set($key, $value) + { + if($key instanceof TSqlMapCacheKey) + $key = $key->getHash(); + + if(!is_null($value)) + $this->_cache->set($key, $value); + } + + public function getHitRatio() + { + if($this->_requests != 0) + return $this->_hits / $this->_requests; + else + return 0; + } +} + + +class TSqlMapCacheKey +{ + private $_key; + + public function __construct($object) + { + $this->_key = $this->generateKey(serialize($object)); + } + + protected function generateKey($string) + { + return sprintf('%x',crc32($string)); + } + + public function getHash() + { + return $this->_key; + } + + public function __toString() + { + return $this->getHash(); + } +} + + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TSqlMapDelete.php b/framework/DataAccess/SQLMap/Configuration/TSqlMapDelete.php new file mode 100644 index 00000000..9ca3843a --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TSqlMapDelete.php @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TSqlMapInsert.php b/framework/DataAccess/SQLMap/Configuration/TSqlMapInsert.php new file mode 100644 index 00000000..c8d798c5 --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TSqlMapInsert.php @@ -0,0 +1,15 @@ +_selectKey; } + public function setSelectKey($value){ $this->_selectKey = $value; } + + public function getGenerate(){ return $this->_generate; } + public function setGenerate($value){ $this->_generate = $value; } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TSqlMapSelect.php b/framework/DataAccess/SQLMap/Configuration/TSqlMapSelect.php new file mode 100644 index 00000000..b33bc27e --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TSqlMapSelect.php @@ -0,0 +1,11 @@ +_generate; } + public function setGenerate($value){ $this->_generate = $value; } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TSqlMapSelectKey.php b/framework/DataAccess/SQLMap/Configuration/TSqlMapSelectKey.php new file mode 100644 index 00000000..b73d4ef0 --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TSqlMapSelectKey.php @@ -0,0 +1,26 @@ +_type; } + public function setType($value){ $this->_type = $value; } + + public function getProperty(){ return $this->_property; } + public function setProperty($value){ $this->_property = $value; } + + public function setExtends($value) + { + throw new TSqlMapConfigurationException( + 'sqlmap_can_not_extend_select_key'); + } + + public function getIsAfter() + { + return $this->_type == 'post'; + } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TSqlMapStatement.php b/framework/DataAccess/SQLMap/Configuration/TSqlMapStatement.php new file mode 100644 index 00000000..228b37d6 --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TSqlMapStatement.php @@ -0,0 +1,109 @@ +_ID; } + public function setID($value){ $this->_ID = $value; } + + public function getParameterMap(){ return $this->_parameterMapName; } + public function setParameterMap($value){ $this->_parameterMapName = $value; } + + public function getParameterClass(){ return $this->_parameterClassName; } + public function setParameterClass($value){ $this->_parameterClassName = $value; } + + public function getResultMap(){ return $this->_resultMapName; } + public function setResultMap($value){ $this->_resultMapName = $value; } + + public function getResultClass(){ return $this->_resultClassName; } + public function setResultClass($value){ $this->_resultClassName = $value; } + + public function getCacheModel(){ return $this->_cacheModelName; } + public function setCacheModel($value){ $this->_cacheModelName = $value; } + + public function getCache(){ return $this->_cache; } + public function setCache($value){ $this->_cache = $value; } + + public function getRemapResults(){ return $this->_remapResults; } + public function setRemapResults($value){ $this->_remapResults = TPropertyValue::ensureBoolean($value,false); } + + public function getSQL(){ return $this->_SQL; } + public function setSQL($value){ $this->_SQL = $value; } + + public function getListClass(){ return $this->_listClass; } + public function setListClass($value){ $this->_listClass = $value; } + + public function getExtends(){ return $this->_extendStatement; } + public function setExtends($value){ $this->_extendStatement = $value; } + + public function resultMap(){ return $this->_resultMap; } + public function parameterMap(){ return $this->_parameterMap; } + + public function setInlineParameterMap($map) + { + $this->_parameterMap = $map; + } +// public function parameterClass(){ return $this->_parameterClass; } +// public function resultClass(){ return $this->_resultClass; } + + public function initialize($sqlMap) + { + $this->_typeHandler = $sqlMap->getTypeHandlerFactory(); + if(strlen($this->_resultMapName) > 0) + $this->_resultMap = $sqlMap->getResultMap($this->_resultMapName); + if(strlen($this->_parameterMapName) > 0) + $this->_parameterMap = $sqlMap->getParameterMap($this->_parameterMapName); + } + + + public function createInstanceOfListClass() + { + if(strlen($type = $this->getListClass()) > 0) + return $this->createInstanceOf($type); + return array(); //new TList; + } + + protected function createInstanceOf($type) + { + $handler = $this->_typeHandler->getTypeHandler($type); + + try + { + if(!is_null($handler)) + return $handler->createNewInstance(); + else + return TTypeHandlerFactory::createInstanceOf($type); + } + catch (TDataMapperException $e) + { + throw new TSqlMapExecutionException( + 'sqlmap_unable_to_create_new_instance', + $type, get_class($handler), $this->getID()); + } + + } + + public function createInstanceOfResultClass() + { + if(strlen($type= $this->getResultClass()) > 0) + return $this->createInstanceOf($type); + } +} + +?> \ No newline at end of file diff --git a/framework/DataAccess/SQLMap/Configuration/TSqlMapUpdate.php b/framework/DataAccess/SQLMap/Configuration/TSqlMapUpdate.php new file mode 100644 index 00000000..26d00e39 --- /dev/null +++ b/framework/DataAccess/SQLMap/Configuration/TSqlMapUpdate.php @@ -0,0 +1,11 @@ +_generate; } + public function setGenerate($value){ $this->_generate = $value; } +} + +?> \ No newline at end of file -- cgit v1.2.3