Delia requires Java 8 or higher.
It can be obtained from Github, or from Maven (available from Maven central):
<dependency>
<groupId>org.delia-lang</groupId>
<artifactId>delia</artifactId>
<version>0.2.0</version>
</dependency>
We will use the H2 database for this tutorial. Add it to your Maven pom.xml file:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
The following Java code will configure Delia for the H2 database.
ConnectionInfo info = ConnectionBuilder.dbType(DBType.H2).jdbcUrl("jdbc:h2:~/test").build();
Delia delia = DeliaBuilder.withConnection(info).build();
We define database types by writing some Delia source code. Normally Delia source would be in a text file, but for simplicity we'll define it directly in Java. We'll define a Customer type with two fields. We will also add some Delia statements to insert a record and then query for it.
String s1 = "type Customer struct {id int primaryKey, name String } end";
String s2 = " insert Customer {id: 1, name: 'Andrew Gonzalez'}";
String s3 = " let cust = Customer[1]"; //query by primary key
String deliaSource = s1 + s2 + s3;
When we execute the delia source code, Delia creates a Customer database table, and then peforms the insert and query. The result is available as a DValue object.
DeliaSession session = delia.beginSession(deliaSource); //execute
ResultValue res = session.getFinalResult();
DValue dval = res.getAsDValue();
System.out.println(dval.asStruct().getField("id").asInt());
System.out.println(dval.asStruct().getField("name").asString());
The output would be:
1
Andrew Gonzalez
Once we have compiled the type definition we can execute additonal Delia statements. Let's do another query;
String s4 = " let cust2 = Customer[name like '%Gon%']"; //SQL LIKE expression
DeliaSession session = delia.continueExecution(s4); //execute
ResultValue res = session.getFinalResult();
DValue dval = res.getAsDValue();
System.out.println(dval.asStruct().getField("id").asInt());
System.out.println(dval.asStruct().getField("name").asString());
The output would be:
1
Andrew Gonzalez
Executing Delia source code is powerful, but it is often simpler to use the Delia DAO (Data Access Object). The DAO provides a CRUD-like API for doing database operations. Internally it is executing Delia source code, like our previous example.
DeliaDao = new DeliaDao(delia);
dao.initialize(s1); //compile Delia and create tables
dao.insertOne("Customer", "id: 1, name: 'Andrew Gonzalez'");
ResultValue res = dao.queryByPrimaryKey("Customer", "1");
DValue dval = res.getAsDValue();
System.out.println(dval.asStruct().getField("id").asInt());
System.out.println(dval.asStruct().getField("name").asString());
The output is the same as before:
1
Andrew Gonzalez
Next, The REPL.