Index: trunk/phase3/tests/phpunit/includes/db/DatabaseTest.php |
— | — | @@ -2,14 +2,22 @@ |
3 | 3 | |
4 | 4 | /** |
5 | 5 | * @group Database |
| 6 | + * @group DatabaseBase |
6 | 7 | */ |
7 | 8 | class DatabaseTest extends MediaWikiTestCase { |
8 | | - var $db; |
| 9 | + var $db, $functionTest = false; |
9 | 10 | |
10 | 11 | function setUp() { |
11 | | - $this->db = wfGetDB( DB_SLAVE ); |
| 12 | + $this->db = wfGetDB( DB_MASTER ); |
12 | 13 | } |
13 | 14 | |
| 15 | + function tearDown() { |
| 16 | + if ( $this->functionTest ) { |
| 17 | + $this->dropFunctions(); |
| 18 | + $this->functionTest = false; |
| 19 | + } |
| 20 | + } |
| 21 | + |
14 | 22 | function testAddQuotesNull() { |
15 | 23 | $check = "NULL"; |
16 | 24 | if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) { |
— | — | @@ -90,6 +98,26 @@ |
91 | 99 | $sql ); |
92 | 100 | } |
93 | 101 | |
| 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 | + } |
94 | 122 | } |
95 | 123 | |
96 | 124 | |
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 |
1 | 14 | + 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 |
1 | 14 | + native |
Index: trunk/phase3/includes/db/DatabaseMysql.php |
— | — | @@ -670,6 +670,15 @@ |
671 | 671 | } |
672 | 672 | } |
673 | 673 | |
| 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 | + |
674 | 683 | /** |
675 | 684 | * @param $lockName string |
676 | 685 | * @param $method string |
Index: trunk/phase3/includes/db/DatabasePostgres.php |
— | — | @@ -1045,4 +1045,17 @@ |
1046 | 1046 | public function getSearchEngine() { |
1047 | 1047 | return 'SearchPostgres'; |
1048 | 1048 | } |
| 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 | + } |
1049 | 1062 | } // end DatabasePostgres class |
Index: trunk/phase3/includes/db/Database.php |
— | — | @@ -228,6 +228,8 @@ |
229 | 229 | |
230 | 230 | protected $htmlErrors; |
231 | 231 | |
| 232 | + protected $delimiter = ';'; |
| 233 | + |
232 | 234 | # ------------------------------------------------------------------------------ |
233 | 235 | # Accessors |
234 | 236 | # ------------------------------------------------------------------------------ |
— | — | @@ -3154,19 +3156,17 @@ |
3155 | 3157 | function sourceStream( $fp, $lineCallback = false, $resultCallback = false, |
3156 | 3158 | $fname = 'DatabaseBase::sourceStream' ) |
3157 | 3159 | { |
3158 | | - $cmd = ""; |
| 3160 | + $cmd = ''; |
3159 | 3161 | $done = false; |
3160 | | - $dollarquote = false; |
3161 | 3162 | |
3162 | | - while ( ! feof( $fp ) ) { |
| 3163 | + while ( !feof( $fp ) ) { |
3163 | 3164 | if ( $lineCallback ) { |
3164 | 3165 | call_user_func( $lineCallback ); |
3165 | 3166 | } |
3166 | 3167 | |
3167 | 3168 | $line = trim( fgets( $fp ) ); |
3168 | | - $sl = strlen( $line ) - 1; |
3169 | 3169 | |
3170 | | - if ( $sl < 0 ) { |
| 3170 | + if ( $line == '' ) { |
3171 | 3171 | continue; |
3172 | 3172 | } |
3173 | 3173 | |
— | — | @@ -3174,31 +3174,15 @@ |
3175 | 3175 | continue; |
3176 | 3176 | } |
3177 | 3177 | |
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 | | - |
3195 | 3178 | if ( $cmd != '' ) { |
3196 | 3179 | $cmd .= ' '; |
3197 | 3180 | } |
3198 | 3181 | |
| 3182 | + $done = $this->streamStatementEnd( $cmd, $line ); |
| 3183 | + |
3199 | 3184 | $cmd .= "$line\n"; |
3200 | 3185 | |
3201 | | - if ( $done ) { |
3202 | | - $cmd = str_replace( ';;', ";", $cmd ); |
| 3186 | + if ( $done || feof( $fp ) ) { |
3203 | 3187 | $cmd = $this->replaceVars( $cmd ); |
3204 | 3188 | $res = $this->query( $cmd, $fname ); |
3205 | 3189 | |
— | — | @@ -3220,6 +3204,24 @@ |
3221 | 3205 | } |
3222 | 3206 | |
3223 | 3207 | /** |
| 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 | + /** |
3224 | 3226 | * Database independent variable replacement. Replaces a set of variables |
3225 | 3227 | * in an SQL statement with their contents as given by $this->getSchemaVars(). |
3226 | 3228 | * |