org.hermit.android.provider
Class TableProvider

java.lang.Object
  extended by ContentProvider
      extended by org.hermit.android.provider.TableProvider

public class TableProvider
extends ContentProvider

This class is a base for content providers which provide access to table-organized data in an SQL database.

Typically, this is used by creating a subclass which is empty other than providing an appropriate schema to this class's constructor. The bulk of the work in creating a content provider is in creating the schema, a subclass of DbSchema.


Constructor Summary
TableProvider(DbSchema schema)
          Create an instance of this content provider.
 
Method Summary
 int delete(Uri uri, java.lang.String where, java.lang.String[] whereArgs)
          A request to delete one or more rows.
protected  DatabaseHelper getHelper()
          Get the database helper which this content provider will use.
protected  DbSchema getSchema()
          Get the database schema.
protected  TableSchema getTableSchema(java.lang.String name)
          Get the schema for a specified table.
 java.lang.String getType(Uri uri)
          Return the MIME type of the data at the given URI.
 Uri insert(Uri uri, ContentValues initValues)
          Implement this to insert a new row.
 boolean onCreate()
          Called when the provider is being started.
protected  void onInsert(Uri uri, TableSchema table, ContentValues initValues)
          This method is called prior to processing an insert; it is called after TableSchema.onInsert(ContentValues).
 Cursor query(Uri uri, java.lang.String[] projection, java.lang.String where, java.lang.String[] whereArgs, java.lang.String sortOrder)
          Receives a query request from a client in a local process, and returns a Cursor.
protected  Cursor queryItem(TableSchema t, java.lang.String[] projection, long id)
          Query for a specified item within a table.
protected  Cursor queryItem(TableSchema t, java.lang.String[] projection, java.lang.String id)
          Query for a specified item within a table.
protected  Cursor queryItems(TableSchema t, java.lang.String[] projection, java.lang.String where, java.lang.String[] whereArgs, java.lang.String sortOrder)
          Query for items within a table.
 int update(Uri uri, ContentValues values, java.lang.String where, java.lang.String[] whereArgs)
          Update a content URI.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TableProvider

public TableProvider(DbSchema schema)
Create an instance of this content provider.

Parameters:
schema - Structure defining the database schema.
Method Detail

onCreate

public boolean onCreate()
Called when the provider is being started.

Returns:
true if the provider was successfully loaded, false otherwise.

getSchema

protected DbSchema getSchema()
Get the database schema.

Returns:
The schema for this database.

getTableSchema

protected TableSchema getTableSchema(java.lang.String name)
                              throws java.lang.IllegalArgumentException
Get the schema for a specified table.

Parameters:
name - The name of the table we want.
Returns:
The schema for the given table.
Throws:
java.lang.IllegalArgumentException - No such table.

getHelper

protected DatabaseHelper getHelper()
Get the database helper which this content provider will use.

Subclasses may override this to provide a smarter database helper; for example, to implement a smarter database upgrade process. See DatabaseHelper.

Returns:
A database helper for this content provider.

getType

public java.lang.String getType(Uri uri)
Return the MIME type of the data at the given URI. This should start with vnd.android.cursor.item/ for a single record, or vnd.android.cursor.dir/ for multiple items.

Parameters:
uri - The URI to query.
Returns:
MIME type string for the given URI, or null if there is no type.

query

public Cursor query(Uri uri,
                    java.lang.String[] projection,
                    java.lang.String where,
                    java.lang.String[] whereArgs,
                    java.lang.String sortOrder)
Receives a query request from a client in a local process, and returns a Cursor. This is called internally by the ContentResolver.

Parameters:
uri - The URI to query. This will be the full URI sent by the client; if the client is requesting a specific record, the URI will end in a record number that the implementation should parse and add to a WHERE or HAVING clause, specifying that _id value.
projection - The list of columns to put into the cursor. If null all columns are included.
where - A selection criteria to apply when filtering rows. If null then all rows are included.
whereArgs - You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
sortOrder - How the rows in the cursor should be sorted. If null then the provider is free to define the sort order.
Returns:
A Cursor or null.

queryItem

protected Cursor queryItem(TableSchema t,
                           java.lang.String[] projection,
                           long id)
Query for a specified item within a table.

Parameters:
t - The schema for the table to query.
projection - The list of columns to put into the cursor. If null all columns are included.
id - The ID of the item we want.
Returns:
A Cursor or null.

queryItem

protected Cursor queryItem(TableSchema t,
                           java.lang.String[] projection,
                           java.lang.String id)
Query for a specified item within a table.

Parameters:
t - The schema for the table to query.
projection - The list of columns to put into the cursor. If null all columns are included.
id - The ID of the item we want, as a String.
Returns:
A Cursor or null.

queryItems

protected Cursor queryItems(TableSchema t,
                            java.lang.String[] projection,
                            java.lang.String where,
                            java.lang.String[] whereArgs,
                            java.lang.String sortOrder)
Query for items within a table.

Parameters:
t - The schema for the table to query.
projection - The list of columns to put into the cursor. If null all columns are included.
where - A selection criteria to apply when filtering rows. If null then all rows are included.
whereArgs - You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
sortOrder - How the rows in the cursor should be sorted. If null then the provider is free to define the sort order.
Returns:
A Cursor or null.

onInsert

protected void onInsert(Uri uri,
                        TableSchema table,
                        ContentValues initValues)
This method is called prior to processing an insert; it is called after TableSchema.onInsert(ContentValues). Subclasses can use this to carry out additional processing.

Parameters:
uri - The content:// URI of the insertion request.
table - The schema of the table we're inserting into.
initValues - A set of column_name/value pairs to add to the database.

insert

public Uri insert(Uri uri,
                  ContentValues initValues)
Implement this to insert a new row. As a courtesy, call notifyChange() after inserting.

Parameters:
uri - The content:// URI of the insertion request.
initValues - A set of column_name/value pairs to add to the database.
Returns:
The URI for the newly inserted item.

delete

public int delete(Uri uri,
                  java.lang.String where,
                  java.lang.String[] whereArgs)
A request to delete one or more rows. The selection clause is applied when performing the deletion, allowing the operation to affect multiple rows in a directory. As a courtesy, call notifyDelete() after deleting. The implementation is responsible for parsing out a row ID at the end of the URI, if a specific row is being deleted. That is, the client would pass in content://contacts/people/22 and the implementation is responsible for parsing the record number (22) when creating an SQL statement.

Parameters:
uri - The full URI to delete, including a row ID (if a specific record is to be deleted).
where - An optional restriction to apply to rows when deleting.
whereArgs - You may include ?s in where, which will be replaced by the values from whereArgs.
Returns:
The number of rows affected.
Throws:
SQLException - Database error.

update

public int update(Uri uri,
                  ContentValues values,
                  java.lang.String where,
                  java.lang.String[] whereArgs)
Update a content URI. All rows matching the optionally provided selection will have their columns listed as the keys in the values map with the values of those keys. As a courtesy, call notifyChange() after updating.

Parameters:
uri - The URI to update. This can potentially have a record ID if this is an update request for a specific record.
values - A Bundle mapping from column names to new column values (NULL is a valid value).
where - An optional restriction to apply to rows when updating.
whereArgs - You may include ?s in where, which will be replaced by the values from whereArgs.
Returns:
The number of rows affected.