r68872 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68871‎ | r68872 | r68873 >
Date:10:01, 2 July 2010
Author:maxsem
Status:ok
Tags:
Comment:
* Replaced the mess of every database class implementing filedExists() is its own way with one simple function in base class. Verified to work on MySQL, Postgres and SQLite.
* Fixed fieldInfo() on Postgres not using tableName() and thus failing for table user, for example.
* Made fieldInfo() on MySQL return false instead of throwing a query error if table does not exist. This is consistent with other databases' behaviour.
Modified paths:
  • /trunk/phase3/includes/db/Database.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseIbm_db2.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseMysql.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseOracle.php (modified) (history)
  • /trunk/phase3/includes/db/DatabasePostgres.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseSqlite.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabaseMysql.php
@@ -262,7 +262,10 @@
263263
264264 function fieldInfo( $table, $field ) {
265265 $table = $this->tableName( $table );
266 - $res = $this->query( "SELECT * FROM $table LIMIT 1" );
 266+ $res = $this->query( "SELECT * FROM $table LIMIT 1", __METHOD__, true );
 267+ if ( !$res ) {
 268+ return false;
 269+ }
267270 $n = mysql_num_fields( $res->result );
268271 for( $i = 0; $i < $n; $i++ ) {
269272 $meta = mysql_fetch_field( $res->result, $i );
Index: trunk/phase3/includes/db/DatabaseOracle.php
@@ -905,10 +905,6 @@
906906 return $this->fieldInfoMulti ($table, $field);
907907 }
908908
909 - function fieldExists( $table, $field, $fname = 'DatabaseOracle::fieldExists' ) {
910 - return (bool)$this->fieldInfo( $table, $field, $fname );
911 - }
912 -
913909 function begin( $fname = '' ) {
914910 $this->mTrxLevel = 1;
915911 }
Index: trunk/phase3/includes/db/DatabasePostgres.php
@@ -31,6 +31,8 @@
3232 AND relname=%s
3333 AND attname=%s;
3434 SQL;
 35+
 36+ $table = $db->tableName( $table );
3537 $res = $db->query(sprintf($q,
3638 $db->addQuotes($wgDBmwschema),
3739 $db->addQuotes($table),
@@ -1264,24 +1266,6 @@
12651267 return $owner;
12661268 }
12671269
1268 - /**
1269 - * Query whether a given column exists in the mediawiki schema
1270 - */
1271 - function fieldExists( $table, $field, $fname = 'DatabasePostgres::fieldExists' ) {
1272 - global $wgDBmwschema;
1273 - $etable = preg_replace("/'/", "''", $table);
1274 - $eschema = preg_replace("/'/", "''", $wgDBmwschema);
1275 - $ecol = preg_replace("/'/", "''", $field);
1276 - $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n, pg_catalog.pg_attribute a "
1277 - . "WHERE c.relnamespace = n.oid AND c.relname = '$etable' AND n.nspname = '$eschema' "
1278 - . "AND a.attrelid = c.oid AND a.attname = '$ecol'";
1279 - $res = $this->query( $SQL, $fname );
1280 - $count = $res ? $res->numRows() : 0;
1281 - if ($res)
1282 - $this->freeResult( $res );
1283 - return $count;
1284 - }
1285 -
12861270 function fieldInfo( $table, $field ) {
12871271 return PostgresField::fromText($this, $table, $field);
12881272 }
Index: trunk/phase3/includes/db/DatabaseIbm_db2.php
@@ -291,8 +291,7 @@
292292 * setFakeSlaveLag [Done]
293293 * setFakeMaster [Done]
294294 *
295 - * Reflection: 6 / 6
296 - * fieldExists [Done]
 295+ * Reflection: 5 / 5
297296 * indexInfo [Done]
298297 * fieldInfo [Done]
299298 * fieldType [Done]
@@ -1535,30 +1534,6 @@
15361535 ######################################
15371536
15381537 /**
1539 - * Query whether a given column exists in the mediawiki schema
1540 - * @param $table String: name of the table
1541 - * @param $field String: name of the column
1542 - * @param $fname String: function name for logging and profiling
1543 - */
1544 - public function fieldExists( $table, $field, $fname = 'DatabaseIbm_db2::fieldExists' ) {
1545 - $table = $this->tableName( $table );
1546 - $schema = $this->mSchema;
1547 - $etable = preg_replace("/'/", "''", $table);
1548 - $eschema = preg_replace("/'/", "''", $schema);
1549 - $ecol = preg_replace("/'/", "''", $field);
1550 - $sql = <<<SQL
1551 -SELECT 1 as fieldexists
1552 -FROM sysibm.syscolumns sc
1553 -WHERE sc.name='$ecol' AND sc.tbname='$etable' AND sc.tbcreator='$eschema'
1554 -SQL;
1555 - $res = $this->query( $sql, $fname );
1556 - $count = $res ? $this->numRows($res) : 0;
1557 - if ($res)
1558 - $this->freeResult( $res );
1559 - return $count;
1560 - }
1561 -
1562 - /**
15631538 * Returns information about an index
15641539 * If errors are explicitly ignored, returns NULL on failure
15651540 * @param $table String: table name
Index: trunk/phase3/includes/db/Database.php
@@ -1017,25 +1017,14 @@
10181018
10191019 /**
10201020 * Determines whether a field exists in a table
1021 - * Usually aborts on failure
1022 - * If errors are explicitly ignored, returns NULL on failure
 1021+ * @param $table: table name
 1022+ * @param $filed: filed to check on that table
 1023+ * @param $fname: calling function name (optional)
 1024+ * @return bool: whether $table has filed $field
10231025 */
10241026 function fieldExists( $table, $field, $fname = 'Database::fieldExists' ) {
1025 - $table = $this->tableName( $table );
1026 - $res = $this->query( 'DESCRIBE '.$table, $fname );
1027 - if ( !$res ) {
1028 - return null;
1029 - }
1030 -
1031 - $found = false;
1032 -
1033 - while ( $row = $this->fetchObject( $res ) ) {
1034 - if ( $row->Field == $field ) {
1035 - $found = true;
1036 - break;
1037 - }
1038 - }
1039 - return $found;
 1027+ $info = $this->fieldInfo( $table, $field );
 1028+ return (bool)$info;
10401029 }
10411030
10421031 /**
Index: trunk/phase3/includes/db/DatabaseSqlite.php
@@ -436,14 +436,6 @@
437437 }
438438
439439 /**
440 - * Query whether a given column exists in the mediawiki schema
441 - */
442 - function fieldExists( $table, $field, $fname = '' ) {
443 - $info = $this->fieldInfo( $table, $field );
444 - return (bool)$info;
445 - }
446 -
447 - /**
448440 * Get information about a given field
449441 * Returns false if the field does not exist.
450442 */

Follow-up revisions

RevisionCommit summaryAuthorDate
r76955Release notes for r68872maxsem19:58, 18 November 2010

Status & tagging log