Cortex Android
 All Classes Namespaces Functions Variables
nl.sense_os.cortex.coaching.db.CoachingDB Class Reference
Inheritance diagram for nl.sense_os.cortex.coaching.db.CoachingDB:
nl.sense_os.cortex.coaching.db.AdviceMessageDB nl.sense_os.cortex.coaching.db.MessageDB nl.sense_os.cortex.coaching.db.StyleMessageDB nl.sense_os.cortex.coaching.db.TaskDB nl.sense_os.cortex.coaching.db.UserDB

Public Member Functions

 CoachingDB (SensePlatform sensePlatform, JSONObject dataStructure)
 
SQLiteDatabase getWritableDatabase ()
 
SQLiteDatabase getReadableDatabase ()
 
void onConfigure (SQLiteDatabase db)
 
void onOpen (SQLiteDatabase db)
 
void onCreate (SQLiteDatabase db)
 
void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)
 
boolean add (JSONObject object)
 
boolean add (JSONObject object, SQLiteDatabase db)
 
JSONArray get (JSONObject criterion)
 
JSONArray get (JSONObject start_criterion, JSONObject end_criterion)
 
void clearTable ()
 

Static Public Attributes

static final String KEY_ID = "_id"
 
static final String KEY_LOGIN_USERNAME = "login_username"
 
static final String KEY_MUTATED_AT = "mutated_at"
 

Protected Member Functions

String getLoginUsername ()
 
String createTableString ()
 
ContentValues toContentValues (JSONObject object)
 
String createEquals (JSONObject criterion)
 
String createBetween (JSONObject first_criterion, JSONObject second_criterion)
 
JSONObject getSingleCreterion (JSONObject first_criterion, JSONObject second_criterion)
 
JSONObject toJSONObject (Cursor c)
 
boolean update (JSONObject object)
 
boolean delete (JSONObject object)
 

Protected Attributes

String tableName
 
JSONObject dataStructure
 
SensePlatform sensePlatform
 
boolean userAutoIncrementKey = true
 
boolean isUserSpecificTable = true
 
boolean tableCreated = false
 

Static Protected Attributes

static String TAG = "CoachingDB"
 
static final String TYPE_INTEGER = " INTEGER"
 
static final String TYPE_TEXT = " TEXT"
 
static final String TYPE_DATETIME = " DATETIME"
 
static final String TYPE_REAL = " REAL"
 

Detailed Description

Helper class for local data storage

This class creates a simple way of storing JSONObject data in an SQLite database.
Based on an example JSONObject a table will be constructed with the appropriate columns names and types.
All the data added, updated, or retrieved will have the described JSONObject.

Author
ted@s.nosp@m.ense.nosp@m.-os.n.nosp@m.l
Date
July 03 2014

Constructor & Destructor Documentation

nl.sense_os.cortex.coaching.db.CoachingDB.CoachingDB ( SensePlatform  sensePlatform,
JSONObject  dataStructure 
)

Create Database Table based on JSONObject

Creates a Database DATABASE_NAME with as table TABLE with a structure defined by the JSONObject dataStructure.
The fields and data types of the values in the JSONObject are used to create the columns with their data types.

Parameters
contextThe application context
dataStructureExample JSONObject representing the Table structure.

Member Function Documentation

boolean nl.sense_os.cortex.coaching.db.CoachingDB.add ( JSONObject  object)

Add a row

Add a row based on the JSONObject parameter.
The parameter should have the same structure as defined in the dataStructure data member of the class.
The mutation date column KEY_MUTATED_AT will be updated.

Parameters
objectThe JSONObject to add as row.
Returns
True if it was successful otherwise False
boolean nl.sense_os.cortex.coaching.db.CoachingDB.add ( JSONObject  object,
SQLiteDatabase  db 
)

Add a row

Add a row based on the JSONObject parameter.
The parameter should have the same structure as defined in the dataStructure data member of the class.
The mutation date column KEY_MUTATED_AT will be updated.

Parameters
objectThe JSONObject to add as row.
dbA SQLiteDatabase instance which is writable
Returns
True if it was successful otherwise False
void nl.sense_os.cortex.coaching.db.CoachingDB.clearTable ( )

Clear table

Removes all the rows from the table.

String nl.sense_os.cortex.coaching.db.CoachingDB.createBetween ( JSONObject  first_criterion,
JSONObject  second_criterion 
)
protected

