advertisement

Javantura Zagreb 2014 - Groovy-SQL - Dinko Srkoč

67 %
33 %
advertisement
Information about Javantura Zagreb 2014 - Groovy-SQL - Dinko Srkoč
Technology

Published on March 2, 2014

Author: hujak

Source: slideshare.net

Description

Konferencija Javantura Zagreb 2014 by HUJAK

All your base are belong to us – pristup bazama podataka na Groovy način by Dinko Srkoč

Službeni Oracleov JDBC tutorijal počinje riječima: “JDBC API je osmišljen kako bi držao jednostavne stvari jednostavnim. To znači da JDBC čini svakodnevni rad s bazama podataka laganim”. Je li moguće taj lagani rad još malo olakšati? Na predavanju ćemo vidjeti kakvu podršku radu s bazama podataka pruža Groovy (groovy.codehaus.org), popularni programski jezik za JVM.
advertisement

All your base are belong to us Pristup bazama podataka na Groovy način Dinko Srkoč, Helix d.o.o.

Groovy ● jezik s dinamičkim tipovima ● mogućnost statičke provjere tipova ● jednostavna integracija s Javom ● meta programiranje ● lagana izrada domenskih jezika (DSL) ● skripte

Groovy @groovy.transform.ToString class Osoba { def ime def prezime } def lista = 1..10 def osobe = lista.collect { i -> new Osoba(ime: "Pero", prezime: "Perić the ${i}.") } println osobe[1] // ispis: Osoba(Pero, Perić the 2.)

“ The JDBC™ API was designed to keep simple things simple. This means that the JDBC makes everyday database tasks easy. ” What happen ? http://docs.oracle.com/javase/tutorial/jdbc/

JDBC Java Database Connectivity ● komunikacija s bazom podataka ili drugim tabličnim izvorom podataka ● neovisan o konkretnom sustavu za upravljanje bazama podataka ● API za pristup bazi koristeći SQL Somebody set us up the bomb.

