SimpleSQLProvider

Additional

Language
Java
Version
N/A
Created
Sep 4, 2015
Updated
Oct 1, 2015 (Retired)
Owner
Kurt Mbanje (ckurtm)
Contributor
Kurt Mbanje (ckurtm)
1
Activity
Badge
Generate
Download
Source code

Advertisement

SimpleSQLProvider

The Fastest Way to create a sql based ContentProvider in Android using annotations (No reflection)

HOW TO ADD TO YOUR PROJECT

Gradle:

add the apt plugin as a dependency to your root build gradle as below:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6'
    }
}

apply the apt plugin to your main project's build.gradle

apply plugin: 'com.neenbedankt.android-apt'

add the required dependencies as below

dependencies {
        compile 'com.squareup:javapoet:1.2.0'
        compile 'ckm.simple:simple_sql_provider_annotation:1.0.6'
        compile 'ckm.simple:simple_sql_provider_processor:1.0.6'
}

QUICK START

  1. Create ProviderConfig class that defines your content providers details e.g.
 @SimpleSQLConfig(
         name = "TestProvider",
         authority = "just.some.test_provider.authority",
         database = "test.db",
         version = 1)
 public class TestProviderConfig implements ProviderConfig {
     @Override
     public UpgradeScript[] getUpdateScripts() {
         return new UpgradeScript[0];
     }
 }

This class file says - Create a ContentProvider called TestProvider - The authority for this Provider is "just.some.test_provider.authority" - The Provider uses a database file named "test.db" - The Current database version is 1 - provider UpdateScripts as a defined by the UpdateScripts class

  1. Annotate your Pojo file that defines a table in the Database as below.
  @SimpleSQLTable(table = "test", provider = "TestProvider")
  public class Test {

  @SimpleSQLColumn("col_str")
  public String myString;

  @SimpleSQLColumn(value = "col_int", primary = true)
  public int anInt;

  @SimpleSQLColumn("col_integer")
  public int myinteger;

  @SimpleSQLColumn("col_short")
  public int myshort;

  @SimpleSQLColumn("col_short2")
  public int myShort;

  @SimpleSQLColumn("col_long")
  public long mylong;

  @SimpleSQLColumn("col_long2")
  public int myLong;

  @SimpleSQLColumn("col_double")
  public long mydouble;

  @SimpleSQLColumn("col_double2")
  public int myDouble;

  @SimpleSQLColumn("col_float")
  public long myfloat;

  @SimpleSQLColumn("col_float2")
  public int myFloat;

  @SimpleSQLColumn("col_bigdecimal")
  public BigDecimal bigD;

  @SimpleSQLColumn("col_bool")
  public boolean mybool;

  @SimpleSQLColumn("col_bool2")
  public boolean myBool;

  @SimpleSQLColumn("col_date")
  public Date mydateCol;
  }
   ```

3. The rebuild your project

4. You will now have access to files generated for you to access the Table prefixed with "Table" using the usual Android ContentProvider methods, e.g. TestTable for the above class.
   The generated files have convinience functions for you to add values to the table, e.g. getContentValues() with an instance of the Test class to insert into db e.g.
   
   ```java
   Test testInstance = new Test(...);
   getContentResolver().insert(TestTable.CONTENT_URI,TestTable.getContentValues(testInstance,false));   
   ```
   
   To get data from the database use:
   
   ```java
      Cursor cursor = getContentResolver().query(TestTable.CONTENT_URI,null,null,null,null);
      //one row
      Test testRow = TestTable.getRow(cursor,true);
      //multiple rows
      List<Test> testRows = TestTable.getRows(cursor,false);   
   ```
   
   
  5. add your provider as usual to your AndroidManifest.xml file with a matching authority as defined in ProviderConfig class
   
   ```xml
       <provider
          android:authorities="just.some.test_provider.authority"
          android:name="example.kurt.test.TestProvider"/>   
   ```



##License

Apache License (Version 2.0)

You may not use this file except in compliance with the License.
You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.