14 releases

0.1.13 Nov 18, 2022
0.1.12 Nov 18, 2022
0.1.10 Oct 30, 2022

#429 in Template engine

Apache-2.0

13KB
217 lines

alisql

test alisql at crates.io alisql at docs.rs

This is library to analize SQL with jinja template.

Example

Get dependecies

First, create sql like Jinja.

-- src/sample_sqls/level1/sample.sql
select 
u.*
, r.* 
from {{ ref("db", "users") }} as u
left join {{ ref("role") }} as r on
u.id = r.user_id
-- src/sample_sqls/sample2.sql
select 
u.*
, r.* 
from {{ ref("db", "sales") }} as u
left join {{ ref("db", "sale_detail") }} as r on
u.id = r.sale_id

Use alias lib from main function.

use alias;
fn main() {
    let d = alias::get_dependencies("src/sample_sqls") 
}

Result is..

d = [
    Table {
        table: "sample",
        sql: SQL {
            path: "src/sample_sqls/level1/sample.sql",
            query: "select \nu.*\n, r.* \nfrom {{ ref(\"db\", \"users\") }} as u\nleft join {{ ref(\"role\") }} as r on\nu.id = r.user_id",
        },
        depends_on: [
            "db.users",
            "role",
        ],
    },
    Table {
        table: "sample2",
        sql: SQL {
            path: "src/sample_sqls/sample2.sql",
            query: "select \nu.*\n, r.* \nfrom {{ ref(\"db\", \"sales\") }} as u\nleft join {{ ref(\"db\", \"sale_detail\") }} as r on\nu.id = r.sale_id",
        },
        depends_on: [
            "db.sales",
            "db.sale_detail",
        ],
    },
]

Get mermaid graph

use alisql;

fn main() {
    let m = alisql::get_mermaid("src/sample_sqls", "TD");
    println!("{}", m);
}

Variable m is

graph TD;
db.users --> sample;
role --> sample;
db.sales --> sample2;
db.sale_detail --> sample2;

This is shown in the following graph.

graph TD;
db.users --> sample;
role --> sample;
db.sales --> sample2;
db.sale_detail --> sample2;

Dependencies

~4–12MB
~139K SLoC