r108671 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108670‎ | r108671 | r108672 >
Date:20:19, 11 January 2012
Author:maxsem
Status:ok
Tags:
Comment:
Revert r108603, which was itself a revert of r107376, r107994. Before considering something unneeded, please ask first ;)
Modified paths:
  • /trunk/phase3/includes/db/Database.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseMysql.php (modified) (history)
  • /trunk/phase3/includes/db/DatabasePostgres.php (modified) (history)
  • /trunk/phase3/tests/phpunit/data/db/mysql (added) (history)
  • /trunk/phase3/tests/phpunit/data/db/postgres (added) (history)
  • /trunk/phase3/tests/phpunit/includes/db/DatabaseTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/db/DatabaseTest.php
@@ -2,14 +2,22 @@
33
44 /**
55 * @group Database
 6+ * @group DatabaseBase
67 */
78 class DatabaseTest extends MediaWikiTestCase {
8 - var $db;
 9+ var $db, $functionTest = false;
910
1011 function setUp() {
11 - $this->db = wfGetDB( DB_SLAVE );
 12+ $this->db = wfGetDB( DB_MASTER );
1213 }
1314
 15+ function tearDown() {
 16+ if ( $this->functionTest ) {
 17+ $this->dropFunctions();
 18+ $this->functionTest = false;
 19+ }
 20+ }
 21+
1422 function testAddQuotesNull() {
1523 $check = "NULL";
1624 if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
@@ -90,6 +98,26 @@
9199 $sql );
92100 }
93101
 102+ /**
 103+ * @group Broken
 104+ */
 105+ function testStoredFunctions() {
 106+ if ( !in_array( wfGetDB( DB_MASTER )->getType(), array( 'mysql', 'postgres' ) ) ) {
 107+ $this->markTestSkipped( 'MySQL or Postgres required' );
 108+ }
 109+ global $IP;
 110+ $this->dropFunctions();
 111+ $this->functionTest = true;
 112+ $this->assertTrue( $this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" ) );
 113+ $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
 114+ $this->assertEquals( 42, $res->fetchObject()->test );
 115+ }
 116+
 117+ private function dropFunctions() {
 118+ $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
 119+ . ( $this->db->getType() == 'postgres' ? '()' : '' )
 120+ );
 121+ }
94122 }
95123
96124
Index: trunk/phase3/tests/phpunit/data/db/mysql/functions.sql
@@ -0,0 +1,12 @@
 2+-- MySQL test file for DatabaseTest::testStoredFunctions()
 3+
 4+DELIMITER //
 5+
 6+CREATE FUNCTION mw_test_function()
 7+RETURNS int DETERMINISTIC
 8+BEGIN
 9+ SET @foo = 21;
 10+ RETURN @foo * 2;
 11+END//
 12+
 13+DELIMITER //
Property changes on: trunk/phase3/tests/phpunit/data/db/mysql/functions.sql
___________________________________________________________________
Added: svn:eol-style
114 + native
Index: trunk/phase3/tests/phpunit/data/db/postgres/functions.sql
@@ -0,0 +1,12 @@
 2+-- Postgres test file for DatabaseTest::testStoredFunctions()
 3+
 4+CREATE FUNCTION mw_test_function()
 5+RETURNS INTEGER
 6+LANGUAGE plpgsql AS
 7+$mw$
 8+DECLARE foo INTEGER;
 9+BEGIN
 10+ foo := 21;
 11+ RETURN foo * 2;
 12+END
 13+$mw$;
Property changes on: trunk/phase3/tests/phpunit/data/db/postgres/functions.sql
___________________________________________________________________
Added: svn:eol-style
114 + native
Index: trunk/phase3/includes/db/DatabaseMysql.php
@@ -670,6 +670,15 @@
671671 }
672672 }
673673
 674+ protected function streamStatementEnd( &$sql, &$newLine ) {
 675+ if ( strtoupper( substr( $newLine, 0, 9 ) ) == 'DELIMITER' ) {
 676+ preg_match( '/^DELIMITER\s+(\S+)/' , $newLine, $m );
 677+ $this->delimiter = $m[1];
 678+ $newLine = '';
 679+ }
 680+ return parent::streamStatementEnd( $sql, $newLine );
 681+ }
 682+
