Kripton and SQLite: generate an SQLite based content provider

Abubusoft Blog

Kripton and SQLite: generate an SQLite based content provider

One of the key features of Kripton ORM is the capability to generate an SQLite content provider starting from a data source definition.

More information about Content Provider can found here. Suppose we have in our application a music data source so defined:

In this data source, there are Artist and Album entities. Every entity got its DAO interface.
The Music data source is so defined:

@BindContentProvider(authority = "com.abubusoft.kripton.example")
@BindDataSource(daoSet = { ArtistDao.class , AlbumDao.class}, fileName = "artists.db")
public interface ArtistsDataSource {
}

The Music data source is so defined:

The data source interface is decorated with @BindDataSource annotation (necessary to define data source itself) and @BindContentProvider that enable content provider generation starting from the data source. The only attribute of @BindContentProvider is the authority associated with the content provider. For this data, source will be generated as a content provider named BindArtistsContentProvider.

The Artist DAO interface is:

@BindContentProviderPath(path = "artists")
@BindDao(Artist.class)
public interface ArtistDao {
  @BindContentProviderEntry(path = "${id}") 
  @BindSqlSelect(where = "id=${id}")
  Artist selectById(long id);
 
  @BindContentProviderEntry
  @BindSqlSelect
  List<Artist> selectAll();
 
  @BindContentProviderEntry
  @BindSqlInsert
  long insert(Artist bean);
 
  @BindContentProviderEntry(path = "${bean.id}")
  @BindSqlUpdate(where = "id=${bean.id}") 
  long update(Artist bean);
 
  @BindContentProviderEntry(path = "${bean.id}")
  @BindSqlDelete(where = "id=${bean.id}")
  long delete(Artist bean);
}

The Album DAO interface:

@BindContentProviderPath(path = "albums")
@BindDao(Album.class)
public interface ArtistDao {
  @BindContentProviderEntry(path = "${id}") 
  @BindSqlSelect(where = "id=${id}")
  AlbumselectById(long id);
 
  @BindContentProviderEntry
  @BindSqlSelect
  List<Album> selectAll();
 
  @BindContentProviderEntry
  @BindSqlInsert
  long insert(Album bean);
 
  @BindContentProviderEntry(path = "${bean.id}")
  @BindSqlUpdate(where = "id=${bean.id}") 
  long update(Album bean);
 
  @BindContentProviderEntry(path = "${bean.id}")
  @BindSqlDelete(where = "id=${bean.id}")
  long delete(Album bean);
}

Every DAO interface is decorated with @BindContentProviderPath, that enable DAO to be used in content provider generation. The attribute path, allow specifying the path that DAO methods cover. Moreover, every DAO method that you need to be exposed within the content provider is decorated with @BindContentProviderEntry. This annotation allows defining last part of the URL covered by DAO method.

The content provider generated will be named BindArtistsContentProvider and it contains Javadoc too to summarize content provider functionality.

As you can see, JavaDoc contains for each type of operation (query, update, insert and delete) all supported URL and DAO’s method that implements it. Quite cool.. isn’t it?

At last, just remember that to expose generated content provider you also need to declare it in manifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abubusoft.kripton.example.MyApplication">
 
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
         <activity android:name=".MainActivity">
            <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
         </activity>
        
      <provider android:name="ArtistsProvider"
         android:authorities="com.abubusoft.kripton.example.BindArtistsContentProvider"/>
   </application>
</manifest>

Kripton in compile-time will test content provider functionality and will report each error that can avoid content provider generation.

As usual, you can found more information about Kripton on:

 

Leave a Reply

Your email address will not be published. Required fields are marked *