| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TCachingStatement class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TCachingStatement.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.SqlMap.Statements
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TCacheingStatement class.
|
|
|
15 |
*
|
|
|
16 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
17 |
* @version $Id: TCachingStatement.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
18 |
* @package System.Data.SqlMap.Statements
|
|
|
19 |
* @since 3.1
|
|
|
20 |
*/
|
|
|
21 |
class TCachingStatement implements IMappedStatement
|
|
|
22 |
{
|
|
|
23 |
private $_mappedStatement;
|
|
|
24 |
|
|
|
25 |
public function __construct(TMappedStatement $statement)
|
|
|
26 |
{
|
|
|
27 |
$this->_mappedStatement = $statement;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function getID()
|
|
|
31 |
{
|
|
|
32 |
return $this->_mappedStatement->getID();
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public function getStatement()
|
|
|
36 |
{
|
|
|
37 |
return $this->_mappedStatement->getStatement();
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public function getManager()
|
|
|
41 |
{
|
|
|
42 |
return $this->_mappedStatement->getManager();
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function executeQueryForMap($connection, $parameter,$keyProperty, $valueProperty=null, $skip=-1, $max=-1,$delegate=null)
|
|
|
46 |
{
|
|
|
47 |
$sql = $this->createCommand($connection, $parameter, $skip, $max);
|
|
|
48 |
$key = $this->getCacheKey(array(clone($sql), $keyProperty, $valueProperty,$skip, $max));
|
|
|
49 |
$map = $this->getStatement()->getCache()->get($key);
|
|
|
50 |
if(is_null($map))
|
|
|
51 |
{
|
|
|
52 |
$map = $this->_mappedStatement->runQueryForMap(
|
|
|
53 |
$connection, $parameter, $sql, $keyProperty, $valueProperty, $delegate);
|
|
|
54 |
$this->getStatement()->getCache()->set($key, $map);
|
|
|
55 |
}
|
|
|
56 |
return $map;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public function executeUpdate($connection, $parameter)
|
|
|
60 |
{
|
|
|
61 |
return $this->_mappedStatement->executeUpdate($connection, $parameter);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public function executeInsert($connection, $parameter)
|
|
|
65 |
{
|
|
|
66 |
return $this->executeInsert($connection, $parameter);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public function executeQueryForList($connection, $parameter, $result=null, $skip=-1, $max=-1, $delegate=null)
|
|
|
70 |
{
|
|
|
71 |
$sql = $this->createCommand($connection, $parameter, $skip, $max);
|
|
|
72 |
$key = $this->getCacheKey(array(clone($sql), $parameter, $skip, $max));
|
|
|
73 |
$list = $this->getStatement()->getCache()->get($key);
|
|
|
74 |
if(is_null($list))
|
|
|
75 |
{
|
|
|
76 |
$list = $this->_mappedStatement->runQueryForList(
|
|
|
77 |
$connection, $parameter, $sql, $result, $delegate);
|
|
|
78 |
$this->getStatement()->getCache()->set($key, $list);
|
|
|
79 |
}
|
|
|
80 |
return $list;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public function executeQueryForObject($connection, $parameter, $result=null)
|
|
|
84 |
{
|
|
|
85 |
$sql = $this->createCommand($connection, $parameter);
|
|
|
86 |
$key = $this->getCacheKey(array(clone($sql), $parameter));
|
|
|
87 |
$object = $this->getStatement()->getCache()->get($key);
|
|
|
88 |
if(is_null($object))
|
|
|
89 |
{
|
|
|
90 |
$object = $this->_mappedStatement->runQueryForObject($connection, $sql, $result);
|
|
|
91 |
$this->getStatement()->getCache()->set($key, $object);
|
|
|
92 |
}
|
|
|
93 |
return $object;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
protected function getCacheKey($object)
|
|
|
97 |
{
|
|
|
98 |
$cacheKey = new TSqlMapCacheKey($object);
|
|
|
99 |
return $cacheKey->getHash();
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
protected function createCommand($connection, $parameter, $skip=null, $max=null)
|
|
|
103 |
{
|
|
|
104 |
return $this->_mappedStatement->getCommand()->create($this->getManager(),
|
|
|
105 |
$connection, $this->getStatement(), $parameter, $skip, $max);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|