2. Agenda
Motivation
TreSQL vs SQL
TreSQL statement structure
Scala API
3. Agenda
Motivation
TreSQL vs SQL
TreSQL statement structure
Scala API
4. Old Good SQL
Traditionally we can get data in the tabular form
DEPTNO DNAME LOC
30 SALES Chicago
5. Old Good SQL
Traditionally we can get data in the tabular form
DEPTNO DNAME LOC
30 SALES Chicago
But we need something like this...
DEPTNO DNAME LOC EMPNO ENAME
30 SALES Chicago 1 ALLEN
2 WARD
3 MARTIN
20 RESEARCH Dallas 4 MILLER
5 SCOTT
6. Old Good SQL
Traditionally we can get data in the tabular form
DEPTNO DNAME LOC
30 SALES Chicago
But we need somethink like this...
DEPTNO DNAME LOC EMPNO ENAME
30 SALES Chicago 1 ALLEN
2 WARD
3 MARTIN
20 RESEARCH Dallas 4 MILLER
5 SCOTT
Or like this...
NAME DRIVER_LIC LANGUAGES
SCOTT A,B EN,LV,RU
MILLER B,C EN,IT
7. Old Good SQL (Cont)
We need to write joins
select * from emp e join dept d on e.deptno = d.deptno
8. Old Good SQL (Cont)
We need to write joins
select * from emp e join dept d on e.deptno = d.deptno
Explicitly specify id column name
select * from dept where id in (10, 20, 30)
9. Problems of JDBC
JDBC
We need to do SQL to Java mappings
setXXX(idx, value)
...well you know
We need to bind every variable in the SQL statement noted as
‘?’ otherwise exception occurs
10. Problems of JDBC
JDBC
We need to do SQL to Java mappings
setXXX(idx, value)
...well you know
We cannot run several queries
select * from dept where deptno = 10,
select * from emp where job = 'MGR'
11. What’s about ORM?
ORM (Hibernate)
We need to define class model and mapping
12. What’s about ORM?
ORM (Hibernate)
We need to define class model and mapping
And we still need SQL (for advanced use)
13. Agenda
Motivation
TreSQL vs SQL
TreSQL statement structure
Scala API
15. Sql vs TreSQL
Simple SQL
SQL: select * from dept
TreSQL: dept
16. Sql vs TreSQL
Select... Where
SQL: select * from dept where deptno = 10
17. Sql vs TreSQL
Select... Where
SQL: select * from dept where deptno = 10
TreSQL: dept[10]
18. Sql vs TreSQL
More complex select
SQL: select deptno, dname from dept
select ename from emp where deptno = :deptno
19. Sql vs TreSQL
More complex select (needs programming when using
SQL)
SQL: select deptno, dname from dept
select ename from emp where deptno = :deptno
TreSQL: dept{deptno, dname,
|emp[:1(1) = deptno] {ename} employees}
20. Sql vs TreSQL
Multiple selects
SQL: select * from dept where deptno = 10
select * from emp where job = 'MGR'
21. Sql vs TreSQL
Multiple selects
SQL: select * from dept where deptno = 10
select * from emp where job = 'MGR‘
TreSQL: dept[10], emp[job = 'MGR']
22. Sql vs TreSQL
Outer join order by
SQL: select * from dept
left join emp on dept.deptno = emp.deptno
order by dname
23. Sql vs TreSQL
Outer join order by
SQL: select * from dept
left join emp on dept.deptno = emp.deptno
order by dname
TreSQL: dept/emp? #(dname)
24. Sql vs TreSQL
In, subselect
SQL: select * from dept where deptno
in (select deptno from emp)
25. Sql vs TreSQL
In, subselect
SQL: select * from dept where deptno
in (select deptno from emp)
TreSQL: dept [deptno in (emp{deptno})]
26. Sql vs TreSQL
Exists, correlated subselect
SQL: select * from dept d where
exists select * from emp e
where d.deptno = e.deptno
27. Sql vs TreSQL
Exists, correlated subselect
SQL: select * from dept d where
exists select * from emp e
where d.deptno = e.deptno
TreSQL: dept d[(emp e[e.deptno = d.deptno])]
28. Sql vs TreSQL
More complex example (using SQL programming is
needed)
dept{deptno, dname,
|emp[sal >= hisal & sal <= losal]salgrade[deptno =
:1(1)]
{grade, hisal, losal, count(empno) empcount,
|emp/dept[sal >= :1(2) & sal <= :1(3) &
emp.deptno = :2(1)]
{ename, emp.deptno, dname, sal}#(empno) emps
} (grade, hisal, losal) #(1) salgrades
} #(deptno)
29. Agenda
Motivation
TreSQL vs SQL
TreSQL statement structure
Scala API
34. Agenda
Motivation
TreSQL vs SQL
TreSQL statement structure
Scala API
35. Scala API
Code example:
import org.tresql._
Env update connection
Query.foreach("dept[?]{dname}", 10) {row=> println(row(0))}
//
val expr = Query.build("emp[deptno = ?]{*}")
val result = expr.select(10)
result foreach println
expr close
Jsonizer.jsonize(Query(expr, pars), writer, rType)
36. Scala API
Code example:
import org.tresql._ org.tresql.Env
Expression execution environment. Used for setting
database connection.
Env update connection
Query.foreach("dept[?]{dname}", 10) {row=> println(row(0))}
//
val expr = Query.build("emp[deptno = ?]{*}")
val result = expr.select(10)
result foreach println
expr close
37. Scala API
Code example:
import org.tresql._
Env update connection
Query.foreach("dept[?]{dname}", 10) {row=> println(row(0))}
// org.tresql.Query
Expression execution, result iteration
val expr = Query.build("emp[deptno = ?]{*}")
val result = expr.select(10)
result foreach println
expr close
38. Scala API
Code example:
import org.tresql._
Env update connection
Query.foreach("dept[?]{dname}", 10) {row=> println(row(0))}
org.tresql.Expr
// Builded expression. Equivalent to java.sql.PreparedStatement
val expr = Query.build("emp[deptno = ?]{*}")
val result = expr.select(10)
result foreach println
expr close
39. Scala API
Code example:
import org.tresql._
Env update connection
Query.foreach("dept[?]{dname}", 10) {row=> println(row(0))}
//
val expr = Query.build("emp[deptno = ?]{*}")
val result = expr.select(10)
org.tresql.Expr
result foreach println Expression result – org.tresql.Result
expr close
40. Scala API
Code example:
import org.tresql._
org.tresql.Env update connection
org.tresql.Query.foreach("dept[?]{dname}", 10) {row=> println(row(0))}
//
val expr = Query.build("emp[deptno = ?]{*}")
val result = expr.select(10)
org.tresql.Expr
result foreach println
Result JSON formatting
expr close
Jsonizer.jsonize(Query(expr, pars), writer, rType)