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 |
||
36 | class Connection extends \Doctrine\DBAL\Connection implements IDBConnection { |
||
|
|||
37 | /** |
||
38 | * @var string $tablePrefix |
||
39 | */ |
||
40 | protected $tablePrefix; |
||
41 | |||
42 | /** |
||
43 | * @var \OC\DB\Adapter $adapter |
||
44 | */ |
||
45 | protected $adapter; |
||
46 | |||
47 | 1752 | public function connect() { |
|
55 | |||
56 | /** |
||
57 | * Returns a QueryBuilder for the connection. |
||
58 | * |
||
59 | * @return \OCP\DB\QueryBuilder\IQueryBuilder |
||
60 | */ |
||
61 | 244 | public function getQueryBuilder() { |
|
64 | |||
65 | /** |
||
66 | * Gets the QueryBuilder for the connection. |
||
67 | * |
||
68 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
69 | * @deprecated please use $this->getQueryBuilder() instead |
||
70 | */ |
||
71 | 63 | public function createQueryBuilder() { |
|
76 | |||
77 | /** |
||
78 | * Gets the ExpressionBuilder for the connection. |
||
79 | * |
||
80 | * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
||
81 | * @deprecated please use $this->getQueryBuilder()->expr() instead |
||
82 | */ |
||
83 | 63 | public function getExpressionBuilder() { |
|
88 | |||
89 | /** |
||
90 | * Get the file and line that called the method where `getCallerBacktrace()` was used |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | 63 | protected function getCallerBacktrace() { |
|
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | 1 | public function getPrefix() { |
|
112 | |||
113 | /** |
||
114 | * Initializes a new instance of the Connection class. |
||
115 | * |
||
116 | * @param array $params The connection parameters. |
||
117 | * @param \Doctrine\DBAL\Driver $driver |
||
118 | * @param \Doctrine\DBAL\Configuration $config |
||
119 | * @param \Doctrine\Common\EventManager $eventManager |
||
120 | * @throws \Exception |
||
121 | */ |
||
122 | 12 | public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
137 | |||
138 | /** |
||
139 | * Prepares an SQL statement. |
||
140 | * |
||
141 | * @param string $statement The SQL statement to prepare. |
||
142 | * @param int $limit |
||
143 | * @param int $offset |
||
144 | * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
||
145 | */ |
||
146 | 1591 | public function prepare( $statement, $limit=null, $offset=null ) { |
|
162 | |||
163 | /** |
||
164 | * Executes an, optionally parametrized, SQL query. |
||
165 | * |
||
166 | * If the query is parametrized, a prepared statement is used. |
||
167 | * If an SQLLogger is configured, the execution is logged. |
||
168 | * |
||
169 | * @param string $query The SQL query to execute. |
||
170 | * @param array $params The parameters to bind to the query, if any. |
||
171 | * @param array $types The types the previous parameters are in. |
||
172 | * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
||
173 | * |
||
174 | * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
||
175 | * |
||
176 | * @throws \Doctrine\DBAL\DBALException |
||
177 | */ |
||
178 | 1048 | public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
184 | |||
185 | /** |
||
186 | * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
||
187 | * and returns the number of affected rows. |
||
188 | * |
||
189 | * This method supports PDO binding types as well as DBAL mapping types. |
||
190 | * |
||
191 | * @param string $query The SQL query. |
||
192 | * @param array $params The query parameters. |
||
193 | * @param array $types The parameter types. |
||
194 | * |
||
195 | * @return integer The number of affected rows. |
||
196 | * |
||
197 | * @throws \Doctrine\DBAL\DBALException |
||
198 | */ |
||
199 | 1451 | public function executeUpdate($query, array $params = array(), array $types = array()) |
|
205 | |||
206 | /** |
||
207 | * Returns the ID of the last inserted row, or the last value from a sequence object, |
||
208 | * depending on the underlying driver. |
||
209 | * |
||
210 | * Note: This method may not return a meaningful or consistent result across different drivers, |
||
211 | * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
||
212 | * columns or sequences. |
||
213 | * |
||
214 | * @param string $seqName Name of the sequence object from which the ID should be returned. |
||
215 | * @return string A string representation of the last inserted ID. |
||
216 | */ |
||
217 | 1136 | public function lastInsertId($seqName = null) |
|
224 | |||
225 | // internal use |
||
226 | 1136 | public function realLastInsertId($seqName = null) { |
|
229 | |||
230 | /** |
||
231 | * Insert a row if the matching row does not exists. |
||
232 | * |
||
233 | * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
||
234 | * @param array $input data that should be inserted into the table (column name => value) |
||
235 | * @param array|null $compare List of values that should be checked for "if not exists" |
||
236 | * If this is null or an empty array, all keys of $input will be compared |
||
237 | * Please note: text fields (clob) must not be used in the compare array |
||
238 | * @return int number of inserted rows |
||
239 | * @throws \Doctrine\DBAL\DBALException |
||
240 | */ |
||
241 | 1164 | public function insertIfNotExist($table, $input, array $compare = null) { |
|
244 | |||
245 | /** |
||
246 | * returns the error code and message as a string for logging |
||
247 | * works with DoctrineException |
||
248 | * @return string |
||
249 | */ |
||
250 | public function getError() { |
||
260 | |||
261 | /** |
||
262 | * Drop a table from the database if it exists |
||
263 | * |
||
264 | * @param string $table table name without the prefix |
||
265 | */ |
||
266 | 3 | View Code Duplication | public function dropTable($table) { |
273 | |||
274 | /** |
||
275 | * Check if a table exists |
||
276 | * |
||
277 | * @param string $table table name without the prefix |
||
278 | * @return bool |
||
279 | */ |
||
280 | 2 | public function tableExists($table){ |
|
285 | |||
286 | // internal use |
||
287 | /** |
||
288 | * @param string $statement |
||
289 | * @return string |
||
290 | */ |
||
291 | 1704 | protected function replaceTablePrefix($statement) { |
|
294 | |||
295 | /** |
||
296 | * Check if a transaction is active |
||
297 | * |
||
298 | * @return bool |
||
299 | * @since 8.2.0 |
||
300 | */ |
||
301 | public function inTransaction() { |
||
304 | |||
305 | /** |
||
306 | * Espace a parameter to be used in a LIKE query |
||
307 | * |
||
308 | * @param string $param |
||
309 | * @return string |
||
310 | */ |
||
311 | public function escapeLikeParameter($param) { |
||
314 | } |
||
315 |