de.unikassel.android.sdcframework.persistence
Class DatabaseAdapterImpl

Package class diagram package DatabaseAdapterImpl
java.lang.Object
  extended by de.unikassel.android.sdcframework.persistence.DatabaseAdapterImpl
All Implemented Interfaces:
DatabaseAdapter

public final class DatabaseAdapterImpl
extends Object
implements DatabaseAdapter

The database adapter class does wrap the direct Android database access.


Nested Class Summary
private static class DatabaseAdapterImpl.DatabaseHelper
          The internal SQLite helper class
 
Field Summary
private  SQLiteDatabase db
          The database
private static String DB_CREATE_INDEX1
          The first index creation statements
private static String DB_CREATE_INDEX2
          The first index creation statements
private static String DB_CREATE_TABLE
          The table creation statement
static String DB_INDEX1
          The first index name
static String DB_INDEX2
          The second index name
static String DB_TABLE
          The table name
private static String DB_UPDATE_1
          The first table update statement statement
private static String DB_UPDATE_2
          The first table update statement statement
private static int DB_VERSION
          The database version
private  DatabaseAdapterImpl.DatabaseHelper dbHelper
          The SQLite helper
private static String KEY_DATA
          The data column name
private static String KEY_DATA_CLASS
          The data type class name
private static String KEY_LOCATION
          The location column name
private static String KEY_PRIO
          The priority column name
private static String KEY_ROWID
          The row identifier column name
private static String KEY_SENSORID
          The sensor identifier column name
private static String KEY_SYNCED
          The time stamp sync state column
private static String KEY_TIMESTAMP
          The sensor identifier column name
private static long MAX_NUM_PLACEHOLDERS
          There's a limit in maximum number of placeholders in compiled SQL statement
private static String ORDER_BY_ASC_PRIO_TS
          The order by time ascending priority and stamp statement ( ascending time stamp order, highest priority first )
private static String ORDER_BY_DESC_PRIO_TS
          The order by time descending priority and stamp statement ( ascending time stamp order, lowest priority first )
private static String ORDER_BY_TS
          The order by time stamp statement ( oldest time stamps first )
 
Constructor Summary
DatabaseAdapterImpl(String dbName, Context applicationContext)
          Constructor
DatabaseAdapterImpl(String dbName, long maxDBSize, Context applicationContext)
          Constructor
 
Method Summary
 void close()
          Method to close the database
 boolean deleteAll()
          Method to delete all stored records at once
private  long deleteSamples(Set<Long> rowIds)
          Method to delete a set of samples from the database
 long deleteSamplesOrdered(long count, boolean deleteLowestPriorityFirst)
          Method to delete a given count of the oldest samples.
private  long deleteSamplesOrdered(long count, String orderByStatement)
          Method to delete a given count of records using the given order by statement
 long getMaximumDatabaseSize()
          Getter for the maximum database size
 long getPageSize()
          Getter for the current database page size in bytes
 long getRecordCount()
          Method to get the current record count
private  Cursor getSamplesOrdered(long limit, String orderByStatement)
          Getter for samples ordered by the given statement
private  long insertSample(DatabaseSample sample)
          Method to insert a sample into the database
 void insertSamples(Collection<DatabaseSample> samples)
          Method to insert a sample collection into the database
 DatabaseAdapter open()
          Method to open the database for write access
 DatabaseAdapter openForRead()
          Method to open the database for read access
private  boolean removeSamples(long count, Collection<DatabaseSample> sampleCollection, String orderByStatement)
          Method to remove the next "count" samples selected ordered by priority and time stamp from the database and stored in a given sample collection.
 boolean removeSamplesHighestPrioFirst(long count, Collection<DatabaseSample> sampleCollection)
          Method to remove the next "count" samples which will be selected ordered by ascending priority and ascending time stamp from the database and stored in a given sample collection.
 boolean removeSamplesLowestPrioFirst(long count, Collection<DatabaseSample> sampleCollection)
          Method to remove the next "count" samples which will be selected ordered by descending priority and ascending time stamps from the database and stored in a given sample collection.
 boolean removeSamplesOldestTimeStampFirst(long count, Collection<DatabaseSample> sampleCollection)
          Method to remove the next "count" samples which will be selected ordered by ascending time stamps from the database and stored in a given sample collection.
private  DatabaseSample sampleFromCursor(Cursor cursor)
          Does create a sample from cursor position
 long setMaximumDatabaseSize(long size)
          Setter for the maximum database size
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_NUM_PLACEHOLDERS

private static final long MAX_NUM_PLACEHOLDERS
There's a limit in maximum number of placeholders in compiled SQL statement

