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 | |||
| 42 | protected $mPort; |
||
| 43 | |||
| 44 | public function cascadingDeletes() { |
||
| 47 | |||
| 48 | public function cleanupTriggers() { |
||
| 51 | |||
| 52 | public function strictIPs() { |
||
| 55 | |||
| 56 | public function realTimestamps() { |
||
| 59 | |||
| 60 | public function implicitGroupby() { |
||
| 63 | |||
| 64 | public function implicitOrderby() { |
||
| 67 | |||
| 68 | public function functionalIndexes() { |
||
| 71 | |||
| 72 | public function unionSupportsOrderAndLimit() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Usually aborts on failure |
||
| 78 | * @param string $server |
||
| 79 | * @param string $user |
||
| 80 | * @param string $password |
||
| 81 | * @param string $dbName |
||
| 82 | * @throws DBConnectionError |
||
| 83 | * @return bool|DatabaseBase|null |
||
| 84 | */ |
||
| 85 | public function open( $server, $user, $password, $dbName ) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Closes a database connection, if it is open |
||
| 137 | * Returns success, true if already closed |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | protected function closeConnection() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param bool|MssqlResultWrapper|resource $result |
||
| 146 | * @return bool|MssqlResultWrapper |
||
| 147 | */ |
||
| 148 | protected function resultObject( $result ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $sql |
||
| 163 | * @return bool|MssqlResult |
||
| 164 | * @throws DBUnexpectedError |
||
| 165 | */ |
||
| 166 | protected function doQuery( $sql ) { |
||
| 244 | |||
| 245 | public function freeResult( $res ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param MssqlResultWrapper $res |
||
| 255 | * @return stdClass |
||
| 256 | */ |
||
| 257 | public function fetchObject( $res ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param MssqlResultWrapper $res |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function fetchRow( $res ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param mixed $res |
||
| 272 | * @return int |
||
| 273 | */ |
||
| 274 | public function numRows( $res ) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param mixed $res |
||
| 284 | * @return int |
||
| 285 | */ |
||
| 286 | public function numFields( $res ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param mixed $res |
||
| 296 | * @param int $n |
||
| 297 | * @return int |
||
| 298 | */ |
||
| 299 | public function fieldName( $res, $n ) { |
||
| 300 | if ( $res instanceof ResultWrapper ) { |
||
| 301 | $res = $res->result; |
||
| 302 | } |
||
| 303 | |||
| 304 | return sqlsrv_field_metadata( $res )[$n]['Name']; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * This must be called after nextSequenceVal |
||
| 309 | * @return int|null |
||
| 310 | */ |
||
| 311 | public function insertId() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param MssqlResultWrapper $res |
||
| 317 | * @param int $row |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | public function dataSeek( $res, $row ) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function lastError() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param array $err |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | private function formatError( $err ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | public function lastErrno() { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @return int |
||
| 363 | */ |
||
| 364 | public function affectedRows() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * SELECT wrapper |
||
| 370 | * |
||
| 371 | * @param mixed $table Array or string, table name(s) (prefix auto-added) |
||
| 372 | * @param mixed $vars Array or string, field name(s) to be retrieved |
||
| 373 | * @param mixed $conds Array or string, condition(s) for WHERE |
||
| 374 | * @param string $fname Calling function name (use __METHOD__) for logs/profiling |
||
| 375 | * @param array $options Associative array of options (e.g. |
||
| 376 | * array('GROUP BY' => 'page_title')), see Database::makeSelectOptions |
||
| 377 | * code for list of supported stuff |
||
| 378 | * @param array $join_conds Associative array of table join conditions |
||
| 379 | * (optional) (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') ) |
||
| 380 | * @return mixed Database result resource (feed to Database::fetchObject |
||
| 381 | * or whatever), or false on failure |
||
| 382 | * @throws DBQueryError |
||
| 383 | * @throws DBUnexpectedError |
||
| 384 | * @throws Exception |
||
| 385 | */ |
||
| 386 | public function select( $table, $vars, $conds = '', $fname = __METHOD__, |
||
| 424 | |||
| 425 | /** |
||
| 426 | * SELECT wrapper |
||
| 427 | * |
||
| 428 | * @param mixed $table Array or string, table name(s) (prefix auto-added) |
||
| 429 | * @param mixed $vars Array or string, field name(s) to be retrieved |
||
| 430 | * @param mixed $conds Array or string, condition(s) for WHERE |
||
| 431 | * @param string $fname Calling function name (use __METHOD__) for logs/profiling |
||
| 432 | * @param array $options Associative array of options (e.g. array('GROUP BY' => 'page_title')), |
||
| 433 | * see Database::makeSelectOptions code for list of supported stuff |
||
| 434 | * @param array $join_conds Associative array of table join conditions (optional) |
||
| 435 | * (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') ) |
||
| 436 | * @return string The SQL text |
||
| 437 | */ |
||
| 438 | public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__, |
||
| 469 | |||
| 470 | View Code Duplication | public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, |
|
| 482 | |||
| 483 | View Code Duplication | public function delete( $table, $conds, $fname = __METHOD__ ) { |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Estimate rows in dataset |
||
| 496 | * Returns estimated count, based on SHOWPLAN_ALL output |
||
| 497 | * This is not necessarily an accurate estimate, so use sparingly |
||
| 498 | * Returns -1 if count cannot be found |
||
| 499 | * Takes same arguments as Database::select() |
||
| 500 | * @param string $table |
||
| 501 | * @param string $vars |
||
| 502 | * @param string $conds |
||
| 503 | * @param string $fname |
||
| 504 | * @param array $options |
||
| 505 | * @return int |
||
| 506 | */ |
||
| 507 | View Code Duplication | public function estimateRowCount( $table, $vars = '*', $conds = '', |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Returns information about an index |
||
| 529 | * If errors are explicitly ignored, returns NULL on failure |
||
| 530 | * @param string $table |
||
| 531 | * @param string $index |
||
| 532 | * @param string $fname |
||
| 533 | * @return array|bool|null |
||
| 534 | */ |
||
| 535 | public function indexInfo( $table, $index, $fname = __METHOD__ ) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * INSERT wrapper, inserts an array into a table |
||
| 569 | * |
||
| 570 | * $arrToInsert may be a single associative array, or an array of these with numeric keys, for |
||
| 571 | * multi-row insert. |
||
| 572 | * |
||
| 573 | * Usually aborts on failure |
||
| 574 | * If errors are explicitly ignored, returns success |
||
| 575 | * @param string $table |
||
| 576 | * @param array $arrToInsert |
||
| 577 | * @param string $fname |
||
| 578 | * @param array $options |
||
| 579 | * @return bool |
||
| 580 | * @throws Exception |
||
| 581 | */ |
||
| 582 | public function insert( $table, $arrToInsert, $fname = __METHOD__, $options = [] ) { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * INSERT SELECT wrapper |
||
| 708 | * $varMap must be an associative array of the form array( 'dest1' => 'source1', ...) |
||
| 709 | * Source items may be literals rather than field names, but strings should |
||
| 710 | * be quoted with Database::addQuotes(). |
||
| 711 | * @param string $destTable |
||
| 712 | * @param array|string $srcTable May be an array of tables. |
||
| 713 | * @param array $varMap |
||
| 714 | * @param array $conds May be "*" to copy the whole table. |
||
| 715 | * @param string $fname |
||
| 716 | * @param array $insertOptions |
||
| 717 | * @param array $selectOptions |
||
| 718 | * @return null|ResultWrapper |
||
| 719 | * @throws Exception |
||
| 720 | */ |
||
| 721 | public function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__, |
||
| 743 | |||
| 744 | /** |
||
| 745 | * UPDATE wrapper. Takes a condition array and a SET array. |
||
| 746 | * |
||
| 747 | * @param string $table Name of the table to UPDATE. This will be passed through |
||
| 748 | * DatabaseBase::tableName(). |
||
| 749 | * |
||
| 750 | * @param array $values An array of values to SET. For each array element, |
||
| 751 | * the key gives the field name, and the value gives the data |
||
| 752 | * to set that field to. The data will be quoted by |
||
| 753 | * DatabaseBase::addQuotes(). |
||
| 754 | * |
||
| 755 | * @param array $conds An array of conditions (WHERE). See |
||
| 756 | * DatabaseBase::select() for the details of the format of |
||
| 757 | * condition arrays. Use '*' to update all rows. |
||
| 758 | * |
||
| 759 | * @param string $fname The function name of the caller (from __METHOD__), |
||
| 760 | * for logging and profiling. |
||
| 761 | * |
||
| 762 | * @param array $options An array of UPDATE options, can be: |
||
| 763 | * - IGNORE: Ignore unique key conflicts |
||
| 764 | * - LOW_PRIORITY: MySQL-specific, see MySQL manual. |
||
| 765 | * @return bool |
||
| 766 | * @throws DBUnexpectedError |
||
| 767 | * @throws Exception |
||
| 768 | * @throws MWException |
||
| 769 | */ |
||
| 770 | function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) { |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Makes an encoded list of strings from an array |
||
| 794 | * @param array $a Containing the data |
||
| 795 | * @param int $mode Constant |
||
| 796 | * - LIST_COMMA: comma separated, no field names |
||
| 797 | * - LIST_AND: ANDed WHERE clause (without the WHERE). See |
||
| 798 | * the documentation for $conds in DatabaseBase::select(). |
||
| 799 | * - LIST_OR: ORed WHERE clause (without the WHERE) |
||
| 800 | * - LIST_SET: comma separated with field names, like a SET clause |
||
| 801 | * - LIST_NAMES: comma separated field names |
||
| 802 | * @param array $binaryColumns Contains a list of column names that are binary types |
||
| 803 | * This is a custom parameter only present for MS SQL. |
||
| 804 | * |
||
| 805 | * @throws MWException|DBUnexpectedError |
||
| 806 | * @return string |
||
| 807 | */ |
||
| 808 | public function makeList( $a, $mode = LIST_COMMA, $binaryColumns = [] ) { |
||
| 836 | |||
| 837 | /** |
||
| 838 | * @param string $table |
||
| 839 | * @param string $field |
||
| 840 | * @return int Returns the size of a text field, or -1 for "unlimited" |
||
| 841 | */ |
||
| 842 | public function textFieldSize( $table, $field ) { |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Construct a LIMIT query with optional offset |
||
| 858 | * This is used for query pages |
||
| 859 | * |
||
| 860 | * @param string $sql SQL query we will append the limit too |
||
| 861 | * @param int $limit The SQL limit |
||
| 862 | * @param bool|int $offset The SQL offset (default false) |
||
| 863 | * @return array|string |
||
| 864 | * @throws DBUnexpectedError |
||
| 865 | */ |
||
| 866 | public function limitResult( $sql, $limit, $offset = false ) { |
||
| 910 | |||
| 911 | /** |
||
| 912 | * If there is a limit clause, parse it, strip it, and pass the remaining |
||
| 913 | * SQL through limitResult() with the appropriate parameters. Not the |
||
| 914 | * prettiest solution, but better than building a whole new parser. This |
||
| 915 | * exists becase there are still too many extensions that don't use dynamic |
||
| 916 | * sql generation. |
||
| 917 | * |
||
| 918 | * @param string $sql |
||
| 919 | * @return array|mixed|string |
||
| 920 | */ |
||
| 921 | public function LimitToTopN( $sql ) { |
||
| 936 | |||
| 937 | /** |
||
| 938 | * @return string Wikitext of a link to the server software's web site |
||
| 939 | */ |
||
| 940 | public function getSoftwareLink() { |
||
| 943 | |||
| 944 | /** |
||
| 945 | * @return string Version information from the database |
||
| 946 | */ |
||
| 947 | public function getServerVersion() { |
||
| 956 | |||
| 957 | /** |
||
| 958 | * @param string $table |
||
| 959 | * @param string $fname |
||
| 960 | * @return bool |
||
| 961 | */ |
||
| 962 | public function tableExists( $table, $fname = __METHOD__ ) { |
||
| 986 | |||
| 987 | /** |
||
| 988 | * Query whether a given column exists in the mediawiki schema |
||
| 989 | * @param string $table |
||
| 990 | * @param string $field |
||
| 991 | * @param string $fname |
||
| 992 | * @return bool |
||
| 993 | */ |
||
| 994 | public function fieldExists( $table, $field, $fname = __METHOD__ ) { |
||
| 1012 | |||
| 1013 | public function fieldInfo( $table, $field ) { |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Begin a transaction, committing any previously open transaction |
||
| 1035 | * @param string $fname |
||
| 1036 | */ |
||
| 1037 | protected function doBegin( $fname = __METHOD__ ) { |
||
| 1041 | |||
| 1042 | /** |
||
| 1043 | * End a transaction |
||
| 1044 | * @param string $fname |
||
| 1045 | */ |
||
| 1046 | protected function doCommit( $fname = __METHOD__ ) { |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Rollback a transaction. |
||
| 1053 | * No-op on non-transactional databases. |
||
| 1054 | * @param string $fname |
||
| 1055 | */ |
||
| 1056 | protected function doRollback( $fname = __METHOD__ ) { |
||
| 1060 | |||
| 1061 | /** |
||
| 1062 | * Escapes a identifier for use inm SQL. |
||
| 1063 | * Throws an exception if it is invalid. |
||
| 1064 | * Reference: http://msdn.microsoft.com/en-us/library/aa224033%28v=SQL.80%29.aspx |
||
| 1065 | * @param string $identifier |
||
| 1066 | * @throws MWException |
||
| 1067 | * @return string |
||
| 1068 | */ |
||
| 1069 | private function escapeIdentifier( $identifier ) { |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * @param string $s |
||
| 1089 | * @return string |
||
| 1090 | */ |
||
| 1091 | public function strencode( $s ) { |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * @param string|Blob $s |
||
| 1099 | * @return string |
||
| 1100 | */ |
||
| 1101 | public function addQuotes( $s ) { |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * @param string $s |
||
| 1119 | * @return string |
||
| 1120 | */ |
||
| 1121 | public function addIdentifierQuotes( $s ) { |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * @param string $name |
||
| 1128 | * @return bool |
||
| 1129 | */ |
||
| 1130 | public function isQuotedIdentifier( $name ) { |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * MS SQL supports more pattern operators than other databases (ex: [,],^) |
||
| 1136 | * |
||
| 1137 | * @param string $s |
||
| 1138 | * @return string |
||
| 1139 | */ |
||
| 1140 | protected function escapeLikeInternal( $s ) { |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * MS SQL requires specifying the escape character used in a LIKE query |
||
| 1146 | * or using Square brackets to surround characters that are to be escaped |
||
| 1147 | * http://msdn.microsoft.com/en-us/library/ms179859.aspx |
||
| 1148 | * Here we take the Specify-Escape-Character approach since it's less |
||
| 1149 | * invasive, renders a query that is closer to other DB's and better at |
||
| 1150 | * handling square bracket escaping |
||
| 1151 | * |
||
| 1152 | * @return string Fully built LIKE statement |
||
| 1153 | */ |
||
| 1154 | View Code Duplication | public function buildLike() { |
|
| 1162 | |||
| 1163 | /** |
||
| 1164 | * @param string $db |
||
| 1165 | * @return bool |
||
| 1166 | */ |
||
| 1167 | public function selectDB( $db ) { |
||
| 1176 | |||
| 1177 | /** |
||
| 1178 | * @param array $options An associative array of options to be turned into |
||
| 1179 | * an SQL query, valid keys are listed in the function. |
||
| 1180 | * @return array |
||
| 1181 | */ |
||
| 1182 | public function makeSelectOptions( $options ) { |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Get the type of the DBMS, as it appears in $wgDBtype. |
||
| 1212 | * @return string |
||
| 1213 | */ |
||
| 1214 | public function getType() { |
||
| 1217 | |||
| 1218 | /** |
||
| 1219 | * @param array $stringList |
||
| 1220 | * @return string |
||
| 1221 | */ |
||
| 1222 | public function buildConcat( $stringList ) { |
||
| 1225 | |||
| 1226 | /** |
||
| 1227 | * Build a GROUP_CONCAT or equivalent statement for a query. |
||
| 1228 | * MS SQL doesn't have GROUP_CONCAT so we emulate it with other stuff (and boy is it nasty) |
||
| 1229 | * |
||
| 1230 | * This is useful for combining a field for several rows into a single string. |
||
| 1231 | * NULL values will not appear in the output, duplicated values will appear, |
||
| 1232 | * and the resulting delimiter-separated values have no defined sort order. |
||
| 1233 | * Code using the results may need to use the PHP unique() or sort() methods. |
||
| 1234 | * |
||
| 1235 | * @param string $delim Glue to bind the results together |
||
| 1236 | * @param string|array $table Table name |
||
| 1237 | * @param string $field Field name |
||
| 1238 | * @param string|array $conds Conditions |
||
| 1239 | * @param string|array $join_conds Join conditions |
||
| 1240 | * @return string SQL text |
||
| 1241 | * @since 1.23 |
||
| 1242 | */ |
||
| 1243 | public function buildGroupConcatField( $delim, $table, $field, $conds = '', |
||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * @return string |
||
| 1260 | */ |
||
| 1261 | public function getSearchEngine() { |
||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Returns an associative array for fields that are of type varbinary, binary, or image |
||
| 1267 | * $table can be either a raw table name or passed through tableName() first |
||
| 1268 | * @param string $table |
||
| 1269 | * @return array |
||
| 1270 | */ |
||
| 1271 | View Code Duplication | private function getBinaryColumns( $table ) { |
|
| 1283 | |||
| 1284 | /** |
||
| 1285 | * @param string $table |
||
| 1286 | * @return array |
||
| 1287 | */ |
||
| 1288 | View Code Duplication | private function getBitColumns( $table ) { |
|
| 1300 | |||
| 1301 | private function populateColumnCaches() { |
||
| 1319 | |||
| 1320 | /** |
||
| 1321 | * @param string $name |
||
| 1322 | * @param string $format |
||
| 1323 | * @return string |
||
| 1324 | */ |
||
| 1325 | View Code Duplication | function tableName( $name, $format = 'quoted' ) { |
|
| 1334 | |||
| 1335 | /** |
||
| 1336 | * call this instead of tableName() in the updater when renaming tables |
||
| 1337 | * @param string $name |
||
| 1338 | * @param string $format One of quoted, raw, or split |
||
| 1339 | * @return string |
||
| 1340 | */ |
||
| 1341 | function realTableName( $name, $format = 'quoted' ) { |
||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * Called in the installer and updater. |
||
| 1356 | * Probably doesn't need to be called anywhere else in the codebase. |
||
| 1357 | * @param bool|null $value |
||
| 1358 | * @return bool|null |
||
| 1359 | */ |
||
| 1360 | public function prepareStatements( $value = null ) { |
||
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Called in the installer and updater. |
||
| 1366 | * Probably doesn't need to be called anywhere else in the codebase. |
||
| 1367 | * @param bool|null $value |
||
| 1368 | * @return bool|null |
||
| 1369 | */ |
||
| 1370 | public function scrollableCursor( $value = null ) { |
||
| 1373 | } // end DatabaseMssql class |
||
| 1374 | |||
| 1519 |
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: