SquiDB: a SQLite layer for Android - Jonathan Koren, Yahoo!
DroidConTLV
1,164 views
25 slides
Jun 18, 2015
Slide 1 of 25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
About This Presentation
As presented in DroidCon Tel Aviv 2015
http://il.droidcon.com
Size: 2.68 MB
Language: en
Added: Jun 18, 2015
Slides: 25 pages
Slide Content
SquiDB: Android SQLite Management
DroidCon Tel Aviv 2015
What is SquiDB?
SQLite with Android
string theory
SQLiteDatabase
public Cursor query(String table, String[] columns, !
String selection, String[] selectionArgs,!
String groupBy, String having, String orderBy,!
String limit)!
public Cursor rawQuery(String sql, String[] sqlArgs)!
!
public long insert(String table, String!
nullColumnHack, ContentValues values)!
public int update(String table, ContentValues values,!
String selection, String[] selectionArgs)!
public int delete(String table, String selection,!
String[] selectionArgs)!
!
public void execSQL(String sql, String[] sqlArgs)!
SQLiteOpenHelper
SQLiteDatabase
SQLite with SquiDB
bring back the fun
Models
! Models generated at compile time
› Define model using minimalist “spec” class
› <type>Property for each member field (column)
› Getters and setters for each property
! Internally represented by ContentValues
› Models are Parcelable!
! Multiple sets of values
› Tracks deltas; update only saves changed (written) values
› Data source ordering
DatabaseDao
! Controls access to database
› CRUD operations
› Transaction management
! fetch(), persist(), and delete() single records
! update() and deleteWhere() for multiple records
! count() for number of matching rows
! query() for cursors
DatabaseDao
DatabaseDao
Queries
! Object-oriented builder classes
! Supports almost the entire SELECT grammar
› Joins, nested queries, compound operators
› Functions and other complex expressions
! Automatic placeholders and argument binding
! Reusable
› Cached compilation
› Use atomics to change arguments between executions
Queries
! Select Fields, Properties, and Functions
› Query.select(Film.PROPERTIES); // all Film properties!
› Query.select(Film.ID, Film.TITLE); // just these ones!
› Query.select(Function.max(Film.RATING)); // function!
› Query.select() // select * -- until you selectMore()!
!
! Predicates formed using Criterions
› Film.IS_FAVORITE.isTrue() // from a Property!
› Film.YEAR.between(2010, 2015).and(Film.RATING.gte(4.0))!
› Criterion.all, Criterion.none // pre-packaged!
› Criterion.not(Film.TITLE.like("L%"))!
Queries
Queries
Queries
SquidCursor
! Parameterized CursorWrapper
! Read values using Property
› String title = cursor.get(Film.TITLE);!
! Populate models easily
› Film film = new Film(cursor); // must be at valid position!
› film.readPropertiesFromCursor (cursor);!
! Model reusability
› Film film = new Film();!
SquidCursor<Film> c = dao.query(Film.class, query);!
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {!
film.readPropertiesFromCursor (c);!
doSomethingWithFilm(film);!
}!
Other useful bits
! SquidCursorAdapter
› Base list adapter backed by a SquidCursor
! SquidCursorLoader
› Loader for querying DatabaseDao in the background
! UriNotifier
› Send URI notifications when write operations happen
Advanced features
! Other SQLite statements
› Inserts, updates, deletes, triggers
! Models based on SQLite Views
! Utilities for building ContentProviders
› Public contract vs. private schema
› Protect against SQL injection
! Code generator plugins
› Use other Java types in your model specs
Questions?
answers
?
Get started with SquiDB
Source:
github.com/yahoo/squidb