See Also:
Constant Field Values

KEY_ROWID

private static final String KEY_ROWID
The row identifier column name

See Also:
Constant Field Values

KEY_SENSORID

private static final String KEY_SENSORID
The sensor identifier column name

See Also:
Constant Field Values

KEY_TIMESTAMP

private static final String KEY_TIMESTAMP
The sensor identifier column name

See Also:
Constant Field Values

KEY_PRIO

private static final String KEY_PRIO
The priority column name

See Also:
Constant Field Values

KEY_SYNCED

private static final String KEY_SYNCED
The time stamp sync state column

See Also:
Constant Field Values

KEY_DATA

private static final String KEY_DATA
The data column name

See Also:
Constant Field Values

KEY_DATA_CLASS

private static final String KEY_DATA_CLASS
The data type class name

See Also:
Constant Field Values

KEY_LOCATION

private static final String KEY_LOCATION
The location column name

See Also:
Constant Field Values

ORDER_BY_TS

private static final String ORDER_BY_TS
The order by time stamp statement ( oldest time stamps first )

See Also:
Constant Field Values

ORDER_BY_ASC_PRIO_TS

private static final String ORDER_BY_ASC_PRIO_TS
The order by time ascending priority and stamp statement ( ascending time stamp order, highest priority first )

See Also:
Constant Field Values

ORDER_BY_DESC_PRIO_TS

private static final String ORDER_BY_DESC_PRIO_TS
The order by time descending priority and stamp statement ( ascending time stamp order, lowest priority first )

See Also:
Constant Field Values

DB_TABLE

public static final String DB_TABLE
The table name

See Also:
Constant Field Values

DB_INDEX1

public static final String DB_INDEX1
The first index name

See Also:
Constant Field Values

DB_INDEX2

public static final String DB_INDEX2
The second index name

See Also:
Constant Field Values

DB_VERSION

private static final int DB_VERSION
The database version

See Also:
Constant Field Values

DB_CREATE_TABLE

private static final String DB_CREATE_TABLE
The table creation statement

See Also:
Constant Field Values

DB_CREATE_INDEX1

private static final String DB_CREATE_INDEX1
The first index creation statements

See Also:
Constant Field Values

DB_CREATE_INDEX2

private static final String DB_CREATE_INDEX2
The first index creation statements

See Also:
Constant Field Values

DB_UPDATE_1

private static final String DB_UPDATE_1
The first table update statement statement

See Also:
Constant Field Values

DB_UPDATE_2

private static final String DB_UPDATE_2
The first table update statement statement

See Also:
Constant Field Values

dbHelper

private final DatabaseAdapterImpl.DatabaseHelper dbHelper
The SQLite helper


db

private SQLiteDatabase db
The database

Constructor Detail

DatabaseAdapterImpl

public DatabaseAdapterImpl(String dbName,
                           Context applicationContext)
Constructor

Parameters:
dbName - the database name
applicationContext - the application context

DatabaseAdapterImpl

public DatabaseAdapterImpl(String dbName,
                           long maxDBSize,
                           Context applicationContext)
Constructor

Parameters:
dbName - the database name
maxDBSize - the maximum databse size
applicationContext - the application context
Method Detail

open

public final DatabaseAdapter open()
                           throws SQLiteException
Description copied from interface: DatabaseAdapter
Method to open the database for write access

Specified by:
open in interface DatabaseAdapter
Returns:
this database adapter
Throws:
SQLiteException - if unable to open the database

openForRead

public final DatabaseAdapter openForRead()
                                  throws SQLiteException
Description copied from interface: DatabaseAdapter
Method to open the database for read access

Specified by:
openForRead in interface DatabaseAdapter
Returns:
this database adapter
Throws:
SQLiteException - if unable to open the database

close

public final void close()
Description copied from interface: DatabaseAdapter
Method to close the database

Specified by:
close in interface DatabaseAdapter

insertSample

private final long insertSample(DatabaseSample sample)
                         throws SQLException
Method to insert a sample into the database

Parameters:
sample - the sample to insert
Returns:
the row id of the sample
Throws:
SQLException

insertSamples

public final void insertSamples(Collection<DatabaseSample> samples)
                         throws Exception
Description copied from interface: DatabaseAdapter
Method to insert a sample collection into the database

Specified by:
insertSamples in interface DatabaseAdapter
Parameters:
samples - the sample collection to insert
Throws:
Exception

deleteSamples

private final long deleteSamples(Set<Long> rowIds)
Method to delete a set of samples from the database

Parameters:
rowIds - a set with unique row identifiers from database to delete
Returns:
the affected record count

