gluesql / gluesql

GlueSQL is quite sticky. It attaches to anywhere.

Home Page:https://gluesql.org/docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convert parsed AST to AST Builder

cemoktra opened this issue · comments

Hi,

i have a use case that would require extending a parsed query. If i read the docs correctly when parsing SQL i get an AST of the ast module. To use the builder i need the ast_builder module. Is there a way to convert them already that overlooked? Is this possible?

Do you mean conversion AstBuilder from an already parsed AST?

Yes

How do you intend to use it?

I'm not sure what's going on, but the output of AstBuilder is AST.
What do you mean by converting from AST to AstBuilder?
For example, the

  • Do you expect creating an AST to generate AstBuilder rust code?

I parse SQL, use AST builder to extend it, then output SQL again

In an AST extended with AstBuilder, you can use ToSql::to_sql to get a sql string.

https://docs.rs/gluesql/latest/gluesql/core/ast/trait.ToSql.html

Is this what we want? 👀

Yes, but i was asking for the conversion between AST and AstBuilder. E.g.:

  • i have a query SELECT a FROM t
  • this query is checked at compile time by sqlx
  • now i parse that query into an AST
  • convert this AST into an AST builder
  • add dynamic where clauses using the AST builder
  • execute final sql

I would like to use the AST builder here for the dynamic query part as its safer than writing sql as text

Oh okay. I get it.

What you said is partially possible.

  • By "partially" I mean the AstBuilderNodes that are implemented.
  • See core/src/ast_builder::* for all implementations.

For example, an ExprNode can be extended with the Expr variant because it exists.

two points

convert this AST into an AST builder

Currently you can directly execute the AST rather than converting that into AST builder.

add dynamic where clauses using the AST builder

though we don't have AST mutable visitor yet, but you can still change the AST by using mutable borrow.


AST Builder is for making AST so.. there's no certain path from AST to AST Builder now.
But AST Builder accepts the AST nodes as a parameter.

e.g.

#[derive(Clone, Debug)]
pub enum ExprNode<'a> {
Expr(Cow<'a, Expr>),
SqlExpr(Cow<'a, str>),
Identifier(Cow<'a, str>),

In ExprNode, you can make ExprNode by using already built ast::Expr because ExprNode has ExprNode::Expr(Cow<'a, Expr>) as a variant.