JavaDatabase
Tables and rows without SQL: a model as data, keys made by the database, conditions with parameters, relations and a transaction.
What you will learn#
- the model of a database as data,
insert,byKey,where,query,update,remove,{0}marks that become real parameters,- relations (
children,parent) andbegin/rollback.
The program#
# Tutorial 24: JavaDatabase - tables and rows without SQL (H2 in memory; the jar comes from Maven Central the first time)
load "java.ring"
jMaven("com.h2database:h2:2.2.224")
# 1. the model: tables and columns are data, the tables are created when they do not exist
oDb = new JavaDatabase("jdbc:h2:mem:tutorial",[
["Authors",[
["Id","int",1],
["Name","string",0]
]],
["Books",[
["Id","int",1],
["Title","string",0],
["Published","int",0],
["AuthorId","int",0,[["references","Authors"]]]
]]
])
# 2. insert gives the key that the database made
nTolkien = oDb.insert("Authors",[["Name","Tolkien"]])
nAdams = oDb.insert("Authors",[["Name","Adams"]])
oDb.insert("Books",[["Title","The Hobbit"],["Published",1937],["AuthorId",nTolkien]])
oDb.insert("Books",[["Title","The Lord of the Rings"],["Published",1954],["AuthorId",nTolkien]])
oDb.insert("Books",[["Title","The Hitchhiker's Guide"],["Published",1979],["AuthorId",nAdams]])
# 3. read: a row is a list of [name, value] pairs
aRow = oDb.byKey("Books",1)
? jDbValue(aRow,"Title") + " (" + jDbValue(aRow,"Published") + ")"
# 4. a condition is SQL with {0} {1} marks that become real parameters
for aBook in oDb.where("Books","Published > {0} AND Title LIKE {1}",[1940,"The%"])
? jDbValue(aBook,"Title")
next
# 5. a query as data, and totals
aRows = oDb.query("Books",[["orderby",[["Published","desc"]]],["take",2],["select",["Title","Published"]]])
for aBook in aRows
? jDbValue(aBook,"Title")
next
? "books: " + oDb.count("Books") + ", first published " + oDb.min("Books","Published",[])
# 6. relations
? "Tolkien wrote " + len(oDb.children("Authors",nTolkien,"Books")) + " books"
? "The author of book 3: " + jDbValue(oDb.parent("Books",3,"Authors"),"Name")
# 7. update, delete and a transaction
oDb.update("Books",1,[["Published",1938]])
oDb.begin()
oDb.remove("Books",3)
oDb.rollback()
? "after the rollback: " + oDb.count("Books") + " books"
oDb.close()The Hobbit (1937)
The Lord of the Rings
The Hitchhiker's Guide
The Hitchhiker's Guide
The Lord of the Rings
books: 3, first published 1937
Tolkien wrote 2 books
The author of book 3: Adams
after the rollback: 3 booksHow it works#
JavaDatabase makes the tables that do not exist from the model, and every method builds a statement with parameters, so a value in a condition is never joined into the SQL text. The names of tables and columns are checked. Avoid the reserved words of SQL for a column (Year, Order, Group, User): the database refuses them. The same program works on SQLite or PostgreSQL by changing the URL and the jar. See Databases.
Exercises#
- Add a
Reviewstable that referencesBooks. - Print the number of books of each author with
groupCount. - Use a SQLite file instead of H2.