Skip to content

Commit eb6cea8

Browse files
Merge pull request #4 from ndrluis/some-improv
fix clippy warnings and remove skipped test
2 parents 97129ba + dcca13d commit eb6cea8

File tree

3 files changed

+54
-64
lines changed

3 files changed

+54
-64
lines changed

native/sqlparser_parse/src/datatypes.rs

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl From<TableFactor> for sqlparser::ast::TableFactor {
235235
with_hints: [].to_vec(),
236236
},
237237
_ => sqlparser::ast::TableFactor::Table {
238-
name: name,
238+
name,
239239
alias: None,
240240
args: None,
241241
with_hints: [].to_vec(),
@@ -244,10 +244,8 @@ impl From<TableFactor> for sqlparser::ast::TableFactor {
244244
}
245245
}
246246
impl From<JoinOperator> for sqlparser::ast::JoinOperator {
247-
fn from(join_operator: JoinOperator) -> Self {
248-
match join_operator {
249-
_ => sqlparser::ast::JoinOperator::CrossJoin,
250-
}
247+
fn from(_: JoinOperator) -> Self {
248+
sqlparser::ast::JoinOperator::CrossJoin
251249
}
252250
}
253251
impl From<Join> for sqlparser::ast::Join {
@@ -298,7 +296,7 @@ impl From<SetExpr> for sqlparser::ast::SetExpr {
298296
.map(|l| sqlparser::ast::TableWithJoins::from(l.clone()))
299297
.collect(),
300298
lateral_views: [].to_vec(),
301-
selection: select.selection.map(|l| sqlparser::ast::Expr::from(l)),
299+
selection: select.selection.map(sqlparser::ast::Expr::from),
302300
group_by: select
303301
.group_by
304302
.iter()
@@ -311,7 +309,7 @@ impl From<SetExpr> for sqlparser::ast::SetExpr {
311309
.iter()
312310
.map(|l| sqlparser::ast::Expr::from(l.clone()))
313311
.collect(),
314-
having: select.having.map(|l| sqlparser::ast::Expr::from(l)),
312+
having: select.having.map(sqlparser::ast::Expr::from),
315313
qualify: None,
316314
}))
317315
}
@@ -339,7 +337,7 @@ impl From<Statement> for sqlparser::ast::Statement {
339337
Statement::Query(query) => {
340338
sqlparser::ast::Statement::Query(Box::new(sqlparser::ast::Query {
341339
body: Box::new(sqlparser::ast::SetExpr::from(query.body)),
342-
limit: query.limit.map(|l| sqlparser::ast::Expr::from(l)),
340+
limit: query.limit.map(sqlparser::ast::Expr::from),
343341
with: None,
344342
order_by: query
345343
.order_by
@@ -432,6 +430,12 @@ impl Wildcard {
432430
}
433431
}
434432

433+
impl Default for Wildcard {
434+
fn default() -> Self {
435+
Wildcard::new()
436+
}
437+
}
438+
435439
#[derive(NifStruct)]
436440
//#[rustler(encode)]
437441
#[module = "SqlParser.ExprWithAlias"]
@@ -602,7 +606,7 @@ impl TableWithJoins {
602606
pub fn new(ast: &sqlparser::ast::TableWithJoins) -> Self {
603607
let relation = TableFactor::from(ast.relation.clone());
604608
Self {
605-
relation: relation,
609+
relation,
606610
joins: ast
607611
.joins
608612
.iter()
@@ -626,10 +630,7 @@ impl From<sqlparser::ast::Ident> for Ident {
626630
fn from(ident: sqlparser::ast::Ident) -> Self {
627631
Self {
628632
value: ident.to_string(),
629-
quote_style: match ident.quote_style {
630-
None => None,
631-
Some(style) => Some(style.to_string()),
632-
},
633+
quote_style: ident.quote_style.map(|s| s.to_string()),
633634
}
634635
}
635636
}
@@ -839,10 +840,7 @@ pub enum Value {
839840
impl From<sqlparser::ast::Value> for Value {
840841
fn from(value: sqlparser::ast::Value) -> Self {
841842
match value {
842-
sqlparser::ast::Value::Number(num, long) => Self::Number(Number {
843-
value: num,
844-
long: long,
845-
}),
843+
sqlparser::ast::Value::Number(num, long) => Self::Number(Number { value: num, long }),
846844
sqlparser::ast::Value::SingleQuotedString(string) => Self::SingleQuotedString(string),
847845
sqlparser::ast::Value::DollarQuotedString(_dollar_quoted_string) => {
848846
Self::NotImplemented(result_atoms::not_implemented())
@@ -1034,7 +1032,7 @@ impl Expr {
10341032
value: ExprEnum::InList(InList {
10351033
expr: Box::new(Expr::new(*expr)),
10361034
list: list.iter().map(|p| Expr::new(p.clone())).collect(),
1037-
negated: negated,
1035+
negated,
10381036
}),
10391037
},
10401038
sqlparser::ast::Expr::InSubquery {
@@ -1046,7 +1044,7 @@ impl Expr {
10461044
value: ExprEnum::InSubquery(InSubquery {
10471045
expr: Box::new(Expr::new(*expr)),
10481046
subquery: Box::new(Query::new(*subquery)),
1049-
negated: negated,
1047+
negated,
10501048
}),
10511049
},
10521050
sqlparser::ast::Expr::InUnnest {
@@ -1058,7 +1056,7 @@ impl Expr {
10581056
value: ExprEnum::InUnnest(InUnnest {
10591057
expr: Box::new(Expr::new(*expr)),
10601058
array_expr: Box::new(Expr::new(*array_expr)),
1061-
negated: negated,
1059+
negated,
10621060
}),
10631061
},
10641062
sqlparser::ast::Expr::Between {
@@ -1070,7 +1068,7 @@ impl Expr {
10701068
r#type: type_atoms::in_subquery(),
10711069
value: ExprEnum::Between(Between {
10721070
expr: Box::new(Expr::new(*expr)),
1073-
negated: negated,
1071+
negated,
10741072
low: Box::new(Expr::new(*low)),
10751073
high: Box::new(Expr::new(*high)),
10761074
}),
@@ -1092,12 +1090,9 @@ impl Expr {
10921090
r#type: type_atoms::like(),
10931091
value: ExprEnum::Like(Like {
10941092
expr: Box::new(Expr::new(*expr)),
1095-
negated: negated,
1093+
negated,
10961094
pattern: Box::new(Expr::new(*pattern)),
1097-
escape_char: match escape_char {
1098-
Some(c) => Some(c.to_string()),
1099-
None => None,
1100-
},
1095+
escape_char: escape_char.map(|c| c.to_string()),
11011096
}),
11021097
},
11031098
sqlparser::ast::Expr::ILike {
@@ -1109,12 +1104,9 @@ impl Expr {
11091104
r#type: type_atoms::ilike(),
11101105
value: ExprEnum::ILike(ILike {
11111106
expr: Box::new(Expr::new(*expr)),
1112-
negated: negated,
1107+
negated,
11131108
pattern: Box::new(Expr::new(*pattern)),
1114-
escape_char: match escape_char {
1115-
Some(c) => Some(c.to_string()),
1116-
None => None,
1117-
},
1109+
escape_char: escape_char.map(|c| c.to_string()),
11181110
}),
11191111
},
11201112
sqlparser::ast::Expr::SimilarTo {
@@ -1126,12 +1118,9 @@ impl Expr {
11261118
r#type: type_atoms::similar_to(),
11271119
value: ExprEnum::SimilarTo(SimilarTo {
11281120
expr: Box::new(Expr::new(*expr)),
1129-
negated: negated,
1121+
negated,
11301122
pattern: Box::new(Expr::new(*pattern)),
1131-
escape_char: match escape_char {
1132-
Some(c) => Some(c.to_string()),
1133-
None => None,
1134-
},
1123+
escape_char: escape_char.map(|c| c.to_string()),
11351124
}),
11361125
},
11371126
sqlparser::ast::Expr::AnyOp(expr) => Expr {
@@ -1240,11 +1229,8 @@ impl Select {
12401229
}
12411230
})
12421231
.collect(),
1243-
from: ast.from.iter().map(|p| TableWithJoins::new(p)).collect(),
1244-
selection: match ast.selection {
1245-
Some(expr) => Some(Expr::new(expr)),
1246-
None => None,
1247-
},
1232+
from: ast.from.iter().map(TableWithJoins::new).collect(),
1233+
selection: ast.selection.map(Expr::new),
12481234
group_by: ast
12491235
.group_by
12501236
.iter()
@@ -1255,10 +1241,7 @@ impl Select {
12551241
.iter()
12561242
.map(|expr| Expr::new(expr.clone()))
12571243
.collect(),
1258-
having: match ast.having {
1259-
Some(expr) => Some(Expr::new(expr)),
1260-
None => None,
1261-
},
1244+
having: ast.having.map(Expr::new),
12621245
}
12631246
}
12641247
}
@@ -1352,10 +1335,7 @@ impl Query {
13521335
nulls_first: order_by_expr.nulls_first,
13531336
})
13541337
.collect(),
1355-
limit: match ast.limit {
1356-
Some(expr) => Some(Expr::new(expr)),
1357-
None => None,
1358-
},
1338+
limit: ast.limit.map(Expr::new),
13591339
offset: match ast.offset {
13601340
Some(offset) => Some(Offset {
13611341
value: Expr::new(offset.value),

native/sqlparser_parse/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ fn parse_statements(
6363

6464
Err(e) => Err(Error::Term(Box::new(e.to_string()))),
6565
};
66-
return ast;
66+
67+
ast
6768
}
6869

6970
#[rustler::nif]
7071
fn to_sql(ast: Document, _dialect: Dialect) -> Result<(Atom, String), Error> {
7172
let statement = sqlparser::ast::Statement::from(ast.statements[0].clone());
72-
return Ok((atom::ok(), statement.to_string()));
73+
Ok((atom::ok(), statement.to_string()))
7374
}
7475

7576
rustler::init!("Elixir.SqlParser.Parse");

test/sql_parser_test.exs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule SqlParserTest do
22
use ExUnit.Case
33
doctest SqlParser
4-
alias SqlParser.Expr
5-
alias SqlParser.Ident
64

75
# test "to_sql" do
86
# # WHERE b.a = d
@@ -26,7 +24,7 @@ defmodule SqlParserTest do
2624
%SqlParser.TableWithJoins{
2725
relation: %SqlParser.Table{
2826
name: %SqlParser.ObjectName{
29-
names: [%Ident{quote_style: nil, value: "a"}]
27+
names: [%SqlParser.Ident{quote_style: nil, value: "a"}]
3028
}
3129
}
3230
}
@@ -55,7 +53,7 @@ defmodule SqlParserTest do
5553
joins: [],
5654
relation: %SqlParser.Table{
5755
name: %SqlParser.ObjectName{
58-
names: [%Ident{quote_style: nil, value: "a"}]
56+
names: [%SqlParser.Ident{quote_style: nil, value: "a"}]
5957
}
6058
}
6159
}
@@ -68,9 +66,9 @@ defmodule SqlParserTest do
6866
},
6967
order_by: [
7068
%SqlParser.OrderByExpr{
71-
expr: %Expr{
69+
expr: %SqlParser.Expr{
7270
type: :identifier,
73-
value: %Ident{quote_style: nil, value: "f"}
71+
value: %SqlParser.Ident{quote_style: nil, value: "f"}
7472
},
7573
asc: nil,
7674
nulls_first: nil
@@ -119,7 +117,7 @@ defmodule SqlParserTest do
119117

120118
assert %SqlParser.Query{
121119
body: %SqlParser.Select{
122-
selection: %Expr{
120+
selection: %SqlParser.Expr{
123121
type: :binary_op,
124122
value: %SqlParser.BinaryOp{
125123
left: _,
@@ -188,21 +186,32 @@ defmodule SqlParserTest do
188186
end
189187

190188
@exprs %{
191-
%Expr{
189+
%SqlParser.Expr{
192190
type: :binary_op,
193191
value: %SqlParser.BinaryOp{
194-
left: %Expr{type: :identifier, value: %Ident{quote_style: nil, value: "a"}},
192+
left: %SqlParser.Expr{
193+
type: :identifier,
194+
value: %SqlParser.Ident{quote_style: nil, value: "a"}
195+
},
195196
op: :eq,
196-
right: %Expr{type: :value, value: {:number, "1", false}}
197+
right: %SqlParser.Expr{
198+
type: :value,
199+
value: %SqlParser.Number{
200+
long: false,
201+
value: "18446744073709551616.18446744073709551616"
202+
}
203+
}
197204
}
198205
} => "a = 18446744073709551616.18446744073709551616",
199-
%Expr{
206+
%SqlParser.Expr{
200207
type: :is_null,
201-
value: %Expr{type: :identifier, value: %Ident{quote_style: nil, value: "c"}}
208+
value: %SqlParser.Expr{
209+
type: :identifier,
210+
value: %SqlParser.Ident{quote_style: nil, value: "c"}
211+
}
202212
} => "c IS NULL"
203213
}
204214

205-
@tag :skip
206215
test "expr work" do
207216
for {expected_selection, expr} <- @exprs do
208217
assert {:ok, [query]} = SqlParser.parse("SELECT c as b from a WHERE #{expr}")

0 commit comments

Comments
 (0)