JDBC spajanje na bazu import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; public class Foo { public static void main (String [] args) { try { Class.forName("org.h2.Driver"); Connection conn = DriverManager.getConnection("jdbc:h2:mem"); // ... } catch (SQLException e) { // ... } catch (ClassNotFoundException e) { // ... } } }

JDBC spajanje na bazu import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; // … try { Connection conn = dataSource.getConnection(); // ... } catch (SQLException e) { // ... } We get signal.

JDBC dohvat podataka import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; Statement stmt = conn.createStatement(); try { ResultSet rs = stmt.executeQuery("SELECT col_a, col_b FROM a_table"); while (rs.next()) { String colA = rs.getString("col_a"); int colB = rs.getInt("col_b"); // napraviti nešto s colA i colB ... } } catch (SQLException e) { // ... } finally { stmt.close(); }

Ostalo (ORM, …) ● rješava problem upravljanja resursima ● nije potrebno pisati SQL za jednostavnije slučajeve ● automatska pretvorba: ResultSet →POJO ali ● izrada domain modela ● konfiguracija (XML, anotacije) ● često vlastiti jezik za složene upite ● dohvat više podataka nego što je potrebno What !

Groovy groovy.sql.Sql

groovy.sql.Sql spajanje na bazu import groovy.sql.Sql def sql = Sql.newInstance('jdbc:h2:mem', 'org.h2.Driver') // ili def sql = new Sql(dataSource) Main screen turn on.

groovy.sql.Sql dohvat podataka def sql = ... sql.eachRow('SELECT col_a, col_b FROM a_table') { row -> // GroovyResultSet row.col_a // napraviti nešto s col_a row.col_b // i col_b }

groovy.sql.Sql dohvat podataka [ meta-podaci ] def sql = ... def printColNames = { meta -> // ResultSetMetaData (1..meta.columnCount).each { print meta.getColumnLabel(it).padRight(20) } println() } sql.eachRow('SELECT * FROM a_table', printColNames) { row -> row.toRowResult().values().each { print it.toString().padRight(20) } println() } It’s you !!

groovy.sql.Sql dohvat podataka [ parametarski upit ] def sql = ... def limit = Date.from('yyyy-MM-dd', '2014-02-22') sql.eachRow("SELECT * FROM a_table WHERE col_c < ?", [limit]) { row -> // ... }

groovy.sql.Sql dohvat podataka [ parametarski upit ] def sql = ... def namedParam = [limit: Date.from('yyyy-MM-dd', '2014-02-22')] sql.eachRow("SELECT * FROM a_table WHERE col_a < :limit", namedParam) { // ... } How are you gentlemen !!

groovy.sql.Sql dohvat podataka [ parametarski upit ] def sql = ... def limit = Date.from('yyyy-MM-dd', '2014-02-22') sql.eachRow("SELECT * FROM a_table WHERE col_a < ${limit}") { row -> // ... }

groovy.sql.Sql dohvat podataka [ parametarski upit ] def sql = ... class LimitMe { def limit // ... } def limitObj = new LimitMe(limit: Date.from('yyyy-MM-dd', '2014-02-22')) sql.eachRow("SELECT * FROM a_table WHERE col_a < :limit", limitObj) { row -> // ... } You are on the way to destruction.

groovy.sql.Sql dohvat podataka [dohvat jednog sloga] def sql = ... def obj = sql.firstRow('SELECT * FROM a_table WHERE col_a = 1') println obj.col_a + obj.col_b

groovy.sql.Sql dohvat podataka [ lista slogova ] class Osoba { def ime def prezime def mjesto_rodjenja def god_rodjenja } sql.rows('SELECT mjesto_rodjenja, god_rodjenja FROM osobe').collect { row -> new Osoba(row) }.findAll { osoba -> osoba.god_rodjenja > 1986 // zadnji prolaz Halleyevog kometa }.groupBy { osoba -> osoba.mjesto_rodjenja }.collectEntries { mjesto, osobe -> [mjesto, osobe.size()] What you say !! }

groovy.sql.Sql def sql = ... sql.execute ''' INSERT INTO a_table (col_a, col_b, col_c) VALUES (?, ?, ?) ''', [42, 'foo', 'bar'] mijenjanje podataka [ insert / update / ...]

groovy.sql.Sql transakcije def sql = ... sql.withTransaction { (1..100).each { sql.execute("UPDATE a_table SET col_b = ${calc(it)} WHERE col_a = ${it}") } } You have no chance to survive make your time.

groovy.sql.Sql batch operacije def sql = ... sql.withTransaction { sql.withBatch(30, 'INSERT INTO a_table VALUES (?, ?, ?)') { pstmt -> sql.eachRow('SELECT * FROM b_table') { bTable -> pstmt.addBatch(bTable.foo, bTable.bar, bTable.baz) } } }

Groovy groovy.sql.DataSet

groovy.sql.DataSet class Osoba { String ime String prezime String mjestoRodjenja Integer godinaRodjenja } def osobe = new DataSet(sql, Osoba) osobe.findAll { it.godinaRodjenja > 1986 }.findAll { it.mjestoRodjenja == 'Zagreb' }.sort { it.prezime }.revert().rows() sql.rows ''' SELECT * FROM osobe WHERE godinarodjenja > 1986 AND mjestorodjenja = 'Zagreb' ORDER BY prezime DESC ''' You know what you doing.

Groovy hr.helix.sqlstream.StreamingResultSet

StreamingResultSet veliki skup podataka def sql = ... sql.withStream('SELECT * FROM the_world') { stream -> stream.collect { row -> new Osoba(row) }.findAll { osoba -> osoba.god_rodjenja > 1986 }.take(1000) .toList() }

Q A & Hvala! For great justice.

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Javantura Zagreb 2014 - Groovy SQL - Dinko Srkoč - YouTube

Javantura Zagreb 2014 - Groovy SQL - Dinko Srkoč Javantura conference. ... Javantura Zagreb 2014 - Nashorn - Miroslav Rešetar - Duration: 30:26.
Read more

Speakers @ Javantura v2 Nov 2014 | Javantura

Speakers @ Javantura v2 Nov 2014. ... Dinko Srkoč Ivo Šalković ... Konferencija Javantura v2 Zagreb 2014;
Read more

Javantura Zagreb 2014 - universAAL - Andrej Grgurić - YouTube

Javantura Zagreb 2014 ... Javantura Zagreb 2014 ... Javantura Zagreb 2014 - Groovy SQL - Dinko Srkoč - Duration: 30:49.
Read more

Javantura v2 Zagreb 2014 - Upitnik o konferenciji

Molimo da ispunite ovaj upitnik i ocijenite konferenciju Javantura v2 Zagreb 2014. ... prostorom i načinom organizacije konferencije Javantura? * 1: 2: 3 ...
Read more

Dinko Srkoč - Google+

Dinko Srkoč - Zagreb - Veni ... 2014 The picture was taken during his Zagreb concert in ... Dinko Srkoč was in a video call with 1 person. Read more

dsrkoc (Dinko Srkoč) · GitHub

Dinko Srkoč. dsrkoc. Zagreb; dinko ... Zagreb; dinko.srkoc@gmail.com; Joined ... groovy-sql-stream-extension 12 Efficient Groovy data set processing using ...
Read more

Stjepan Matijašević | LinkedIn

View Stjepan Matijašević’s professional profile on LinkedIn. ... Javantura Zagreb 2014 conference. ... Dinko Srkoc. Senior Software ...
Read more

Studentske aktivnosti - IEEE

Studentske aktivnosti. ... Studentski ogranak Rijeka. Studentski ogranak Split. Studentski ogranak Zagreb. ... Odnosi s gospodarstvom . Konferencije. 2017 ...
Read more