O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Querier – simple relational database access

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Querier:
simple relational database access
Michal Balda
What is Querier?
 A library for querying relational databases.
 Focused on simplicity.
 Heavily inspired by NotORM
(htt...
Why?
Accessing a relational database in Pharo:

Vídeos do YouTube não são mais aceitos pelo SlideShare

Visualizar original no YouTube

Confira estes a seguir

1 de 45 Anúncio

Querier – simple relational database access

Baixar para ler offline

Querier – simple relational database access
Tue, July 14, 3:00pm – 3:15pm

Video: https://www.youtube.com/watch?v=c2yhd8BjFjU

First Name: Michal
Last Name: Balda
Type: Talk

Abstract: Querier is a library which simplifies the usage of relational
databases in Pharo. It directly exposes tables and rows, there is no ORM
layer. Table querying uses a collection-like interface, queries are
built transparently and executed lazily. Simple and effective access to
related tables is one of the key features of Querier.

Bio: Michal Balda is a web developer, coming from the Czech Technical
University in Prague. He's currently using Pharo and Seaside to build a
commercial web application in a university research project.

Querier – simple relational database access
Tue, July 14, 3:00pm – 3:15pm

Video: https://www.youtube.com/watch?v=c2yhd8BjFjU

First Name: Michal
Last Name: Balda
Type: Talk

Abstract: Querier is a library which simplifies the usage of relational
databases in Pharo. It directly exposes tables and rows, there is no ORM
layer. Table querying uses a collection-like interface, queries are
built transparently and executed lazily. Simple and effective access to
related tables is one of the key features of Querier.

Bio: Michal Balda is a web developer, coming from the Czech Technical
University in Prague. He's currently using Pharo and Seaside to build a
commercial web application in a university research project.

Anúncio
Anúncio

Mais Conteúdo rRelacionado

Anúncio

Mais de ESUG (20)

Mais recentes (20)

Anúncio

Querier – simple relational database access

  1. 1. Querier: simple relational database access Michal Balda
  2. 2. What is Querier?  A library for querying relational databases.  Focused on simplicity.  Heavily inspired by NotORM (http://www.notorm.com/).
  3. 3. Why? Accessing a relational database in Pharo:
  4. 4. Why? Accessing a relational database in Pharo: 1) Write SQL by hand.
  5. 5. Why? Accessing a relational database in Pharo: 1) Write SQL by hand. 2) Use an object-relational mapper (GLORP).
  6. 6. Why? Accessing a relational database in Pharo: 1) Write SQL by hand. 1.5) Use Querier. 2) Use an object-relational mapper (GLORP).
  7. 7. Database structure Primary Key Column id Foreign Key Column {table}_id Table Name {table}
  8. 8. Setup | driver structure db | driver := "…". structure := QRRConventionalStructure new. db := Querier withDriver: driver structure: structure
  9. 9. Accessing a table db table: #song "or use a shortcut:" db song song id Integer, Primary Key title Varchar length Integer album_id Integer, Foreign Key
  10. 10. Accessing a table db table: #song "or use a shortcut:" db song SELECT * FROM song
  11. 11. Accessing a table db song do: [ :row | Transcript show: row title; cr ]
  12. 12. Two principles 1) A table is a collection of rows. 2) A row is a dictionary of values.
  13. 13. WHERE db song select: [ :row | (row length >= 180) & (row length <= 300) ] SELECT * FROM song WHERE length >= 180 AND length <= 300
  14. 14. WHERE db song select: [ :row | (row length >= 180) & (row length <= 300) ] Udo Schneider: BlockTranslators - parsing magic http://readthesourceluke.blogspot.com/2014/09/ block-translators-parsing-magic.html
  15. 15. ORDER BY db song sorted: [ :a :b | a length < b length ] SELECT * FROM song ORDER BY length ASC
  16. 16. LIMIT and OFFSET (db song sorted: [ :a :b | a length < b length ]) first: 10 SELECT * FROM song ORDER BY length ASC LIMIT 10
  17. 17. LIMIT and OFFSET (db song sorted: [ :a :b | a length < b length ]) allButFirst: 10 SELECT * FROM song ORDER BY length ASC OFFSET 10
  18. 18. Selecting a single row row := db song detect: [ :row | row id = 123 ] SELECT * FROM song WHERE id = 123 LIMIT 1
  19. 19. Selecting by primary key row := db song at: 123 SELECT * FROM song WHERE id = 123 LIMIT 1
  20. 20. Selecting by primary key row := db song at: 123 SELECT * FROM song WHERE id = 123 LIMIT 1
  21. 21. Aggregations db song average: [ :row | row length ] db song average: #length SELECT AVG(length) FROM song
  22. 22. Enumerating the result db song collect: [ :row | row title ] db song do: [ :row | Transcript show: row title; cr ] db song size
  23. 23. Accessing related tables song id Integer, Primary Key title Varchar length Integer album_id Integer, Foreign Key album id Integer, Primary Key title Varchar
  24. 24. Accessing related tables db song select: [ :row | row album name = 'Unknown Album' ] song id Integer, Primary Key title Varchar length Integer album_id Integer, Foreign Key album id Integer, Primary Key title Varchar
  25. 25. Accessing related tables db song select: [ :row | row album name = 'Unknown Album' ] SELECT * FROM song LEFT JOIN album ON song.album_id = album.id WHERE album.name = 'Unknown Album'
  26. 26. Accessing related tables db song select: [ :row | row album name = 'Unknown Album' ] SELECT * FROM song LEFT JOIN album ON song.album_id = album.id WHERE album.name = 'Unknown Album'
  27. 27. Accessing related tables db song select: [ :row | row album artist name = 'Unknown Artist' ] SELECT * FROM song LEFT JOIN album ON song.album_id = album.id LEFT JOIN artist ON album.artist_id = artist.id WHERE artist.name = 'Unknown Artist'
  28. 28. Accessing related tables db song do: [ :row | Transcript show: row album name ]
  29. 29. Accessing related tables db song do: [ :row | Transcript show: row album name ] 1) SELECT * FROM song
  30. 30. Accessing related tables db song do: [ :row | Transcript show: row album name ] 1) SELECT * FROM song
  31. 31. Accessing related tables db song do: [ :row | Transcript show: row album name ] 1) SELECT * FROM song 2) SELECT * FROM album WHERE id IN (1, 2, 3, …)
  32. 32. Accessing related tables song id Integer, Primary Key title Varchar length Integer album_id Integer, Foreign Key album id Integer, Primary Key title Varchar
  33. 33. The opposite direction db album do: [ :row | row songCollection do: [ :song | Transcript show: song name; cr ] ] song id Integer, Primary Key title Varchar length Integer album_id Integer, Foreign Key album id Integer, Primary Key title Varchar
  34. 34. The opposite direction db album do: [ :row | row songCollection do: [ :song | Transcript show: song name; cr ] ] 1) SELECT * FROM album
  35. 35. The opposite direction db album do: [ :row | row songCollection do: [ :song | Transcript show: song name; cr ] ] 1) SELECT * FROM album 2) SELECT * FROM song WHERE album_id IN (1, 2, 3, …)
  36. 36. UPDATE db song do: [ :row | row length: row length + 10. row save ] 1) SELECT * FROM song 2) UPDATE song SET length = 325 WHERE id = 1 3) UPDATE song SET length = 648 WHERE id = 2 4) … and many more
  37. 37. Better UPDATE db song update: [ :row | row length: row length + 10 ] UPDATE song SET length = length + 10
  38. 38. Better UPDATE (db song select: [ :row | row length < 180 ]) update: [ :row | row length: row length + 10 ] UPDATE song SET length = length + 10 WHERE length < 180
  39. 39. DELETE (db song select: [ :row | row length < 180 ]) removeAll db song delete: [ :row | row length < 180 ] DELETE FROM song WHERE length < 180
  40. 40. INSERT | row | row := db song new. row title: 'New Song'. row length: 316. row album: (db album detect: [ :row | row album name = 'Unknown Album' ]). row save. Transcript show: row id
  41. 41. Current Status  A proof-of-concept for Pharo + Postgres.  Working on polishing all features + querying other RDBMS through Garage (https://guillep.github.io/DBXTalk/garage/).
  42. 42. Future work  Add ORM-like features (instantiate your entity classes instead of dictionaries).  Add at least partial support for non-relational databases (like MongoDB).
  43. 43. Questions? http://querier.xmb.cz/
  44. 44. Thank you for your attention! http://querier.xmb.cz/

×