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

feat: display comment on result file #59

Merged
merged 2 commits into from
Jul 3, 2023
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
1 change: 1 addition & 0 deletions sqlness-cli/tests/local/input.result
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html
DROP TABLE IF EXISTS animals;

affected_rows: 0
Expand Down
1 change: 1 addition & 0 deletions sqlness/examples/interceptor-replace/simple/replace.result
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ SELECT 0;

SELECT 1;

-- example of capture group replacement
-- SQLNESS REPLACE (?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2}) $m/$d/$y
2012-03-14, 2013-01-01 and 2014-07-05;

Expand Down
27 changes: 18 additions & 9 deletions sqlness/src/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ impl TestCase {
let reader = BufReader::new(file);
for line in reader.lines() {
let line = line?;
// intercept command start with INTERCEPTOR_PREFIX
if line.starts_with(&cfg.interceptor_prefix) {
query.push_interceptor(&cfg.interceptor_prefix, line);

// record comment
if line.starts_with(COMMENT_PREFIX) {
query.push_comment(line.clone());

// intercept command start with INTERCEPTOR_PREFIX
if line.starts_with(&cfg.interceptor_prefix) {
query.push_interceptor(&cfg.interceptor_prefix, line);
}
continue;
}

// ignore comment and empty line
if line.starts_with(COMMENT_PREFIX) || line.is_empty() {
// ignore empty line
if line.is_empty() {
continue;
}

Expand Down Expand Up @@ -89,8 +95,8 @@ pub struct QueryContext {

#[derive(Default)]
struct Query {
comment_lines: Vec<String>,
query_lines: Vec<String>,
interceptor_lines: Vec<String>,
interceptor_factories: Vec<InterceptorFactoryRef>,
interceptors: Vec<InterceptorRef>,
}
Expand All @@ -112,7 +118,10 @@ impl Query {
self.interceptors.push(interceptor);
}
}
self.interceptor_lines.push(interceptor_line);
}

fn push_comment(&mut self, comment_line: String) {
self.comment_lines.push(comment_line);
}

fn append_query_line(&mut self, line: &str) {
Expand Down Expand Up @@ -165,8 +174,8 @@ impl Query {
where
W: Write,
{
for interceptor in &self.interceptor_lines {
writer.write_all(interceptor.as_bytes())?;
for comment in &self.comment_lines {
writer.write_all(comment.as_bytes())?;
writer.write("\n".as_bytes())?;
}
for line in &self.query_lines {
Expand Down