2 releases

Uses old Rust 2015

0.1.1 Apr 21, 2022
0.1.0 Nov 13, 2015

#674 in Database interfaces

Download history 1064/week @ 2023-11-30 810/week @ 2023-12-07 643/week @ 2023-12-14 501/week @ 2023-12-21 316/week @ 2023-12-28 899/week @ 2024-01-04 982/week @ 2024-01-11 1422/week @ 2024-01-18 1013/week @ 2024-01-25 1659/week @ 2024-02-01 1010/week @ 2024-02-08 996/week @ 2024-02-15 1326/week @ 2024-02-22 1664/week @ 2024-02-29 1754/week @ 2024-03-07 1175/week @ 2024-03-14

6,075 downloads per month

MIT license

11KB
133 lines

rawsql

A rust library for using and reusing SQL.

Build Status Latest Version

is heavily influenced by yesql (many thanks @krisajenkins)

DOC

You can integrate rawsql into your project through the releases on crates.io:

# Cargo.toml
[dependencies]
rawsql = "0.1.1"

Overview

You need to write SQL and you need to reuse it. You don't want to duplicate the queries all over the code. This lib is for you.

This lib does not execute any sql in the DB.

Usage

The basic idea is to separate the sql part from the code and put it into its own sql files. With this approach, you gain sql powers and the ability to write sql only once that runs on your DB (your dba could modify these files too)

The sql file needs to be with this format :

-- name: insert-person
INSERT INTO "person" (name, data) VALUES ($1, $2);

-- name: select-persons
SELECT id, name, data FROM person;

comment with the **-- name: ** , it will be the key value for getting each query.

the ";" will be needed at the end of the query.


extern crate rawsql;

use rawsql::Loader;


fn main() {

    let queries = Loader::get_queries_from("examples/postgre.sql").unwrap().queries;

    //Insert query
    let qinsert = queries.get("insert-person").unwrap();

    println!("{}", qinsert);

    //Select query
    let qselect = queries.get("select-persons").unwrap();
    println!("{}", qselect);

}

See the full example here

License

Copyright © 2015 Manuel Alonso

MIT License

Why not execute SQL this lib?

In rust there is not yet a general driver like JDBC or go's database/sql so I decide to abstract first the parser of sql files to use directly with the libs already exists for each DB.

No runtime deps