jsql-sx Wiki
jsql-sx is a javascript API for simple SQL-like local data handling
Brought to you by:
portucalle
Some examples on how SQL-like query strings can be mapped into the jsql-sx API programatic calls.
SQL> CREATE DATABASE mail;
var db = jsql.DB.open("mail");
SQL> CREATE TABLE inbox (
id INT NOT NULL,
date TEXT NOT NULL,
from TEXT NOT NULL,
to, TEXT NOT NULL,
msg, TEXT NOT NULL,
new, BIT NOT NULL,
PRIMARY KEY(id)
);
db.create("inbox",
[["id", "INTEGER", "PRIMARY KEY"],
["date", "TEXT", "NOT NULL"],
["from", "TEXT", "NOT NULL"],
["to", "TEXT", "NOT NULL"],
["msg", "TEXT", "NULL"],
["new", "BOOLEAN", "NOT NULL"]]
);
SQL> INSERT INTO inbox VALUES (
NULL,
"20151005",
"richard@company.com",
"anthony@company.com",
"Hi There!",
1
);
var id = db.insert("inbox",
[null,
"20151005",
"richard@company.com",
"anthony@company.com",
"Hi There!",
true]
);
SQL> SELECT from, to FROM inbox WHERE (id > 10 AND new = 1);
var result = db.select("inbox", ["from", "to"], [["id", ">", 10], "AND", ["new", "=", true]]);
SQL> SELECT * FROM inbox WHERE (
(from = 'john@company.com' AND to = 'martha@company.com') OR
(date >= '20151001' AND new = 0)
);
var result = db.select("inbox", ["*"],
[
[["from", "=", "john@company.com"], "AND", ["to", "=", "martha@company.com"]],
"OR",
[["date", ">=", "20151001"], "AND", ["new", "=", false]]
]
);
SQL> UPDATE inbox SET new = 0, date = "20151006" WHERE (id = 10 AND id = 7);
var n = db.update("inbox",
["new", false, "date", "20151006"],
[["id", "=", 10], "AND", ["id", "=", 7]]
);
SQL> ALTER TABLE inbox ADD tag TEXT;
var success = db.alter("inbox", "add", "tag", "TEXT", "NULL");
SQL> ALTER TABLE inbox MODIFY tag TEXT NOT NULL;
var success = db.alter("inbox", "MODIFY", "tag", "TEXT", "NOT NULL");
SQL> ALTER TABLE inbox DROP tag;
var success = db.alter("inbox", "drop", "tag");
SQL> DELETE FROM inbox WHERE (date < '20140101' AND new = 0);
var n = db.delete("inbox", [["date", "<", "20140101"], "AND", ["new", "=", false]]);
SQL> DROP TABLE inbox;
db.drop("inbox");
SQL> DROP DATABASE mail;
// Invoke the 'drop' class method //
jsql.DB.drop("mail");