de.theappguys.winzigsql
Class Cruddable

java.lang.Object
  extended by de.theappguys.winzigsql.Cruddable
All Implemented Interfaces:
Serializable

public abstract class Cruddable
extends Object
implements Serializable

Base class for simple domain objects that allow CRUD operations via a Content Provider. Cruddable provides a number of non-static inner classes that allow persisting a number of different basic types. Contains a default

equals
and
hashCode
implementation that will work correctly if you only add members that are derived of
CrudValue
.

 Note that classes deriving from this class are mutable. This is done on
 purpose for efficiency reasons (we run on Android, after all). THIS MAKES IT
 DANGEROUS TO USE THIS CLASS AS KEYS IN MAPS AND VALUES IN SETS. If you do so,
 make sure you understand the implications of using mutable classes in these
 instances. If you do not know what I am talking about: Just dont't use
 subclasses of this class in sets or as keys for maps.

 To create you own Cruddable instances, do the following:

 

 public class MyCruddable extends Cruddable {

   public final CrudLong   columnname        = new CrudLong("columnname");
   public final CrudString anothercolumnname = new CrudString("anothercolumnname");

   public Chart() {
       //pass the number of members here for great justice
       super("mytablename", 2);
   }
 }
 
There are CrudValue classes for all primitive types in a nullable and non-nullable version.

See Also:
Serialized Form

Nested Class Summary
 class Cruddable.CrudBlob
           
 class Cruddable.CrudBoolean
           
 class Cruddable.CrudDate
           
 class Cruddable.CrudDouble
           
 class Cruddable.CrudEnum<T extends Enum<T>>
           
 class Cruddable.CrudFloat
           
 class Cruddable.CrudInteger
           
 class Cruddable.CrudLong
           
 class Cruddable.CrudNotNullableObjectValue<T>
           
 class Cruddable.CrudNotNullableValue<T>
           
 class Cruddable.CrudNullableBlob
           
 class Cruddable.CrudNullableBoolean
           
 class Cruddable.CrudNullableDate
           
 class Cruddable.CrudNullableDouble
           
 class Cruddable.CrudNullableEnum<T extends Enum<T>>
           
 class Cruddable.CrudNullableFloat
           
 class Cruddable.CrudNullableInteger
           
 class Cruddable.CrudNullableLong
           
 class Cruddable.CrudNullableShort
           
 class Cruddable.CrudNullableString
           
 class Cruddable.CrudNullableValue<T>
           
 class Cruddable.CrudShort
           
 class Cruddable.CrudString
           
 class Cruddable.CrudValue<T>
           
 
Field Summary
 Cruddable.CrudNullableLong _id
          every crud type must have at least an "_id" field
static String ID
          name for the primary id column of all Android sqlite tables
protected  ArrayList<Cruddable.CrudValue<?>> values
          Internal list where all crud values register themselves upon creation.
 
Constructor Summary
Cruddable(String tableName, String authority, int valueCount)
           
 
Method Summary
static String[] combineProjections(String[]... projections)
          Combines the given arrays into one.
 Long create(android.content.ContentResolver resolver)
          Creates a new db entry with the current values, sets the id to the id of the new entry.
 Long create(android.database.sqlite.SQLiteDatabase db)
          Creates a new db entry with the current values, sets the id to the id of the new entry.
 void createOrUpdate(android.content.ContentResolver resolver)
          Depending on whether the id of this Cruddable is set, either creates or updates a row through the given content resolver.
 void createOrUpdate(android.database.sqlite.SQLiteDatabase db)
          Depending on whether the id of this Cruddable is set, either creates or updates a row.
 void delete(android.content.ContentResolver resolver)
          Deletes the row corresponding to the current id.
 void delete(android.database.sqlite.SQLiteDatabase db)
          Deletes the row corresponding to the current id.
 boolean equals(Object other)
          Compares this cruddable by the values it contains
 void fromCursor(android.database.Cursor cursor)
          Sets this instances' values with the values found in the cursor at the cursor's current position
static void fromCursor(android.database.Cursor cursor, Cruddable... cruddables)
          Reads the given cruddables from the given cursor in the order in which they are listed as arguments.
 void fromCursor(int offset, android.database.Cursor cursor)
          Sets this instances' values with the values found in the cursor at the cursor's current position.
static void fromCursor(int offset, android.database.Cursor cursor, Cruddable... cruddables)
           
 String getAuthority()
           
 android.net.Uri getBaseUri()
           
 String[] getProjection()
           
 String[] getProjection(String tablePrefix)
           
 int getProjectionLength()
           
 String getTableName()
           
 int hashCode()
          Creates a hash code based on the current values.
 void query(android.content.ContentResolver resolver, long _id)
          simple query by id, sets the values of this instance to the values of the row with the given id.
 void query(android.database.sqlite.SQLiteDatabase db, long _id)
          simple query by id, sets the values of this instance to the values of the row with the given id.
 android.content.ContentValues toContentValues()
           
 String toString()
          Returns a debug String representation of the current values
 void update(android.content.ContentResolver resolver)
          Updates the table row with the same id as this cruddable with the current values.
 void update(android.database.sqlite.SQLiteDatabase db)
          Updates the table row with the same id as this cruddable with the current values.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ID

public static final String ID
name for the primary id column of all Android sqlite tables