Create SQL between statement

Creates an SQL between String based on the criterion given by the first and second JSONObject.
Based on the fields and values in both JSONObject a "field > 'first_value' AND field < 'second_value'" String concatenated with 'AND' for all the field and values is created.

Parameters
criterionThe JSONObject holding the fields and values.
Returns
The SQL WHERE String
String nl.sense_os.cortex.coaching.db.CoachingDB.createEquals ( JSONObject  criterion)
protected

Create SQL equals statement

Creates an SQL equals String based on the criterion given by the JSONObject.
Based on the fields and values in the JSONObject a 'column = value' String concatenated with 'AND' for all the field and values is created.

Parameters
criterionThe JSONObject holding the fields and values.
Returns
The SQL WHERE String
String nl.sense_os.cortex.coaching.db.CoachingDB.createTableString ( )
protected

Get CREATE TABLE IF NOT EXISTS String

Constructs the CREATE TABLE String based on the dataStructure data member of the class.
The columns will be created with the types of the values in the dataStructure.

Returns
The CREATE TABLE String
boolean nl.sense_os.cortex.coaching.db.CoachingDB.delete ( JSONObject  object)
protected

Delete a row

Deletes a row based on the primary key (KEY_ID) given in the JSONObject.
The JSONObject parameter should have the same structure as defined in the dataStructure data member of the class.

Parameters
objectThe JSONObject to update.
Returns
True if at least one object was updated, false otherwise.
JSONArray nl.sense_os.cortex.coaching.db.CoachingDB.get ( JSONObject  criterion)

Get similar data

Based on the fields and values in the JSONObject a criterion is made for selecting the data.
Only equals is used as criterion for all the fields an values.
All the fields of the row are returned as JSONObject.

Parameters
criterionThe JSONObject holding the fields and values.
Returns
JSONArray with row data as JSONObject
JSONArray nl.sense_os.cortex.coaching.db.CoachingDB.get ( JSONObject  start_criterion,
JSONObject  end_criterion 
)

Get range data

Based on the fields and values in the JSONObject a criterion is made for selecting the data.
For fields only available in one of the criteria equals function is used as criterion.
For fields which are available in both criteria between is used as criterion.
All the fields of the row are returned as JSONObject.

Parameters
start_criterionThe start criterion JSONObject holding the fields and values.
end_criterionThe end criterion JSONObject holding the fields and values.
Returns
JSONArray with row data as JSONObject.
String nl.sense_os.cortex.coaching.db.CoachingDB.getLoginUsername ( )
protected

Get a user specific database prefix

Use the username as the prefix so that each user has his own database.

Parameters
sensePlatformThe binded SensePlatform
Returns
A user specific prefix String
JSONObject nl.sense_os.cortex.coaching.db.CoachingDB.getSingleCreterion ( JSONObject  first_criterion,
JSONObject  second_criterion 
)
protected

Create single criterion

Look at the fields which are only available in one of the JSONObjects and use this for creating a new criterion.

Parameters
first_criterionThe first criterion.
second_criterionThe second criterion.
Returns
A single criterion.
ContentValues nl.sense_os.cortex.coaching.db.CoachingDB.toContentValues ( JSONObject  object)
protected

Convert a JSONOObject to ContentValues

Creates the ContentValues representation for a JSONObject.
It uses the data types of the fields, used in the JSONObject.
These data types should resemble the structure used to create the TABLE.

Parameters
objectJSONObject to represent
Returns
The ContentValues representation
JSONObject nl.sense_os.cortex.coaching.db.CoachingDB.toJSONObject ( Cursor  c)
protected

Convert Cursor to JSONOBject

Converts a row to a JSONObject, using the data types as defined in the dataStructure of the class.

Parameters
cThe Cursors to convert to a JSONObject
Returns
The JSONObject representation of the Cursor
boolean nl.sense_os.cortex.coaching.db.CoachingDB.update ( JSONObject  object)
protected

Update a row

Updates a row based on the primary key (KEY_ID) given in the JSONObject.
The mutation date column KEY_MUTATED_AT will be updated.
The JSONObject parameter should have the same structure as defined in the dataStructure data member of the class.

Parameters
objectThe JSONObject to update.
Returns
True if at least one object was updated, false otherwise.

The documentation for this class was generated from the following file: