Skip to content

Latest commit

 

History

History
149 lines (104 loc) · 4.48 KB

README.md

File metadata and controls

149 lines (104 loc) · 4.48 KB

ProviGen Build Status Maven Central

Easily make a ContentProvider from a ContractClass.

Setup

public interface MyContract extends ProviGenBaseContract {

	@Column(Type.INTEGER)
	public static final String MY_INT_COLUMN = "int";

	@Column(Type.TEXT)
	public static final String MY_STRING_COLUMN = "string";

	@ContentUri
	public static final Uri CONTENT_URI = Uri.parse("content://com.myapp/table_name");
}
  • Extend the ProviGenProvider.
public class MyContentProvider extends ProviGenProvider {

    private static Class[] contracts = new Class[]{MyContract.class};

    @Override
    public SQLiteOpenHelper openHelper(Context context) {
        return new ProviGenOpenHelper(getContext(), "dbName", null, 1, contracts);
    }

    @Override
    public Class[] contractClasses() {
        return contracts;
    }
}
  • Add your provider in your manifest.
<provider
    android:name="com.myapp.MyContentProvider"
    android:authorities="com.myapp" >
</provider>
  • You're done.

Usage

You can make the usual insert, update, delete and query using a ContentResolver.
For example querying a single row boils down to:

getContentResolver().query(	
	Uri.withAppendedPath(MyContract.CONTENT_URI, myId),
	null, "", null, "");

or

getContentResolver().query(
	MyContract.CONTENT_URI, null, 
	MyContract._ID + " = ? ", new String[]{ myId }, "");

Features

Table creation and contract upgrades

ProviGen comes with an implementation of the SQLiteOpenHelper called ProviGenOpenHelper. This default implementation will

  • automatically create the needed tables on the first application launch
  • automatically add missing columns every time the database version increases

Notifications and observers

ProviGen fully supports the uri notification mechanism.
You can safely use it with CursorLoaders and ContentObservers.

Custom SQLiteOpenHelper

You can provide your own implementation of the SQLiteOpenHelper for initial population, complex contract upgrades or anything else database related you want to achieve.

public class MyContentProvider extends ProviGenProvider {

    @Override
    public Class[] contractClasses() {
        return new Class[]{MyContract.class};
    }

    @Override
    public SQLiteOpenHelper openHelper(Context context) {
    
        return new SQLiteOpenHelper(getContext(), "databaseName", null, 1) {
        
            @Override
            public void onCreate(SQLiteDatabase database) {
                // Automatically creates table and needed columns.
                new TableBuilder(MyContract.class).createTable(database);

                // Do initial population here.
            }

            @Override
            public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
                // Automatically adds new columns.
                TableUpdater.addMissingColumns(database, MyContract.class);

                // Anything else related to database upgrade should be done here.
            }
        };
    }
}

Data constraint

You can apply a UNIQUE or a NOT_NULL constraint to a column using the appropriate TableBuilder methods.

new TableBuilder(MyContract.class)
        .addConstraint(MyContract.MY_INT, Constraint.UNIQUE, OnConflict.ABORT)
        .addConstraint(MyContract.MY_STRING, Constraint.NOT_NULL, OnConflict.IGNORE)
        .createTable(database);

License

This content is released under the MIT License.