Creating StorIOSQLite interface

The final step before we can start using StorIO is to create a StorIOSQLite interface. It will be an object that will tie all our resolvers configuration and will be a central go-to interface for operations with the database.

However, before we can do that, there is one more class that's missing. It's the StorIODbHelper class, and it will extend SQLiteOpenHelperSQLiteOpenHelper is often used to handle the initialization of the database--creating tables, migrating old versions of tables, adding columns and so on. Basically, it ensures that the database is ready for work. We will keep this class very brief:

class StorIODbHelper extends SQLiteOpenHelper {

StorIODbHelper
(@NonNull Context context) {
super(context, "reactivestocks.db", null, 1);
}

@Override
public void onCreate(@NonNull SQLiteDatabase db) {
db.execSQL(StockUpdateTable.createTableQuery());
}

@Override
public void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion,
int newVersion) {
}
}

As we do not care (yet) about database upgrade because we always start with a clean state, we set the onUpgrade method to be empty. In our case, it's just the onCreate method that needs to be filled with the code:

db.execSQL(StockUpdateTable.createTableQuery());

This will create the table for StockUpdate tweet. Consider the following line:

super(context, "reactivestocks.db", null, 1);

It tells the helper that the filename for the database will be reactivestocks.db, and it's at version 1.

After the SQLiteOpenHelper class is created, we can proceed with the creation of the main interface. To keep the code well organized, we will put interface creation code in the StorIOFactory class in packt.reactivestocks.storio with the rest of the classes. The factory method itself will be as illustrated:

private static StorIOSQLite INSTANCE;

public synchronized static
StorIOSQLite get(Context context) {
if (INSTANCE != null) {
return INSTANCE;
}

INSTANCE
= DefaultStorIOSQLite.builder()
.sqliteOpenHelper(new StorIODbHelper(context))
.addTypeMapping(StockUpdate.class, SQLiteTypeMapping.
<StockUpdate>builder()
.putResolver(new StockUpdatePutResolver())
.getResolver(createGetResolver())
.deleteResolver(createDeleteResolver())
.build())
.build();

return
INSTANCE;
}

Let's dissect this piece by piece. Consider the given line:

.sqliteOpenHelper(new StorIODbHelper(context))

It gives StorIO access to the original SQLite database. Also, it will ensure that the tables are created before we start using the database. After that, a mapping configuration follows with this:

.addTypeMapping(StockUpdate.class, ...)

The purpose of this line is to specify the configuration that will handle the StockUpdate class. Take a look at the following lines which are a part of the configuration:

SQLiteTypeMapping.<StockUpdate>builder()
.putResolver(new StockUpdatePutResolver())
.getResolver(createGetResolver())
.deleteResolver(createDeleteResolver())
.build()

The following code specifies a class that will be used to handle write operations and we have just created this class a while ago:

 .putResolver(new StockUpdatePutResolver())

The following call sets the configuration that will specify how the database reads are handled:

.getResolver(createGetResolver())

However, since we will be leaving this for a later section, we will plug in a stub implementation here:

private static GetResolver<StockUpdate> createGetResolver() {
return new DefaultGetResolver<StockUpdate>() {
@NonNull
@Override
public StockUpdate mapFromCursor(@NonNull Cursor cursor) {
return null;
}
};
}

The final line will configure the delete action to correctly delete the record in the database when the entity is deleted in the code:

.deleteResolver(createDeleteResolver())

Again, we will use a stub implementation of the following:

private static DeleteResolver<StockUpdate> createDeleteResolver() {
return new DefaultDeleteResolver<StockUpdate>() {
@NonNull
@Override
protected DeleteQuery mapToDeleteQuery(@NonNull StockUpdate
object) {
return null;
}
};
}

Finally, the mapping configuration is created by calling .build(), and then the DefaultStorIOSQLite implementation is initialized with the same method (but on a different class).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.19.243