diff --git a/ChangeLog b/ChangeLog index 0b9c39572d..5ee8714d13 100644 --- a/ChangeLog +++ b/ChangeLog @@ -20,7 +20,8 @@ phpMyAdmin - ChangeLog - bug #3941 Recent tables list always empty - bug #3933 Do not translate "Open Document" in export settings - bug #3927 List of tables is missing after expanding in the navigation frame -- bug #3942 Warnings about reserved word for many non reserved words +- bug #3942 Warnings about reserved word for many non reserved words +- bug #3912 Exporting row selection, resulted by ORDER BY query 4.0.2.0 (2013-05-24) - bug #3902 Cannot browse when table name contains keyword "call" diff --git a/libraries/database_interface.lib.php b/libraries/database_interface.lib.php index abf8205ce3..233acf05f6 100644 --- a/libraries/database_interface.lib.php +++ b/libraries/database_interface.lib.php @@ -2099,4 +2099,36 @@ function PMA_isSystemSchema($schema_name, $test_for_mysql_schema = false) || (PMA_DRIZZLE && strtolower($schema_name) == 'data_dictionary') || ($test_for_mysql_schema && !PMA_DRIZZLE && $schema_name == 'mysql'); } + +/** + * Get regular expression which occur first inside the given sql query. + * + * @param Array $regex_array Comparing regular expressions. + * @param String $query SQL query to be checked. + * + * @return String Matching regular expression. + */ +function PMA_getFirstOccurringRegularExpression($regex_array, $query) +{ + + $minimum_first_occurence_index = null; + $regex = null; + + for ($i = 0; $i < count($regex_array); $i++) { + if (preg_match($regex_array[$i], $query, $matches, PREG_OFFSET_CAPTURE)) { + + if (is_null($minimum_first_occurence_index) + || ($matches[0][1] < $minimum_first_occurence_index) + ) { + $regex = $regex_array[$i]; + $minimum_first_occurence_index = $matches[0][1]; + } + + } + } + + return $regex; + +} + ?> diff --git a/tbl_export.php b/tbl_export.php index 557dcb352b..fbc585c6e9 100644 --- a/tbl_export.php +++ b/tbl_export.php @@ -37,14 +37,27 @@ if (! empty($sql_query)) { // Need to generate WHERE clause? if (isset($where_clause)) { - $temp_sql_array = preg_split("/\bwhere\b/i", $sql_query); + // Regular expressions which can appear in sql query, + // before the sql segment which remains as it is. + $regex_array = array( + '/\bwhere\b/i', '/\bgroup by\b/i', '/\bhaving\b/i', '/\border by\b/i' + ); + + $first_occurring_regex = PMA_getFirstOccurringRegularExpression( + $regex_array, $sql_query + ); + unset($regex_array); // The part "SELECT `id`, `name` FROM `customers`" // is not modified by the next code segment, when exporting // the result set from a query such as // "SELECT `id`, `name` FROM `customers` WHERE id NOT IN // ( SELECT id FROM companies WHERE name LIKE '%u%')" - $sql_query = $temp_sql_array[0]; + if (! is_null($first_occurring_regex)) { + $temp_sql_array = preg_split($first_occurring_regex, $sql_query); + $sql_query = $temp_sql_array[0]; + } + unset($first_occurring_regex, $temp_sql_array); // Append the where clause using the primary key of each row if (is_array($where_clause) && (count($where_clause) > 0)) {