Function query

Source
pub async fn query<T: AsRef<str>>(sql: T, suffix: &str) -> Result<DataSet>
Expand description

从 from 中获取数据,从 where 中过滤,最后选取需要返回的列

Examples found in repository?
examples/covid.rs (line 18)
5async fn main() -> Result<()> {
6    tracing_subscriber::fmt::init();
7
8    let url = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/latest/owid-covid-latest.csv";
9
10    // 使用 sql 从 URL 里获取数据
11    let sql = format!(
12        "SELECT location name, total_cases, new_cases, total_deaths, new_deaths \
13        FROM {} where new_deaths >= 200 ORDER BY new_cases DESC",
14        url
15    );
16    println!("{sql}");
17
18    let df = query(sql, "csv").await?;
19    println!("{:?}", df);
20
21    Ok(())
22}
More examples
Hide additional examples
examples/ps.rs (line 23)
5async fn main() -> Result<()> {
6    tracing_subscriber::fmt::init();
7
8    // For windows power shell, we need to run the encoding to utf-8
9    // [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
10    let file = "comm://tasklist";
11
12    let sql = format!(
13        "SELECT * \
14        FROM {}",
15        file
16    );
17
18    println!("{sql}");
19
20    // let ast = sqlparser::parser::Parser::parse_sql(&queryer::TyrDialect::default(), &sql);
21    // println!("{:#?}", ast);
22
23    let df = query(sql, "comm").await?;
24    println!("{:?}", df);
25
26    Ok(())
27}
examples/juventus.rs (line 25)
5async fn main() -> Result<()> {
6    tracing_subscriber::fmt::init();
7
8    let file = "file://juventus.json";
9
10    // 使用 sql 从 URL 里获取数据
11    // let sql = format!(
12    //     "SELECT Name,Position,Nationality,\"Kit Number\",DOB \
13    //     FROM {} where Nationality = 'Italy'",
14    //     file
15    // );
16    let sql = format!(
17        "SELECT * \
18        FROM {} where Nationality = 'Italy'",
19        file
20    );
21    println!("{sql}");
22    // let ast = sqlparser::parser::Parser::parse_sql(&queryer::TyrDialect::default(), &sql);
23    // println!("{:#?}", ast);
24
25    let df1 = query(sql, "json").await?;
26    println!("{:?}", df1);
27
28    Ok(())
29}