Improved USE query parsing

pull/4605/head
Rene Cannao 2 years ago
parent eedb3978bf
commit 9f0af17249

@ -9,8 +9,6 @@
#include <iostream>
//#endif
#include "pcrecpp.h"
using namespace std;
@ -624,8 +622,14 @@ std::string pattern = "";
re2::RE2 re2("\\s*$", opt2);
re2::RE2::Replace(&dbname, re2, "");
if (dbname[0] == '`') {
if (dbname[dbname.length()-1] == '`') {
return dbname;
if (dbname.length() > 2) {
if (dbname[dbname.length()-1] == '`') {
// Remove the first character
dbname.erase(0, 1);
// Remove the last character
dbname.erase(dbname.length() - 1);
return dbname;
}
}
} else {
return dbname;
@ -698,46 +702,44 @@ void SetParser::test_parse_USE_query() {
// Define vector of pairs (query, expected dbname)
std::vector<std::pair<std::string, std::string>> testCases = {
{"USE my_database", "my_database"}, // Basic Case
{"USE my_database", "my_database"}, // Basic Case
{"USE my_database", "my_database"}, // Basic Case
{"USE my_database", "my_database"}, // Basic Case
{"USE my_database ", "my_database"}, // Basic Case
{"/* comment */USE dbname /* comment */", "dbname"}, // With Comments
{"/* comment */ USE dbname", "dbname"}, // With Comments
{"USE dbname /* comment */", "dbname"}, // With Comments
{"/* comment */USE `dbname` /* comment */", "`dbname`"}, // With backtick
{"/* comment */USE `dbname`/* comment */", "`dbname`"}, // With backtick
{"/* comment */USE`dbname` /* comment */", "`dbname`"}, // With backtick
{"/* comment */USE `dbname`/* comment */", "`dbname`"}, // With backtick
{"/* comment */USE dbname /* comment */", "dbname"}, // With Comments
{"/* comment */ USE dbname", "dbname"}, // With Comments
{"USE dbname /* comment */", "dbname"}, // With Comments
{"/* comment */USE `dbname` /* comment */", "dbname"}, // With backtick
{"/* comment */USE `dbname`/* comment */", "dbname"}, // With backtick
{"/* comment */USE`dbname` /* comment */", "dbname"}, // With backtick
{"/* comment */USE `dbname`/* comment */", "dbname"}, // With backtick
{"/* comment\nmultiline comment */USE dbname /* comment */", "dbname"}, // Multiline Comment
{"/* comment */USE dbname # comment", "dbname"}, // Hash Comment
{"/* comment */USE dbname -- comment", "dbname"}, // Double Dash Comment
{"/* comment */USE dbname # comment", "dbname"}, // Hash Comment
{"/* comment */USE dbname -- comment", "dbname"}, // Double Dash Comment
{"USE dbname # comment", "dbname"}, // Hash Comment
{"USE dbname -- comment", "dbname"}, // Double Dash Comment
{"/* comment */USE dbname # comment", "dbname"}, // Hash Comment
{"/* comment */USE dbname -- comment", "dbname"}, // Double Dash Comment
{"/* comment */USE dbname # comment", "dbname"}, // Hash Comment
{"/* comment */USE dbname -- comment", "dbname"}, // Double Dash Comment
{"USE dbname # comment", "dbname"}, // Hash Comment
{"USE dbname -- comment", "dbname"}, // Double Dash Comment
{"SELECT * FROM my_table", ""}, // No match
{"/*+ placeholder_comment */ USE test_use_comment", "test_use_comment"},
{"USE /*+ placeholder_comment */ `test_use_comment-a1`", "`test_use_comment-a1`"},
{"USE /*+ placeholder_comment */ `test_use_comment_1`", "`test_use_comment_1`"},
{"USE/*+ placeholder_comment */ `test_use_comment_2`", "`test_use_comment_2`"},
{"USE /*+ placeholder_comment */`test_use_comment_3`", "`test_use_comment_3`"},
{"USE /*+ placeholder_comment */ test_use_comment_4", "test_use_comment_4"},
{"USE/*+ placeholder_comment */ test_use_comment_5", "test_use_comment_5"},
{"USE /*+ placeholder_comment */test_use_comment_6", "test_use_comment_6"},
{"USE /*+ placeholder_comment */ `test_use_comment-1`", "`test_use_comment-1`"},
{"USE /*+ placeholder_comment */ `test_use_comment-a1`", "test_use_comment-a1"},
{"USE /*+ placeholder_comment */ `test_use_comment_1`", "test_use_comment_1"},
{"USE/*+ placeholder_comment */ `test_use_comment_2`", "test_use_comment_2"},
{"USE /*+ placeholder_comment */`test_use_comment_3`", "test_use_comment_3"},
{"USE /*+ placeholder_comment */ test_use_comment_4", "test_use_comment_4"},
{"USE/*+ placeholder_comment */ test_use_comment_5", "test_use_comment_5"},
{"USE /*+ placeholder_comment */test_use_comment_6", "test_use_comment_6"},
{"USE /*+ placeholder_comment */ `test_use_comment-1`", "test_use_comment-1"},
{"use my_database", "my_database"},
{"/* comment */ use dbname -- comment", "dbname"},
{"/* comment\nmultiline comment */USE dbname /* comment\nmultiline comment */", "dbname"}, // Multiline Comment
// db_query.push_back(std::make_tuple("`test_use_comment-2`", "USE/*+ placeholder_comment */ `test_use_comment-2`", false));
// db_query.push_back(std::make_tuple("`test_use_comment-3`", "USE /*+ placeholder_comment */`test_use_comment-3`", true));
// db_query.push_back(std::make_tuple("`test_use_comment-4`", "/*+ placeholder_comment */USE `test_use_comment-4`", false));
// db_query.push_back(std::make_tuple("`test_use_comment-5`", "USE/*+ placeholder_comment */`test_use_comment-5`", false));
// db_query.push_back(std::make_tuple("`test_use_comment-6`", "/* comment */USE`test_use_comment-6`", false));
// db_query.push_back(std::make_tuple("`test_use_comment-7`", "USE`test_use_comment-7`", false));
{"USE/*+ placeholder_comment */ `test_use_comment-2`", "test_use_comment-2"},
{"USE /*+ placeholder_comment */`test_use_comment-3`", "test_use_comment-3"},
{"/*+ placeholder_comment */USE `test_use_comment-4`", "test_use_comment-4"},
{"USE/*+ placeholder_comment */`test_use_comment-5`", "test_use_comment-5"},
{"/* comment */USE`test_use_comment-6`", "test_use_comment-6"},
{"USE`test_use_comment-7`", "test_use_comment-7"},
};
// Run tests for each pair
@ -745,7 +747,6 @@ void SetParser::test_parse_USE_query() {
set_query(p.first);
std::string dbname = parse_USE_query();
if (dbname != p.second) {
//std::cout << dbname << " : " << query << std::endl;
// we call parse_USE_query() again just to make it easier to create a breakpoint
std::string s = parse_USE_query();
assert(s == p.second);

Loading…
Cancel
Save