Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
26 | class Driver extends \Query\AbstractDriver { |
||
27 | |||
28 | /** |
||
29 | * Reference to the last query executed |
||
30 | * |
||
31 | * @var object |
||
32 | */ |
||
33 | protected $statement = NULL; |
||
34 | |||
35 | /** |
||
36 | * Reference to the resource returned by |
||
37 | * the last query executed |
||
38 | * |
||
39 | * @var resource |
||
40 | */ |
||
41 | protected $statement_link = NULL; |
||
42 | |||
43 | /** |
||
44 | * Reference to the current transaction |
||
45 | * |
||
46 | * @var resource |
||
47 | */ |
||
48 | protected $trans = NULL; |
||
49 | |||
50 | /** |
||
51 | * Reference to the connection resource |
||
52 | * |
||
53 | * @var resource |
||
54 | */ |
||
55 | protected $conn = NULL; |
||
56 | |||
57 | /** |
||
58 | * Reference to the service resource |
||
59 | * |
||
60 | * @var resource |
||
61 | */ |
||
62 | protected $service = NULL; |
||
63 | |||
64 | /** |
||
65 | * Firebird doesn't have the truncate keyword |
||
66 | * |
||
67 | * @var bool |
||
68 | */ |
||
69 | protected $has_truncate = FALSE; |
||
70 | |||
71 | /** |
||
72 | * Open the link to the database |
||
73 | * |
||
74 | * @param string $dbpath |
||
75 | * @param string $user |
||
76 | * @param string $pass |
||
77 | * @param array $options |
||
78 | * @throws \PDOException |
||
79 | */ |
||
80 | public function __construct($dbpath, $user='SYSDBA', $pass='masterkey', array $options = array()) |
||
101 | |||
102 | // -------------------------------------------------------------------------- |
||
103 | |||
104 | /** |
||
105 | * Cleanup some loose ends |
||
106 | * @codeCoverageIgnore |
||
107 | */ |
||
108 | public function __destruct() |
||
112 | |||
113 | // -------------------------------------------------------------------------- |
||
114 | |||
115 | /** |
||
116 | * Return service handle |
||
117 | * |
||
118 | * @return resource |
||
119 | */ |
||
120 | public function get_service() |
||
124 | |||
125 | |||
126 | // -------------------------------------------------------------------------- |
||
127 | |||
128 | /** |
||
129 | * Execute an sql statement and return number of affected rows |
||
130 | * |
||
131 | * @param string $sql |
||
132 | * @return int |
||
133 | */ |
||
134 | public function exec($sql) |
||
138 | |||
139 | // -------------------------------------------------------------------------- |
||
140 | |||
141 | /** |
||
142 | * Implement for compatibility with PDO |
||
143 | * |
||
144 | * @param int $attribute |
||
145 | * @return mixed |
||
146 | */ |
||
147 | public function getAttribute($attribute) |
||
151 | |||
152 | // -------------------------------------------------------------------------- |
||
153 | |||
154 | /** |
||
155 | * Return whether the current statement is in a transaction |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function inTransaction() |
||
163 | |||
164 | // -------------------------------------------------------------------------- |
||
165 | |||
166 | /** |
||
167 | * Returns the last value of the specified generator |
||
168 | * |
||
169 | * @param string $name |
||
170 | * @return mixed |
||
171 | */ |
||
172 | public function lastInsertId($name = NULL) |
||
176 | |||
177 | // -------------------------------------------------------------------------- |
||
178 | |||
179 | /** |
||
180 | * Wrapper public function to better match PDO |
||
181 | * |
||
182 | * @param string $sql |
||
183 | * @return Result |
||
184 | * @throws PDOException |
||
185 | */ |
||
186 | public function query($sql = '') |
||
209 | |||
210 | // -------------------------------------------------------------------------- |
||
211 | |||
212 | /** |
||
213 | * Emulate PDO prepare |
||
214 | * |
||
215 | * @param string $query |
||
216 | * @param array $options |
||
217 | * @return Result |
||
218 | * @throws \PDOException |
||
219 | */ |
||
220 | public function prepare($query, $options=array()) |
||
234 | |||
235 | // -------------------------------------------------------------------------- |
||
236 | |||
237 | /** |
||
238 | * Start a database transaction |
||
239 | * |
||
240 | * @return boolean|null |
||
241 | */ |
||
242 | public function beginTransaction() |
||
246 | |||
247 | // -------------------------------------------------------------------------- |
||
248 | |||
249 | /** |
||
250 | * Commit a database transaction |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function commit() |
||
260 | |||
261 | // -------------------------------------------------------------------------- |
||
262 | |||
263 | /** |
||
264 | * Rollback a transaction |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function rollBack() |
||
274 | |||
275 | // -------------------------------------------------------------------------- |
||
276 | |||
277 | /** |
||
278 | * Set a connection attribute |
||
279 | * @param int $attribute |
||
280 | * @param mixed $value |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function setAttribute($attribute, $value) |
||
287 | |||
288 | // -------------------------------------------------------------------------- |
||
289 | |||
290 | /** |
||
291 | * Prepare and execute a query |
||
292 | * |
||
293 | * @param string $sql |
||
294 | * @param array $args |
||
295 | * @return Result |
||
296 | */ |
||
297 | public function prepare_execute($sql, $args) |
||
306 | |||
307 | // -------------------------------------------------------------------------- |
||
308 | |||
309 | /** |
||
310 | * Method to emulate PDO->quote |
||
311 | * |
||
312 | * @param string $str |
||
313 | * @param int $param_type |
||
314 | * @return string |
||
315 | */ |
||
316 | public function quote($str, $param_type = \PDO::PARAM_STR) |
||
325 | |||
326 | // -------------------------------------------------------------------------- |
||
327 | |||
328 | /** |
||
329 | * Method to emulate PDO->errorInfo / PDOStatement->errorInfo |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | public function errorInfo() |
||
340 | |||
341 | // -------------------------------------------------------------------------- |
||
342 | |||
343 | /** |
||
344 | * Method to emulate PDO->errorCode |
||
345 | * |
||
346 | * @return array |
||
347 | */ |
||
348 | public function errorCode() |
||
352 | |||
353 | // -------------------------------------------------------------------------- |
||
354 | |||
355 | /** |
||
356 | * Bind a prepared query with arguments for executing |
||
357 | * |
||
358 | * @param string $sql |
||
359 | * @param array $params |
||
360 | * @return NULL |
||
361 | */ |
||
362 | public function prepare_query($sql, $params) |
||
368 | |||
369 | // -------------------------------------------------------------------------- |
||
370 | |||
371 | /** |
||
372 | * Create sql for batch insert |
||
373 | * |
||
374 | * @param string $table |
||
375 | * @param array $data |
||
376 | * @return array |
||
377 | */ |
||
378 | public function insert_batch($table, $data=array()) |
||
413 | } |
||
414 | // End of firebird_driver.php |