1 | <?php |
||
38 | class Select extends AbstractCommand |
||
39 | { |
||
40 | |||
41 | private $table; |
||
42 | private $fields = ['*']; |
||
43 | private $data = null; |
||
44 | private $fetchStyle = \PDO::FETCH_ASSOC; |
||
45 | private $fetchFlat = false; |
||
46 | private $defaultValue = []; |
||
47 | |||
48 | use WhereTrait; |
||
49 | |||
50 | /** |
||
51 | * retrieves data from a PDO connected resource |
||
52 | * @param string $table name of the table that houses the data |
||
53 | * @param array $fields optional default value: empty array - defines which |
||
54 | * fields are returned if no fields defined a star will be used |
||
55 | * @param null|WhereStatementInterface $where optional default value: null - |
||
56 | * object used to limit the returned results |
||
57 | * @param integer $fetchStyle optional default value: PDO::FETCH_ASSOC - |
||
58 | * sets how the PDO statement will return records |
||
59 | * @param boolean $fetchFlat optional default value: false - true will |
||
60 | * return one record, false will return all records |
||
61 | * @param mixed $defaultValue optional default value: empty array - |
||
62 | * value to be returned on query failure |
||
63 | * @since v1.0.0 |
||
64 | */ |
||
65 | 23 | public function __construct( |
|
83 | |||
84 | /** |
||
85 | * Runs a query and returns the result of the SQL |
||
86 | * @param PDO $pdo a PDO connection object |
||
87 | * @return mixed with return the result set as defined by fetchStyle |
||
88 | * @throws ExecutionErrorException on SQL error with the message of the exception |
||
89 | * @since v1.0.0 |
||
90 | */ |
||
91 | 10 | public function execute(PDO $pdo) |
|
110 | |||
111 | /** |
||
112 | * Runs the sql statement inserting the query's data |
||
113 | * @param PDOStatement $stmt PDO statement of a prepared SQL statement |
||
114 | * @param array $whereData data to be used to fill out a where statement |
||
115 | * @return bool true query succeeds, false there was an error executing the query |
||
116 | * @since v1.0.0 |
||
117 | */ |
||
118 | 9 | protected function statementExecute(PDOStatement $stmt, array $whereData) |
|
125 | |||
126 | /** |
||
127 | * Fetches the data from PDO resource |
||
128 | * @param PDOStatement $stmt PDO statement of a prepared SQL statement |
||
129 | * @param bool $fetchFlat true returns one record, false returns all records |
||
130 | * @param integer $fetchStyle a \PDO::FETCH_* variable defining the format of |
||
131 | * the returned object |
||
132 | * @return array returns the results of the query |
||
133 | * @since v1.0.0 |
||
134 | */ |
||
135 | 8 | protected function fetchData(PDOStatement $stmt, $fetchFlat, $fetchStyle) |
|
142 | |||
143 | /** |
||
144 | * returns a PDO SQL string that has parameter syntax |
||
145 | * @return string |
||
146 | * @since v1.0.0 |
||
147 | */ |
||
148 | 11 | public function getSql() |
|
159 | |||
160 | /** |
||
161 | * acts as a cache to house ran queries |
||
162 | * @param mixed $data data to be stored |
||
163 | * @return Select for method chaining |
||
164 | * @since v1.0.0 |
||
165 | */ |
||
166 | 9 | public function setData($data) |
|
171 | |||
172 | /** |
||
173 | * Acts as a cache array that holds the data returned from a query |
||
174 | * @return mixed |
||
175 | * @since v1.0.0 |
||
176 | */ |
||
177 | 9 | public function getData() |
|
181 | |||
182 | /** |
||
183 | * retrieves the table with which you wish to select from |
||
184 | * @return string table with which you wish to select from |
||
185 | * @since v1.0.0 |
||
186 | */ |
||
187 | 13 | public function getTable() |
|
191 | |||
192 | /** |
||
193 | * defines a table with which you wish to append to |
||
194 | * @param string $table a table with which you wish to append to |
||
195 | * @return Select for method chaining |
||
196 | * @since v1.0.0 |
||
197 | */ |
||
198 | 23 | public function setTable($table) |
|
203 | |||
204 | /** |
||
205 | * Returns the fields to be returned, if no fields defined all fields are returned |
||
206 | * @return array |
||
207 | * @since v1.0.0 |
||
208 | */ |
||
209 | 15 | public function getFields() |
|
213 | |||
214 | /** |
||
215 | * Defines the fields to be returned, if no fields defined all fields are returned |
||
216 | * @param null|array $fields |
||
217 | * @return Select for method chaining |
||
218 | * @since v1.0.0 |
||
219 | */ |
||
220 | 23 | public function setFields(array $fields = null) |
|
225 | |||
226 | /** |
||
227 | * defines how data is returned |
||
228 | * @return int $fetchStyle one of the PDO::FETCH_* |
||
229 | * @since v1.0.0 |
||
230 | */ |
||
231 | 10 | public function getFetchStyle() |
|
235 | |||
236 | /** |
||
237 | * used to define how data is returned |
||
238 | * @param int $fetchStyle one of the PDO::FETCH_* |
||
239 | * @return Select for method chaining |
||
240 | * @since v1.0.0 |
||
241 | */ |
||
242 | 23 | public function setFetchStyle($fetchStyle) |
|
247 | |||
248 | /** |
||
249 | * sets if one or many records will be returned. |
||
250 | * @return bool true for one record, false for all records |
||
251 | * @since v1.0.0 |
||
252 | */ |
||
253 | 9 | public function getFetchFlat() |
|
257 | |||
258 | /** |
||
259 | * sets if one or many records will be returned. |
||
260 | * @param boolean $fetchFlat optional default value: false - true will |
||
261 | * return one record, false will return all records |
||
262 | * @return Select for method chaining |
||
263 | * @since v1.0.0 |
||
264 | */ |
||
265 | 23 | public function setFetchFlat($fetchFlat = true) |
|
270 | |||
271 | /** |
||
272 | * Value to be returned if no data is found or query fails |
||
273 | * @return mixed return the value used when the query returns false |
||
274 | * @since v1.0.0 |
||
275 | */ |
||
276 | 5 | public function getDefaultValue() |
|
280 | |||
281 | /** |
||
282 | * Value to be returned if no data is found or query fails |
||
283 | * @param array $defaultValue optional default value: empty array - value to |
||
284 | * be returned on query failure |
||
285 | * @return Select for method chaining |
||
286 | * @since v1.0.0 |
||
287 | */ |
||
288 | 23 | public function setDefaultValue($defaultValue = []) |
|
293 | } |
||
294 |