TreSQL
-
Upload
neueda -
Category
Technology
-
view
1.014 -
download
0
Transcript of TreSQL
TreSQL (Tree SQL)
Mārtiņš Rumovskis
Agenda
Motivation
TreSQL vs SQL
TreSQL statement structure
Scala API
Agenda
Motivation
TreSQL vs SQL
TreSQL statement structure
Scala API
Old Good SQL
Traditionally we can get data in the tabular form DEPTNO DNAME LOC
30 SALES Chicago
Old Good SQL
But we need something like this...
Traditionally we can get data in the tabular form DEPTNO DNAME LOC
30 SALES Chicago
DEPTNO DNAME LOC EMPNO ENAME
30 SALES Chicago 1 ALLEN
2 WARD
3 MARTIN
20 RESEARCH Dallas 4 MILLER
5 SCOTT
Old Good SQL
But we need somethink like this...
Traditionally we can get data in the tabular form
Or like this...
DEPTNO DNAME LOC EMPNO ENAME
30 SALES Chicago 1 ALLEN
2 WARD
3 MARTIN
20 RESEARCH Dallas 4 MILLER
5 SCOTT
DEPTNO DNAME LOC
30 SALES Chicago
NAME DRIVER_LIC LANGUAGES
SCOTT A,B EN,LV,RU
MILLER B,C EN,IT
Old Good SQL (Cont)
We need to write joins
select * from emp e join dept d on e.deptno = d.deptno
Old Good SQL (Cont)
We need to write joins
Explicitly specify id column name
select * from emp e join dept d on e.deptno = d.deptno
select * from dept where id in (10, 20, 30)
Problems of JDBC
JDBC
We need to do SQL to Java mappings
...well you know
We need to bind every variable in the SQL statement noted as
‘?’ otherwise exception occurs
setXXX(idx, value)
Problems of JDBC
JDBC
We need to do SQL to Java mappings
...well you know
We cannot run several queries
setXXX(idx, value)
select * from dept where deptno = 10,
select * from emp where job = 'MGR'
What’s about ORM?
ORM (Hibernate)
We need to define class model and mapping
What’s about ORM?
ORM (Hibernate)
We need to define class model and mapping
And we still need SQL (for advanced use)
Agenda
Motivation
TreSQL vs SQL
TreSQL statement structure
Scala API
Sql vs TreSQL
Simple SQL
SQL: select * from dept
Sql vs TreSQL
SQL: select * from dept
TreSQL: dept
Simple SQL
Sql vs TreSQL
SQL: select * from dept where deptno = 10
Select... Where
Sql vs TreSQL
SQL: select * from dept where deptno = 10
TreSQL: dept[10]
Select... Where
Sql vs TreSQL
More complex select
SQL: select deptno, dname from dept
select ename from emp where deptno = :deptno
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}
Sql vs TreSQL
Multiple selects
SQL: select * from dept where deptno = 10
select * from emp where job = 'MGR'
Sql vs TreSQL
Multiple selects
SQL: select * from dept where deptno = 10
select * from emp where job = 'MGR‘
TreSQL: dept[10], emp[job = 'MGR']
Sql vs TreSQL
Outer join order by
SQL: select * from dept
left join emp on dept.deptno = emp.deptno
order by dname
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)
Sql vs TreSQL
In, subselect
SQL: select * from dept where deptno
in (select deptno from emp)
Sql vs TreSQL
In, subselect
SQL: select * from dept where deptno
in (select deptno from emp)
TreSQL: dept [deptno in (emp{deptno})]
Sql vs TreSQL
Exists, correlated subselect
SQL: select * from dept d where
exists select * from emp e
where d.deptno = e.deptno
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])]
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)
Agenda
Motivation
TreSQL vs SQL
TreSQL statement structure
Scala API
TreSQL Language Structure
SELECT statement structure
<from>[<where>][<columns>][<group by>
[<having>]][<order>][<offset> [<limit>]]
table1[join cond]table2[where]{columns} (group
cols)^(having expr) #(order) (offset limit)
TreSQL Language Structure
INSERT
Statement structure
DEPT {DEPTNO, DNAME, LOC}
+ [10, "ACCOUNTING", "NEW YORK"]
table {col1, col2} + [val1, val2]
TreSQL Language Structure
UPDATE
Statement structure
emp[1] {ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO} =
[?, ?, ?, ?, ?, ?, ?]
table [where] {col1, col2} = [val1, val2]
TreSQL Language Structure
DELETE
Statement structure
emp - [1]
table - [where]
Agenda
Motivation
TreSQL vs SQL
TreSQL statement structure
Scala API
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)
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
org.tresql.Env Expression execution environment. Used for setting
database connection.
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
org.tresql.Query
Expression execution, result iteration
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
org.tresql.Expr
Builded expression. Equivalent to java.sql.PreparedStatement
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
org.tresql.Expr
Expression result – org.tresql.Result
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)
result foreach println
expr close
Jsonizer.jsonize(Query(expr, pars), writer, rType)
org.tresql.Expr
Result JSON formatting
Scala API
Code example:
import org.tresql._
org.tresql.Env update connection
org.tresql.Query.foreach("emp[sal > ? & deptno = ?]", 1000, 10) {row=>
println(row(0))}
org.tresql.Query.foreach("emp[sal > ? & deptno = ?]", 1000) {row=>
println(row(0))}
org.tresql.Expr
Optional binding
That’s about it!
Visit: https://github.com/mrumkovskis/Query
Q&A