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:
Complex classes like DatabaseMssql often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DatabaseMssql, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class DatabaseMssql extends Database { |
||
| 32 | protected $mInsertId = null; |
||
| 33 | protected $mLastResult = null; |
||
| 34 | protected $mAffectedRows = null; |
||
| 35 | protected $mSubqueryId = 0; |
||
| 36 | protected $mScrollableCursor = true; |
||
| 37 | protected $mPrepareStatements = true; |
||
| 38 | protected $mBinaryColumnCache = null; |
||
| 39 | protected $mBitColumnCache = null; |
||
| 40 | protected $mIgnoreDupKeyErrors = false; |
||
| 41 | protected $mIgnoreErrors = []; |
||
| 42 | |||
| 43 | protected $mPort; |
||
| 44 | |||
| 45 | public function cascadingDeletes() { |
||
| 48 | |||
| 49 | public function cleanupTriggers() { |
||
| 52 | |||
| 53 | public function strictIPs() { |
||
| 56 | |||
| 57 | public function realTimestamps() { |
||
| 60 | |||
| 61 | public function implicitGroupby() { |
||
| 64 | |||
| 65 | public function implicitOrderby() { |
||
| 68 | |||
| 69 | public function functionalIndexes() { |
||
| 72 | |||
| 73 | public function unionSupportsOrderAndLimit() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Usually aborts on failure |
||
| 79 | * @param string $server |
||
| 80 | * @param string $user |
||
| 81 | * @param string $password |
||
| 82 | * @param string $dbName |
||
| 83 | * @throws DBConnectionError |
||
| 84 | * @return bool|DatabaseBase|null |
||
| 85 | */ |
||
| 86 | public function open( $server, $user, $password, $dbName ) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Closes a database connection, if it is open |
||
| 138 | * Returns success, true if already closed |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | protected function closeConnection() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param bool|MssqlResultWrapper|resource $result |
||
| 147 | * @return bool|MssqlResultWrapper |
||
| 148 | */ |
||
| 149 | protected function resultObject( $result ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $sql |
||
| 164 | * @return bool|MssqlResult |
||
| 165 | * @throws DBUnexpectedError |
||
| 166 | */ |
||
| 167 | protected function doQuery( $sql ) { |
||
| 241 | |||
| 242 | public function freeResult( $res ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param MssqlResultWrapper $res |
||
| 252 | * @return stdClass |
||
| 253 | */ |
||
| 254 | public function fetchObject( $res ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param MssqlResultWrapper $res |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function fetchRow( $res ) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param mixed $res |
||
| 269 | * @return int |
||
| 270 | */ |
||
| 271 | public function numRows( $res ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param mixed $res |
||
| 289 | * @return int |
||
| 290 | */ |
||
| 291 | public function numFields( $res ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param mixed $res |
||
| 301 | * @param int $n |
||
| 302 | * @return int |
||
| 303 | */ |
||
| 304 | public function fieldName( $res, $n ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * This must be called after nextSequenceVal |
||
| 314 | * @return int|null |
||
| 315 | */ |
||
| 316 | public function insertId() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param MssqlResultWrapper $res |
||
| 322 | * @param int $row |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | public function dataSeek( $res, $row ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function lastError() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param array $err |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | private function formatError( $err ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function lastErrno() { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return int |
||
| 368 | */ |
||
| 369 | public function affectedRows() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * SELECT wrapper |
||
| 375 | * |
||
| 376 | * @param mixed $table Array or string, table name(s) (prefix auto-added) |
||
| 377 | * @param mixed $vars Array or string, field name(s) to be retrieved |
||
| 378 | * @param mixed $conds Array or string, condition(s) for WHERE |
||
| 379 | * @param string $fname Calling function name (use __METHOD__) for logs/profiling |
||
| 380 | * @param array $options Associative array of options (e.g. |
||
| 381 | * array('GROUP BY' => 'page_title')), see Database::makeSelectOptions |
||
| 382 | * code for list of supported stuff |
||
| 383 | * @param array $join_conds Associative array of table join conditions |
||
| 384 | * (optional) (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') ) |
||
| 385 | * @return mixed Database result resource (feed to Database::fetchObject |
||
| 386 | * or whatever), or false on failure |
||
| 387 | * @throws DBQueryError |
||
| 388 | * @throws DBUnexpectedError |
||
| 389 | * @throws Exception |
||
| 390 | */ |
||
| 391 | public function select( $table, $vars, $conds = '', $fname = __METHOD__, |
||
| 429 | |||
| 430 | /** |
||
| 431 | * SELECT wrapper |
||
| 432 | * |
||
| 433 | * @param mixed $table Array or string, table name(s) (prefix auto-added) |
||
| 434 | * @param mixed $vars Array or string, field name(s) to be retrieved |
||
| 435 | * @param mixed $conds Array or string, condition(s) for WHERE |
||
| 436 | * @param string $fname Calling function name (use __METHOD__) for logs/profiling |
||
| 437 | * @param array $options Associative array of options (e.g. array('GROUP BY' => 'page_title')), |
||
| 438 | * see Database::makeSelectOptions code for list of supported stuff |
||
| 439 | * @param array $join_conds Associative array of table join conditions (optional) |
||
| 440 | * (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') ) |
||
| 441 | * @return string The SQL text |
||
| 442 | */ |
||
| 443 | public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__, |
||
| 474 | |||
| 475 | View Code Duplication | public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, |
|
| 487 | |||
| 488 | View Code Duplication | public function delete( $table, $conds, $fname = __METHOD__ ) { |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Estimate rows in dataset |
||
| 501 | * Returns estimated count, based on SHOWPLAN_ALL output |
||
| 502 | * This is not necessarily an accurate estimate, so use sparingly |
||
| 503 | * Returns -1 if count cannot be found |
||
| 504 | * Takes same arguments as Database::select() |
||
| 505 | * @param string $table |
||
| 506 | * @param string $vars |
||
| 507 | * @param string $conds |
||
| 508 | * @param string $fname |
||
| 509 | * @param array $options |
||
| 510 | * @return int |
||
| 511 | */ |
||
| 512 | View Code Duplication | public function estimateRowCount( $table, $vars = '*', $conds = '', |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Returns information about an index |
||
| 534 | * If errors are explicitly ignored, returns NULL on failure |
||
| 535 | * @param string $table |
||
| 536 | * @param string $index |
||
| 537 | * @param string $fname |
||
| 538 | * @return array|bool|null |
||
| 539 | */ |
||
| 540 | public function indexInfo( $table, $index, $fname = __METHOD__ ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * INSERT wrapper, inserts an array into a table |
||
| 575 | * |
||
| 576 | * $arrToInsert may be a single associative array, or an array of these with numeric keys, for |
||
| 577 | * multi-row insert. |
||
| 578 | * |
||
| 579 | * Usually aborts on failure |
||
| 580 | * If errors are explicitly ignored, returns success |
||
| 581 | * @param string $table |
||
| 582 | * @param array $arrToInsert |
||
| 583 | * @param string $fname |
||
| 584 | * @param array $options |
||
| 585 | * @return bool |
||
| 586 | * @throws Exception |
||
| 587 | */ |
||
| 588 | public function insert( $table, $arrToInsert, $fname = __METHOD__, $options = [] ) { |
||
| 717 | |||
| 718 | /** |
||
| 719 | * INSERT SELECT wrapper |
||
| 720 | * $varMap must be an associative array of the form array( 'dest1' => 'source1', ...) |
||
| 721 | * Source items may be literals rather than field names, but strings should |
||
| 722 | * be quoted with Database::addQuotes(). |
||
| 723 | * @param string $destTable |
||
| 724 | * @param array|string $srcTable May be an array of tables. |
||
| 725 | * @param array $varMap |
||
| 726 | * @param array $conds May be "*" to copy the whole table. |
||
| 727 | * @param string $fname |
||
| 728 | * @param array $insertOptions |
||
| 729 | * @param array $selectOptions |
||
| 730 | * @return null|ResultWrapper |
||
| 731 | * @throws Exception |
||
| 732 | */ |
||
| 733 | public function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__, |
||
| 755 | |||
| 756 | /** |
||
| 757 | * UPDATE wrapper. Takes a condition array and a SET array. |
||
| 758 | * |
||
| 759 | * @param string $table Name of the table to UPDATE. This will be passed through |
||
| 760 | * DatabaseBase::tableName(). |
||
| 761 | * |
||
| 762 | * @param array $values An array of values to SET. For each array element, |
||
| 763 | * the key gives the field name, and the value gives the data |
||
| 764 | * to set that field to. The data will be quoted by |
||
| 765 | * DatabaseBase::addQuotes(). |
||
| 766 | * |
||
| 767 | * @param array $conds An array of conditions (WHERE). See |
||
| 768 | * DatabaseBase::select() for the details of the format of |
||
| 769 | * condition arrays. Use '*' to update all rows. |
||
| 770 | * |
||
| 771 | * @param string $fname The function name of the caller (from __METHOD__), |
||
| 772 | * for logging and profiling. |
||
| 773 | * |
||
| 774 | * @param array $options An array of UPDATE options, can be: |
||
| 775 | * - IGNORE: Ignore unique key conflicts |
||
| 776 | * - LOW_PRIORITY: MySQL-specific, see MySQL manual. |
||
| 777 | * @return bool |
||
| 778 | * @throws DBUnexpectedError |
||
| 779 | * @throws Exception |
||
| 780 | * @throws MWException |
||
| 781 | */ |
||
| 782 | function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) { |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Makes an encoded list of strings from an array |
||
| 806 | * @param array $a Containing the data |
||
| 807 | * @param int $mode Constant |
||
| 808 | * - LIST_COMMA: comma separated, no field names |
||
| 809 | * - LIST_AND: ANDed WHERE clause (without the WHERE). See |
||
| 810 | * the documentation for $conds in DatabaseBase::select(). |
||
| 811 | * - LIST_OR: ORed WHERE clause (without the WHERE) |
||
| 812 | * - LIST_SET: comma separated with field names, like a SET clause |
||
| 813 | * - LIST_NAMES: comma separated field names |
||
| 814 | * @param array $binaryColumns Contains a list of column names that are binary types |
||
| 815 | * This is a custom parameter only present for MS SQL. |
||
| 816 | * |
||
| 817 | * @throws MWException|DBUnexpectedError |
||
| 818 | * @return string |
||
| 819 | */ |
||
| 820 | public function makeList( $a, $mode = LIST_COMMA, $binaryColumns = [] ) { |
||
| 848 | |||
| 849 | /** |
||
| 850 | * @param string $table |
||
| 851 | * @param string $field |
||
| 852 | * @return int Returns the size of a text field, or -1 for "unlimited" |
||
| 853 | */ |
||
| 854 | public function textFieldSize( $table, $field ) { |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Construct a LIMIT query with optional offset |
||
| 870 | * This is used for query pages |
||
| 871 | * |
||
| 872 | * @param string $sql SQL query we will append the limit too |
||
| 873 | * @param int $limit The SQL limit |
||
| 874 | * @param bool|int $offset The SQL offset (default false) |
||
| 875 | * @return array|string |
||
| 876 | * @throws DBUnexpectedError |
||
| 877 | */ |
||
| 878 | public function limitResult( $sql, $limit, $offset = false ) { |
||
| 922 | |||
| 923 | /** |
||
| 924 | * If there is a limit clause, parse it, strip it, and pass the remaining |
||
| 925 | * SQL through limitResult() with the appropriate parameters. Not the |
||
| 926 | * prettiest solution, but better than building a whole new parser. This |
||
| 927 | * exists becase there are still too many extensions that don't use dynamic |
||
| 928 | * sql generation. |
||
| 929 | * |
||
| 930 | * @param string $sql |
||
| 931 | * @return array|mixed|string |
||
| 932 | */ |
||
| 933 | public function LimitToTopN( $sql ) { |
||
| 948 | |||
| 949 | /** |
||
| 950 | * @return string Wikitext of a link to the server software's web site |
||
| 951 | */ |
||
| 952 | public function getSoftwareLink() { |
||
| 955 | |||
| 956 | /** |
||
| 957 | * @return string Version information from the database |
||
| 958 | */ |
||
| 959 | public function getServerVersion() { |
||
| 968 | |||
| 969 | /** |
||
| 970 | * @param string $table |
||
| 971 | * @param string $fname |
||
| 972 | * @return bool |
||
| 973 | */ |
||
| 974 | public function tableExists( $table, $fname = __METHOD__ ) { |
||
| 998 | |||
| 999 | /** |
||
| 1000 | * Query whether a given column exists in the mediawiki schema |
||
| 1001 | * @param string $table |
||
| 1002 | * @param string $field |
||
| 1003 | * @param string $fname |
||
| 1004 | * @return bool |
||
| 1005 | */ |
||
| 1006 | public function fieldExists( $table, $field, $fname = __METHOD__ ) { |
||
| 1024 | |||
| 1025 | public function fieldInfo( $table, $field ) { |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Begin a transaction, committing any previously open transaction |
||
| 1047 | * @param string $fname |
||
| 1048 | */ |
||
| 1049 | protected function doBegin( $fname = __METHOD__ ) { |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * End a transaction |
||
| 1056 | * @param string $fname |
||
| 1057 | */ |
||
| 1058 | protected function doCommit( $fname = __METHOD__ ) { |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Rollback a transaction. |
||
| 1065 | * No-op on non-transactional databases. |
||
| 1066 | * @param string $fname |
||
| 1067 | */ |
||
| 1068 | protected function doRollback( $fname = __METHOD__ ) { |
||
| 1072 | |||
| 1073 | /** |
||
| 1074 | * Escapes a identifier for use inm SQL. |
||
| 1075 | * Throws an exception if it is invalid. |
||
| 1076 | * Reference: http://msdn.microsoft.com/en-us/library/aa224033%28v=SQL.80%29.aspx |
||
| 1077 | * @param string $identifier |
||
| 1078 | * @throws MWException |
||
| 1079 | * @return string |
||
| 1080 | */ |
||
| 1081 | private function escapeIdentifier( $identifier ) { |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * @param string $s |
||
| 1101 | * @return string |
||
| 1102 | */ |
||
| 1103 | public function strencode( $s ) { |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * @param string|Blob $s |
||
| 1111 | * @return string |
||
| 1112 | */ |
||
| 1113 | public function addQuotes( $s ) { |
||
| 1128 | |||
| 1129 | /** |
||
| 1130 | * @param string $s |
||
| 1131 | * @return string |
||
| 1132 | */ |
||
| 1133 | public function addIdentifierQuotes( $s ) { |
||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * @param string $name |
||
| 1140 | * @return bool |
||
| 1141 | */ |
||
| 1142 | public function isQuotedIdentifier( $name ) { |
||
| 1145 | |||
| 1146 | /** |
||
| 1147 | * MS SQL supports more pattern operators than other databases (ex: [,],^) |
||
| 1148 | * |
||
| 1149 | * @param string $s |
||
| 1150 | * @return string |
||
| 1151 | */ |
||
| 1152 | protected function escapeLikeInternal( $s ) { |
||
| 1155 | |||
| 1156 | /** |
||
| 1157 | * MS SQL requires specifying the escape character used in a LIKE query |
||
| 1158 | * or using Square brackets to surround characters that are to be escaped |
||
| 1159 | * http://msdn.microsoft.com/en-us/library/ms179859.aspx |
||
| 1160 | * Here we take the Specify-Escape-Character approach since it's less |
||
| 1161 | * invasive, renders a query that is closer to other DB's and better at |
||
| 1162 | * handling square bracket escaping |
||
| 1163 | * |
||
| 1164 | * @return string Fully built LIKE statement |
||
| 1165 | */ |
||
| 1166 | View Code Duplication | public function buildLike() { |
|
| 1174 | |||
| 1175 | /** |
||
| 1176 | * @param string $db |
||
| 1177 | * @return bool |
||
| 1178 | */ |
||
| 1179 | public function selectDB( $db ) { |
||
| 1188 | |||
| 1189 | /** |
||
| 1190 | * @param array $options An associative array of options to be turned into |
||
| 1191 | * an SQL query, valid keys are listed in the function. |
||
| 1192 | * @return array |
||
| 1193 | */ |
||
| 1194 | public function makeSelectOptions( $options ) { |
||
| 1221 | |||
| 1222 | /** |
||
| 1223 | * Get the type of the DBMS, as it appears in $wgDBtype. |
||
| 1224 | * @return string |
||
| 1225 | */ |
||
| 1226 | public function getType() { |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * @param array $stringList |
||
| 1232 | * @return string |
||
| 1233 | */ |
||
| 1234 | public function buildConcat( $stringList ) { |
||
| 1237 | |||
| 1238 | /** |
||
| 1239 | * Build a GROUP_CONCAT or equivalent statement for a query. |
||
| 1240 | * MS SQL doesn't have GROUP_CONCAT so we emulate it with other stuff (and boy is it nasty) |
||
| 1241 | * |
||
| 1242 | * This is useful for combining a field for several rows into a single string. |
||
| 1243 | * NULL values will not appear in the output, duplicated values will appear, |
||
| 1244 | * and the resulting delimiter-separated values have no defined sort order. |
||
| 1245 | * Code using the results may need to use the PHP unique() or sort() methods. |
||
| 1246 | * |
||
| 1247 | * @param string $delim Glue to bind the results together |
||
| 1248 | * @param string|array $table Table name |
||
| 1249 | * @param string $field Field name |
||
| 1250 | * @param string|array $conds Conditions |
||
| 1251 | * @param string|array $join_conds Join conditions |
||
| 1252 | * @return string SQL text |
||
| 1253 | * @since 1.23 |
||
| 1254 | */ |
||
| 1255 | public function buildGroupConcatField( $delim, $table, $field, $conds = '', |
||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * @return string |
||
| 1272 | */ |
||
| 1273 | public function getSearchEngine() { |
||
| 1276 | |||
| 1277 | /** |
||
| 1278 | * Returns an associative array for fields that are of type varbinary, binary, or image |
||
| 1279 | * $table can be either a raw table name or passed through tableName() first |
||
| 1280 | * @param string $table |
||
| 1281 | * @return array |
||
| 1282 | */ |
||
| 1283 | View Code Duplication | private function getBinaryColumns( $table ) { |
|
| 1295 | |||
| 1296 | /** |
||
| 1297 | * @param string $table |
||
| 1298 | * @return array |
||
| 1299 | */ |
||
| 1300 | View Code Duplication | private function getBitColumns( $table ) { |
|
| 1312 | |||
| 1313 | private function populateColumnCaches() { |
||
| 1331 | |||
| 1332 | /** |
||
| 1333 | * @param string $name |
||
| 1334 | * @param string $format |
||
| 1335 | * @return string |
||
| 1336 | */ |
||
| 1337 | View Code Duplication | function tableName( $name, $format = 'quoted' ) { |
|
| 1346 | |||
| 1347 | /** |
||
| 1348 | * call this instead of tableName() in the updater when renaming tables |
||
| 1349 | * @param string $name |
||
| 1350 | * @param string $format One of quoted, raw, or split |
||
| 1351 | * @return string |
||
| 1352 | */ |
||
| 1353 | function realTableName( $name, $format = 'quoted' ) { |
||
| 1365 | |||
| 1366 | /** |
||
| 1367 | * Delete a table |
||
| 1368 | * @param string $tableName |
||
| 1369 | * @param string $fName |
||
| 1370 | * @return bool|ResultWrapper |
||
| 1371 | * @since 1.18 |
||
| 1372 | */ |
||
| 1373 | View Code Duplication | public function dropTable( $tableName, $fName = __METHOD__ ) { |
|
| 1383 | |||
| 1384 | /** |
||
| 1385 | * Called in the installer and updater. |
||
| 1386 | * Probably doesn't need to be called anywhere else in the codebase. |
||
| 1387 | * @param bool|null $value |
||
| 1388 | * @return bool|null |
||
| 1389 | */ |
||
| 1390 | public function prepareStatements( $value = null ) { |
||
| 1393 | |||
| 1394 | /** |
||
| 1395 | * Called in the installer and updater. |
||
| 1396 | * Probably doesn't need to be called anywhere else in the codebase. |
||
| 1397 | * @param bool|null $value |
||
| 1398 | * @return bool|null |
||
| 1399 | */ |
||
| 1400 | public function scrollableCursor( $value = null ) { |
||
| 1403 | |||
| 1404 | /** |
||
| 1405 | * Called in the installer and updater. |
||
| 1406 | * Probably doesn't need to be called anywhere else in the codebase. |
||
| 1407 | * @param array|null $value |
||
| 1408 | * @return array|null |
||
| 1409 | */ |
||
| 1410 | public function ignoreErrors( array $value = null ) { |
||
| 1413 | } // end DatabaseMssql class |
||
| 1414 | |||
| 1559 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: