Goal: Parse SQL query with sqlparser and develop a library that allows you to change table / column names etc.
This is the structure that sqlparser gives:
query: SELECT a FROM tbl
[src/main.rs:27] ast = [
Query(
Query {
with: None,
body: Select(
Select {
// --- snip ---
projection: [
UnnamedExpr(
Identifier(
Ident {
value: "a",
quote_style: None,
},
),
),
],
into: None,
from: [
TableWithJoins {
relation: Table {
name: ObjectName(
[
Ident {
value: "tbl",
quote_style: None,
},
],
),
// --- snip ---
},
joins: [],
},
],
// --- snip ---
},
),
// --- snip ---
},
),
]
I'm most interested in Ident and Table structs. What are my options to store the location / reference for later use, so that I can do something like mapper.change_table_name('tbl', 'new_tbl') without traversing the whole tree again.
I think my options are:
- Refcell / Rc, but as far as i know that would mean I'd have to recreate the whole ast again to get it to accept Refcell/Rc
- Store the position somehow, but I have no idea how to do this with more complicated structures like enums
- Traverse the whole ast every time something needs to be changed. I'd rather not do this as it gets tricky when I'm trying to keep count which column belongs to which table
- Is there already a library that allows something like this?