Laws on a table
Grammar: table-law
A law written for table <name> applies to every query on that table. It takes one parameter, the value a query is run for, such as the contact or session asking. Its body is a SQL filter, not statements: Hive appears only in {…} values and if conditions, as in a query. A body that starts with if, or or and is a WHERE { } block, and its filter applies where its ifs hold; any other body always applies.
Every query that touches the table must take a value of the law's parameter type, and the compiler reads its SQL to hold it to the filter:
- what it reads, changes or deletes carries the filter, wherever the law's
ifholds: one of the parts its WHERE ANDs together is the filter, or something stricter.fk_contact_from_id = {contact.id}alone carries a filter that allows either column; - a row it inserts lands inside the filter, because one of its
column = valueparts writes the filter's value; - a row it updates stays inside the filter: setting a filtered column sets it to the filter's value.
Joins, subqueries and each side of a UNION are checked separately, and a column the law names counts as the table's when written through the table's alias. hive.sql.unsafe is not a query, so no law checks it: SQL assembled at run time is outside the guarantee, and easy to find.
type Contact {
id: Int
}
type Chat {
body: Str
}
law ownChatsOnly(contact: Contact) for table chats {
fk_contact_from_id = {contact.id} or
fk_contact_to_id = {contact.id}
}
query sent(contact: Contact): Chat[dyn] {
SELECT body FROM chats WHERE fk_contact_from_id = {contact.id}
}
query send(contact: Contact, to: Int, body: Str): void {
INSERT INTO chats (fk_contact_from_id, fk_contact_to_id, body) VALUES ({contact.id}, {to}, {body})
}
proc main(): void {
opened := hive.sql.pool(hive.sql.DatabaseDriver.SQLite(), ":memory:", 1, 1)
if opened is Result.Ok(db) {
_ := hive.sql.unsafe(db, "CREATE TABLE chats (fk_contact_from_id INTEGER, fk_contact_to_id INTEGER, body TEXT)")
_ := hive.sql.run(db, send(Contact(1), 2, "lunch?"))
echo hive.sql.run(db, sent(Contact(1)))
}
}type Organization {
id: Int
name: Str
}
type User {
Admin
Regular
id: Int
name: Str
org: Organization
}
type Session {
user: User
}
law sameOrganization(session: Session) for table people {
if session.user is User.Regular {
org_id = {session.user.org.id}
}
}
query names(session: Session): Str[dyn] {
SELECT name FROM people
WHERE {
if session.user is User.Regular { org_id = {session.user.org.id} }
}
ORDER BY name
}
proc main(): void {
bees := Organization(1, "Bees")
ada := Session(User.Regular(1, "Ada", bees))
root := Session(User.Admin(2, "Root", bees))
opened := hive.sql.pool(hive.sql.DatabaseDriver.SQLite(), ":memory:", 1, 1)
if opened is Result.Ok(db) {
hive.sql.unsafe(db, "CREATE TABLE people (name TEXT, org_id INTEGER)")
hive.sql.unsafe(db, "INSERT INTO people VALUES ('Ada', 1), ('Grace', 1), ('Tim', 2)")
echo hive.sql.run(db, names(ada))
echo hive.sql.run(db, names(root))
hive.sql.close(db)
}
}type Contact {
id: Int
}
type Chat {
body: Str
}
law ownChatsOnly(contact: Contact) for table chats {
fk_contact_from_id = {contact.id} or
fk_contact_to_id = {contact.id}
}
query everything(contact: Contact): Chat[dyn] {
SELECT body FROM chats
}
proc main(): void {
}$ hive check chats.hive
chats.hive:14: in `everything`: this query reads `chats` without the filter law `ownChatsOnly` keeps on it
its WHERE has to carry `fk_contact_from_id = {contact.id} OR fk_contact_to_id = {contact.id}`Run a program to see what it prints here.