See Also:
Constant Field Values

values

protected final ArrayList<Cruddable.CrudValue<?>> values
Internal list where all crud values register themselves upon creation.


_id

public final Cruddable.CrudNullableLong _id
every crud type must have at least an "_id" field

Constructor Detail

Cruddable

public Cruddable(String tableName,
                 String authority,
                 int valueCount)
Parameters:
valueCount - the number of values expected for this crud instance (excluding the _id field) set this to the correct value initially to avoid resizing of the internal list holding the members
Method Detail

getAuthority

public String getAuthority()
Returns:
the authority for the content provider url for this cruddable

getTableName

public String getTableName()
Returns:
name of the table for this Cruddable

getBaseUri

public android.net.Uri getBaseUri()
Returns:
the url to the table for this cruddable

query

public void query(android.content.ContentResolver resolver,
                  long _id)
simple query by id, sets the values of this instance to the values of the row with the given id.

Parameters:
resolver - not null
_id - the id of the row to use
Throws:
IllegalArgumentException - if the id was not found in the database

query

public void query(android.database.sqlite.SQLiteDatabase db,
                  long _id)
simple query by id, sets the values of this instance to the values of the row with the given id.

Parameters:
db - database to use, not null
_id - the id of the row to use

fromCursor

public void fromCursor(android.database.Cursor cursor)
Sets this instances' values with the values found in the cursor at the cursor's current position

Parameters:
cursor - not null, must be positioned at a valid row

fromCursor

public void fromCursor(int offset,
                       android.database.Cursor cursor)
Sets this instances' values with the values found in the cursor at the cursor's current position. The fields are queried from the given cursor with the given offset. Use this for queries spanning multiple tables.

Parameters:
offset - the offset into the field indices, must be >= 0
cursor - not null, must be positioned at a valid row

fromCursor

public static void fromCursor(android.database.Cursor cursor,
                              Cruddable... cruddables)
Reads the given cruddables from the given cursor in the order in which they are listed as arguments. Takes care of the correct offsets for each cruddable. Use this for queries spanning multiple tables.

Parameters:
cursor - not null, must be positioned at a valid row
cruddables - the cruddables to fill from the current row

fromCursor

public static void fromCursor(int offset,
                              android.database.Cursor cursor,
                              Cruddable... cruddables)

create

public Long create(android.content.ContentResolver resolver)
Creates a new db entry with the current values, sets the id to the id of the new entry.

Parameters:
resolver - resolver to use for accessing the db, not null
Returns:
the id of the newly created table record
Throws:
IllegalArgumentException - if creation failed

create

public Long create(android.database.sqlite.SQLiteDatabase db)
Creates a new db entry with the current values, sets the id to the id of the new entry.

Parameters:
db - the db to insert the row in, not null

update

public void update(android.content.ContentResolver resolver)
Updates the table row with the same id as this cruddable with the current values.

Parameters:
resolver - resolver to use for accessing the db, not null
Throws:
IllegalArgumentException - if the current id is null or if more than one row was updated

update

public void update(android.database.sqlite.SQLiteDatabase db)
Updates the table row with the same id as this cruddable with the current values.

Parameters:
db - the database to update in, not null
Throws:
IllegalArgumentException - if the current id is null or if more than one row was updated

createOrUpdate

public void createOrUpdate(android.content.ContentResolver resolver)
Depending on whether the id of this Cruddable is set, either creates or updates a row through the given content resolver.

Parameters:
resolver - resolver to use for accessing the db, not null

createOrUpdate

public void createOrUpdate(android.database.sqlite.SQLiteDatabase db)
Depending on whether the id of this Cruddable is set, either creates or updates a row.

Parameters:
db - the database to update in, not null

delete

public void delete(android.content.ContentResolver resolver)
Deletes the row corresponding to the current id.

Parameters:
resolver - resolver to use for accessing the db, not null
Throws:
IllegalStateException - if the current id is null

delete

public void delete(android.database.sqlite.SQLiteDatabase db)
Deletes the row corresponding to the current id.

Parameters:
db - the database to delete the entry in, not null
Throws:
IllegalStateException - if the current id is null

getProjection

public String[] getProjection()
Returns:
the projection (array of column names) for this Cruddable, has at least length 1

getProjection

public String[] getProjection(String tablePrefix)
Parameters:
tablePrefix - identifier to prefix to the column names. Without the ".". So if you want the field "_id" to be returned as "foo._id", pass in "foo"
Returns:
a projection with all fields, all prepended with a table prefix

getProjectionLength

public int getProjectionLength()
Returns:
number of values of this Cruddable (== size of a full projection)

combineProjections

public static String[] combineProjections(String[]... projections)
Combines the given arrays into one. Use this to create a projection for a query spanning multiple tables.

Parameters:
projections - a number of projections
Returns:
an array with the contents of the given projections joined

toContentValues

public android.content.ContentValues toContentValues()
Returns:
the state of this cruddable as ContentValues

hashCode

public int hashCode()
Creates a hash code based on the current values.

Overrides:
hashCode in class Object

equals

public boolean equals(Object other)
Compares this cruddable by the values it contains

Overrides:
equals in class Object

toString

public String toString()
Returns a debug String representation of the current values

Overrides:
toString in class Object


Copyright © 2013. All Rights Reserved.