Slide 1

Slide 1 text

How to be a Database Lumberjack (In an ORM world) Alberto Ballano Android Developer @ XING AG

Slide 2

Slide 2 text

WHW (why? how? what?)

Slide 3

Slide 3 text

Why? - DBs are the most efficient way of storing mid-large amount of long-living data.

Slide 4

Slide 4 text

Why? - DBs are the most efficient way of storing mid-large amount of long-living data. - Basic SQL is boring and very annoying to write.

Slide 5

Slide 5 text

Why? - DBs are the most efficient way of storing mid-large amount of long-living data. - Basic SQL is boring and very annoying to write. - Most people don’t know/care about performance.

Slide 6

Slide 6 text

What about ORM? Pros: - Easier to use than SQLiteDatabase/ContentResolver - You don't have to deal with SQL - Object mapping Cons: - ORM can generate slow and/or crazy SQL queries - ORM often becomes bottleneck (performance & flexibility) in critical period of application development (quoted from Artem Zinnatullin in reddit)

Slide 7

Slide 7 text

What about ORM? Pros: - Apparently easier to use than SQLiteDatabase/ContentResolver - You won’t learn SQL - Object mapping? Cons: - Less powerful than SQL - You have to deal with ORM (not even talking about migrations) - ORM can generate slow and/or crazy SQL queries - ORM often becomes bottleneck (performance & flexibility) in critical period of application development (quoted from myself)

Slide 8

Slide 8 text

But Realm... - Lib size (.jar is around 4MB) - Method count - Autoincrement - Complex queries - Models need to extend RealmObject - Concurrency

Slide 9

Slide 9 text

DB performance? Is that a thing?

Slide 10

Slide 10 text

DB performance? Is that a thing? - Query on UI thread

Slide 11

Slide 11 text

DB performance? Is that a thing? - Query on UI thread - SELECT * for only 1 column

Slide 12

Slide 12 text

DB performance? Is that a thing? - Query on UI thread - SELECT * for only 1 column - No Null/count check

Slide 13

Slide 13 text

DB performance? Is that a thing? - Query on UI thread - SELECT * for only 1 column - No Null/count check - [Bonus] SQL injection available!

Slide 14

Slide 14 text

DB vs ORM ORM - Usually faster development at the beginning (depends on the lib) - Powerful enough for day to day requirements - Enough control with right lib - Ok to good speed always (except for SugarORM)

Slide 15

Slide 15 text

DB vs ORM DB - Can be harder at the beginning, specially without right tools/knowledge - Very powerful - Full control - Optimal speed if used well

Slide 16

Slide 16 text

How?

Slide 17

Slide 17 text

How? - SQLDelight

Slide 18

Slide 18 text

How? - SQLDelight (+ optional plugin)

Slide 19

Slide 19 text

How? - SQLDelight (+ optional plugin) - SQLBrite

Slide 20

Slide 20 text

How? - SQLDelight (+ optional plugin) - SQLBrite - [Extra] Autovalue/Retrolambda/Kotlin for reduced amount of code

Slide 21

Slide 21 text

How? - SQLDelight (+ optional plugin) - SQLBrite - [Extra] Autovalue/Retrolambda/Kotlin for reduced amount of code

Slide 22

Slide 22 text

How? - Define your model

Slide 23

Slide 23 text

How? - Define your model - Create your .sq file

Slide 24

Slide 24 text

How? - Define your model - Create your .sq file - Create the queries

Slide 25

Slide 25 text

How? - Define your model - Create your .sq file - Create the queries - Implement your generated Model + Factory + Mapper (Cursor -> Article) + Marshall (Article -> CV) + Adapter (for custom types) + Implementation class (for JOINs)

Slide 26

Slide 26 text

How? - Profit

Slide 27

Slide 27 text

What?

Slide 28

Slide 28 text

What? - Pure SQL -> JavaModel

Slide 29

Slide 29 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?"

Slide 30

Slide 30 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?" - Everything in one place (src/main/sqldelight/your/package/model.sq)

Slide 31

Slide 31 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?" - Everything in one place (src/main/sqldelight/your/package/model.sq) - Anyone can write the SQL model (Backend devs, for example)

Slide 32

Slide 32 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?" - Everything in one place (src/main/sqldelight/your/package/model.sq) - Anyone can write the SQL model (Backend devs, for example) - Low model pollution

Slide 33

Slide 33 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?" - Everything in one place (src/main/sqldelight/your/package/model.sq) - Anyone can write the SQL model (Backend devs, for example) - Low model pollution - Implement interface + declare Factory & Mapper (the rest can be getters!).

Slide 34

Slide 34 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?" - Everything in one place (src/main/sqldelight/your/package/model.sq) - Anyone can write the SQL model (Backend devs, for example) - Low model pollution - Implement interface + declare Factory & Mapper (the rest can be getters!). - Easy conversion cursor -> model and viceversa

Slide 35

Slide 35 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?" - Everything in one place (src/main/sqldelight/your/package/model.sq) - Anyone can write the SQL model (Backend devs, for example) - Low model pollution - Implement interface + declare Factory & Mapper (the rest can be getters!). - Easy conversion cursor -> model and viceversa - Easy storage of custom classes (via adapter)

Slide 36

Slide 36 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?" - Everything in one place (src/main/sqldelight/your/package/model.sq) - Anyone can write the SQL model (Backend devs, for example) - Low model pollution - Implement interface + declare Factory & Mapper (the rest can be getters!). - Easy conversion cursor -> model and viceversa - Easy storage of custom classes (via adapter) // Hello old friend Calendar ;)

Slide 37

Slide 37 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?" - Everything in one place (src/main/sqldelight/your/package/model.sq) - Anyone can write the SQL model (Backend devs, for example) - Low model pollution - Implement interface + declare Factory & Mapper (the rest can be getters!). - Easy conversion cursor -> model and viceversa - Easy storage of custom classes (via adapter) // Hello old friend Calendar ;) - Listen for changes in tables in the Rx way.

Slide 38

Slide 38 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?" - Everything in one place (src/main/sqldelight/your/package/model.sq) - Anyone can write the SQL model (Backend devs, for example) - Low model pollution - Implement interface + declare Factory & Mapper (the rest can be getters!). - Easy conversion cursor -> model and viceversa - Easy storage of custom classes (via adapter) // Hello old friend Calendar ;) - Listen for changes in tables in the Rx way. - Relatively* easy Join projections

Slide 39

Slide 39 text

What? - Pure SQL -> JavaModel - Focus. Avoid "SELECT * FROM " + MY_TABLE + " WHERE " + MyTable.COLUMN + " = ?" - Everything in one place (src/main/sqldelight/your/package/model.sq) - Anyone can write the SQL model (Backend devs, for example) - Low model pollution - Implement interface + declare Factory & Mapper (the rest can be getters!). - Easy conversion cursor -> model and viceversa - Easy storage of custom classes (via adapter) // Hello old friend Calendar ;) - Listen for changes in tables in the Rx way. - Relatively* easy Join projections *Really easy if using AutoValue

Slide 40

Slide 40 text

Links http://blog.alexsimo.com/delightful-persistence-android/ https://github.com/square/sqldelight https://github.com/google/auto/tree/master/value

Slide 41

Slide 41 text

The end Q&A

Slide 42

Slide 42 text

The end Thank you!