summaryrefslogtreecommitdiff
path: root/framework/Data/SqlMap/Statements/TCachingStatement.php
blob: 54664e37c74af7ba60dfa5532b5dd44e88588f45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
 * TCachingStatement class file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2012 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Data.SqlMap.Statements
 */

/**
 * TCacheingStatement class.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Id$
 * @package System.Data.SqlMap.Statements
 * @since 3.1
 */
class TCachingStatement extends TComponent implements IMappedStatement
{
	private $_mappedStatement;

	public function __construct(TMappedStatement $statement)
	{
		$this->_mappedStatement = $statement;
	}

	public function getID()
	{
		return $this->_mappedStatement->getID();
	}

	public function getStatement()
	{
		return $this->_mappedStatement->getStatement();
	}

	public function getManager()
	{
		return $this->_mappedStatement->getManager();
	}

	public function executeQueryForMap($connection, $parameter,$keyProperty, $valueProperty=null,  $skip=-1, $max=-1,$delegate=null)
	{
		$sql = $this->createCommand($connection, $parameter, $skip, $max);
		$key = $this->getCacheKey(array(clone($sql), $keyProperty, $valueProperty,$skip, $max));
		$map = $this->getStatement()->getCache()->get($key);
		if($map===null)
		{
			$map = $this->_mappedStatement->runQueryForMap(
				$connection, $parameter, $sql, $keyProperty, $valueProperty,  $delegate);
			$this->getStatement()->getCache()->set($key, $map);
		}
		return $map;
	}

	public function executeUpdate($connection, $parameter)
	{
		return $this->_mappedStatement->executeUpdate($connection, $parameter);
	}

	public function executeInsert($connection, $parameter)
	{
		return $this->executeInsert($connection, $parameter);
	}

	public function executeQueryForList($connection, $parameter, $result=null, $skip=-1, $max=-1, $delegate=null)
	{
		$sql = $this->createCommand($connection, $parameter, $skip, $max);
		$key = $this->getCacheKey(array(clone($sql), $parameter, $skip, $max));
		$list = $this->getStatement()->getCache()->get($key);
		if($list===null)
		{
			$list = $this->_mappedStatement->runQueryForList(
				$connection, $parameter, $sql, $result, $delegate);
			$this->getStatement()->getCache()->set($key, $list);
		}
		return $list;
	}

	public function executeQueryForObject($connection, $parameter, $result=null)
	{
		$sql = $this->createCommand($connection, $parameter);
		$key = $this->getCacheKey(array(clone($sql), $parameter));
		$object = $this->getStatement()->getCache()->get($key);
		if($object===null)
		{
			$object = $this->_mappedStatement->runQueryForObject($connection, $sql, $result);
			$this->getStatement()->getCache()->set($key, $object);
		}
		return $object;
	}

	protected function getCacheKey($object)
	{
		$cacheKey = new TSqlMapCacheKey($object);
		return $cacheKey->getHash();
	}

	protected function createCommand($connection, $parameter, $skip=null, $max=null)
	{
		return $this->_mappedStatement->getCommand()->create($this->getManager(),
					$connection, $this->getStatement(), $parameter, $skip, $max);
	}
}