1 | <?php |
||
14 | abstract class SqlActions |
||
15 | { |
||
16 | /** |
||
17 | * @var \BfwSql\SqlConnect $sqlConnect SqlConnect object |
||
18 | */ |
||
19 | protected $sqlConnect; |
||
20 | |||
21 | /** |
||
22 | * @var string $assembledRequest The request will be executed |
||
23 | */ |
||
24 | protected $assembledRequest = ''; |
||
25 | |||
26 | /** |
||
27 | * @var boolean $isPreparedRequest If is a prepared request |
||
28 | */ |
||
29 | protected $isPreparedRequest = true; |
||
30 | |||
31 | /** |
||
32 | * @var string $tableName The main table name for request |
||
33 | */ |
||
34 | protected $tableName = ''; |
||
35 | |||
36 | /** |
||
37 | * @var array $columns List of impacted columns by the request |
||
38 | */ |
||
39 | protected $columns = array(); |
||
40 | |||
41 | /** |
||
42 | * @var string[] $where All filter use in where part of the request |
||
43 | */ |
||
44 | protected $where = array(); |
||
45 | |||
46 | /** |
||
47 | * @var string[] $preparedRequestArgs Arguments used by prepared request |
||
48 | */ |
||
49 | protected $preparedRequestArgs = array(); |
||
50 | |||
51 | /** |
||
52 | * @var array $prepareDriversOptions SGBD driver option used for |
||
53 | * prepared request |
||
54 | * |
||
55 | * @link http://php.net/manual/en/pdo.prepare.php |
||
56 | */ |
||
57 | protected $prepareDriversOptions = array(); |
||
58 | |||
59 | /** |
||
60 | * @var boolean $noResult If request has sent no result. |
||
61 | */ |
||
62 | protected $noResult = false; |
||
63 | |||
64 | /** |
||
65 | * @var \PDOStatement $lastRequestStatement The PDOStatement pour the |
||
66 | * last request executed. |
||
67 | */ |
||
68 | protected $lastRequestStatement; |
||
69 | |||
70 | /** |
||
71 | * Constructor |
||
72 | * |
||
73 | * @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion |
||
74 | */ |
||
75 | public function __construct(\BfwSql\SqlConnect $sqlConnect) |
||
79 | |||
80 | /** |
||
81 | * Getter to access at sqlConnect property |
||
82 | * |
||
83 | * @return \BfwSql\SqlConnect |
||
84 | */ |
||
85 | public function getSqlConnect() |
||
89 | |||
90 | /** |
||
91 | * Setter to enable or disable prepared request |
||
92 | * |
||
93 | * @param boolean $preparedRequestStatus The new status for prepared request |
||
94 | * |
||
95 | * @return \BfwSql\SqlActions |
||
96 | */ |
||
97 | public function setIsPreparedRequest($preparedRequestStatus) |
||
103 | |||
104 | /** |
||
105 | * Getter to access at preparedRequestArgs property |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getPreparedRequestArgs() |
||
113 | |||
114 | /** |
||
115 | * Getter to access at prepareDriversOptions property |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | public function getPrepareDriversOptions() |
||
123 | |||
124 | /** |
||
125 | * Define driver options to prepared request |
||
126 | * |
||
127 | * @link http://php.net/manual/fr/pdo.prepare.php |
||
128 | * |
||
129 | * @param array $driverOptions Drivers options |
||
130 | * |
||
131 | * @return \BfwSql\SqlActions |
||
132 | */ |
||
133 | public function setPrepareDriversOptions($driverOptions) |
||
139 | |||
140 | /** |
||
141 | * Check if a request is assemble or not. |
||
142 | * If not, run the method assembleRequest. |
||
143 | * |
||
144 | * @return boolean |
||
145 | */ |
||
146 | public function isAssembled() |
||
154 | |||
155 | /** |
||
156 | * Write the query |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | protected abstract function assembleRequest(); |
||
161 | |||
162 | /** |
||
163 | * Return the assembled request |
||
164 | * |
||
165 | * @param boolean $force : Force to re-assemble request |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function assemble($force = false) |
||
177 | |||
178 | /** |
||
179 | * Execute the assembled request |
||
180 | * |
||
181 | * @return array The pdo errorInfo array |
||
182 | */ |
||
183 | protected function executeQuery() |
||
211 | |||
212 | /** |
||
213 | * Execute the assembled request and check if there are errors |
||
214 | * Update property noResult |
||
215 | * |
||
216 | * @throws \Exception If the request fail |
||
217 | * |
||
218 | * @return \PDOStatement|integer |
||
219 | */ |
||
220 | public function execute() |
||
242 | |||
243 | /** |
||
244 | * Closes the cursor, enabling the statement to be executed again. |
||
245 | * |
||
246 | * @link http://php.net/manual/fr/pdostatement.closecursor.php |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | public function closeCursor() |
||
254 | |||
255 | /** |
||
256 | * Return the number of impacted rows by the last request |
||
257 | * |
||
258 | * @return int|bool |
||
259 | */ |
||
260 | public function obtainImpactedRows() |
||
273 | |||
274 | /** |
||
275 | * To call this own request without use query writer |
||
276 | * |
||
277 | * @param string $request The user request |
||
278 | * |
||
279 | * @return void |
||
280 | */ |
||
281 | public function query($request) |
||
285 | |||
286 | /** |
||
287 | * Add a filter to where part of the request |
||
288 | * |
||
289 | * @param string $filter The filter to add |
||
290 | * @param array|null $preparedFilters (default: null) Filters to add |
||
291 | * in prepared request |
||
292 | * |
||
293 | * @throws \Exception If key on prepared request is already used |
||
294 | * |
||
295 | * @return \BfwSql\SqlActions |
||
296 | */ |
||
297 | public function where($filter, $preparedFilters = null) |
||
307 | |||
308 | /** |
||
309 | * Add filters to prepared requests |
||
310 | * |
||
311 | * @param array $preparedFilters Filters to add in prepared request |
||
312 | * |
||
313 | * @return void |
||
314 | */ |
||
315 | protected function addPreparedFilters($preparedFilters) |
||
321 | |||
322 | /** |
||
323 | * Write the where part of a sql query and return it |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | protected function generateWhere() |
||
347 | |||
348 | /** |
||
349 | * Add datas to insert or update for a column. |
||
350 | * Used by UPDATE and INSERT requests |
||
351 | * |
||
352 | * @param array $columns Datas to add or update |
||
353 | * Format : array('sqlColumnName' => 'valueForThisColumn', ...); |
||
354 | * |
||
355 | * @return \BfwSql\SqlActions |
||
356 | */ |
||
357 | public function addDatasForColumns(array $columns) |
||
375 | |||
376 | /** |
||
377 | * Send a notify to application observers |
||
378 | * |
||
379 | * @return void |
||
380 | */ |
||
381 | protected function callObserver() |
||
387 | } |
||
388 |