Skip to content
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
78 changes: 29 additions & 49 deletions native/sqlparser_parse/src/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl From<TableFactor> for sqlparser::ast::TableFactor {
with_hints: [].to_vec(),
},
_ => sqlparser::ast::TableFactor::Table {
name: name,
name,
alias: None,
args: None,
with_hints: [].to_vec(),
Expand All @@ -244,10 +244,8 @@ impl From<TableFactor> for sqlparser::ast::TableFactor {
}
}
impl From<JoinOperator> for sqlparser::ast::JoinOperator {
fn from(join_operator: JoinOperator) -> Self {
match join_operator {
_ => sqlparser::ast::JoinOperator::CrossJoin,
}
fn from(_: JoinOperator) -> Self {
sqlparser::ast::JoinOperator::CrossJoin
}
}
impl From<Join> for sqlparser::ast::Join {
Expand Down Expand Up @@ -298,7 +296,7 @@ impl From<SetExpr> for sqlparser::ast::SetExpr {
.map(|l| sqlparser::ast::TableWithJoins::from(l.clone()))
.collect(),
lateral_views: [].to_vec(),
selection: select.selection.map(|l| sqlparser::ast::Expr::from(l)),
selection: select.selection.map(sqlparser::ast::Expr::from),
group_by: select
.group_by
.iter()
Expand All @@ -311,7 +309,7 @@ impl From<SetExpr> for sqlparser::ast::SetExpr {
.iter()
.map(|l| sqlparser::ast::Expr::from(l.clone()))
.collect(),
having: select.having.map(|l| sqlparser::ast::Expr::from(l)),
having: select.having.map(sqlparser::ast::Expr::from),
qualify: None,
}))
}
Expand Down Expand Up @@ -339,7 +337,7 @@ impl From<Statement> for sqlparser::ast::Statement {
Statement::Query(query) => {
sqlparser::ast::Statement::Query(Box::new(sqlparser::ast::Query {
body: Box::new(sqlparser::ast::SetExpr::from(query.body)),
limit: query.limit.map(|l| sqlparser::ast::Expr::from(l)),
limit: query.limit.map(sqlparser::ast::Expr::from),
with: None,
order_by: query
.order_by
Expand Down Expand Up @@ -432,6 +430,12 @@ impl Wildcard {
}
}

impl Default for Wildcard {
fn default() -> Self {
Wildcard::new()
}
}

#[derive(NifStruct)]
//#[rustler(encode)]
#[module = "SqlParser.ExprWithAlias"]
Expand Down Expand Up @@ -602,7 +606,7 @@ impl TableWithJoins {
pub fn new(ast: &sqlparser::ast::TableWithJoins) -> Self {
let relation = TableFactor::from(ast.relation.clone());
Self {
relation: relation,
relation,
joins: ast
.joins
.iter()
Expand All @@ -626,10 +630,7 @@ impl From<sqlparser::ast::Ident> for Ident {
fn from(ident: sqlparser::ast::Ident) -> Self {
Self {
value: ident.to_string(),
quote_style: match ident.quote_style {
None => None,
Some(style) => Some(style.to_string()),
},
quote_style: ident.quote_style.map(|s| s.to_string()),
}
}
}
Expand Down Expand Up @@ -839,10 +840,7 @@ pub enum Value {
impl From<sqlparser::ast::Value> for Value {
fn from(value: sqlparser::ast::Value) -> Self {
match value {
sqlparser::ast::Value::Number(num, long) => Self::Number(Number {
value: num,
long: long,
}),
sqlparser::ast::Value::Number(num, long) => Self::Number(Number { value: num, long }),
sqlparser::ast::Value::SingleQuotedString(string) => Self::SingleQuotedString(string),
sqlparser::ast::Value::DollarQuotedString(_dollar_quoted_string) => {
Self::NotImplemented(result_atoms::not_implemented())
Expand Down Expand Up @@ -1034,7 +1032,7 @@ impl Expr {
value: ExprEnum::InList(InList {
expr: Box::new(Expr::new(*expr)),
list: list.iter().map(|p| Expr::new(p.clone())).collect(),
negated: negated,
negated,
}),
},
sqlparser::ast::Expr::InSubquery {
Expand All @@ -1046,7 +1044,7 @@ impl Expr {
value: ExprEnum::InSubquery(InSubquery {
expr: Box::new(Expr::new(*expr)),
subquery: Box::new(Query::new(*subquery)),
negated: negated,
negated,
}),
},
sqlparser::ast::Expr::InUnnest {
Expand All @@ -1058,7 +1056,7 @@ impl Expr {
value: ExprEnum::InUnnest(InUnnest {
expr: Box::new(Expr::new(*expr)),
array_expr: Box::new(Expr::new(*array_expr)),
negated: negated,
negated,
}),
},
sqlparser::ast::Expr::Between {
Expand All @@ -1070,7 +1068,7 @@ impl Expr {
r#type: type_atoms::in_subquery(),
value: ExprEnum::Between(Between {
expr: Box::new(Expr::new(*expr)),
negated: negated,
negated,
low: Box::new(Expr::new(*low)),
high: Box::new(Expr::new(*high)),
}),
Expand All @@ -1092,12 +1090,9 @@ impl Expr {
r#type: type_atoms::like(),
value: ExprEnum::Like(Like {
expr: Box::new(Expr::new(*expr)),
negated: negated,
negated,
pattern: Box::new(Expr::new(*pattern)),
escape_char: match escape_char {
Some(c) => Some(c.to_string()),
None => None,
},
escape_char: escape_char.map(|c| c.to_string()),
}),
},
sqlparser::ast::Expr::ILike {
Expand All @@ -1109,12 +1104,9 @@ impl Expr {
r#type: type_atoms::ilike(),
value: ExprEnum::ILike(ILike {
expr: Box::new(Expr::new(*expr)),
negated: negated,
negated,
pattern: Box::new(Expr::new(*pattern)),
escape_char: match escape_char {
Some(c) => Some(c.to_string()),
None => None,
},
escape_char: escape_char.map(|c| c.to_string()),
}),
},
sqlparser::ast::Expr::SimilarTo {
Expand All @@ -1126,12 +1118,9 @@ impl Expr {
r#type: type_atoms::similar_to(),
value: ExprEnum::SimilarTo(SimilarTo {
expr: Box::new(Expr::new(*expr)),
negated: negated,
negated,
pattern: Box::new(Expr::new(*pattern)),
escape_char: match escape_char {
Some(c) => Some(c.to_string()),
None => None,
},
escape_char: escape_char.map(|c| c.to_string()),
}),
},
sqlparser::ast::Expr::AnyOp(expr) => Expr {
Expand Down Expand Up @@ -1240,11 +1229,8 @@ impl Select {
}
})
.collect(),
from: ast.from.iter().map(|p| TableWithJoins::new(p)).collect(),
selection: match ast.selection {
Some(expr) => Some(Expr::new(expr)),
None => None,
},
from: ast.from.iter().map(TableWithJoins::new).collect(),
selection: ast.selection.map(Expr::new),
group_by: ast
.group_by
.iter()
Expand All @@ -1255,10 +1241,7 @@ impl Select {
.iter()
.map(|expr| Expr::new(expr.clone()))
.collect(),
having: match ast.having {
Some(expr) => Some(Expr::new(expr)),
None => None,
},
having: ast.having.map(Expr::new),
}
}
}
Expand Down Expand Up @@ -1352,10 +1335,7 @@ impl Query {
nulls_first: order_by_expr.nulls_first,
})
.collect(),
limit: match ast.limit {
Some(expr) => Some(Expr::new(expr)),
None => None,
},
limit: ast.limit.map(Expr::new),
offset: match ast.offset {
Some(offset) => Some(Offset {
value: Expr::new(offset.value),
Expand Down
5 changes: 3 additions & 2 deletions native/sqlparser_parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ fn parse_statements(

Err(e) => Err(Error::Term(Box::new(e.to_string()))),
};
return ast;

ast
}

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

rustler::init!("Elixir.SqlParser.Parse");
35 changes: 22 additions & 13 deletions test/sql_parser_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule SqlParserTest do
use ExUnit.Case
doctest SqlParser
alias SqlParser.Expr
alias SqlParser.Ident

# test "to_sql" do
# # WHERE b.a = d
Expand All @@ -26,7 +24,7 @@ defmodule SqlParserTest do
%SqlParser.TableWithJoins{
relation: %SqlParser.Table{
name: %SqlParser.ObjectName{
names: [%Ident{quote_style: nil, value: "a"}]
names: [%SqlParser.Ident{quote_style: nil, value: "a"}]
}
}
}
Expand Down Expand Up @@ -55,7 +53,7 @@ defmodule SqlParserTest do
joins: [],
relation: %SqlParser.Table{
name: %SqlParser.ObjectName{
names: [%Ident{quote_style: nil, value: "a"}]
names: [%SqlParser.Ident{quote_style: nil, value: "a"}]
}
}
}
Expand All @@ -68,9 +66,9 @@ defmodule SqlParserTest do
},
order_by: [
%SqlParser.OrderByExpr{
expr: %Expr{
expr: %SqlParser.Expr{
type: :identifier,
value: %Ident{quote_style: nil, value: "f"}
value: %SqlParser.Ident{quote_style: nil, value: "f"}
},
asc: nil,
nulls_first: nil
Expand Down Expand Up @@ -119,7 +117,7 @@ defmodule SqlParserTest do

assert %SqlParser.Query{
body: %SqlParser.Select{
selection: %Expr{
selection: %SqlParser.Expr{
type: :binary_op,
value: %SqlParser.BinaryOp{
left: _,
Expand Down Expand Up @@ -188,21 +186,32 @@ defmodule SqlParserTest do
end

@exprs %{
%Expr{
%SqlParser.Expr{
type: :binary_op,
value: %SqlParser.BinaryOp{
left: %Expr{type: :identifier, value: %Ident{quote_style: nil, value: "a"}},
left: %SqlParser.Expr{
type: :identifier,
value: %SqlParser.Ident{quote_style: nil, value: "a"}
},
op: :eq,
right: %Expr{type: :value, value: {:number, "1", false}}
right: %SqlParser.Expr{
type: :value,
value: %SqlParser.Number{
long: false,
value: "18446744073709551616.18446744073709551616"
}
}
}
} => "a = 18446744073709551616.18446744073709551616",
%Expr{
%SqlParser.Expr{
type: :is_null,
value: %Expr{type: :identifier, value: %Ident{quote_style: nil, value: "c"}}
value: %SqlParser.Expr{
type: :identifier,
value: %SqlParser.Ident{quote_style: nil, value: "c"}
}
} => "c IS NULL"
}

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