StORM: a lightweight ORM for Android SQLite
-
Upload
david-chandler -
Category
Software
-
view
85 -
download
0
Transcript of StORM: a lightweight ORM for Android SQLite
![Page 1: StORM: a lightweight ORM for Android SQLite](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/1.jpg)
Project stORM: an ORM for SQLite+David Chandler(ex) Android Developer Advocatehttp://turbomanage.com
![Page 2: StORM: a lightweight ORM for Android SQLite](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/2.jpg)
simple template-based Object Relational Mapping
David Chandlerturbomanage.wordpress.com
1.0
![Page 3: StORM: a lightweight ORM for Android SQLite](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/3.jpg)
whyhand-coded SQL, ugh
![Page 4: StORM: a lightweight ORM for Android SQLite](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/4.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/5.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/6.jpg)
non-goalskitchen sinkmodel all relationsabsolute max performance
![Page 7: StORM: a lightweight ORM for Android SQLite](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/7.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/8.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/9.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/10.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/12.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/13.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/14.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/15.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/16.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/17.jpg)
sqlite3adb shellcd /data/data/your_app/databasessqlite3 name_of_db.schemaSELECT * FROM ...
![Page 18: StORM: a lightweight ORM for Android SQLite](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/18.jpg)
dbFactorystatic db name, versionsingleton instance of DatabaseHelpergetTableHelpers()
![Page 19: StORM: a lightweight ORM for Android SQLite](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/19.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/20.jpg)
tableall SQLall getX() / bindX()Cursor --> obj --> ContentValuesobj --> String[] (for csv)
![Page 21: StORM: a lightweight ORM for Android SQLite](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/21.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/22.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/23.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/24.jpg)
Freebie: adb back upadb backup -apk -all -nosystem -f ~/mybackupfile.ab
adb restore ~/mybackupfile.ab
![Page 25: StORM: a lightweight ORM for Android SQLite](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/25.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/26.jpg)
src
github.com/turbomanage/storm-gen
![Page 27: StORM: a lightweight ORM for Android SQLite](https://reader030.fdocuments.us/reader030/viewer/2022032619/55c055adbb61eb26558b4579/html5/thumbnails/27.jpg)
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.)