[][src]Crate include_sql

include-sql is a macro for using SQL from Rust. When using include-sql you create raw .sql files that contain your queries and then execute them from Rust.

Example

SQL file src/crew.sql

-- name: select_ship_crew
-- Selects ship crew (sailors of a given ship)
SELECT id, name, rank
  FROM sailors
 WHERE ship_id = :ship

To execute this query then in Rust (let's say the database is Postgres)...

use postgres::{ Connection, Result };
use postgres::types::ToSql;
use include_sql::include_sql;

include_sql!("src/crew.sql","$");

fn print_ship_crew(conn: &Connection, ship_id: i32) -> Result<()> {
    let rows = conn.query(SELECT_SHIP_CREW, using_select_ship_crew_args! {
        ship: &ship_id
    })?;

    println!("Ship crew:");
    for row in &rows {
        let id : i32     = row.get(0);
        let name: String = row.get(1);
        let rank: String = row.get(2);
        println!(" - {} {}, ID: {}", rank, name, id);
    }
    Ok(())
}

Features

The basis of using SQL employed by include-sql was inspired by Yesql. However include-sql is not a Yesql implemented in Rust as there is one key difference - include-sql assists in using externally defined SQL, but it offloads the actual work to the database interface. Unlike Yesql it does not generate functions that abstract database access.

Macros

include_sql

Includes SQL from the provided file.