Just like any other ORM solution available, Storm has the capability of generating SQL queries for you; also managing table relations.
Annotations are used by Storm to read the Meta data associated. There are only four major annotations that you need to be aware of:
@Database
This annotation provides the details of the database tables and the corresponding SQLite version. This annotation would also take care of creating and upgrading your database. You may also wish to do some custom SQL queries on create or application database upgrade and Storm very well provides options to do so.
Sample Usage:
@Database (name = "TEST_DB1", tables = { TestEntity.class, TestChild.class}, handler = TestDBHandler.class, version = 1 ) public class TestDatabase { }
Defining annotation properties:
Version:
The version of the SQLIte database, this version is passed directly to SQLIteOpenerHelper and hence manages the lifecycle and udpates. In case of upgrades to your application, please change the database version for the annotation to take advantage of Auto Updates.
So initially the version would default to one, when you need to upgrade your database, such as adding new tables or adding a column to the table, increment or change the version number.
Tables:
The Java classes corresponding to the ORM mapped entities in the SQLite database. Only constraint is each class in tables should have a @Table annotation.
Name:
The actual name for the database, this would be passed on to SQLiteOpenerHelper and further to file system.
Handler:
Need custom code to execute once tables have been created or updated? You may use this to implement an instance of DatabaseUpdatesHandler class. This would provide you onCreate and onUpgrade methods which can be overridden and would be called by Storm engine.
Handler would be invoked once the tables have been created or upgraded, unless you have very specific use case(such as add data to new added column) this can be ignored. The handler is to provide more control and still a layer of API.
Using Databases:
Once the annotation is ready, you may wish to use it by creating an instance of Storm service. Storm provided both Sync and Async ways of interacting with the Database (Using callbacks)
You may choose to extend or use directly instances of either BaseService or AsyncSupportService.
StormService service = new BaseService(getContext(),TestDatabase.class); List<Entity> savedEntities = service.findAll(TestEntity.class);
Above would fetch all rows from the table.
You may also choose to use Async service with below code
StormService service = new AsyncSupportService (getContext(),TestDatabase.class); service.findAll(TestEntity.class, new StormCallBack<TestEntity>() { @Override public void onResults(List<TestEntity> results) { //Do something with results, this would be callback on UI Thread } });
0 Comments