r72235 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72234‎ | r72235 | r72236 >
Date:21:56, 2 September 2010
Author:ashley
Status:ok
Tags:
Comment:
coding style tweaks
Modified paths:
  • /trunk/phase3/includes/db/DatabaseIbm_db2.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabaseIbm_db2.php
@@ -2,7 +2,7 @@
33 /**
44 * This is the IBM DB2 database abstraction layer.
55 * See maintenance/ibm_db2/README for development notes
6 - * and other specific information
 6+ * and other specific information
77 *
88 * @file
99 * @ingroup Database
@@ -37,13 +37,17 @@
3838 FROM sysibm.syscolumns
3939 WHERE tbcreator=%s AND tbname=%s AND name=%s;
4040 SQL;
41 - $res = $db->query( sprintf( $q,
 41+ $res = $db->query(
 42+ sprintf( $q,
4243 $db->addQuotes( $wgDBmwschema ),
4344 $db->addQuotes( $table ),
44 - $db->addQuotes( $field )) );
 45+ $db->addQuotes( $field )
 46+ )
 47+ );
4548 $row = $db->fetchObject( $res );
46 - if ( !$row )
 49+ if ( !$row ) {
4750 return null;
 51+ }
4852 $n = new IBM_DB2Field;
4953 $n->type = $row->typname;
5054 $n->nullable = ( $row->attnotnull == 'N' );
@@ -94,10 +98,9 @@
9599 return $this->mData;
96100 }
97101
98 - public function __toString()
99 - {
100 - return $this->mData;
101 - }
 102+ public function __toString() {
 103+ return $this->mData;
 104+ }
102105 }
103106
104107 /**
@@ -142,7 +145,7 @@
143146 public $mStmtOptions = array();
144147
145148 /** Default schema */
146 - const USE_GLOBAL = "mediawiki";
 149+ const USE_GLOBAL = 'mediawiki';
