Ideas | Charlie Lidbury

Contents

https://www.reddit.com/r/ProgrammerHumor/s/5SCidwqdye

Introduction

Relational databases are centred around the concept of sets of tuples, and set operations on those.

In my opinion, with modern language abstractions, these operations are not complicated enough to warrant having a separate textual DSL like SQL.

This project is an attempt to make these sets of tuples a library level construct, and throw them around like that.

I choose Rust as my weapon of choice for this, but anything with type classes should work like Haskell.

Heads up: In Rust, |x| x + 1 denotes λx. x + 1 (lambda calculus), or [](auto x) { return x + 1; } (C++). Not very intuitive syntax.

I have made a repo for the project, but it doesn’t have very much in it.

https://github.com/charlielidbury/rust_orm

Interface

Queries

The goal of this project is to allow something like the following.

// Fetch the name of all users
// who's age is greater than 18
let drinkers = db.users
  .filter(|u| u.age > 18)
  .map(|u| u.name)
  .query();

Explanation

db represents the entire database.

db.users represents the set of all users.

db.users.filter(...) represents the set of all users which satisfy the predicate. The predicate is only used to construct the query; in this example it is only invoked once.

(...).query() uses the connection to the database to get the list of drinkers.

Notice, we’re now dealing with sets of things which look like structs in our host language, instead of columns, so it would make sense to do things like this:

db.users
	.map(|u| (u, 1, u))

And instead of getting a flat tuple of values out, we get a triple with two users and a number in it. I am hoping this avoids the need to constantly marshal your data from it’s SQL form into your internal business logic objects.