summaryrefslogtreecommitdiff
path: root/framework/DataAccess/SQLMap
diff options
context:
space:
mode:
authorwei <>2006-07-23 11:11:21 +0000
committerwei <>2006-07-23 11:11:21 +0000
commitcbb7a11179b9c1c46e35f04d07d6386a44e400b2 (patch)
treedacfc2a5fdbcce3973e228eaca8002ff799e45d1 /framework/DataAccess/SQLMap
parentc0d9d27f16bae2e428225302da144e9cc6d4adc8 (diff)
Update time-tracker demo and add simple dynamic SQLMap
Diffstat (limited to 'framework/DataAccess/SQLMap')
-rw-r--r--framework/DataAccess/SQLMap/Configuration/TDomSqlMapBuilder.php120
-rw-r--r--framework/DataAccess/SQLMap/Configuration/TInlineParameterMapParser.php2
-rw-r--r--framework/DataAccess/SQLMap/Configuration/TParameterProperty.php10
-rw-r--r--framework/DataAccess/SQLMap/Configuration/TResultProperty.php6
-rw-r--r--framework/DataAccess/SQLMap/Configuration/TSimpleDynamicParser.php23
-rw-r--r--framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php10
-rw-r--r--framework/DataAccess/SQLMap/Statements/TMappedStatement.php1
-rw-r--r--framework/DataAccess/SQLMap/Statements/TPreparedCommand.php2
-rw-r--r--framework/DataAccess/SQLMap/Statements/TPreparedStatementFactory.php15
-rw-r--r--framework/DataAccess/SQLMap/Statements/TSimpleDynamicSql.php32
-rw-r--r--framework/DataAccess/SQLMap/Statements/TStaticSql.php2
11 files changed, 135 insertions, 88 deletions
diff --git a/framework/DataAccess/SQLMap/Configuration/TDomSqlMapBuilder.php b/framework/DataAccess/SQLMap/Configuration/TDomSqlMapBuilder.php
index ba8323cc..0d1172b0 100644
--- a/framework/DataAccess/SQLMap/Configuration/TDomSqlMapBuilder.php
+++ b/framework/DataAccess/SQLMap/Configuration/TDomSqlMapBuilder.php
@@ -64,8 +64,8 @@ class TDomSqlMapBuilder
if(isset($document->settings) && isset($document->settings->setting))
$this->configureSettings($document->settings);
- if(isset($document->typeHandler))
- $this->loadTypeHandler($document, $this->_configFile);
+ foreach($document->xpath('//typeHandler') as $handler)
+ $this->loadTypeHandler($handler, $this->_configFile);
//load database provider
if(isset($document->provider) && isset($document->provider->datasource))
@@ -79,8 +79,8 @@ class TDomSqlMapBuilder
'sqlmap_unable_to_find_db_config', $this->_configFile);
}
- if(isset($document->sqlMaps) && isset($document->sqlMaps->sqlMap))
- $this->loadSqlMappingFiles($document->sqlMaps);
+ foreach($document->xpath('//sqlMap') as $sqlmap)
+ $this->loadSqlMappingFiles($sqlmap);
if($this->_sqlMapper->getIsCacheModelsEnabled())
$this->attachCacheModel();
@@ -182,46 +182,40 @@ class TDomSqlMapBuilder
//var_dump($node);
}
- protected function loadTypeHandler($nodes, $file)
+ protected function loadTypeHandler($node, $file)
{
- foreach($nodes->typeHandler as $node)
+ if(!is_null($node['type']) && !is_null($node['callback']))
{
- if(!is_null($node['type']) && !is_null($node['callback']))
+ $type = (string)$node['type'];
+ $dbType = (string)$node['dbType'];
+ $class = (string)$node['callback'];
+ if(class_exists('Prado', false))
{
- $type = (string)$node['type'];
- $class = (string)$node['callback'];
- if(class_exists('Prado', false))
- {
- $handler = Prado::createComponent($class);
- }
- else
- {
- if(class_exists($class,false))
- $handler = new $class;
- else
- throw new TSqlMapConfigurationException(
- 'sqlmap_type_handler_class_undef', $file, $class);
- }
- $factory = $this->_sqlMapper->getTypeHandlerFactory();
- $factory->register($type, $handler);
+ $handler = Prado::createComponent($class);
}
else
{
+ if(class_exists($class,false))
+ $handler = new $class;
+ else
throw new TSqlMapConfigurationException(
- 'sqlmap_type_handler_callback_undef', $file);
+ 'sqlmap_type_handler_class_undef', $file, $class);
}
+ $factory = $this->_sqlMapper->getTypeHandlerFactory();
+ $factory->register($type, $handler, $dbType);
}
- }
-
- protected function loadSqlMappingFiles($sqlmappings)
- {
- foreach($sqlmappings->sqlMap as $node)
+ else
{
- $resource = $this->getResourceFromPath((string)$node['resource']);
- $sqlmap = $this->getConfigAsXmlDocument($resource);
- $this->configureSqlMap($sqlmap,$resource);
+ throw new TSqlMapConfigurationException(
+ 'sqlmap_type_handler_callback_undef', $file);
}
+ }
+ protected function loadSqlMappingFiles($node)
+ {
+ $resource = $this->getResourceFromPath((string)$node['resource']);
+ $sqlmap = $this->getConfigAsXmlDocument($resource);
+ $this->configureSqlMap($sqlmap,$resource);
$this->resolveResultMapping();
}
@@ -243,27 +237,26 @@ class TDomSqlMapBuilder
// 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);
+ foreach($document->xpath('//resultMap') as $node)
+ $this->loadResultMap($node,$document,$file);
+
+ foreach($document->xpath('//parameterMap') as $node)
+ $this->loadParameterMap($node, $document, $file);
+
+ foreach($document->xpath('//statement') as $node)
+ $this->loadStatementTag($node, $document,$file);
+
+ foreach($document->xpath('//select') as $node)
+ $this->loadSelectTag($node, $document, $file);
+
+ foreach($document->xpath('//insert') as $node)
+ $this->loadInsertTag($node, $document, $file);
+
+ foreach($document->xpath('//update') as $node)
+ $this->loadUpdateTag($node, $document, $file);
+
+ foreach($document->xpath('//delete') as $node)
+ $this->loadDeleteTag($node, $document, $file);
/* if(isset($document->procedure))
foreach($document->procedure as $node)
$this->loadProcedureTag($node);
@@ -391,7 +384,7 @@ class TDomSqlMapBuilder
if(isset($superNodes[0]))
$commandText = (string)$superNodes[0] . $commandText;
else
- throw TSqlMapConfigurationException(
+ throw new TSqlMapConfigurationException(
'sqlmap_unable_to_find_parent_sql', $extend, $file);
}
@@ -404,15 +397,15 @@ class TDomSqlMapBuilder
protected function applyInlineParameterMap($statement, $sqlStatement, $node, $file)
{
+ $scope['statement'] = $statement->getID();
+ $scope['file'] = $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(
+ $sqlText = $parameterParser->parse(
$this->_sqlMapper, $statement, $sqlStatement, $scope);
if(count($sqlText['parameters']) > 0)
{
@@ -424,7 +417,16 @@ class TDomSqlMapBuilder
}
$sqlStatement = $sqlText['sql'];
}
- $sql = new TStaticSql();
+
+ $simpleDynamic = new TSimpleDynamicParser;
+ $dynamics = $simpleDynamic->parse($this->_sqlMapper, $statement, $sqlStatement, $scope);
+ if(count($dynamics['parameters']) > 0)
+ {
+ $sql = new TSimpleDynamicSql($dynamics['parameters']);
+ $sqlStatement = $dynamics['sql'];
+ }
+ else
+ $sql = new TStaticSql();
$sql->buildPreparedStatement($statement, $sqlStatement);
$statement->setSql($sql);
}
diff --git a/framework/DataAccess/SQLMap/Configuration/TInlineParameterMapParser.php b/framework/DataAccess/SQLMap/Configuration/TInlineParameterMapParser.php
index 8b1ecf04..bc633b2c 100644
--- a/framework/DataAccess/SQLMap/Configuration/TInlineParameterMapParser.php
+++ b/framework/DataAccess/SQLMap/Configuration/TInlineParameterMapParser.php
@@ -4,7 +4,7 @@ class TInlineParameterMapParser
{
private $PARAMETER_TOKEN_REGEXP = '/#(#?[^#]+#?)#/';
- public function parseInlineParameterMap($sqlMap, $statement, $sqlText, $scope)
+ public function parse($sqlMap, $statement, $sqlText, $scope)
{
$parameterClass = !is_null($statement)
? $statement->getParameterClass() : null;
diff --git a/framework/DataAccess/SQLMap/Configuration/TParameterProperty.php b/framework/DataAccess/SQLMap/Configuration/TParameterProperty.php
index 2ff55f73..196f27f5 100644
--- a/framework/DataAccess/SQLMap/Configuration/TParameterProperty.php
+++ b/framework/DataAccess/SQLMap/Configuration/TParameterProperty.php
@@ -19,9 +19,13 @@ class TParameterProperty extends TComponent
{
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());
+ {
+ return $this->_typeHandlerFactory->getTypeHandler(
+ $this->_typeHandler, $this->_dbType);
+ }
+ else if(!is_null($this->getType()))
+ return $this->_typeHandlerFactory->getTypeHandler(
+ $this->getType(), $this->_dbType);
else
return null;
}
diff --git a/framework/DataAccess/SQLMap/Configuration/TResultProperty.php b/framework/DataAccess/SQLMap/Configuration/TResultProperty.php
index 9cc0df3a..29ee366e 100644
--- a/framework/DataAccess/SQLMap/Configuration/TResultProperty.php
+++ b/framework/DataAccess/SQLMap/Configuration/TResultProperty.php
@@ -45,9 +45,11 @@ class TResultProperty extends TComponent
{
if(is_null($this->_typeHandlerFactory)) return null;
if(!is_null($this->_typeHandler))
- return $this->_typeHandlerFactory->getTypeHandler($this->_typeHandler);
+ return $this->_typeHandlerFactory->getTypeHandler(
+ $this->_typeHandler, $this->_dbType);
else if(!is_null($this->getType()))
- return $this->_typeHandlerFactory->getTypeHandler($this->getType());
+ return $this->_typeHandlerFactory->getTypeHandler(
+ $this->getType(), $this->_dbType);
else
return null;
}
diff --git a/framework/DataAccess/SQLMap/Configuration/TSimpleDynamicParser.php b/framework/DataAccess/SQLMap/Configuration/TSimpleDynamicParser.php
new file mode 100644
index 00000000..e58d8af8
--- /dev/null
+++ b/framework/DataAccess/SQLMap/Configuration/TSimpleDynamicParser.php
@@ -0,0 +1,23 @@
+<?php
+
+class TSimpleDynamicParser
+{
+ private $PARAMETER_TOKEN_REGEXP = '/\$([^\$]+)\$/';
+
+ const DYNAMIC_TOKEN = '`!`';
+
+ public function parse($sqlMap, $statement, $sqlText, $scope)
+ {
+ $matches = array();
+ $mappings = array();
+ preg_match_all($this->PARAMETER_TOKEN_REGEXP, $sqlText, $matches);
+ for($i = 0, $k=count($matches[1]); $i<$k; $i++)
+ {
+ $mappings[] = $matches[1][$i];
+ $sqlText = str_replace($matches[0][$i], self::DYNAMIC_TOKEN, $sqlText);
+ }
+ return array('sql'=>$sqlText, 'parameters'=>$mappings);
+ }
+}
+
+?> \ No newline at end of file
diff --git a/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php b/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php
index 55fb1f20..80af4345 100644
--- a/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php
+++ b/framework/DataAccess/SQLMap/DataMapper/TTypeHandlerFactory.php
@@ -11,25 +11,25 @@ class TTypeHandlerFactory
$this->_typeHandlerMap = new TMap;
}
- public function getTypeHandler($type, $dbType=null)
+ public function getTypeHandler($type, $dbType='')
{
$dbTypeHandlerMap = $this->_typeHandlerMap[$type];
$handler = null;
if(!is_null($dbTypeHandlerMap))
{
- if(is_null($dbType))
+ if(empty($dbType))
$handler = $dbTypeHandlerMap[self::NullDbType];
else
{
$handler = $dbTypeHandlerMap[$dbType];
- if(is_null($handler))
+ if(is_null($handler))
$handler = $dbTypeHandlerMap[self::NullDbType];
}
}
return $handler;
}
- public function register($type, $handler, $dbType=null)
+ public function register($type, $handler, $dbType='')
{
$map = $this->_typeHandlerMap[$type];
if(is_null($map))
@@ -37,7 +37,7 @@ class TTypeHandlerFactory
$map = new TMap;
$this->_typeHandlerMap->add($type, $map);
}
- if(is_null($dbType))
+ if(empty($dbType))
$map->add(self::NullDbType, $handler);
else
$map->add($dbType, $handler);
diff --git a/framework/DataAccess/SQLMap/Statements/TMappedStatement.php b/framework/DataAccess/SQLMap/Statements/TMappedStatement.php
index c621f285..9bc1143f 100644
--- a/framework/DataAccess/SQLMap/Statements/TMappedStatement.php
+++ b/framework/DataAccess/SQLMap/Statements/TMappedStatement.php
@@ -220,7 +220,6 @@ class TMappedStatement extends TComponent implements IMappedStatement
$list = $result instanceof ArrayAccess ? $result :
$this->_statement->createInstanceOfListClass();
$recordSet = $this->executeSQLQueryLimit($connection, $sql, $max, $skip);
-
if(!is_null($delegate))
{
while($row = $recordSet->fetchRow())
diff --git a/framework/DataAccess/SQLMap/Statements/TPreparedCommand.php b/framework/DataAccess/SQLMap/Statements/TPreparedCommand.php
index 1c0e5ad7..3557fc1f 100644
--- a/framework/DataAccess/SQLMap/Statements/TPreparedCommand.php
+++ b/framework/DataAccess/SQLMap/Statements/TPreparedCommand.php
@@ -5,7 +5,7 @@ class TPreparedCommand
public function create($connection, $statement, $parameterObject)
{
- $prepared = $statement->getSQL()->getPreparedStatement();
+ $prepared = $statement->getSQL()->getPreparedStatement($parameterObject);
$parameters = $this->applyParameterMap($connection,
$prepared, $statement, $parameterObject);
return array('sql'=>$prepared->getPreparedSql(),
diff --git a/framework/DataAccess/SQLMap/Statements/TPreparedStatementFactory.php b/framework/DataAccess/SQLMap/Statements/TPreparedStatementFactory.php
index 41059934..52461983 100644
--- a/framework/DataAccess/SQLMap/Statements/TPreparedStatementFactory.php
+++ b/framework/DataAccess/SQLMap/Statements/TPreparedStatementFactory.php
@@ -11,36 +11,21 @@ class TPreparedStatementFactory
{
$this->_statement = $statement;
$this->_commandText = $sqlString;
- // $this->_statement = new TPreparedStatement();
-// $this->_statement->setSqlString($sqlString);
}
public function prepare()
{
- //$this->createParametersFromTextCommand();
- //return $this->_statement;
$this->_preparedStatement = new TPreparedStatement();
$this->_preparedStatement->setPreparedSql($this->_commandText);
if(!is_null($this->_statement->parameterMap()))
- {
$this->createParametersForTextCommand();
- //$this->evaluateParameterMap();
- }
- //var_dump($this->_preparedStatement);
return $this->_preparedStatement;
}
protected function createParametersForTextCommand()
{
- /*$matches = array();
- $string = $this->_statement->getSqlString();
- preg_match_all('/#([a-zA-Z0-9._]+)#/', $string, $matches);
- $this->_statement->getParameterNames()->copyFrom($matches[0]);*/
- //var_dump($this->_statement);
foreach($this->_statement->ParameterMap()->getProperties() as $prop)
- {
$this->_preparedStatement->getParameterNames()->add($prop->getProperty());
- }
}
}
diff --git a/framework/DataAccess/SQLMap/Statements/TSimpleDynamicSql.php b/framework/DataAccess/SQLMap/Statements/TSimpleDynamicSql.php
new file mode 100644
index 00000000..1230b4f7
--- /dev/null
+++ b/framework/DataAccess/SQLMap/Statements/TSimpleDynamicSql.php
@@ -0,0 +1,32 @@
+<?php
+
+class TSimpleDynamicSql extends TStaticSql
+{
+ private $_mappings=array();
+
+ public function __construct($mappings)
+ {
+ $this->_mappings = $mappings;
+ }
+
+ public function getPreparedStatement($parameter=null)
+ {
+ $statement = parent::getPreparedStatement($parameter);
+ if($parameter !== null)
+ $this->mapDynamicParameter($statement, $parameter);
+ return $statement;
+ }
+
+ protected function mapDynamicParameter($statement, $parameter)
+ {
+ $sql = $statement->getPreparedSql();
+ foreach($this->_mappings as $property)
+ {
+ $value = TPropertyAccess::get($parameter, $property);
+ $sql = preg_replace('/'.TSimpleDynamicParser::DYNAMIC_TOKEN.'/', $value, $sql, 1);
+ }
+ $statement->setPreparedSql($sql);
+ }
+}
+
+?> \ No newline at end of file
diff --git a/framework/DataAccess/SQLMap/Statements/TStaticSql.php b/framework/DataAccess/SQLMap/Statements/TStaticSql.php
index bf6e4a18..b99b86ed 100644
--- a/framework/DataAccess/SQLMap/Statements/TStaticSql.php
+++ b/framework/DataAccess/SQLMap/Statements/TStaticSql.php
@@ -10,7 +10,7 @@ class TStaticSql extends TComponent
$this->_preparedStatement = $factory->prepare();
}
- public function getPreparedStatement()
+ public function getPreparedStatement($parameter=null)
{
return $this->_preparedStatement;
}