674683 /**
675684 * @param $lockName string
676685 * @param $method string
Index: trunk/phase3/includes/db/DatabasePostgres.php
@@ -1045,4 +1045,17 @@
10461046 public function getSearchEngine() {
10471047 return 'SearchPostgres';
10481048 }
 1049+
 1050+ protected function streamStatementEnd( &$sql, &$newLine ) {
 1051+ # Allow dollar quoting for function declarations
 1052+ if ( substr( $newLine, 0, 4 ) == '$mw$' ) {
 1053+ if ( $this->delimiter ) {
 1054+ $this->delimiter = false;
 1055+ }
 1056+ else {
 1057+ $this->delimiter = ';';
 1058+ }
 1059+ }
 1060+ return parent::streamStatementEnd( $sql, $newLine );
 1061+ }
10491062 } // end DatabasePostgres class
Index: trunk/phase3/includes/db/Database.php
@@ -228,6 +228,8 @@
229229
230230 protected $htmlErrors;
231231
 232+ protected $delimiter = ';';
 233+
232234 # ------------------------------------------------------------------------------
233235 # Accessors
234236 # ------------------------------------------------------------------------------
@@ -3154,19 +3156,17 @@
31553157 function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
31563158 $fname = 'DatabaseBase::sourceStream' )
31573159 {
3158 - $cmd = "";
 3160+ $cmd = '';
31593161 $done = false;
3160 - $dollarquote = false;
31613162
3162 - while ( ! feof( $fp ) ) {
 3163+ while ( !feof( $fp ) ) {
31633164 if ( $lineCallback ) {
31643165 call_user_func( $lineCallback );
31653166 }
31663167
31673168 $line = trim( fgets( $fp ) );
3168 - $sl = strlen( $line ) - 1;
31693169
3170 - if ( $sl < 0 ) {
 3170+ if ( $line == '' ) {
31713171 continue;
31723172 }
31733173
@@ -3174,31 +3174,15 @@
31753175 continue;
31763176 }
31773177
3178 - # # Allow dollar quoting for function declarations
3179 - if ( substr( $line, 0, 4 ) == '$mw$' ) {
3180 - if ( $dollarquote ) {
3181 - $dollarquote = false;
3182 - $done = true;
3183 - }
3184 - else {
3185 - $dollarquote = true;
3186 - }
3187 - }
3188 - elseif ( !$dollarquote ) {
3189 - if ( ';' == $line[$sl] && ( $sl < 2 || ';' != $line[$sl - 1] ) ) {
3190 - $done = true;
3191 - $line = substr( $line, 0, $sl );
3192 - }
3193 - }
3194 -
31953178 if ( $cmd != '' ) {
31963179 $cmd .= ' ';
31973180 }
31983181
 3182+ $done = $this->streamStatementEnd( $cmd, $line );
 3183+
31993184 $cmd .= "$line\n";
32003185
3201 - if ( $done ) {
3202 - $cmd = str_replace( ';;', ";", $cmd );
 3186+ if ( $done || feof( $fp ) ) {
32033187 $cmd = $this->replaceVars( $cmd );
32043188 $res = $this->query( $cmd, $fname );
32053189
@@ -3220,6 +3204,24 @@
32213205 }
32223206
32233207 /**
 3208+ * Called by sourceStream() to check if we've reached a statement end
 3209+ *
 3210+ * @param $sql String: SQL assembled so far
 3211+ * @param $newLine String: New line about to be added to $sql
 3212+ * @returns Bool: Whether $newLine contains end of the statement
 3213+ */
 3214+ protected function streamStatementEnd( &$sql, &$newLine ) {
 3215+ if ( $this->delimiter ) {
 3216+ $prev = $newLine;
 3217+ $newLine = preg_replace( '/' . preg_quote( $this->delimiter, '/' ) . '$/', '', $newLine );
 3218+ if ( $newLine != $prev ) {
 3219+ return true;
 3220+ }
 3221+ }
 3222+ return false;
 3223+ }
 3224+
 3225+ /**
32243226 * Database independent variable replacement. Replaces a set of variables
32253227 * in an SQL statement with their contents as given by $this->getSchemaVars().
32263228 *

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r107376Added support for stored procedures/functions to MySQL:...maxsem12:29, 27 December 2011
r107994Follow-up r107376: disable test by default, causes failures in some configura...maxsem08:38, 4 January 2012
r108603Reverts MySQL stored procedure support...hashar09:46, 11 January 2012

Status & tagging log