deleteAll

public final boolean deleteAll()
Method to delete all stored records at once

Returns:
true if successful, false otherwise

getRecordCount

public final long getRecordCount()
Description copied from interface: DatabaseAdapter
Method to get the current record count

Specified by:
getRecordCount in interface DatabaseAdapter
Returns:
the current count of records stored

getSamplesOrdered

private final Cursor getSamplesOrdered(long limit,
                                       String orderByStatement)
Getter for samples ordered by the given statement

Parameters:
limit - the count of rows to retrieve
orderByStatement - the order by statement
Returns:
the cursor for all samples sorted by priority descending

sampleFromCursor

private final DatabaseSample sampleFromCursor(Cursor cursor)
Does create a sample from cursor position

Parameters:
cursor - the database cursor
Returns:
the sample created from cursor position

removeSamples

private final boolean removeSamples(long count,
                                    Collection<DatabaseSample> sampleCollection,
                                    String orderByStatement)
Method to remove the next "count" samples selected ordered by priority and time stamp from the database and stored in a given sample collection.

Parameters:
count - the sample count to remove
sampleCollection - the sample collection to store removed samples in
orderByStatement - the order by statement to use for the query
Returns:
true if successful, false otherwise

deleteSamplesOrdered

private final long deleteSamplesOrdered(long count,
                                        String orderByStatement)
Method to delete a given count of records using the given order by statement

Parameters:
count - the count of records to delete
orderByStatement - the order by statement
Returns:
the count of deleted records

deleteSamplesOrdered

public final long deleteSamplesOrdered(long count,
                                       boolean deleteLowestPriorityFirst)
Description copied from interface: DatabaseAdapter
Method to delete a given count of the oldest samples.

Specified by:
deleteSamplesOrdered in interface DatabaseAdapter
Parameters:
count - the count of records to delete
deleteLowestPriorityFirst - if true the sample for deletion will be selected first by lowest priority, second oldest time stamp
Returns:
the count of deleted records

removeSamplesHighestPrioFirst

public final boolean removeSamplesHighestPrioFirst(long count,
                                                   Collection<DatabaseSample> sampleCollection)
Description copied from interface: DatabaseAdapter
Method to remove the next "count" samples which will be selected ordered by ascending priority and ascending time stamp from the database and stored in a given sample collection.
This method is used to select samples for transmission.
Here the oldest samples with the highest priority will be selected first.

Specified by:
removeSamplesHighestPrioFirst in interface DatabaseAdapter
Parameters:
count - the sample count to remove
sampleCollection - the sample collection to store removed samples in
Returns:
true if successful, false otherwise

removeSamplesLowestPrioFirst

public final boolean removeSamplesLowestPrioFirst(long count,
                                                  Collection<DatabaseSample> sampleCollection)
Description copied from interface: DatabaseAdapter
Method to remove the next "count" samples which will be selected ordered by descending priority and ascending time stamps from the database and stored in a given sample collection.
This method is used to select samples for deletion in case of exceeding database size.
Here the oldest samples with the lowest priority will be selected first.

Specified by:
removeSamplesLowestPrioFirst in interface DatabaseAdapter
Parameters:
count - the sample count to remove
sampleCollection - the sample collection to store removed samples in
Returns:
true if successful, false otherwise

removeSamplesOldestTimeStampFirst

public final boolean removeSamplesOldestTimeStampFirst(long count,
                                                       Collection<DatabaseSample> sampleCollection)
Description copied from interface: DatabaseAdapter
Method to remove the next "count" samples which will be selected ordered by ascending time stamps from the database and stored in a given sample collection.
This method is used to select samples for deletion in case of exceeding database size.
Here, independent of the sample priority, the oldest samples will be selected first.

Specified by:
removeSamplesOldestTimeStampFirst in interface DatabaseAdapter
Parameters:
count - the sample count to remove
sampleCollection - the sample collection to store removed samples in
Returns:
true if successful, false otherwise

setMaximumDatabaseSize

public final long setMaximumDatabaseSize(long size)
Description copied from interface: DatabaseAdapter
Setter for the maximum database size

Specified by:
setMaximumDatabaseSize in interface DatabaseAdapter
Parameters:
size - the maximum database size
Returns:
the new maximum database size

getMaximumDatabaseSize

public final long getMaximumDatabaseSize()
Description copied from interface: DatabaseAdapter
Getter for the maximum database size

Specified by:
getMaximumDatabaseSize in interface DatabaseAdapter
Returns:
the maximum database size

getPageSize

public final long getPageSize()
Getter for the current database page size in bytes

Returns:
the page size in bytes