r37119 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r37118‎ | r37119 | r37120 >
Date:17:00, 5 July 2008
Author:greg
Status:old
Tags:
Comment:
Handle multi-insert ignores properly, as pointed out on wikitech.
Modified paths:
  • /trunk/phase3/includes/db/DatabasePostgres.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabasePostgres.php
@@ -2,16 +2,8 @@
33 /**
44 * @ingroup Database
55 * @file
6 - */
7 -
8 -/**
96 * This is the Postgres database abstraction layer.
107 *
11 - * As it includes more generic version for DB functions,
12 - * than MySQL ones, some of them should be moved to parent
13 - * Database class.
14 - *
15 - * @ingroup Database
168 */
179 class PostgresField {
1810 private $name, $tablename, $type, $nullable, $max_length;
@@ -747,20 +739,26 @@
748740 $keys = array_keys( $args );
749741 }
750742
 743+ // If IGNORE is set, we use savepoints to emulate mysql's behavior
751744 $ignore = in_array( 'IGNORE', $options ) ? 'mw' : '';
 745+
 746+ // If we are not in a transaction, we need to be for savepoint trickery
 747+ $didbegin = 0;
752748 if ( $ignore ) {
753 - $didbegin = 0;
754749 if (! $this->mTrxLevel) {
755750 $this->begin();
756751 $didbegin = 1;
757752 }
758 - pg_query($this->mConn, "SAVEPOINT $ignore");
759753 $olde = error_reporting( 0 );
 754+ // For future use, we may want to track the number of actual inserts
 755+ // Right now, insert (all writes) simply return true/false
 756+ $numrowsinserted = 0;
760757 }
 758+
761759 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
762760
763761 if ( $multi ) {
764 - if ( $wgDBversion >= 8.2 ) {
 762+ if ( $wgDBversion >= 8.2 && !$ignore ) {
765763 $first = true;
766764 foreach ( $args as $row ) {
767765 if ( $first ) {
@@ -778,33 +776,63 @@
779777 foreach ( $args as $row ) {
780778 $tempsql = $origsql;
781779 $tempsql .= '(' . $this->makeList( $row ) . ')';
 780+
 781+ if ( $ignore ) {
 782+ pg_query($this->mConn, "SAVEPOINT $ignore");
 783+ }
 784+
782785 $tempres = (bool)$this->query( $tempsql, $fname, $ignore );
 786+
 787+ if ( $ignore ) {
 788+ $bar = pg_last_error();
 789+ if ($bar != false) {
 790+ pg_query( $this->mConn, "ROLLBACK TO $ignore" );
 791+ }
 792+ else {
 793+ pg_query( $this->mConn, "RELEASE $ignore" );
 794+ $numrowsinserted++;
 795+ }
 796+ }
 797+
 798+ // If any of them fail, we fail overall for this function call
 799+ // Note that this will be ignored if IGNORE is set
783800 if (! $tempres)
784801 $res = false;
785802 }
786803 }
787804 }
788805 else {
 806+ // Not multi, just a lone insert
 807+ if ( $ignore ) {
 808+ pg_query($this->mConn, "SAVEPOINT $ignore");
 809+ }
 810+
789811 $sql .= '(' . $this->makeList( $args ) . ')';
790812 $res = (bool)$this->query( $sql, $fname, $ignore );
 813+
 814+ if ( $ignore ) {
 815+ $bar = pg_last_error();
 816+ if ($bar != false) {
 817+ pg_query( $this->mConn, "ROLLBACK TO $ignore" );
 818+ }
 819+ else {
 820+ pg_query( $this->mConn, "RELEASE $ignore" );
 821+ $numrowsinserted++;
 822+ }
 823+ }
791824 }
792825
793826 if ( $ignore ) {
794827 $olde = error_reporting( $olde );
795 - $bar = pg_last_error();
796 - if ($bar !== FALSE) {
797 - pg_query( $this->mConn, "ROLLBACK TO $ignore" );
798 - }
799 - else {
800 - pg_query( $this->mConn, "RELEASE $ignore" );
801 - }
802828 if ($didbegin) {
803829 $this->commit();
804830 }
805 - ## IGNORE always returns true
 831+
 832+ // IGNORE always returns true
806833 return true;
807834 }
808835
 836+
809837 return $res;
810838
811839 }

Status & tagging log