Top Banner
Project stORM: an ORM for SQLite +David Chandler (ex) Android Developer Advocate http://turbomanage.com
27

StORM: a lightweight ORM for Android SQLite

Aug 04, 2015

Download

Software

David Chandler
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: StORM: a lightweight ORM for Android SQLite

Project stORM: an ORM for SQLite+David Chandler(ex) Android Developer Advocatehttp://turbomanage.com

Page 2: StORM: a lightweight ORM for Android SQLite

simple template-based Object Relational Mapping

David Chandlerturbomanage.wordpress.com

1.0

Page 3: StORM: a lightweight ORM for Android SQLite

whyhand-coded SQL, ugh

Page 4: StORM: a lightweight ORM for Android SQLite

whySQLite already has obj-like wrappers- ContentValues (insert, update)

- Cursor (query)

many apps just require a place to stuff some objectsexisting ORMs use reflection or require up-front modeling

Page 5: StORM: a lightweight ORM for Android SQLite

goalseasyconvention over configurationannotation-driven- @Database, @Entity

- seamless code gen with JDT

easy to debugminimal performance overhead

Page 6: StORM: a lightweight ORM for Android SQLite

non-goalskitchen sinkmodel all relationsabsolute max performance

Page 7: StORM: a lightweight ORM for Android SQLite

setup (Eclipse / ADT)add storm-api.jar to libs/add storm-impl.jar to annotation factory classpath

Page 8: StORM: a lightweight ORM for Android SQLite

setup (Android Studio)github.com/turbomanage/storm-gen

buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.13.+' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' } }

apply plugin: 'android-apt'

dependencies { apt ‘com.turbomanage.storm:storm-impl:1.0' compile 'com.android.support:appcompat-v7:19.1.0' compile 'log4j:log4j:1.2.17' compile 'javax.persistence:persistence-api:1.0' compile ‘com.turbomanage.storm:storm-api:1.0' }

Page 9: StORM: a lightweight ORM for Android SQLite

useExtend DatabaseHelper, annotate with @DatabaseCreate POJOs, annotate with @EntityGenerates- DbFactory

- EntityTable

- EntityDao

new EntityDao(ctx).insert/get/query...

Page 10: StORM: a lightweight ORM for Android SQLite

use (like Objectify)dao.insert(T obj)dao.insertMany(Iterable<T> objs)T dao.get(long id)dao.update(T obj)dao.delete(long id) / dao.deleteAll()List<T> dao.listAll() or load().list()dao.listAllByExample(T exampleObj)dao.load().eq(COL, val).eq(...).exec()

Page 11: StORM: a lightweight ORM for Android SQLite

conventionsgenerated package names- +.db, +.dao

COLNAME == fieldName.toUpperCase()transient fields are not persistedid- long id (default)

- or annotate another field with @Id

- column name is _id (for CursorAdapter)

Page 12: StORM: a lightweight ORM for Android SQLite

supported typesall primitives & wrappers- boolean, byte, byte[], char, double, enum,

float, int, long, short, String

- byte[] have affinity BLOB

- boolean, byte, char have affinity INTEGER

roll-your-own- extend TypeConverter<J,S>

- annotate with @Converter

Page 13: StORM: a lightweight ORM for Android SQLite

niceList<T> dao.listByExample(T obj)T dao.getByExample(T obj)- throws TooManyResultsException

insertMany uses 1 transactioncolumn name enum in Table class- filter().eq(Columns.FIRSTNAME, “David”)...

red squigglies from APT

Page 14: StORM: a lightweight ORM for Android SQLite

csvdao.getDatabaseHelper(ctx)dbHelper.backupAllTablesToCsv()dbHelper.restore...FromCsv()exact type conversions- blobs are Base64 encoded

- doubles saved as raw hex values

file named dbName.vn.TableName+?

Page 15: StORM: a lightweight ORM for Android SQLite

UpgradeStrategyDROP_CREATEBACKUP_RESTORE (csv)- dropped cols disappear

- new cols get default values

- renaming not yet supported

UPGRADE- override DatabaseHelper.upgrade() and/or

- override TableHelper.onUpgrade(...) for each

Page 16: StORM: a lightweight ORM for Android SQLite

limitsno relations (yet)- want: order.items

- have: item.orderId

- could: orderDao.getItems()

- leaning toward Ref<Order>.get()

can’t compare doubles or blobs with FilterBuilder.eq()- planned: .eq(COL, val, delta)

- alt: dao.query(String where, String[] args)

Page 17: StORM: a lightweight ORM for Android SQLite

sqlite3adb shellcd /data/data/your_app/databasessqlite3 name_of_db.schemaSELECT * FROM ...

Page 18: StORM: a lightweight ORM for Android SQLite

dbFactorystatic db name, versionsingleton instance of DatabaseHelpergetTableHelpers()

Page 19: StORM: a lightweight ORM for Android SQLite

daoextends SQLiteDao<T>- most of the code lives in the base class

points to DatabaseFactorypoints to TableHelper

Page 20: StORM: a lightweight ORM for Android SQLite

tableall SQLall getX() / bindX()Cursor --> obj --> ContentValuesobj --> String[] (for csv)

Page 21: StORM: a lightweight ORM for Android SQLite

implhow to support incremental compilation?- anno processing happens in rounds

- full src not available in every round

- stormEnv file under .apt_generated (ADT)

- or build/generated/apt/debug (AS)

- if in doubt, del stormEnv & Project | Clean

Freemarker templates in impl/src/reswatch the Error Log view

Page 22: StORM: a lightweight ORM for Android SQLite

working with APTStart gradle daemonConnect a remote debugger to itInitiate your build (including APT processing)In IntelliJ, use Rebuild to force APT runhttp://turbomanage.wordpress.com/2014/06/09/debug-an-annotation-processor-with-intellij-and-gradle/

Page 23: StORM: a lightweight ORM for Android SQLite

Android Debug Bridgeadb logcatadb shelladb shell dumpsys meminfo <pkg>adb kill-server :-(other command lines- android (launches SDK manager)

- hierarchyviewer

- emulator @avd_name (see ~/.android/avd)

Page 24: StORM: a lightweight ORM for Android SQLite

Freebie: adb back upadb backup -apk -all -nosystem -f ~/mybackupfile.ab

adb restore ~/mybackupfile.ab

Page 25: StORM: a lightweight ORM for Android SQLite

futurelimited relations (@Key, Ref<T>)more filter methods gt(), lt(), etc.@Id for custom id col@Column(name=”custom”)

Page 26: StORM: a lightweight ORM for Android SQLite

src

github.com/turbomanage/storm-gen

Page 27: StORM: a lightweight ORM for Android SQLite

Randroidannotations.orgMark Murphy’s Busy Coder’s Guide to Android DevelopmentGoogle I/O sessions+Android Developersturbomanage.com (my blog)google.com/+DavidMichaelChandlersquare.github.com (event bus, DI, etc.)