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 | * Define driver options to prepared request |
||
106 | * |
||
107 | * @link http://php.net/manual/fr/pdo.prepare.php |
||
108 | * |
||
109 | * @param array $driverOptions Drivers options |
||
110 | * |
||
111 | * @return \BfwSql\SqlActions |
||
112 | */ |
||
113 | public function setPrepareDriversOptions($driverOptions) |
||
119 | |||
120 | /** |
||
121 | * Check if a request is assemble or not. |
||
122 | * If not, run the method assembleRequest. |
||
123 | * |
||
124 | * @return boolean |
||
125 | */ |
||
126 | public function isAssembled() |
||
134 | |||
135 | /** |
||
136 | * Write the query |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | protected abstract function assembleRequest(); |
||
141 | |||
142 | /** |
||
143 | * Return the assembled request |
||
144 | * |
||
145 | * @param boolean $force : Force to re-assemble request |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | public function assemble($force = false) |
||
157 | |||
158 | /** |
||
159 | * Execute the assembled request |
||
160 | * |
||
161 | * @return array The pdo errorInfo array |
||
162 | */ |
||
163 | protected function executeQuery() |
||
191 | |||
192 | /** |
||
193 | * Execute the assembled request and check if there are errors |
||
194 | * Update property noResult |
||
195 | * |
||
196 | * @throws \Exception If the request fail |
||
197 | * |
||
198 | * @return \PDOStatement|integer |
||
199 | */ |
||
200 | public function execute() |
||
222 | |||
223 | /** |
||
224 | * Closes the cursor, enabling the statement to be executed again. |
||
225 | * |
||
226 | * @link http://php.net/manual/fr/pdostatement.closecursor.php |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | public function closeCursor() |
||
234 | |||
235 | /** |
||
236 | * Return the number of impacted rows by the last request |
||
237 | * |
||
238 | * @return int|bool |
||
239 | */ |
||
240 | public function obtainImpactedRows() |
||
253 | |||
254 | /** |
||
255 | * To call this own request without use query writer |
||
256 | * |
||
257 | * @param string $request The user request |
||
258 | * |
||
259 | * @return void |
||
260 | */ |
||
261 | public function query($request) |
||
265 | |||
266 | /** |
||
267 | * Add a filter to where part of the request |
||
268 | * |
||
269 | * @param string $filter The filter to add |
||
270 | * @param array|null $preparedFilters (default: null) Filters to add |
||
271 | * in prepared request |
||
272 | * |
||
273 | * @throws \Exception If key on prepared request is already used |
||
274 | * |
||
275 | * @return \BfwSql\SqlActions |
||
276 | */ |
||
277 | public function where($filter, $preparedFilters = null) |
||
287 | |||
288 | /** |
||
289 | * Add filters to prepared requests |
||
290 | * |
||
291 | * @param array $preparedFilters Filters to add in prepared request |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | protected function addPreparedFilters($preparedFilters) |
||
301 | |||
302 | /** |
||
303 | * Write the where part of a sql query and return it |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | protected function generateWhere() |
||
327 | |||
328 | /** |
||
329 | * Add datas to insert or update for a column. |
||
330 | * Used by UPDATE and INSERT requests |
||
331 | * |
||
332 | * @param array $columns Datas to add or update |
||
333 | * Format : array('sqlColumnName' => 'valueForThisColumn', ...); |
||
334 | * |
||
335 | * @return \BfwSql\SqlActions |
||
336 | */ |
||
337 | public function addDatasForColumns(array $columns) |
||
355 | |||
356 | /** |
||
357 | * Send a notify to application observers |
||
358 | * |
||
359 | * @return void |
||
360 | */ |
||
361 | protected function callObserver() |
||
367 | } |
||
368 |