public class SQLiteConnectorImpl extends java.lang.Object implements ISQLiteConnector
SQLiteConnectorImpl
is a simple implementation of
ISQLiteConnector
which handles the communication with a given SQLite
table.
All instances share a common database named SQLiteConnectorImpl. The
name of the underlying database can be changed by overwriting
getDatabaseName()
.
By creating this class, clients have to deliver the names and data types of each column, passed as String arrays with the constructor. As an example:
final String[] columnNames = new String[] { "FIRST", "SECOND" }; final String[] columnTypes = new String[] { "TEXT", "INT" }; final SQLiteConnectorImpl store = new SQLiteConnectorImpl("MyTable", columnNames, columnTypes);Supported types are listed in the SQLite online documentation. The first value within
rowNames
(rowNames[0]
) is treated as
the primary key.
It's recommended to introduce subclasses to properly overwrite
bindFromCursor(Cursor)
to return a specific object. By default,
bindFromCursor(Cursor)
will return an array containing the values
for each column.
Uses Logger.info(String, String)
to print all executed queries.
Constructor and Description |
---|
SQLiteConnectorImpl(java.lang.String tableName,
java.lang.String[] rowNames,
java.lang.String[] rowTypes)
Creates a new instance and tries to create - or open - the underlying
database.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
bindFromCursor(com.softwareag.mobile.runtime.database.Cursor cursor)
Binds the values contained in the delivered
Cursor to an array of
objects. |
void |
createTable()
Creates a new table if it's not existing.
|
void |
delete(java.lang.Object primaryKey)
Deletes a object identified by it's primary key value.
|
void |
deleteAll()
Deletes all objects from within this table.
|
int |
getCount()
Returns the number of stored elements.
|
java.lang.String |
getDatabaseName()
Returns the name of the underlying database.
If no subclass is used, returns DATABASE_NAME . |
java.lang.String |
getTableName()
Returns the name of the table.
|
void |
insertOrUpdate(java.lang.Object[] data)
Inserts, or if already existing updates, a given object.
|
java.lang.Object[] |
selectAll()
Returns all contained elements as an array of
Object s. |
public SQLiteConnectorImpl(java.lang.String tableName, java.lang.String[] rowNames, java.lang.String[] rowTypes)
tableName
- rowNames
- rowTypes
- public java.lang.String getDatabaseName()
DATABASE_NAME
.public void createTable() throws com.softwareag.mobile.runtime.database.DatabaseException
ISQLiteConnector
ISQLiteConnector.getTableName()
as
the table name identifier.createTable
in interface ISQLiteConnector
com.softwareag.mobile.runtime.database.DatabaseException
public java.lang.String getTableName()
ISQLiteConnector
getTableName
in interface ISQLiteConnector
public int getCount() throws com.softwareag.mobile.runtime.database.DatabaseException
ISQLiteConnector
getCount
in interface ISQLiteConnector
com.softwareag.mobile.runtime.database.DatabaseException
public java.lang.Object[] selectAll() throws com.softwareag.mobile.runtime.database.DatabaseException
ISQLiteConnector
Object
s.selectAll
in interface ISQLiteConnector
com.softwareag.mobile.runtime.database.DatabaseException
public void insertOrUpdate(java.lang.Object[] data) throws com.softwareag.mobile.runtime.database.DatabaseException
ISQLiteConnector
data[0]
as value for the primary key.insertOrUpdate
in interface ISQLiteConnector
com.softwareag.mobile.runtime.database.DatabaseException
public java.lang.Object bindFromCursor(com.softwareag.mobile.runtime.database.Cursor cursor) throws com.softwareag.mobile.runtime.database.DatabaseException
Cursor
to an array of
objects. This method is called by selectAll()
. cursor
- com.softwareag.mobile.runtime.database.DatabaseException
selectAll()
public void delete(java.lang.Object primaryKey) throws com.softwareag.mobile.runtime.database.DatabaseException
ISQLiteConnector
delete
in interface ISQLiteConnector
com.softwareag.mobile.runtime.database.DatabaseException
public void deleteAll() throws com.softwareag.mobile.runtime.database.DatabaseException
ISQLiteConnector
deleteAll
in interface ISQLiteConnector
com.softwareag.mobile.runtime.database.DatabaseException