#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!

19 releases

new 0.2.2 Oct 15, 2020
0.2.1 Oct 12, 2020
0.1.16 Sep 24, 2020
0.1.15 Aug 27, 2020

#21 in WebAssembly

Download history 65/week @ 2020-08-04 94/week @ 2020-08-11 53/week @ 2020-08-18 65/week @ 2020-08-25 47/week @ 2020-09-01 53/week @ 2020-09-08 10/week @ 2020-09-15 37/week @ 2020-09-22 29/week @ 2020-09-29 22/week @ 2020-10-06 33/week @ 2020-10-13

161 downloads per month
Used in 2 crates

Apache-2.0

185KB
5K SLoC

GlueSQL

crates.io docs.rs LICENSE Rust

SQL Database Engine as a Library

GlueSQL is a SQL database library written in Rust which provides parser (sqlparser-rs), execution layer, and an optional storage (sled).
Developers can use GlueSQL to build their own SQL databases or they can simply use GlueSQL as an embedded SQL database using default storage.

Standalone Mode

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

Installation

In your Cargo.toml

[dependencies]
gluesql = "0.2"

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

Now you don't need to include sled-storage. So in Cargo.toml,

[dependencies]
gluesql = { version = "0.2", 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.2", default-features = false }

Usage

All you only need to do is implementing 2 traits: Store and StoreMut! In src/store/mod.rs,

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

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

And a single optional trait, whether implementing this is all up to you! In src/store/alter_table.rs,

pub trait AlterTable where Self: Sized {
    fn rename_schema(..) -> ..;
    fn rename_column(..) -> ..;
    fn add_column(..) -> ..;
    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 limited queries, it's in very early stage.

  • 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 current query supports in src/tests/*.

Other expected use cases

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

Contribution

It's very early stage, please feel free to do whatever you want to.
Only the thing you need to be aware of is...

  • Except for src/glue.rs, src/tests/ and src/utils/, there is no place to use mut keyword.

Dependencies

~2.6–3.5MB
~81K SLoC