I
- The implementation class which extends QueryBuilder
. This
is set explicitly in the declaration of the implementation class,
and allows the superclass to provide concrete implementations of
shared methods.R
- The return type of the underlying SQLiteDatabase
function.
This is set explicitly in the declaration of the implementation
class, and will be the return type of execute()
and
executeOn(android.database.sqlite.SQLiteDatabase)
.public abstract class QueryBuilder<I extends QueryBuilder,R>
extends java.lang.Object
SQLiteDatabase
methods that use long lists of parameters.
This class is supposed to do very little; the consumer should use the
static methods insertInto(java.lang.String)
, update(java.lang.String)
, selectFrom(java.lang.String)
, and
deleteFrom(java.lang.String)
to spawn instances of the various implementation classes
corresponding to database actions (INSERT
, UPDATE
,
SELECT
, and DELETE
).
In its initial implementation, the class provides methods to set an
explicit format string and parameter list for the WHERE
clause,
similar to the syntax used by the various Android functions in
SQLiteDatabase
. In the future, the API of this class will be extended
to provide a grammar for constructing the conditional statement through
builder methods.
The class builds on the foundation of SQLitePortal
and derives
several of its static constants and design decisions from features of that
class.
Modifier and Type | Class and Description |
---|---|
static class |
QueryBuilder.UnboundException
Informs a consumer that the builder's
execute() method was invoked
while the builder was not bound to a target database instance. |
Modifier and Type | Field and Description |
---|---|
protected java.lang.String[] |
m_asExplicitWhereParams
A substitute, explicit list of parameters for the
WHERE clause
format specified in m_sExplicitWhereFormat . |
protected android.database.sqlite.SQLiteDatabase |
m_dbTarget
A persistent binding to a specific database, used by
execute() . |
protected java.lang.String |
m_sExplicitWhereFormat
A substitute, explicit format string for a
WHERE clause, for
which m_asExplicitWhereParams provides the values. |
protected java.lang.String |
m_sTableName
The name of the table on which the query will operate.
|
protected android.content.ContentValues |
m_valsToWrite
For
INSERT and DELETE operations, these are the values to
be written. |
Constructor and Description |
---|
QueryBuilder(java.lang.String sTableName)
Superclass's constructor, which initializes the shared member fields.
|
Modifier and Type | Method and Description |
---|---|
static DeletionBuilder |
deleteFrom(android.database.sqlite.SQLiteDatabase db,
java.lang.String sTableName)
Kicks off construction of a
DELETE query, bound to a specific
database instance. |
static DeletionBuilder |
deleteFrom(java.lang.String sTableName)
Kicks off construction of a
DELETE query. |
R |
execute()
Executes the query that has been built by the implementation class, on
the database instance to which the builder has been bound, either by a
constructor, or by
onDatabase(android.database.sqlite.SQLiteDatabase) . |
abstract R |
executeOn(android.database.sqlite.SQLiteDatabase db)
Executes the query that has been built by the implementation class.
|
protected java.lang.String |
getWhereClause()
Creates a raw SQLite
WHERE clause based on the format and params
created for the instance. |
protected java.lang.String |
getWhereFormat()
Creates the Android
WHERE clause template to be passed to a
SQLiteDatabase function. |
protected java.lang.String[] |
getWhereParams()
Creates the array of
WHERE clause value substitutions to be
passed to a SQLiteDatabase function. |
static InsertionBuilder |
insertInto(android.database.sqlite.SQLiteDatabase db,
java.lang.String sTableName)
Kicks off construction of an
INSERT query, bound to a specific
database instance. |
static InsertionBuilder |
insertInto(java.lang.String sTableName)
Kicks off construction of an
INSERT query. |
I |
onDatabase(android.database.sqlite.SQLiteDatabase db)
Binds the builder to a specific database instance, to be used by
execute() . |
static SelectionBuilder |
selectFrom(android.database.sqlite.SQLiteDatabase db,
java.lang.String sTableName)
Kicks off construction of a
SELECT query, bound to a specific
database instance. |
static SelectionBuilder |
selectFrom(java.lang.String sTableName)
Kicks off construction of a
SELECT query. |
protected I |
setTableName(java.lang.String sTableName)
Sets the table name in which the query will be executed.
|
I |
setValues(android.content.ContentValues vals)
Sets values to be written as part of an
INSERT or UPDATE
query. |
static java.lang.String |
toSQLInputParams(android.content.ContentValues vals)
Renders the key/value pairs in a set of
ContentValues as a list
of input parameters to an SQL INSERT or UPDATE query's
SET clause. |
static UpdateBuilder |
update(android.database.sqlite.SQLiteDatabase db,
java.lang.String sTableName)
Kicks off construction of an
UPDATE query, bound to a specific
database instance. |
static UpdateBuilder |
update(java.lang.String sTableName)
Kicks off construction of an
UPDATE query. |
I |
where(java.lang.String sWhereClause)
Constructs an explicit
WHERE clause for the query. |
I |
where(java.lang.String sWhereFormat,
java.util.Collection<java.lang.String> asWhereParams)
Constructs an explicit
WHERE clause for the query. |
I |
where(java.lang.String sWhereFormat,
java.lang.String... asWhereParams)
Constructs an explicit
WHERE clause for the query. |
protected java.lang.String m_sTableName
protected android.content.ContentValues m_valsToWrite
INSERT
and DELETE
operations, these are the values to
be written.protected java.lang.String m_sExplicitWhereFormat
WHERE
clause, for
which m_asExplicitWhereParams
provides the values.protected java.lang.String[] m_asExplicitWhereParams
WHERE
clause
format specified in m_sExplicitWhereFormat
.protected android.database.sqlite.SQLiteDatabase m_dbTarget
execute()
.public QueryBuilder(java.lang.String sTableName)
sTableName
- the name of the table on which the query will be
performedpublic static InsertionBuilder insertInto(java.lang.String sTableName)
INSERT
query.sTableName
- the name of the table into which rows will be insertedpublic static InsertionBuilder insertInto(android.database.sqlite.SQLiteDatabase db, java.lang.String sTableName)
INSERT
query, bound to a specific
database instance.db
- the database on which the query should be executedsTableName
- the name of the table into which rows will be insertedpublic static UpdateBuilder update(java.lang.String sTableName)
UPDATE
query.sTableName
- the name of the table in which rows will be updatedpublic static UpdateBuilder update(android.database.sqlite.SQLiteDatabase db, java.lang.String sTableName)
UPDATE
query, bound to a specific
database instance.db
- the database on which the query should be executedsTableName
- the name of the table in which rows will be updatedpublic static SelectionBuilder selectFrom(java.lang.String sTableName)
SELECT
query.sTableName
- the name of the table from which rows will be selectedpublic static SelectionBuilder selectFrom(android.database.sqlite.SQLiteDatabase db, java.lang.String sTableName)
SELECT
query, bound to a specific
database instance.db
- the database on which the query should be executedsTableName
- the name of the table from which rows will be selectedpublic static DeletionBuilder deleteFrom(java.lang.String sTableName)
DELETE
query.sTableName
- the name of the table from which rows will be deletedpublic static DeletionBuilder deleteFrom(android.database.sqlite.SQLiteDatabase db, java.lang.String sTableName)
DELETE
query, bound to a specific
database instance.db
- the database on which the query should be executedsTableName
- the name of the table from which rows will be deletedpublic static java.lang.String toSQLInputParams(android.content.ContentValues vals)
ContentValues
as a list
of input parameters to an SQL INSERT
or UPDATE
query's
SET
clause.
So that the fields always appear in consistent order (rather than by hash
code), the method will sort lexically by key before rendering the output
string.vals
- the key/value pairs to be renderedSET
clause bodypublic I onDatabase(android.database.sqlite.SQLiteDatabase db)
execute()
.db
- the database instance on which the query should be executedprotected I setTableName(java.lang.String sTableName)
sTableName
- the name of the tablepublic I setValues(android.content.ContentValues vals)
INSERT
or UPDATE
query.vals
- the values to be writtenpublic I where(java.lang.String sWhereClause)
WHERE
clause for the query.
The supplied string is used as a format string for the WHERE
clause and should contain no variable substitutions. The
collection of variable values will be set to null
by this method,
based on this assumption.sWhereClause
- the explicit WHERE
clause, containing
no variable substitution placeholderspublic I where(java.lang.String sWhereFormat, java.lang.String... asWhereParams)
WHERE
clause for the query.sWhereFormat
- the format string for the WHERE
clause; uses
?
for parameter substitutionasWhereParams
- the parameters for the WHERE
clausepublic I where(java.lang.String sWhereFormat, java.util.Collection<java.lang.String> asWhereParams)
WHERE
clause for the query.sWhereFormat
- the format string for the WHERE
clause; uses
?
for parameter substitutionasWhereParams
- the parameters for the WHERE
clauseprotected java.lang.String getWhereFormat()
WHERE
clause template to be passed to a
SQLiteDatabase
function.WHERE
clause templateprotected java.lang.String[] getWhereParams()
WHERE
clause value substitutions to be
passed to a SQLiteDatabase
function.WHERE
clause template parametersprotected java.lang.String getWhereClause()
WHERE
clause based on the format and params
created for the instance.WHERE
clausepublic abstract R executeOn(android.database.sqlite.SQLiteDatabase db)
db
- the database instance on which the query should be executed.public final R execute() throws QueryBuilder.UnboundException
onDatabase(android.database.sqlite.SQLiteDatabase)
.QueryBuilder.UnboundException
- if the builder is not yet bound to
a database instance