#sql-query #mysql

query_builder

Easy to use library for creating SQL-Statements

6 releases

Uses old Rust 2015

0.3.1 May 3, 2018
0.2.0 Apr 13, 2018
0.1.3 Apr 13, 2018

#2695 in Database interfaces

MIT license

31KB
421 lines

--- Under construction ---

Overview

What is this?

This is a Library written in pure Rust intended to be easy to use for creating SQL Queries. This is also a project I mainly started to improve my Rust skills.

Usage

In order to use this library add the line query_builder = "*" to the [dependencies] section of your Cargo.toml. Then in your code you can use extern crate query_builder; and access it with use query_builder::*

Creating a basic query that selects data from a table you can use the following code:

extern crate query_builder;
use query_builder::query_builder::*;

fn main() {
    let mut query = SelectQuery::select(&["user"]).from("users");

    query.whre.insert("name", Value::Varchar("greg"));
    query.limit(1);

    assert_eq!(query.as_string(), "SELECT user FROM users WHERE name = 'greg' LIMIT 1");
}

lib.rs:

About

This crate is intended to be easy to use for creating SQL-Queries dynamically as you need it.

Usage

For creating a simple SELECT-Query you first need to add query_builder = "*" to your Cargo.toml file (you may of cource replace * with any version)
In your code write extern crate query_builder and use query_builder::* to import all structs and enums.
Finally creating the SelectQuery looks like this:

use query_builder::SelectQuery;

let query = SelectQuery::select(&["*"]).from("users");
// make sure the query looks like expected
assert_eq!(query.as_string(), "SELECT * FROM users");

Creating a [`InsertQuery`] works similar:
use query_builder::{InsertQuery, Value};

// create the basic query
let mut query = InsertQuery::into("users");
// add values to the query
query.values.insert("name", Value::Varchar("george"));

// make sure that the query looks like expected
assert_eq!(query.as_string(), "INSERT INTO users(name) VALUES('george')");

More detailed explanations and examples can be found at the corresponding sections to the structs and enums

No runtime deps