Skip to content

Commit

Permalink
Add \r\n new line support in sqllogictest
Browse files Browse the repository at this point in the history
Signed-off-by: Wenbo Li <[email protected]>
  • Loading branch information
hnjylwb committed Mar 17, 2024
1 parent 6c8372f commit ebd71b9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
53 changes: 28 additions & 25 deletions test/sqllogicparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ bool SQLLogicParser::OpenFile(fs::path path) {
}
std::string line;
while (std::getline(file, line)) {
lines.push_back(line);
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
lines_.push_back(line);
}
return !file.bad();
}

std::vector<std::string> SQLLogicParser::Tokenize() {
const auto &line = *line_iter;
const auto &line = *line_iter_;

const char delimiter = ' ';
std::string::size_type pos;
Expand All @@ -44,18 +47,18 @@ std::vector<std::string> SQLLogicParser::Tokenize() {
}

void SQLLogicParser::Parse() {
line_iter = lines.cbegin();
while (line_iter != lines.cend()) {
const auto &line = *line_iter;
line_iter_ = lines_.cbegin();
while (line_iter_ != lines_.cend()) {
const auto &line = *line_iter_;
if (line.empty() || line[0] == '#' || line == "\r") {
line_iter++;
line_iter_++;
continue;
}
auto loc = Location{path_.parent_path().filename().string() + "/" + path_.filename().string(),
static_cast<size_t>(std::distance(lines.cbegin(), line_iter) + 1)};
static_cast<size_t>(std::distance(lines_.cbegin(), line_iter_) + 1)};
auto tokens = Tokenize();
if (tokens.empty()) {
line_iter++;
line_iter_++;
continue;
}
if (tokens[0] == "statement") {
Expand All @@ -74,22 +77,22 @@ void SQLLogicParser::Parse() {
if (tokens.size() > 2) {
connection_name = tokens[2];
}
if (line_iter != lines.cend()) {
line_iter++;
if (line_iter_ != lines_.cend()) {
line_iter_++;
}
std::string sql;
while (line_iter != lines.cend()) {
const auto &line = *line_iter;
while (line_iter_ != lines_.cend()) {
const auto &line = *line_iter_;
if (line.empty()) {
break;
}
sql += line;
sql += "\n";
line_iter++;
line_iter_++;
}
records.emplace_back(
std::make_unique<StatementRecord>(loc, std::move(sql), result_type, std::move(connection_name)));
if (line_iter == lines.end()) {
if (line_iter_ == lines_.end()) {
break;
}
} else if (tokens[0] == "query") {
Expand All @@ -107,46 +110,46 @@ void SQLLogicParser::Parse() {
if (tokens.size() > 2) {
connection_name = tokens[2];
}
line_iter++;
if (line_iter == lines.cend()) {
line_iter_++;
if (line_iter_ == lines_.cend()) {
throw huadb::DbException("Unexpected end of file");
}

std::string sql;
bool has_result = false;
while (line_iter != lines.cend()) {
const auto &line = *line_iter;
while (line_iter_ != lines_.cend()) {
const auto &line = *line_iter_;
if (line == "----") {
line_iter++;
line_iter_++;
has_result = true;
break;
}
sql += line;
sql += "\n";
line_iter++;
line_iter_++;
}
if (!has_result) {
throw huadb::DbException(loc.ToString() + ": No result for query record");
}

std::string result;
while (line_iter != lines.end()) {
const auto &line = *line_iter;
while (line_iter_ != lines_.end()) {
const auto &line = *line_iter_;
if (line.empty()) {
break;
}
result += line;
result += "\n";
line_iter++;
line_iter_++;
}
records.emplace_back(
std::make_unique<QueryRecord>(loc, std::move(sql), sort_mode, std::move(connection_name), std::move(result)));
if (line_iter == lines.end()) {
if (line_iter_ == lines_.end()) {
break;
}
} else {
throw huadb::DbException(loc.ToString() + ": Unknown command " + tokens[0]);
}
line_iter++;
line_iter_++;
}
}
4 changes: 2 additions & 2 deletions test/sqllogicparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ class SQLLogicParser {
std::vector<std::string> Tokenize();

fs::path path_;
std::vector<std::string> lines;
std::vector<std::string>::const_iterator line_iter;
std::vector<std::string> lines_;
std::vector<std::string>::const_iterator line_iter_;
};

0 comments on commit ebd71b9

Please sign in to comment.