1 | <?php |
||
16 | abstract class SearchHelperBase |
||
17 | { |
||
18 | /** @var PdoDatabase */ |
||
19 | protected $database; |
||
20 | /** @var array */ |
||
21 | protected $parameterList = array(); |
||
22 | /** @var null|int */ |
||
23 | private $limit = null; |
||
24 | /** @var null|int */ |
||
25 | private $offset = null; |
||
26 | private $orderBy = null; |
||
27 | /** |
||
28 | * @var string The where clause. |
||
29 | * |
||
30 | * (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here). Note |
||
31 | * that we use positional parameters instead of named parameters because we don't know many times different options |
||
32 | * will be called (looking at excluding() here, but there's the option for others). |
||
33 | */ |
||
34 | protected $whereClause = ' WHERE 1 = 1'; |
||
35 | /** @var string */ |
||
36 | protected $table; |
||
37 | protected $joinClause = ''; |
||
38 | private $targetClass; |
||
39 | |||
40 | /** |
||
41 | * SearchHelperBase constructor. |
||
42 | * |
||
43 | * @param PdoDatabase $database |
||
44 | * @param string $table |
||
45 | * @param $targetClass |
||
46 | * @param null|string $order Order by clause, excluding ORDER BY. |
||
47 | */ |
||
48 | protected function __construct(PdoDatabase $database, $table, $targetClass, $order = null) |
||
55 | |||
56 | /** |
||
57 | * Finalises the database query, and executes it, returning a set of objects. |
||
58 | * |
||
59 | * @return DataObject[] |
||
60 | */ |
||
61 | public function fetch() |
||
73 | |||
74 | /** |
||
75 | * Finalises the database query, and executes it, returning only the requested column. |
||
76 | * |
||
77 | * @param string $column The required column |
||
78 | * @return array |
||
79 | */ |
||
80 | public function fetchColumn($column){ |
||
85 | |||
86 | public function fetchMap($column){ |
||
87 | $statement = $this->getData(array('id', $column)); |
||
88 | |||
89 | $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
||
90 | $map = array(); |
||
91 | |||
92 | foreach ($data as $row) { |
||
93 | $map[$row['id']] = $row[$column]; |
||
94 | } |
||
95 | |||
96 | return $map; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param int $count Returns the record count of the result set |
||
101 | * |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function getRecordCount(&$count) |
||
105 | { |
||
106 | $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
||
107 | $query .= $this->joinClause . $this->whereClause; |
||
108 | |||
109 | $statement = $this->database->prepare($query); |
||
110 | $statement->execute($this->parameterList); |
||
111 | |||
112 | $count = $statement->fetchColumn(0); |
||
113 | $statement->closeCursor(); |
||
114 | |||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Limits the results |
||
120 | * |
||
121 | * @param integer $limit |
||
122 | * @param integer|null $offset |
||
123 | * |
||
124 | * @return $this |
||
125 | * |
||
126 | */ |
||
127 | public function limit($limit, $offset = null) |
||
134 | |||
135 | private function applyLimit() |
||
136 | { |
||
150 | |||
151 | private function applyOrder() |
||
159 | |||
160 | /** |
||
161 | * @param array $columns |
||
162 | * |
||
163 | * @return PDOStatement |
||
164 | */ |
||
165 | private function getData($columns = array('*')) |
||
176 | |||
177 | /** |
||
178 | * @param array $columns |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | protected function buildQuery($columns) |
||
194 | |||
195 | public function inIds($idList) { |
||
199 | |||
200 | protected function inClause($column, $values) { |
||
214 | } |
||
215 |