r75346 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75345‎ | r75346 | r75347 >
Date:10:56, 25 October 2010
Author:freakolowsky
Status:resolved (Comments)
Tags:
Comment:
* null value => default value handling
* duplicateTable call fix
* some internal calls to query replaced with doQuery
* added update method overload
Modified paths:
  • /trunk/phase3/includes/db/DatabaseOracle.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabaseOracle.php
@@ -54,6 +54,7 @@
5555 if ( ( $this->nrows = oci_fetch_all( $stmt, $this->rows, 0, - 1, OCI_FETCHSTATEMENT_BY_ROW | OCI_NUM ) ) === false ) {
5656 $e = oci_error( $stmt );
5757 $db->reportQueryError( $e['message'], $e['code'], '', __METHOD__ );
 58+ $this->free();
5859 return;
5960 }
6061
@@ -185,10 +186,10 @@
186187 var $mFieldInfoCache = array();
187188
188189 function __construct( $server = false, $user = false, $password = false, $dbName = false,
189 - $flags = 0, $tablePrefix = 'get from global' )
 190+ $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
190191 {
191192 $tablePrefix = $tablePrefix == 'get from global' ? $tablePrefix : strtoupper( $tablePrefix );
192 - parent::__construct( $server, $user, $password, $dbName, $flags, $tablePrefix );
 193+ parent::__construct( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
193194 wfRunHooks( 'DatabaseOraclePostInit', array( &$this ) );
194195 }
195196
@@ -218,12 +219,14 @@
219220 return true;
220221 }
221222
222 - static function newFromParams( $server, $user, $password, $dbName, $flags = 0, $tablePrefix ) {
223 - return new DatabaseOracle( $server, $user, $password, $dbName, $flags, $tablePrefix );
 223+ static function newFromParams( $server, $user, $password, $dbName, $failFunction = false, $flags = 0, $tablePrefix )
 224+ {
 225+ return new DatabaseOracle( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
224226 }
225227
226228 /**
227229 * Usually aborts on failure
 230+ * If the failFunction is set to a non-zero integer, returns success
228231 */
229232 function open( $server, $user, $password, $dbName ) {
230233 if ( !function_exists( 'oci_connect' ) ) {
@@ -471,6 +474,36 @@
472475 return $retVal;
473476 }
474477
 478+ private function fieldBindStatement ( $table, $col, &$val, $includeCol = false ) {
 479+ $col_info = $this->fieldInfoMulti( $table, $col );
 480+ $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
 481+
 482+ $bind = '';
 483+ if ( is_numeric( $col ) ) {
 484+ $bind = $val;
 485+ $val = null;
 486+ return $bind;
 487+ } else if ( $includeCol ) {
 488+ $bind = "$col = ";
 489+ }
 490+
 491+ if ( $val == '' && $val !== 0 && $col_type != 'BLOB' && $col_type != 'CLOB' ) {
 492+ $val = null;
 493+ }
 494+
 495+ if ( $val === null ) {
 496+ if ( $col_info != false && $col_info->nullable() == 0 && $col_info->defaultValue() != null ) {
 497+ $bind .= 'DEFAULT';
 498+ } else {
 499+ $bind .= 'NULL';
 500+ }
 501+ } else {
 502+ $bind .= ':' . $col;
 503+ }
 504+
 505+ return $bind;
 506+ }
 507+
475508 private function insertOneRow( $table, $row, $fname ) {
476509 global $wgContLang;
477510
@@ -481,18 +514,22 @@
482515
483516 // for each value, append ":key"
484517 $first = true;
485 - foreach ( $row as $col => $val ) {
486 - if ( $first ) {
487 - $sql .= $val !== null ? ':' . $col : 'NULL';
 518+ foreach ( $row as $col => &$val ) {
 519+ if ( !$first ) {
 520+ $sql .= ', ';
488521 } else {
489 - $sql .= $val !== null ? ', :' . $col : ', NULL';
 522+ $first = false;
490523 }
491 -
492 - $first = false;
 524+
 525+ $sql .= $this->fieldBindStatement( $table, $col, $val );
493526 }
494527 $sql .= ')';
495528
496 - $stmt = oci_parse( $this->mConn, $sql );
 529+ if ( ( $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql ) ) === false ) {
 530+ $e = oci_error( $this->mConn );
 531+ $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
 532+ return false;
 533+ }
497534 foreach ( $row as $col => &$val ) {
498535 $col_info = $this->fieldInfoMulti( $table, $col );
499536 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
@@ -510,7 +547,8 @@
511548
512549 $val = ( $wgContLang != null ) ? $wgContLang->checkTitleEncoding( $val ) : $val;
513550 if ( oci_bind_by_name( $stmt, ":$col", $val ) === false ) {
514 - $this->reportQueryError( $this->lastErrno(), $this->lastError(), $sql, __METHOD__ );
 551+ $e = oci_error( $stmt );
 552+ $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
515553 return false;
516554 }
517555 } else {
@@ -519,8 +557,8 @@
520558 throw new DBUnexpectedError( $this, "Cannot create LOB descriptor: " . $e['message'] );
521559 }
522560
523 - if ( $col_type == 'BLOB' ) { // is_object($val)) {
524 - $lob[$col]->writeTemporary( $val ); // ->getData());
 561+ if ( $col_type == 'BLOB' ) {
 562+ $lob[$col]->writeTemporary( $val );
525563 oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, SQLT_BLOB );
526564 } else {
527565 $lob[$col]->writeTemporary( $val );
@@ -533,7 +571,6 @@
534572
535573 if ( oci_execute( $stmt, OCI_DEFAULT ) === false ) {
536574 $e = oci_error( $stmt );
537 -
538575 if ( !$this->ignore_DUP_VAL_ON_INDEX || $e['code'] != '1' ) {
539576 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
540577 return false;
@@ -657,7 +694,7 @@
658695 if ( isset( $database ) ) {
659696 $database = ( $database[0] == '"' ? $database : "\"{$database}\"" );
660697 }
661 - $table = ( $table[0] == '"' ? $table : "\"{$prefix}{$table}\"" );
 698+ $table = ( $table[0] == '"') ? $table : "\"{$prefix}{$table}\"" ;
662699
663700 $tableName = ( isset( $database ) ? "{$database}.{$table}" : "{$table}" );
664701
@@ -679,7 +716,7 @@
680717 */
681718 private function getSequenceData( $table ) {
682719 if ( $this->sequenceData == null ) {
683 - $result = $this->query( "SELECT lower(us.sequence_name), lower(utc.table_name), lower(utc.column_name) from user_sequences us, user_tab_columns utc where us.sequence_name = utc.table_name||'_'||utc.column_name||'_SEQ'" );
 720+ $result = $this->doQuery( 'SELECT lower(us.sequence_name), lower(utc.table_name), lower(utc.column_name) from user_sequences us, user_tab_columns utc where us.sequence_name = utc.table_name||\'_\'||utc.column_name||\'_SEQ\'' );
684721
685722 while ( ( $row = $result->fetchRow() ) !== false ) {
686723 $this->sequenceData[$this->tableName( $row[1] )] = array(
@@ -789,28 +826,17 @@
790827 }
791828
792829 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseOracle::duplicateTableStructure' ) {
 830+ global $wgDBprefix;
 831+
793832 $temporary = $temporary ? 'TRUE' : 'FALSE';
794 - $oldName = trim( strtoupper( $oldName ), '"');
795 - $oldParts = explode( '_', $oldName );
796833
797834 $newName = trim( strtoupper( $newName ), '"');
798 - $newParts = explode( '_', $newName );
 835+ $oldName = trim( strtoupper( $oldName ), '"');
799836
800 - $oldPrefix = '';
801 - $newPrefix = '';
802 - for ( $i = count( $oldParts ) - 1; $i >= 0; $i-- ) {
803 - if ( $oldParts[$i] != $newParts[$i] ) {
804 - $oldPrefix = implode( '_', $oldParts ) . '_';
805 - $newPrefix = implode( '_', $newParts ) . '_';
806 - break;
807 - }
808 - unset( $oldParts[$i] );
809 - unset( $newParts[$i] );
810 - }
 837+ $tabName = substr( $newName, strlen( $wgDBprefix ) );
 838+ $oldPrefix = substr( $oldName, 0, strlen( $oldName ) - strlen( $tabName ) );
811839
812 - $tabName = substr( $oldName, strlen( $oldPrefix ) );
813 -
814 - return $this->query( 'BEGIN DUPLICATE_TABLE(\'' . $tabName . '\', \'' . $oldPrefix . '\', \''.$newPrefix.'\', ' . $temporary . '); END;', $fname );
 840+ return $this->doQuery( 'BEGIN DUPLICATE_TABLE(\'' . $tabName . '\', \'' . $oldPrefix . '\', \'' . strtoupper( $wgDBprefix ) . '\', ' . $temporary . '); END;' );
815841 }
816842
817843 function timestamp( $ts = 0 ) {
@@ -917,12 +943,14 @@
918944 } else {
919945 $this->mFieldInfoCache["$table.$field"] = false;
920946 }
 947+ $fieldInfoTemp = null;
921948 } else {
922949 $fieldInfoTemp = new ORAField( $res->fetchRow() );
923950 $table = $fieldInfoTemp->tableName();
924951 $this->mFieldInfoCache["$table.$field"] = $fieldInfoTemp;
925 - return $fieldInfoTemp;
926952 }
 953+ $res->free();
 954+ return $fieldInfoTemp;
927955 }
928956
929957 function fieldInfo( $table, $field ) {
@@ -1000,7 +1028,7 @@
10011029 }
10021030
10031031 $cmd = $this->replaceVars( $cmd );
1004 - $res = $this->query( $cmd, __METHOD__ );
 1032+ $res = $this->doQuery( $cmd );
10051033 if ( $resultCallback ) {
10061034 call_user_func( $resultCallback, $res, $this );
10071035 }
@@ -1094,7 +1122,7 @@
10951123 $conds2[$col] = $val;
10961124 }
10971125 }
1098 -
 1126+
10991127 return parent::selectRow( $table, $vars, $conds2, $fname, $options, $join_conds );
11001128 }
11011129
@@ -1170,6 +1198,98 @@
11711199 }
11721200 }
11731201
 1202+ function update( $table, $values, $conds, $fname = 'DatabaseOracle::update', $options = array() ) {
 1203+ $table = $this->tableName( $table );
 1204+ $opts = $this->makeUpdateOptions( $options );
 1205+ $sql = "UPDATE $opts $table SET ";
 1206+
 1207+ $first = true;
 1208+ foreach ( $values as $col => &$val ) {
 1209+ $sqlSet = $this->fieldBindStatement( $table, $col, $val, true );
 1210+
 1211+ if ( !$first ) {
 1212+ $sqlSet = ', ' . $sqlSet;
 1213+ } else {
 1214+ $first = false;
 1215+ }
 1216+ $sql .= $sqlSet;
 1217+ }
 1218+
 1219+ if ( $conds != '*' ) {
 1220+ $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
 1221+ }
 1222+
 1223+ if ( ( $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql ) ) === false ) {
 1224+ $e = oci_error( $this->mConn );
 1225+ $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
 1226+ return false;
 1227+ }
 1228+ foreach ( $values as $col => &$val ) {
 1229+ $col_info = $this->fieldInfoMulti( $table, $col );
 1230+ $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
 1231+
 1232+ if ( $val === null ) {
 1233+ // do nothing ... null was inserted in statement creation
 1234+ } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
 1235+ if ( is_object( $val ) ) {
 1236+ $val = $val->getData();
 1237+ }
 1238+
 1239+ if ( preg_match( '/^timestamp.*/i', $col_type ) == 1 && strtolower( $val ) == 'infinity' ) {
 1240+ $val = '31-12-2030 12:00:00.000000';
 1241+ }
 1242+
 1243+ $val = ( $wgContLang != null ) ? $wgContLang->checkTitleEncoding( $val ) : $val;
 1244+ if ( oci_bind_by_name( $stmt, ":$col", $val ) === false ) {
 1245+ $e = oci_error( $stmt );
 1246+ $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
 1247+ return false;
 1248+ }
 1249+ } else {
 1250+ if ( ( $lob[$col] = oci_new_descriptor( $this->mConn, OCI_D_LOB ) ) === false ) {
 1251+ $e = oci_error( $stmt );
 1252+ throw new DBUnexpectedError( $this, "Cannot create LOB descriptor: " . $e['message'] );
 1253+ }
 1254+
 1255+ if ( $col_type == 'BLOB' ) {
 1256+ $lob[$col]->writeTemporary( $val );
 1257+ oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, SQLT_BLOB );
 1258+ } else {
 1259+ $lob[$col]->writeTemporary( $val );
 1260+ oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, OCI_B_CLOB );
 1261+ }
 1262+ }
 1263+ }
 1264+
 1265+ wfSuppressWarnings();
 1266+
 1267+ if ( oci_execute( $stmt, OCI_DEFAULT ) === false ) {
 1268+ $e = oci_error( $stmt );
 1269+ if ( !$this->ignore_DUP_VAL_ON_INDEX || $e['code'] != '1' ) {
 1270+ $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
 1271+ return false;
 1272+ } else {
 1273+ $this->mAffectedRows = oci_num_rows( $stmt );
 1274+ }
 1275+ } else {
 1276+ $this->mAffectedRows = oci_num_rows( $stmt );
 1277+ }
 1278+
 1279+ wfRestoreWarnings();
 1280+
 1281+ if ( isset( $lob ) ) {
 1282+ foreach ( $lob as $lob_v ) {
 1283+ $lob_v->free();
 1284+ }
 1285+ }
 1286+
 1287+ if ( !$this->mTrxLevel ) {
 1288+ oci_commit( $this->mConn );
 1289+ }
 1290+
 1291+ oci_free_statement( $stmt );
 1292+ }
 1293+
11741294 function bitNot( $field ) {
11751295 // expecting bit-fields smaller than 4bytes
11761296 return 'BITNOT(' . $field . ')';

Comments

#Comment by Jack Phoenix (talk | contribs)   11:48, 25 October 2010

This seems to be reverting r75343 for this file, I'm quite sure that wasn't intended.

#Comment by Freakolowsky (talk | contribs)   12:07, 25 October 2010

fixed in r75350.

#Comment by Platonides (talk | contribs)   16:02, 25 October 2010

$wgContLang is used as local variable in line 1241, function update

#Comment by Freakolowsky (talk | contribs)   16:50, 25 October 2010

fixed in r75366.

Status & tagging log