147150
148151 /** Option that applies to nothing */
149152 const NONE_OPTION = 0x00;
@@ -275,8 +278,7 @@
276279
277280 if ( $schema == self::USE_GLOBAL ) {
278281 $this->mSchema = $wgDBmwschema;
279 - }
280 - else {
 282+ } else {
281283 $this->mSchema = $schema;
282284 }
283285
@@ -298,15 +300,14 @@
299301 * @param $type Integer: whether this is a Connection or Statement otion
300302 */
301303 private function setDB2Option( $name, $const, $type ) {
302 - if ( defined( $const )) {
 304+ if ( defined( $const ) ) {
303305 if ( $type & self::CONN_OPTION ) {
304306 $this->mConnOptions[$name] = constant( $const );
305307 }
306308 if ( $type & self::STMT_OPTION ) {
307309 $this->mStmtOptions[$name] = constant( $const );
308310 }
309 - }
310 - else {
 311+ } else {
311312 $this->installPrint(
312313 "$const is not defined. ibm_db2 version is likely too low." );
313314 }
@@ -327,14 +328,14 @@
328329 /**
329330 * Opens a database connection and returns it
330331 * Closes any existing connection
331 - * @return a fresh connection
 332+ *
332333 * @param $server String: hostname
333334 * @param $user String
334335 * @param $password String
335336 * @param $dbName String: database name
 337+ * @return a fresh connection
336338 */
337 - public function open( $server, $user, $password, $dbName )
338 - {
 339+ public function open( $server, $user, $password, $dbName ) {
339340 // Load the port number
340341 global $wgDBport;
341342 wfProfileIn( __METHOD__ );
@@ -352,7 +353,7 @@
353354 $this->reportConnectionError( $error );
354355 }
355356
356 - if ( strlen( $user ) < 1) {
 357+ if ( strlen( $user ) < 1 ) {
357358 return null;
358359 }
359360
@@ -393,8 +394,7 @@
394395 /**
395396 * Opens a cataloged database connection, sets mConn
396397 */
397 - protected function openCataloged( $dbName, $user, $password )
398 - {
 398+ protected function openCataloged( $dbName, $user, $password ) {
399399 @$this->mConn = db2_pconnect( $dbName, $user, $password );
400400 }
401401
@@ -422,12 +422,11 @@
423423 public function close() {
424424 $this->mOpened = false;
425425 if ( $this->mConn ) {
426 - if ($this->trxLevel() > 0) {
 426+ if ( $this->trxLevel() > 0 ) {
427427 $this->commit();
428428 }
429429 return db2_close( $this->mConn );
430 - }
431 - else {
 430+ } else {
432431 return true;
433432 }
434433 }
@@ -503,15 +502,15 @@
504503 $this->applySchema();
505504
506505 $ret = db2_exec( $this->mConn, $sql, $this->mStmtOptions );
507 - if( $ret == FALSE ) {
 506+ if( $ret == false ) {
508507 $error = db2_stmt_errormsg();
509508 $this->installPrint( "<pre>$sql</pre>" );
510509 $this->installPrint( $error );
511 - throw new DBUnexpectedError( $this, 'SQL error: '
 510+ throw new DBUnexpectedError( $this, 'SQL error: '
512511 . htmlspecialchars( $error ) );
513512 }
514513 $this->mLastResult = $ret;
515 - $this->mAffectedRows = null; // Not calculated until asked for
 514+ $this->mAffectedRows = null; // Not calculated until asked for
516515 return $ret;
517516 }
518517
@@ -534,12 +533,14 @@
535534 WHERE ST.NAME = '$table' AND ST.CREATOR = '$schema'
536535 EOF;
537536 $res = $this->query( $sql );
538 - if ( !$res ) return false;
 537+ if ( !$res ) {
 538+ return false;
 539+ }
539540
540541 // If the table exists, there should be one of it
541542 @$row = $this->fetchRow( $res );
542543 $count = $row[0];
543 - if ( $count == '1' or $count == 1 ) {
 544+ if ( $count == '1' || $count == 1 ) {
544545 return true;
545546 }
546547
@@ -569,7 +570,7 @@
570571
571572 /**
572573 * Fetch the next row from the given result object, in associative array
573 - * form. Fields are retrieved with $row['fieldname'].
 574+ * form. Fields are retrieved with $row['fieldname'].
574575 *
575576 * @param $res SQL result object as returned from Database::query(), etc.
576577 * @return DB2 row object
@@ -607,15 +608,15 @@
608609
609610 $res = $this->sourceFile( "../maintenance/ibm_db2/tables.sql" );
610611 if ( $res !== true ) {
611 - print " <b>FAILED</b>: " . htmlspecialchars( $res ) . "</li>";
 612+ print ' <b>FAILED</b>: ' . htmlspecialchars( $res ) . '</li>';
612613 } else {
613 - print " done</li>";
 614+ print ' done</li>';
614615 }
615616 $res = $this->sourceFile( "../maintenance/ibm_db2/foreignkeys.sql" );
616617 if ( $res !== true ) {
617 - print " <b>FAILED</b>: " . htmlspecialchars( $res ) . "</li>";
 618+ print ' <b>FAILED</b>: ' . htmlspecialchars( $res ) . '</li>';
618619 } else {
619 - print "<li>Foreign keys done</li>";
 620+ print '<li>Foreign keys done</li>';
620621 }
621622 $res = null;
622623
@@ -623,16 +624,13 @@
624625
625626 if ( $this->lastError() ) {
626627 $this->installPrint(
627 - "Errors encountered during table creation -- rolled back" );
628 - $this->installPrint( "Please install again" );
 628+ 'Errors encountered during table creation -- rolled back' );
 629+ $this->installPrint( 'Please install again' );
629630 $this->rollback();
630 - }
631 - else {
 631+ } else {
632632 $this->commit();
633633 }
634 - }
635 - catch ( MWException $mwe )
636 - {
 634+ } catch ( MWException $mwe ) {
637635 print "<br><pre>$mwe</pre><br>";
638636 }
639637 }
@@ -640,43 +638,44 @@
641639 /**
642640 * Escapes strings
643641 * Doesn't escape numbers
 642+ *
644643 * @param $s String: string to escape
645644 * @return escaped string
646645 */
647646 public function addQuotes( $s ) {
648647 //$this->installPrint( "DB2::addQuotes( $s )\n" );
649648 if ( is_null( $s ) ) {
650 - return "NULL";
651 - } else if ( $s instanceof Blob ) {
 649+ return 'NULL';
 650+ } elseif ( $s instanceof Blob ) {
652651 return "'" . $s->fetch( $s ) . "'";
653 - } else if ( $s instanceof IBM_DB2Blob ) {
 652+ } elseif ( $s instanceof IBM_DB2Blob ) {
654653 return "'" . $this->decodeBlob( $s ) . "'";
655654 }
656655 $s = $this->strencode( $s );
657656 if ( is_numeric( $s ) ) {
658657 return $s;
659 - }
660 - else {
 658+ } else {
661659 return "'$s'";
662660 }
663661 }
664662
665663 /**
666664 * Verifies that a DB2 column/field type is numeric
667 - * @return bool true if numeric
 665+ *
668666 * @param $type String: DB2 column type
 667+ * @return Boolean: true if numeric
669668 */
670669 public function is_numeric_type( $type ) {
671 - switch ( strtoupper( $type )) {
672 - case 'SMALLINT':
673 - case 'INTEGER':
674 - case 'INT':
675 - case 'BIGINT':
676 - case 'DECIMAL':
677 - case 'REAL':
678 - case 'DOUBLE':
679 - case 'DECFLOAT':
680 - return true;
 670+ switch ( strtoupper( $type ) ) {
 671+ case 'SMALLINT':
 672+ case 'INTEGER':
 673+ case 'INT':
 674+ case 'BIGINT':
 675+ case 'DECIMAL':
 676+ case 'REAL':
 677+ case 'DOUBLE':
 678+ case 'DECFLOAT':
 679+ return true;
681680 }
682681 return false;
683682 }
@@ -695,7 +694,7 @@
696695 $s = utf8_encode( $s );
697696 // Fix its stupidity
698697 $from = array( "\\\\", "\\'", '\\n', '\\t', '\\"', '\\r' );
699 - $to = array( "\\", "''", "\n", "\t", '"', "\r" );
 698+ $to = array( "\\", "''", "\n", "\t", '"', "\r" );
700699 $s = str_replace( $from, $to, $s ); // DB2 expects '', not \' escaping
701700 return $s;
702701 }
@@ -778,8 +777,7 @@
779778 foreach ( $a as $field => $value ) {
780779 if ( !$first ) {
781780 $list .= ", $field = ?";
782 - }
783 - else {
 781+ } else {
784782 $list .= "$field = ?";
785783 $first = false;
786784 }
@@ -796,6 +794,7 @@
797795 /**
798796 * Construct a LIMIT query with optional offset
799797 * This is used for query pages
 798+ *
800799 * @param $sql string SQL query we will append the limit too
801800 * @param $limit integer the SQL limit
802801 * @param $offset integer the SQL offset (default false)
@@ -808,8 +807,7 @@
809808 if( $offset ) {
810809 if ( stripos( $sql, 'where' ) === false ) {
811810 return "$sql AND ( ROWNUM BETWEEN $offset AND $offset+$limit )";
812 - }
813 - else {
 811+ } else {
814812 return "$sql WHERE ( ROWNUM BETWEEN $offset AND $offset+$limit )";
815813 }
816814 }
@@ -818,8 +816,9 @@
819817
820818 /**
821819 * Handle reserved keyword replacement in table names
822 - * @return
 820+ *
823821 * @param $name Object
 822+ * @return String
824823 */
825824 public function tableName( $name ) {
826825 // we want maximum compatibility with MySQL schema
@@ -828,10 +827,11 @@
829828
830829 /**
831830 * Generates a timestamp in an insertable format
832 - * @return string timestamp value
 831+ *
833832 * @param $ts timestamp
 833+ * @return String: timestamp value
834834 */
835 - public function timestamp( $ts=0 ) {
 835+ public function timestamp( $ts = 0 ) {
836836 // TS_MW cannot be easily distinguished from an integer
837837 return wfTimestamp( TS_DB2, $ts );
838838 }
@@ -867,6 +867,7 @@
868868 /**
869869 * Updates the mInsertId property with the value of the last insert
870870 * into a generated column
 871+ *
871872 * @param $table String: sanitized table name
872873 * @param $primaryKey Mixed: string name of the primary key
873874 * @param $stmt Resource: prepared statement resource
@@ -925,7 +926,7 @@
926927 // assume success
927928 $res = true;
928929 // If we are not in a transaction, we need to be for savepoint trickery
929 - if ( ! $this->mTrxLevel ) {
 930+ if ( !$this->mTrxLevel ) {
930931 $this->begin();
931932 }
932933
@@ -950,14 +951,13 @@
951952 // insert each row into the database
952953 $res = $res & $this->execute( $stmt, $row );
953954 if ( !$res ) {
954 - $this->installPrint( "Last error:" );
 955+ $this->installPrint( 'Last error:' );
955956 $this->installPrint( $this->lastError() );
956957 }
957958 // get the last inserted value into a generated column
958959 $this->calcInsertId( $table, $primaryKey, $stmt );
959960 }
960 - }
961 - else {
 961+ } else {
962962 $olde = error_reporting( 0 );
963963 // For future use, we may want to track the number of actual inserts
964964 // Right now, insert (all writes) simply return true/false
@@ -970,11 +970,10 @@
971971 $overhead = "SAVEPOINT $ignore ON ROLLBACK RETAIN CURSORS";
972972 db2_exec( $this->mConn, $overhead, $this->mStmtOptions );
973973
974 -
975974 $this->execute( $stmt, $row );
976975
977976 if ( !$res2 ) {
978 - $this->installPrint( "Last error:" );
 977+ $this->installPrint( 'Last error:' );
979978 $this->installPrint( $this->lastError() );
980979 }
981980 // get the last inserted value into a generated column
@@ -984,8 +983,7 @@
985984 if ( $errNum ) {
986985 db2_exec( $this->mConn, "ROLLBACK TO SAVEPOINT $ignore",
987986 $this->mStmtOptions );
988 - }
989 - else {
 987+ } else {
990988 db2_exec( $this->mConn, "RELEASE SAVEPOINT $ignore",
991989 $this->mStmtOptions );
992990 $numrowsinserted++;
@@ -1015,12 +1013,14 @@
10161014 $schema = $this->mSchema;
10171015 // find out the primary keys
10181016 $keyres = db2_primary_keys( $this->mConn, null, strtoupper( $schema ),
1019 - strtoupper( $table ));
 1017+ strtoupper( $table )
 1018+ );
10201019 $keys = array();
10211020 for (
10221021 $row = $this->fetchObject( $keyres );
10231022 $row != null;
1024 - $row = $this->fetchObject( $keyres ))
 1023+ $row = $this->fetchObject( $keyres )
 1024+ )
10251025 {
10261026 $keys[] = strtolower( $row->column_name );
10271027 }
@@ -1060,7 +1060,7 @@
10611061 $sql .= " WHERE " . $this->makeList( $conds, LIST_AND );
10621062 }
10631063 $stmt = $this->prepare( $sql );
1064 - $this->installPrint( "UPDATE: " . print_r( $values, TRUE ));
 1064+ $this->installPrint( 'UPDATE: ' . print_r( $values, true ) );
10651065 // assuming for now that an array with string keys will work
10661066 // if not, convert to simple array first
10671067 $result = $this->execute( $stmt, $values );
@@ -1098,8 +1098,9 @@
10991099 // Forced result for simulated queries
11001100 return $this->mAffectedRows;
11011101 }
1102 - if( empty( $this->mLastResult ) )
 1102+ if( empty( $this->mLastResult ) ) {
11031103 return 0;
 1104+ }
11041105 return db2_num_rows( $this->mLastResult );
11051106 }
11061107
@@ -1133,7 +1134,7 @@
11341135 foreach ( $uniqueIndexes as $index ) {
11351136 if ( $first ) {
11361137 $first = false;
1137 - $sql .= "( ";
 1138+ $sql .= '( ';
11381139 } else {
11391140 $sql .= ' ) OR ( ';
11401141 }
@@ -1175,8 +1176,7 @@
11761177 }
11771178 if ( $this->mNumRows ) {
11781179 return $this->mNumRows;
1179 - }
1180 - else {
 1180+ } else {
11811181 return 0;
11821182 }
11831183 }
@@ -1208,7 +1208,7 @@
12091209 $res = $res->result;
12101210 }
12111211 if ( !@db2_free_result( $res ) ) {
1212 - throw new DBUnexpectedError( $this, "Unable to free DB2 result\n" );
 1212+ throw new DBUnexpectedError( $this, "Unable to free DB2 result\n" );
12131213 }
12141214 }
12151215
@@ -1255,7 +1255,7 @@
12561256 * @return Mixed: database result resource for fetch functions or false
12571257 * on failure
12581258 */
1259 - public function select( $table, $vars, $conds='', $fname = 'DatabaseIbm_db2::select', $options = array(), $join_conds = array() )
 1259+ public function select( $table, $vars, $conds = '', $fname = 'DatabaseIbm_db2::select', $options = array(), $join_conds = array() )
12601260 {
12611261 $res = parent::select( $table, $vars, $conds, $fname, $options,
12621262 $join_conds );
@@ -1291,7 +1291,6 @@
12921292 $obj = $this->fetchObject( $res2 );
12931293 $this->mNumRows = $obj->num_rows;
12941294
1295 -
12961295 return $res;
12971296 }
12981297
@@ -1337,10 +1336,10 @@
13381337
13391338 /**
13401339 * Returns link to IBM DB2 free download
1341 - * @return string wikitext of a link to the server software's web site
 1340+ * @return String: wikitext of a link to the server software's web site
13421341 */
13431342 public static function getSoftwareLink() {
1344 - return "[http://www.ibm.com/db2/express/ IBM DB2]";
 1343+ return '[http://www.ibm.com/db2/express/ IBM DB2]';
13451344 }
13461345
13471346 /**
@@ -1350,7 +1349,7 @@
13511350 * @return String
13521351 */
13531352 public function getSearchEngine() {
1354 - return "SearchIBM_DB2";
 1353+ return 'SearchIBM_DB2';
13551354 }
13561355
13571356 /**
@@ -1392,9 +1391,9 @@
13931392 * Not implemented
13941393 * @return string ''
13951394 */
1396 - public function getStatus( $which="%" ) {
1397 - $this->installPrint( 'Not implemented for DB2: getStatus()' );
1398 - return '';
 1395+ public function getStatus( $which = '%' ) {
 1396+ $this->installPrint( 'Not implemented for DB2: getStatus()' );
 1397+ return '';
13991398 }
14001399 /**
14011400 * Not implemented
@@ -1443,8 +1442,9 @@
14441443 $row = $this->fetchObject( $res );
14451444 if ( $row != null ) {
14461445 return $row;
 1446+ } else {
 1447+ return false;
14471448 }
1448 - else return false;
14491449 }
14501450
14511451 /**
@@ -1681,7 +1681,7 @@
16821682 * Switches module between regular and install modes
16831683 */
16841684 public function setMode( $mode ) {
1685 - $old = $this->mMode;
 1685+ $old = $this->mMode;
16861686 $this->mMode = $mode;
16871687 return $old;
16881688 }
@@ -1693,7 +1693,7 @@
16941694 * @return String
16951695 */
16961696 function bitNot( $field ) {
1697 - //expecting bit-fields smaller than 4bytes
 1697+ // expecting bit-fields smaller than 4bytes
16981698 return "BITNOT( $field )";
16991699 }
17001700

Status & tagging log