This repository was archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathDbSelect.php
154 lines (128 loc) · 3.79 KB
/
DbSelect.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Paginator\Adapter;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Select;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\ResultSet\ResultSetInterface;
class DbSelect implements AdapterInterface
{
const ROW_COUNT_COLUMN_NAME = 'C';
/**
* @var Sql
*/
protected $sql;
/**
* Database query
*
* @var Select
*/
protected $select;
/**
* Database count query
*
* @var Select|null
*/
protected $countSelect;
/**
* @var ResultSet
*/
protected $resultSetPrototype;
/**
* Total item count
*
* @var int
*/
protected $rowCount;
/**
* Constructor.
*
* @param Select $select The select query
* @param Adapter|Sql $adapterOrSqlObject DB adapter or Sql object
* @param null|ResultSetInterface $resultSetPrototype
* @param null|Select $countSelect
*
* @throws Exception\InvalidArgumentException
*/
public function __construct(
Select $select,
$adapterOrSqlObject,
ResultSetInterface $resultSetPrototype = null,
Select $countSelect = null
) {
$this->select = $select;
$this->countSelect = $countSelect;
if ($adapterOrSqlObject instanceof Adapter) {
$adapterOrSqlObject = new Sql($adapterOrSqlObject);
}
if (!$adapterOrSqlObject instanceof Sql) {
throw new Exception\InvalidArgumentException(
'$adapterOrSqlObject must be an instance of Zend\Db\Adapter\Adapter or Zend\Db\Sql\Sql'
);
}
$this->sql = $adapterOrSqlObject;
$this->resultSetPrototype = ($resultSetPrototype) ?: new ResultSet;
}
/**
* Returns an array of items for a page.
*
* @param int $offset Page offset
* @param int $itemCountPerPage Number of items per page
* @return array
*/
public function getItems($offset, $itemCountPerPage)
{
$select = clone $this->select;
$select->offset($offset);
$select->limit($itemCountPerPage);
$statement = $this->sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
$resultSet = clone $this->resultSetPrototype;
$resultSet->initialize($result);
return iterator_to_array($resultSet);
}
/**
* Returns the total number of rows in the result set.
*
* @return int
*/
public function count()
{
if ($this->rowCount !== null) {
return $this->rowCount;
}
$select = $this->getSelectCount();
$statement = $this->sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
$row = $result->current();
$this->rowCount = (int) $row[self::ROW_COUNT_COLUMN_NAME];
return $this->rowCount;
}
/**
* Returns select query for count
*
* @return Select
*/
protected function getSelectCount()
{
if ($this->countSelect) {
return $this->countSelect;
}
$select = clone $this->select;
$select->reset(Select::LIMIT);
$select->reset(Select::OFFSET);
$select->reset(Select::ORDER);
$countSelect = new Select;
$countSelect->columns(array(self::ROW_COUNT_COLUMN_NAME => new Expression('COUNT(1)')));
$countSelect->from(array('original_select' => $select));
return $countSelect;
}
}