Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #42 - allow for quotes in MySQL connect DSN #48

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion duckdb
Submodule duckdb updated 1257 files
73 changes: 64 additions & 9 deletions src/mysql_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,76 @@

namespace duckdb {

static bool ParseValue(const string &dsn, idx_t &pos, string &result) {
// skip leading spaces
while(pos < dsn.size() && StringUtil::CharacterIsSpace(dsn[pos])) {
pos++;
}
if (pos >= dsn.size()) {
return false;
}
// check if we are parsing a quoted value or not
if (dsn[pos] == '"') {
pos++;
// scan until we find another quote
bool found_quote = false;
for(; pos < dsn.size(); pos++) {
if (dsn[pos] == '"') {
found_quote = true;
pos++;
break;
}
if (dsn[pos] == '\\') {
// backslash escapes the backslash or double-quote
if (pos + 1 >= dsn.size()) {
throw InvalidInputException("Invalid dsn \"%s\" - backslash at end of dsn", dsn);
}
if (dsn[pos + 1] != '\\' && dsn[pos + 1] != '"') {
throw InvalidInputException("Invalid dsn \"%s\" - backslash can only escape \\ or \"", dsn);
}
result += dsn[pos + 1];
pos++;
} else {
result += dsn[pos];
}
}
if (!found_quote) {
throw InvalidInputException("Invalid dsn \"%s\" - unterminated quote", dsn);
}
} else {
// unquoted value, continue until space, equality sign or end of string
for(; pos < dsn.size(); pos++) {
if (dsn[pos] == '=') {
break;
}
if (StringUtil::CharacterIsSpace(dsn[pos])) {
break;
}
result += dsn[pos];
}
}
return true;
}

MySQLConnectionParameters MySQLUtils::ParseConnectionParameters(const string &dsn) {
MySQLConnectionParameters result;
// parse options
auto parameters = StringUtil::Split(dsn, ' ');
for (auto &param : parameters) {
StringUtil::Trim(param);
if (param.empty()) {
continue;

idx_t pos = 0;
while(pos < dsn.size()) {
string key;
string value;
if (!ParseValue(dsn, pos, key)) {
break;
}
if (pos >= dsn.size() || dsn[pos] != '=') {
throw InvalidInputException("Invalid dsn \"%s\" - expected key=value pairs separated by spaces", dsn);
}
auto splits = StringUtil::Split(param, '=');
if (splits.size() != 2) {
pos++;
if (!ParseValue(dsn, pos, value)) {
throw InvalidInputException("Invalid dsn \"%s\" - expected key=value pairs separated by spaces", dsn);
}
auto key = StringUtil::Lower(splits[0]);
auto &value = splits[1];
key = StringUtil::Lower(key);
if (key == "host") {
result.host = value;
} else if (key == "user") {
Expand Down
38 changes: 38 additions & 0 deletions test/sql/attach_dsn.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# name: test/sql/attach_dsn.test
# description: Test attaching with complex DSNs
# group: [sql]

require mysql_scanner

require-env MYSQL_TEST_DATABASE_AVAILABLE

# dsn parsing failures
statement error
ATTACH 'host' AS s (TYPE MYSQL_SCANNER)
----
expected key=value pairs separated by spaces

statement error
ATTACH 'host=' AS s (TYPE MYSQL_SCANNER)
----
expected key=value pairs separated by spaces

statement error
ATTACH 'host="' AS s (TYPE MYSQL_SCANNER)
----
unterminated quote

statement error
ATTACH 'host="\' AS s (TYPE MYSQL_SCANNER)
----
backslash at end of dsn

statement error
ATTACH 'host="\a' AS s (TYPE MYSQL_SCANNER)
----
backslash can only escape

statement error
ATTACH 'host="this string contains \"quoted\" \\spaces"' AS s (TYPE MYSQL_SCANNER)
----
this string contains "quoted" \spaces
2 changes: 1 addition & 1 deletion test/sql/attach_fake_booleans.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ statement ok
SET GLOBAL mysql_experimental_filter_pushdown=true;

statement ok
ATTACH 'host=localhost user=root port=0 database=mysqlscanner' AS s1 (TYPE MYSQL_SCANNER)
ATTACH ' host="localhost" user=root port=0 database=mysqlscanner ' AS s1 (TYPE MYSQL_SCANNER)

query II
SELECT * FROM s1.fake_booleans
Expand Down
2 changes: 1 addition & 1 deletion test/sql/attach_filter_pushdown.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ statement ok
SET GLOBAL mysql_experimental_filter_pushdown=true;

statement ok
ATTACH 'host=localhost user=root port=0 database=mysqlscanner' AS s1 (TYPE MYSQL_SCANNER)
ATTACH 'host=localhost user=root port="0" database="mysqlscanner"' AS s1 (TYPE MYSQL_SCANNER)

statement ok
CREATE OR REPLACE TABLE s1.filter_pushdown(i INTEGER)
Expand Down
Loading