SelectStatement.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\model\mapper;
12 
13 use \Zend_Db_Select;
14 
17 
18 /**
19  * Select statement
20  *
21  * @author ingo herwig <ingo@wemove.com>
22  */
24 
25  const NO_CACHE = 'no_cache';
26  const CACHE_KEY = 'select';
27 
28  protected $_id = null;
29  protected $_type = null;
30  protected $_meta = array();
31  protected $_cachedSql = array();
32 
33  /**
34  * Get the SelectStatement instance with the given id.
35  * If the id equals SelectStatement::NO_CACHE or is not cached, a new one will be created.
36  * @param $mapper RDBMapper instance used to retrieve the database connection
37  * @param $id The statement id (optional, default: _SelectStatement::NO_CACHE_)
38  * @return SelectStatement
39  */
40  public static function get(RDBMapper $mapper, $id=self::NO_CACHE) {
41  $cache = ObjectFactory::getInstance('cache');
42  $cacheSection = self::getCacheSection($mapper->getType());
43  $cacheId = self::getCacheId($id);
44  if ($id == self::NO_CACHE || !$cache->exists($cacheSection, $cacheId)) {
45  $selectStmt = new SelectStatement($mapper, $id);
46  }
47  else {
48  $selectStmt = $cache->get($cacheSection, $cacheId);
49  }
50  return $selectStmt;
51  }
52 
53  /**
54  * Constructor
55  * @param $mapper RDBMapper instance
56  * @param $id The statement id (optional, default: _SelectStatement::NO_CACHE_)
57  */
58  public function __construct(RDBMapper $mapper, $id=self::NO_CACHE) {
59  parent::__construct($mapper->getConnection());
60  $this->_id = $id;
61  $this->_type = $mapper->getType();
62  }
63 
64  /**
65  * Get the entity type associated with the statement
66  * @return String
67  */
68  public function getType() {
69  return $this->_type;
70  }
71 
72  /**
73  * Check if the statement is cached already
74  * @return Boolean
75  */
76  public function isCached() {
77  $cache = ObjectFactory::getInstance('cache');
78  return $this->_id == self::NO_CACHE ? false :
79  $cache->exists(self::getCacheSection($this->_type), self::getCacheId($this->_id));
80  }
81 
82  /**
83  * Add customt meta value
84  * @param $key
85  * @param $value
86  */
87  public function setMeta($key, $value) {
88  $this->_meta[$key] = $value;
89  }
90 
91  /**
92  * Get customt meta value
93  * @param $key
94  * @return Mixed
95  */
96  public function getMeta($key) {
97  if (isset($this->_meta[$key])) {
98  return $this->_meta[$key];
99  }
100  return null;
101  }
102 
103  /**
104  * Execute a count query and return the row count
105  * @return Integer
106  */
107  public function getRowCount() {
108  // empty columns, order and limit
109  $columnPart = $this->_parts[self::COLUMNS];
110  $orderPart = $this->_parts[self::ORDER];
111  $limitCount = $this->_parts[self::LIMIT_COUNT];
112  $limitOffset = $this->_parts[self::LIMIT_OFFSET];
113  $this->_parts[self::COLUMNS] = self::$_partsInit[self::COLUMNS];
114  $this->_parts[self::ORDER] = self::$_partsInit[self::ORDER];
115  $this->_parts[self::LIMIT_COUNT] = self::$_partsInit[self::LIMIT_COUNT];
116  $this->_parts[self::LIMIT_OFFSET] = self::$_partsInit[self::LIMIT_OFFSET];
117 
118  // do count query
119  $this->columns(array('nRows' => SQLConst::COUNT()));
120  $stmt = $this->getAdapter()->prepare($this->assemble('count'));
121  $stmt->execute($this->getBind());
122  $row = $stmt->fetch();
123  $nRows = $row['nRows'];
124 
125  // reset columns and order
126  $this->_parts[self::COLUMNS] = $columnPart;
127  $this->_parts[self::ORDER] = $orderPart;
128  $this->_parts[self::LIMIT_COUNT] = $limitCount;
129  $this->_parts[self::LIMIT_OFFSET] = $limitOffset;
130 
131  return $nRows;
132  }
133 
134  /**
135  * Put the statement into the cache
136  */
137  public function save() {
138  if ($this->_id != self::NO_CACHE) {
139  $cache = ObjectFactory::getInstance('cache');
140  $cache->put(self::getCacheSection($this->_type), self::getCacheId($this->_id), $this);
141  }
142  }
143 
144  /**
145  * @see Select::assemble()
146  */
147  public function assemble($cacheKey=null) {
148  if (!isset($this->_cachedSql[$cacheKey])) {
149  $sql = parent::assemble();
150  $this->_cachedSql[$cacheKey] = $sql;
151  }
152  return $this->_cachedSql[$cacheKey];
153  }
154 
155  /**
156  * @see Select::query()
157  */
158  public function query($fetchMode = null, $bind = array()) {
159  $stmt = $this->getAdapter()->prepare($this->assemble('select'));
160  $stmt->execute($this->getBind());
161  return $stmt;
162  }
163 
164  /**
165  * Get the cache section
166  * @param $type The type
167  * @return String
168  */
169  protected static function getCacheSection($type) {
170  return self::CACHE_KEY.'/'.$type;
171  }
172 
173  /**
174  * Get the compressed cache id from the id
175  * @param $id
176  * @return String
177  */
178  protected static function getCacheId($id) {
179  return md5($id);
180  }
181 
182  /**
183  * Serialization handlers
184  */
185 
186  public function __sleep() {
187  return array('_id', '_type', '_meta', '_cachedSql', '_parts');
188  }
189 
190  public function __wakeup() {
191  $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
192  $mapper = $persistenceFacade->getMapper($this->_type);
193  $this->_adapter = $mapper->getConnection();
194  }
195 }
196 ?>
static COUNT()
Get the COUNT(*) expression.
Definition: SQLConst.php:40
getMeta($key)
Get customt meta value.
query($fetchMode=null, $bind=array())
RDBMapper maps objects of one type to a relational database schema.
Definition: RDBMapper.php:49
getConnection()
Get the database connection.
Definition: RDBMapper.php:865
setMeta($key, $value)
Add customt meta value.
static getInstance($name, $dynamicConfiguration=array())
getType()
Get the entity type that this mapper handles.
__construct(RDBMapper $mapper, $id=self::NO_CACHE)
Constructor.
isCached()
Check if the statement is cached already.
save()
Put the statement into the cache.
getType()
Get the entity type associated with the statement.
static getCacheSection($type)
Get the cache section.
getRowCount()
Execute a count query and return the row count.
static getCacheId($id)
Get the compressed cache id from the id.