Home /Modules/sleela-sql/SLEELASQL.md

SLEELASQL.md

lib/sleela-sql/docs/SLEELASQL.md · 101 line(s) · markdown

SLeeLaSQL — the fluent dialect

SLeeLaSQL is a clean, method-chaining surface language for the sleela-sql engine. It is a second front-end to the engine, not a second engine: every SLeeLaSQL statement compiles to the same internal operation as its classic SQL counterpart and runs on the same executor, so the two dialects are feature-complete with each other and produce byte-identical results.

It reads the way SLeeLa code reads — a root, then dotted verbs — so a developer already fluent in SLeeLa can drive the database without switching mental models.

                 SQL  ──┐
                        ├──►  one compiled statement  ──►  one executor
           SLeeLaSQL  ──┘        (ssql_stmt IR)

Why a second dialect

Grammar

A statement is a root that names the table, followed by a dotted verb (and, for select, an optional .where(...)). ? is a positional placeholder in both dialects. Values may be bare, single-quoted, or double-quoted.

OperationSLeeLaSQLClassic SQL
Createtable("t").create(c1, c2, ...)CREATE TABLE t (c1, c2, ...)
Dropfrom("t").drop()DROP TABLE t
Drop if existsfrom("t").drop(ifExists)DROP TABLE IF EXISTS t
Insertinto("t").insert(v1, v2, ...)INSERT INTO t VALUES (v1, v2, ...)
Select allfrom("t").select(*)SELECT * FROM t
Select colsfrom("t").select(c1, c2)SELECT c1, c2 FROM t
Filterfrom("t").select(*).where(c == v)SELECT * FROM t WHERE c = v
List tablestables()SHOW TABLES

Notes:

Prepared statements (both dialects)

Compile once, bind, run — repeatedly. The ? placeholders are 1-based, as in JDBC/SQLite.

ssql_stmt *st = NULL;
ssql_prepare(&db, "into('games').insert(?, ?, ?)", SSQL_DIALECT_SLEELA, &st);

for (int i = 0; i < n; i++) {
    ssql_reset(st);
    ssql_bind(st, 1, ids[i]);
    ssql_bind(st, 2, titles[i]);
    ssql_bind(st, 3, years[i]);
    ssql_run(st, NULL);          /* parsed once; only bind+execute per row */
}
ssql_finalize(st);

The identical pattern works for classic SQL — just prepare "INSERT INTO games VALUES (?, ?, ?)" with SSQL_DIALECT_SQL (or AUTO).

Dialect selection

Worked example

# identical effect, two dialects
sleela-sql ./data "SELECT title, year FROM games WHERE year = '1986'"
sleela-sql ./data "from('games').select(title, year).where(year == '1986')"

Both print:

title,year
Metroid,1986

See ../samples/demo.ssql for a full SLeeLaSQL session.