3 use wcf\system\database\util\PreparedStatementConditionBuilder
;
4 use wcf\system\exception\SystemException
;
6 use wcf\util\StringUtil
;
9 * Abstract class for a list of database objects.
12 * @copyright 2001-2012 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package com.woltlab.wcf
16 * @category Community Framework
18 abstract class DatabaseObjectList
implements \Countable
, ITraversableObject
{
23 public $className = '';
29 public $objectClassName = '';
33 * @var array<wcf\data\DatabaseObject>
35 public $objects = array();
38 * ids of result objects
41 public $objectIDs = null;
47 public $sqlOffset = 0;
53 public $sqlLimit = 20;
56 * sql order by statement
59 public $sqlOrderBy = '';
62 * sql select parameters
65 public $sqlSelects = '';
68 * sql select joins which are necessary for where statements
71 public $sqlConditionJoins = '';
77 public $sqlJoins = '';
80 * enables the automatic usage of the qualified shorthand
83 public $useQualifiedShorthand = true;
87 * @var wcf\system\database\util\PreparedStatementConditionBuilder
89 protected $conditionBuilder = null;
92 * current iterator index
98 * list of index to object relation
101 protected $indexToObject = null;
104 * Creates a new DatabaseObjectList object.
106 public function __construct() {
108 if (empty($this->className
)) {
109 $className = get_called_class();
111 if (StringUtil
::substring($className, -4) == 'List') {
112 $this->className
= StringUtil
::substring($className, 0, -4);
116 $this->conditionBuilder
= new PreparedStatementConditionBuilder();
120 * Counts the number of objects.
124 public function countObjects() {
125 $sql = "SELECT COUNT(*) AS count
126 FROM ".$this->getDatabaseTableName()." ".$this->getDatabaseTableAlias()."
127 ".$this->sqlConditionJoins
."
128 ".$this->getConditionBuilder()->__toString();
129 $statement = WCF
::getDB()->prepareStatement($sql);
130 $statement->execute($this->getConditionBuilder()->getParameters());
131 $row = $statement->fetchArray();
132 return $row['count'];
136 * Reads the object ids from database.
138 public function readObjectIDs() {
139 $this->objectIDs
= array();
140 $sql = "SELECT ".$this->getDatabaseTableAlias().".".$this->getDatabaseTableIndexName()." AS objectID
141 FROM ".$this->getDatabaseTableName()." ".$this->getDatabaseTableAlias()."
142 ".$this->sqlConditionJoins
."
143 ".$this->getConditionBuilder()->__toString()."
144 ".(!empty($this->sqlOrderBy
) ?
"ORDER BY ".$this->sqlOrderBy
: '');
145 $statement = WCF
::getDB()->prepareStatement($sql, $this->sqlLimit
, $this->sqlOffset
);
146 $statement->execute($this->getConditionBuilder()->getParameters());
147 while ($row = $statement->fetchArray()) {
148 $this->objectIDs
[] = $row['objectID'];
153 * Reads the objects from database.
155 public function readObjects() {
156 if ($this->objectIDs
!== null) {
157 if (!count($this->objectIDs
)) {
160 $sql = "SELECT ".(!empty($this->sqlSelects
) ?
$this->sqlSelects
.($this->useQualifiedShorthand ?
',' : '') : '')."
161 ".($this->useQualifiedShorthand ?
$this->getDatabaseTableAlias().'.*' : '')."
162 FROM ".$this->getDatabaseTableName()." ".$this->getDatabaseTableAlias()."
164 WHERE ".$this->getDatabaseTableAlias().".".$this->getDatabaseTableIndexName()." IN (?".str_repeat(',?', count($this->objectIDs
) - 1).")
165 ".(!empty($this->sqlOrderBy
) ?
"ORDER BY ".$this->sqlOrderBy
: '');
166 $statement = WCF
::getDB()->prepareStatement($sql);
167 $statement->execute($this->objectIDs
);
168 $this->objects
= $statement->fetchObjects(($this->objectClassName ?
: $this->className
));
171 $sql = "SELECT ".(!empty($this->sqlSelects
) ?
$this->sqlSelects
.($this->useQualifiedShorthand ?
',' : '') : '')."
172 ".($this->useQualifiedShorthand ?
$this->getDatabaseTableAlias().'.*' : '')."
173 FROM ".$this->getDatabaseTableName()." ".$this->getDatabaseTableAlias()."
175 ".$this->getConditionBuilder()->__toString()."
176 ".(!empty($this->sqlOrderBy
) ?
"ORDER BY ".$this->sqlOrderBy
: '');
177 $statement = WCF
::getDB()->prepareStatement($sql, $this->sqlLimit
, $this->sqlOffset
);
178 $statement->execute($this->getConditionBuilder()->getParameters());
179 $this->objects
= $statement->fetchObjects(($this->objectClassName ?
: $this->className
));
182 // use table index as array index
184 foreach($this->objects
as $object) {
185 $objectID = $object->{$this->getDatabaseTableIndexName()};
186 $objects[$objectID] = $object;
188 $this->indexToObject
[] = $objectID;
190 $this->objectIDs
= $this->indexToObject
;
191 $this->objects
= $objects;
195 * Returns the object ids of the list.
197 * @return array<integer>
199 public function getObjectIDs() {
200 return $this->objectIDs
;
204 * Sets the object ids.
206 * @param array<integer> $objectIDs
208 public function setObjectIDs(array $objectIDs) {
209 $this->objectIDs
= $objectIDs;
213 * Returns the objects of the list.
215 * @return array<wcf\data\DatabaseObject>
217 public function getObjects() {
218 return $this->objects
;
222 * Returns the condition builder object.
224 * @return wcf\system\database\util\PreparedStatementConditionBuilder
226 public function getConditionBuilder() {
227 return $this->conditionBuilder
;
231 * Returns the name of the database table.
235 public function getDatabaseTableName() {
236 return call_user_func(array($this->className
, 'getDatabaseTableName'));
240 * Returns the name of the database table.
244 public function getDatabaseTableIndexName() {
245 return call_user_func(array($this->className
, 'getDatabaseTableIndexName'));
249 * Returns the name of the database table alias.
253 public function getDatabaseTableAlias() {
254 return call_user_func(array($this->className
, 'getDatabaseTableAlias'));
258 * @see \Countable::count()
260 public function count() {
261 return count($this->objects
);
265 * @see \Iterator::current()
267 public function current() {
268 $objectID = $this->indexToObject
[$this->index
];
269 return $this->objects
[$objectID];
273 * CAUTION: This methods does not return the current iterator index,
274 * rather than the object key which maps to that index.
276 * @see \Iterator::key()
278 public function key() {
279 return $this->indexToObject
[$this->index
];
283 * @see \Iterator::next()
285 public function next() {
290 * @see \Iterator::rewind()
292 public function rewind() {
297 * @see \Iterator::valid()
299 public function valid() {
300 return isset($this->indexToObject
[$this->index
]);
304 * @see \SeekableIterator::seek()
306 public function seek($index) {
307 $this->index
= $index;
309 if (!$this->valid()) {
310 throw new \
OutOfBoundsException();
315 * @see wcf\data\ITraversableObject::seekTo()
317 public function seekTo($objectID) {
318 $this->index
= array_search($objectID, $this->indexToObject
);
320 if ($this->index
=== false) {
321 throw new SystemException("object id '".$objectID."' is invalid");
326 * @see wcf\data\ITraversableObject::search()
328 public function search($objectID) {
330 $this->seekTo($objectID);
331 return $this->current();
333 catch (SystemException
$e) {