Skip to content

Commit 0b48c04

Browse files
Merge pull request #5 from davidsmfreire/refactor/fix-issues
Refactor | Fix issues
2 parents 5a922e1 + 9bbe154 commit 0b48c04

5 files changed

Lines changed: 65 additions & 48 deletions

File tree

sqlshield/src/finder/mod.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ pub fn find_queries_in_code(
3434
match file_extension.as_ref() {
3535
"py" => (
3636
tree_sitter_python::language(),
37-
python::extract_query_from_node,
37+
python::extract_query_string_from_node,
38+
),
39+
"rs" => (
40+
tree_sitter_rust::language(),
41+
rust::extract_query_string_from_node,
3842
),
39-
"rs" => (tree_sitter_rust::language(), rust::extract_query_from_node),
4043
_ => panic!("{}", format!("File not supported {file_extension}")),
4144
};
4245

@@ -78,39 +81,33 @@ fn find_queries_in_ast(
7881
let mut cursor = node.walk();
7982

8083
for child in node.children(&mut cursor) {
81-
let mut child_cursor = child.walk();
82-
83-
for component in child.children(&mut child_cursor) {
84-
match query_extractor(&component, code) {
85-
Some(string_content) => {
86-
// ! Duct tape
87-
if string_content.find("REPLACE").is_some() {
88-
continue;
89-
}
90-
// !
84+
match query_extractor(&child, code) {
85+
Some(string_content) => {
86+
// ! Duct tape
87+
if string_content.find("REPLACE").is_some() {
88+
find_queries_in_ast(&child, code, query_extractor, dialect, queries, None)
89+
}
90+
// !
9191

92-
let query_at = component.start_position();
92+
let query_at = child.start_position();
9393

94-
let statements = sqlparser::parser::Parser::parse_sql(dialect, &string_content);
94+
let statements = sqlparser::parser::Parser::parse_sql(dialect, &string_content);
9595

96-
match statements {
97-
Ok(statements) => {
98-
queries.push(super::QueryInCode {
99-
line: query_at.row + 1,
100-
statements,
101-
});
102-
}
103-
Err(err) => {
104-
if verbose.unwrap_or(0) > 0 {
105-
eprintln!("{err} {string_content}");
106-
}
96+
match statements {
97+
Ok(statements) => {
98+
queries.push(super::QueryInCode {
99+
line: query_at.row + 1,
100+
statements,
101+
});
102+
}
103+
Err(err) => {
104+
if verbose.unwrap_or(0) > 0 {
105+
eprintln!("{err} {string_content}");
107106
}
108107
}
109108
}
110-
None => continue,
111109
}
110+
None => find_queries_in_ast(&child, code, query_extractor, dialect, queries, None),
112111
}
113-
114-
find_queries_in_ast(&child, code, query_extractor, dialect, queries, None);
115112
}
116113
}

sqlshield/src/finder/python.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use regex::Regex;
22

3-
pub fn extract_query_from_node(node: &tree_sitter::Node, code: &[u8]) -> Option<String> {
3+
pub fn extract_query_string_from_node(node: &tree_sitter::Node, code: &[u8]) -> Option<String> {
44
if node.kind() != "string" {
55
return None;
66
}

sqlshield/src/finder/rust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use regex::Regex;
22

3-
pub fn extract_query_from_node(node: &tree_sitter::Node, code: &[u8]) -> Option<String> {
3+
pub fn extract_query_string_from_node(node: &tree_sitter::Node, code: &[u8]) -> Option<String> {
44
if node.kind() != "string_literal" {
55
return None;
66
}

sqlshield/src/schema/mod.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,17 @@ pub fn load_schema_from_file(file_path: &Path) -> Result<TablesAndColumns, Strin
1313
let file_extension = &file_path.extension().unwrap().to_string_lossy();
1414

1515
match schema {
16-
Ok(schema) => match file_extension.as_ref() {
17-
"sql" => sql::load_schema(&schema),
18-
_ => Err(format!("File not supported {file_extension}")),
19-
},
16+
Ok(schema) => load_schema(&schema, file_extension),
2017
Err(err) => Err(format!(
2118
"Could not open {:?} due to error: {err}",
2219
file_path
2320
)),
2421
}
2522
}
2623

27-
trait LoadSchema {
28-
fn to_schema(&self, schema_type: &str) -> Result<TablesAndColumns, String>;
29-
}
30-
31-
impl LoadSchema for String {
32-
fn to_schema(&self, schema_type: &str) -> Result<TablesAndColumns, String> {
33-
load_schema(&self.as_bytes(), schema_type)
34-
}
35-
}
36-
3724
pub fn load_schema(schema: &[u8], schema_type: &str) -> Result<TablesAndColumns, String> {
3825
match schema_type.as_ref() {
39-
"sql" => sql::load_schema(&schema),
40-
_ => Err(format!("Schema type not supported {schema_type}")),
26+
"sql" => sql::load_schema(schema),
27+
_ => panic!("{}", format!("Schema type not supported {schema_type}")),
4128
}
4229
}

sqlshield/src/schema/sql.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use sqlparser::{ast::Statement, dialect::GenericDialect, parser::Parser};
22
use std::collections::{HashMap, HashSet};
33

44
pub fn load_schema(schema: &[u8]) -> Result<super::TablesAndColumns, String> {
5-
// TODO return err instead of panics
6-
75
let schema_str = String::from_utf8_lossy(schema);
86

97
let dialect = GenericDialect {};
@@ -25,3 +23,38 @@ pub fn load_schema(schema: &[u8]) -> Result<super::TablesAndColumns, String> {
2523
}
2624
return Ok(tables);
2725
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use std::collections::{HashMap, HashSet};
30+
31+
use super::load_schema;
32+
33+
#[test]
34+
fn test_load_schema() {
35+
let schema = "
36+
CREATE TABLE users (
37+
id INT PRIMARY KEY AUTO_INCREMENT,
38+
name VARCHAR(255) NOT NULL
39+
);
40+
CREATE TABLE receipt (
41+
id INT PRIMARY KEY AUTO_INCREMENT,
42+
content VARCHAR(128),
43+
user_id INT,
44+
FOREIGN KEY (user_id) REFERENCES users(id)
45+
);
46+
";
47+
48+
let expected_result: HashMap<String, HashSet<String>> = HashMap::from([
49+
("users".into(), HashSet::from(["id".into(), "name".into()])),
50+
(
51+
"receipt".into(),
52+
HashSet::from(["id".into(), "content".into(), "user_id".into()]),
53+
),
54+
]);
55+
56+
let result = load_schema(&schema.as_bytes()).unwrap();
57+
58+
assert_eq!(result, expected_result);
59+
}
60+
}

0 commit comments

Comments
 (0)