#sql #functional #webassembly #sql-database #no-mut-in-the-middle

gluesql

GlueSQL - Open source SQL database engine fully written in Rust with pure functional execution layer, easily swappable storage and web assembly support!

24 releases

0.4.1 Feb 21, 2021
0.4.0 Dec 14, 2020
0.3.1 Dec 5, 2020
0.3.0 Nov 15, 2020
0.1.15 Aug 27, 2020

#23 in WebAssembly

Download history 36/week @ 2020-11-08 39/week @ 2020-11-15 31/week @ 2020-11-22 20/week @ 2020-11-29 36/week @ 2020-12-06 22/week @ 2020-12-13 5/week @ 2020-12-20 3/week @ 2020-12-27 31/week @ 2021-01-03 11/week @ 2021-01-10 46/week @ 2021-01-17 29/week @ 2021-01-24 4/week @ 2021-01-31 30/week @ 2021-02-07 254/week @ 2021-02-14 22/week @ 2021-02-21

173 downloads per month
Used in 2 crates

Apache-2.0

220KB
6K SLoC

GlueSQL

crates.io docs.rs LICENSE Rust Chat

SQL Database Engine as a Library

GlueSQL is a SQL database library written in Rust. It provides a parser (sqlparser-rs), execution layer, and optional storage (sled) packaged into a single library. Developers can choose to use GlueSQL to build their own SQL database, or as an embedded SQL database using the default storage engine.

Standalone Mode

You can use GlueSQL as an embedded SQL database. GlueSQL provides sled as a default storage engine.

Installation

In your Cargo.toml:

[dependencies]
gluesql = "0.4"

Usage

use gluesql::{parse, Glue, SledStorage};

fn main() {
    let storage = SledStorage::new("data.db").unwrap();
    let mut glue = Glue::new(storage);

    let sqls = "
        CREATE TABLE Glue (id INTEGER);
        INSERT INTO Glue VALUES (100);
        INSERT INTO Glue VALUES (200);
        SELECT * FROM Glue WHERE id > 100;
        DROP TABLE Glue;
    ";
    
    for query in parse(sqls).unwrap() {
        glue.execute(&query).unwrap();
    }
}

SQL Library Mode (For Custom Storage)

Installation

sled-storage is optional. So in Cargo.toml:

[dependencies]
gluesql = { version = "0.4", default-features = false, features = ["alter-table"] }

# alter-table is optional.
# If your DB does not have plan to support ALTER TABLE, then use this below.
gluesql = { version = "0.4", default-features = false }

Usage

There are two required 2 traits for using GlueSQL: Store and StoreMut. In src/store/mod.rs,

pub trait Store<T: Debug> {
    async fn fetch_schema(..) -> ..;
    async fn scan_data(..) -> ..;
}

pub trait StoreMut<T: Debug> where Self: Sized {
    async fn insert_schema(..) -> ..;
    async fn delete_schema(..) -> ..;
    async fn insert_data(..) -> ..;
    async fn update_data(..) -> ..;
    async fn delete_data(..) -> ..;
}

.. there is also a single, optional trait: In src/store/alter_table.rs,

pub trait AlterTable where Self: Sized {
    async fn rename_schema(..) -> ..;
    async fn rename_column(..) -> ..;
    async fn add_column(..) -> ..;
    async fn drop_column(..) -> ..;
}

Examples - GlueSQL-js

Use SQL in web browsers! GlueSQL-js provides 3 storage options,

  • in-memory
  • localStorage
  • sessionStorage.

SQL Features

GlueSQL currently supports a limited subset of queries. It's being actively developed.

  • CREATE with 4 types: INTEGER, FLOAT, BOOLEAN, TEXT with an optional NULL attribute.
  • ALTER TABLE with 4 operations: ADD COLUMN, DROP COLUMN, RENAME COLUMN and RENAME TO.
  • INSERT, UPDATE, DELETE, SELECT, DROP TABLE
  • GROUP BY, HAVING
  • Nested select, join, aggregations ...

You can see tests for the currently supported queries in src/tests/*.

Other expected use cases

  • Run SQL in web browsers - gluesql-js It would be cool to make a state management library using gluesql-js.
  • Add SQL layer to NoSQL databases: Redis, CouchDB...
  • Build new SQL database management system

Contribution

GlueSQL is still in the very early stages of development. Please feel free to contribute however you'd like! The only thing you need to be aware of is...

  • Except for src/glue.rs, src/tests/ and src/utils/, using the mut keyword is discouraged.

Dependencies

~3.5–4.5MB
~99K SLoC