A query builder that builds and typechecks queries at compile time

Related tags

Database mysql rust
Overview

crates.io docs

typed-qb: a compile-time typed "query builder"

typed-qb is a compile-time, typed, query builder. The goal of this crate is to explore the gap between compile-time verification of raw SQL queries (like sqlx) and runtime generation of queries via query builders (like diesel).

The query is transformed into an SQL query string at compile time. If code compiles and the schema in the code matches the database, it should be (almost) impossible to write queries that produce errors.

Usage

You can specify your table structure by pasting the output of SHOW CREATE TABLE .. into the tables! macro. You will need to clean up the backticks (`), because Rust cannot parse them. For example:

tables! {
    CREATE TABLE Users (
        Id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
        Name VARCHAR(64) NOT NULL,
        PRIMARY KEY(Id)
    );
}

Field names will automatically be converted to snake_case if you write them in camelCase or PascalCase. You can then query the table as follows:

let conn = ...; // some connection

let all_users = conn.typed_query(
    Users::query(|user| data! {
        id: user.id,
        name: user.name,
    })
)?;

for user in all_users {
    let user = user?;
    println!("{} ({})", user.id, user.name);
}

The query is transformed at compile-time into:

SELECT `t0`.`Id` AS `id`, `t0`.`Name` AS `name` 
FROM `Users` AS t0

You can specify WHERE, ORDER BY, etc. clauses with the select function:

Users::query(|user| select(data! {
    id: user.id,
    name: user.name,
}, |selected| {
    expr!(selected.id < 5)
        .order_by(selected.name.asc()
            .then_by(selected.id.desc())
        )
        .limit::<3>()
}))

The type returned by typed_query depends on the query itself. The two queries above return an iterator over an anonymous struct. If a single value is selected, for example:

let user_count = conn.typed_query(Users::query(|_| expr!(COUNT(*))))?;

The type of user_count will be a u64.

See more examples.

(In)stability

This project requires Rust nightly, and uses a bunch of features, some incomplete. You might encounter ICEs. There will probably be many breaking changes to typed-qb itself as well. You probably shouldn't use typed-qb for serious projects.

License

typed-qb is licensed under the Mozilla Public License 2.0 (MPL2.0). See the LICENSE file.

You might also like...
Run SQL queries on CSV files

zsql run SQL queries on csv files A terminal utility to easily run SQL queries on CSV files. zsql is shipped as a small single binary powered by rust

Running SQL-like queries on files.

filesql Running SQL-like queries on files. Features Supported: REPL Basic SQL expressions. INSERT clause. (which inserts data into another file) WHERE

This crate allows you to send cypher queries to the REST endpoint of a neo4j database

rusted_cypher Rust crate for accessing the cypher endpoint of a neo4j server This crate allows you to send cypher queries to the REST endpoint of a ne

Implementation of generic IBC queries in CosmWasm.

CosmWasm IBC Queries Implements generic IBC queries in CosmWasm. This implementation requires the same contract to be deployed on both chains wishing

Scalable and fast data store optimised for time series data such as financial data, events, metrics for real time analysis

OnTimeDB Scalable and fast data store optimised for time series data such as financial data, events, metrics for real time analysis OnTimeDB is a time

The rust client for CeresDB. CeresDB is a high-performance, distributed, schema-less, cloud native time-series database that can handle both time-series and analytics workloads.

The rust client for CeresDB. CeresDB is a high-performance, distributed, schema-less, cloud native time-series database that can handle both time-series and analytics workloads.

rust_arango enables you to connect with ArangoDB server, access to database, execute AQL query, manage ArangoDB in an easy and intuitive way, both async and plain synchronous code with any HTTP ecosystem you love.

rust_arango enables you to connect with ArangoDB server, access to database, execute AQL query, manage ArangoDB in an easy and intuitive way, both async and plain synchronous code with any HTTP ecosystem you love.

ReefDB is a minimalistic, in-memory and on-disk database management system written in Rust, implementing basic SQL query capabilities and full-text search.
ReefDB is a minimalistic, in-memory and on-disk database management system written in Rust, implementing basic SQL query capabilities and full-text search.

ReefDB ReefDB is a minimalistic, in-memory and on-disk database management system written in Rust, implementing basic SQL query capabilities and full-

Query LDAP and AD with SQL

SQLDAP Ever wanted to query AD or LDAP with SQL like queries ? I'm going to answer this question myself: yes ! Why ? Because I never could remember al

Owner
ferrouille
ferrouille
A safe, extensible ORM and Query Builder for Rust

A safe, extensible ORM and Query Builder for Rust API Documentation: latest release – master branch Homepage Diesel gets rid of the boilerplate for da

Diesel 9.7k Jan 3, 2023
Diesel - A safe, extensible ORM and Query Builder for Rust

A safe, extensible ORM and Query Builder for Rust API Documentation: latest release – master branch Homepage Diesel gets rid of the boilerplate for da

Takayuki Maeda 0 Aug 31, 2020
Diesel - ORM and Query Builder for Rust

A safe, extensible ORM and Query Builder for Rust API Documentation: latest release – master branch Homepage Diesel gets rid of the boilerplate for da

Diesel 9.7k Jan 6, 2023
A Rust SQL query builder with a pleasant fluent API closely imitating actual SQL

Scooby An SQL query builder with a pleasant fluent API closely imitating actual SQL. Meant to comfortably build dynamic queries with a little bit of s

Aleksei Voronov 100 Nov 11, 2022
Fully typed SQL query builder for Rust [deprecated]

What is Deuterium? Deuterium is a fancy SQL builder for Rust. It's designed to provide a DSL to easily build SQL queries in safe and typed way. Like R

Stanislav Panferov 169 Nov 20, 2022
Rust library to parse, deparse and normalize SQL queries using the PostgreSQL query parser

This Rust library uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.

pganalyze 37 Dec 18, 2022
An object-relational in-memory cache, supports queries with an SQL-like query language.

qlcache An object-relational in-memory cache, supports queries with an SQL-like query language. Warning This is a rather low-level library, and only p

null 3 Nov 14, 2021
Rust High Performance compile-time ORM(RBSON based)

WebSite | 简体中文 | Showcase | 案例 A highly Performant,Safe,Dynamic SQL(Compile time) ORM framework written in Rust, inspired by Mybatis and MybatisPlus.

rbatis 1.7k Jan 7, 2023
Distributed, version controlled, SQL database with cryptographically verifiable storage, queries and results. Think git for postgres.

SDB - SignatureDB Distributed, version controlled, SQL database with cryptographically verifiable storage, queries and results. Think git for postgres

Fremantle Industries 5 Apr 26, 2022
fast & easy CLI and vscode extension specialized to format MySQL INSERT queries.

insertfmt fast & easy CLI specialized to format MySQL INSERT queries. format queries so that they look like a table. NOTE: If you wanna use the VSCode

canalun 7 May 2, 2023