TreSQL

43
TreSQL (Tree SQL) Mārtiņš Rumovskis

Transcript of TreSQL

Page 1: TreSQL

TreSQL (Tree SQL)

Mārtiņš Rumovskis

Page 2: TreSQL

Agenda

Motivation

TreSQL vs SQL

TreSQL statement structure

Scala API

Page 3: TreSQL

Agenda

Motivation

TreSQL vs SQL

TreSQL statement structure

Scala API

Page 4: TreSQL

Old Good SQL

Traditionally we can get data in the tabular form DEPTNO DNAME LOC

30 SALES Chicago

Page 5: TreSQL

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

Page 6: TreSQL

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

Page 7: TreSQL

Old Good SQL (Cont)

We need to write joins

select * from emp e join dept d on e.deptno = d.deptno

Page 8: TreSQL

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)

Page 9: TreSQL

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)

Page 10: TreSQL

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'

Page 11: TreSQL

What’s about ORM?

ORM (Hibernate)

We need to define class model and mapping

Page 12: TreSQL

What’s about ORM?

ORM (Hibernate)

We need to define class model and mapping

And we still need SQL (for advanced use)

Page 13: TreSQL

Agenda

Motivation

TreSQL vs SQL

TreSQL statement structure

Scala API

Page 14: TreSQL

Sql vs TreSQL

Simple SQL

SQL: select * from dept

Page 15: TreSQL

Sql vs TreSQL

SQL: select * from dept

TreSQL: dept

Simple SQL

Page 16: TreSQL

Sql vs TreSQL

SQL: select * from dept where deptno = 10

Select... Where

Page 17: TreSQL

Sql vs TreSQL

SQL: select * from dept where deptno = 10

TreSQL: dept[10]

Select... Where

Page 18: TreSQL

Sql vs TreSQL

More complex select

SQL: select deptno, dname from dept

select ename from emp where deptno = :deptno

Page 19: TreSQL

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}

Page 20: TreSQL

Sql vs TreSQL

Multiple selects

SQL: select * from dept where deptno = 10

select * from emp where job = 'MGR'

Page 21: TreSQL

Sql vs TreSQL

Multiple selects

SQL: select * from dept where deptno = 10

select * from emp where job = 'MGR‘

TreSQL: dept[10], emp[job = 'MGR']

Page 22: TreSQL

Sql vs TreSQL

Outer join order by

SQL: select * from dept

left join emp on dept.deptno = emp.deptno

order by dname

Page 23: TreSQL

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)

Page 24: TreSQL

Sql vs TreSQL

In, subselect

SQL: select * from dept where deptno

in (select deptno from emp)

Page 25: TreSQL

Sql vs TreSQL

In, subselect

SQL: select * from dept where deptno

in (select deptno from emp)

TreSQL: dept [deptno in (emp{deptno})]

Page 26: TreSQL

Sql vs TreSQL

Exists, correlated subselect

SQL: select * from dept d where

exists select * from emp e

where d.deptno = e.deptno

Page 27: TreSQL

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])]

Page 28: TreSQL

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)

Page 29: TreSQL

Agenda

Motivation

TreSQL vs SQL

TreSQL statement structure

Scala API

Page 30: TreSQL

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)

Page 31: TreSQL

TreSQL Language Structure

INSERT

Statement structure

DEPT {DEPTNO, DNAME, LOC}

+ [10, "ACCOUNTING", "NEW YORK"]

table {col1, col2} + [val1, val2]

Page 32: TreSQL

TreSQL Language Structure

UPDATE

Statement structure

emp[1] {ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO} =

[?, ?, ?, ?, ?, ?, ?]

table [where] {col1, col2} = [val1, val2]

Page 33: TreSQL

TreSQL Language Structure

DELETE

Statement structure

emp - [1]

table - [where]

Page 34: TreSQL

Agenda

Motivation

TreSQL vs SQL

TreSQL statement structure

Scala API

Page 35: TreSQL

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)

Page 36: TreSQL

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.

Page 37: TreSQL

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

Page 38: TreSQL

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

Page 39: TreSQL

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

Page 40: TreSQL

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

Page 41: TreSQL

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

Page 42: TreSQL

That’s about it!

Visit: https://github.com/mrumkovskis/Query

Page 43: TreSQL

Q&A