| java.lang.Object simpleorm.core.SQuery
SQuery | public class SQuery implements SConstants(Code) | | Provides the non-SQL query interface. This is still a thin layer
on SQL, but provides more run time type checking at the price of
slightly longer queries. There is also a very small performance overhead.
For example:-
SResultSet res = Employee.meta.newQuery()
.gt(Employee.NAME, "'J'")
//.and() // AND is implied.
.eq(Employee.DEPARTMENT, myDept)
.decending(Employee.NAME) // ie. Order By
.execute();
while (res.hasNext()) {...
is equivalent to
SPreparedStatement stmt = Employee.meta.select(
"NAME > 'j' AND DEPT_ID = ?", "NAME DESC");
stmt.setInt(1, myDept.getInt(DEPT_ID));
SResultSet res = stmt.execute();
while (res.hasNext()) {...
This API is a little unusual in that there is only one SQuery instance
created, and each of the methods update its state and then return
this .
The raw* methods can be used to poke any string into the SQL Where or
Order By clauses in the order they appear. But more commonly the raw
methods are called by higher level methods such as eq and
and .
todo Some of this logic should be moved to sdriver, and integrated with its selectSQL etc.
|
Constructor Summary | |
protected | SQuery(SRecordMeta record, long sqy_bitSet, SFieldMeta[] selectList) SQueries are created by SRecordMeta.newQuery(). |
Method Summary | |
public SQuery | and() Adds rawClause("AND");
This method is optional. | public SQuery | ascending(SFieldMeta field) | public SQuery | combineBegin() | public SQuery | combineEnd() Inserts ")" into the query. | public SQuery | descending(SFieldMeta field) | public SQuery | eq(SFieldMeta field, Object value) Normally just adds fieldRelopParameter(field, "=",
value) .
If field is a reference it recursively expands the foreign
keys, and value must be an instance of the same record type.
value must not be null, you need the special case IS NULL test. | public SQuery | eq(SFieldMeta field, int value) | public SQuery | eq(SFieldMeta field, long value) | public SQuery | eq(SFieldMeta field, double value) | public SQuery | eq(SFieldMeta field1, SFieldMeta field2) | SQuery | eqNeAux(SFieldMeta field, Object value, boolean isEq) | public SQuery | equivalent(SFieldBoolean field, boolean value) True if boolean field == writeFieldValue(value).
Ie. | public SResultSet | execute() Execute the query, set the previously specified query values,
and return the result set. | public SResultSet | execute(SPreparedStatement stmt) | public SQuery | fieldQuery(SFieldMeta field, String clause) Generates field clause , ie the clause string is poked
literally into the query. | public SQuery | fieldRelopParameter(SFieldMeta field, String relop, Object value) Generates field relop ? and then subsequently sets the
parameter value. | public SQuery | fieldRelopParameter(SFieldMeta field1, String relop, SFieldMeta field2) Generates field1 relop field2
E.g. | public SQuery | ge(SFieldMeta field, Object value) | public SQuery | ge(SFieldMeta field, int value) | public SQuery | ge(SFieldMeta field, long value) | public SQuery | ge(SFieldMeta field, double value) | public SQuery | ge(SFieldMeta field1, SFieldMeta field2) | public SRecordMeta | getSRecordMeta() | public SQuery | gt(SFieldMeta field, Object value) | public SQuery | gt(SFieldMeta field, int value) | public SQuery | gt(SFieldMeta field, long value) | public SQuery | gt(SFieldMeta field, double value) | public SQuery | gt(SFieldMeta field1, SFieldMeta field2) | public SQuery | in(SFieldMeta field, Object[] values) | public SQuery | in(SFieldMeta field, int[] values) | public SQuery | in(SFieldMeta field, String value) value is tokenized into a list of values. | public boolean | isDistinct() Return true if this query will use 'SELECT DISTINCT'. | public SQuery | isFalse(SFieldBoolean field) | public SQuery | isNotNull(SFieldMeta field) | public SQuery | isNotNull(SFieldMeta field, Object value) | public SQuery | isNull(SFieldMeta field) | public SQuery | isNull(SFieldMeta field, Object value) | public SQuery | isTrue(SFieldBoolean field) | public SQuery | join(SFieldReference reference) Join to another table, based on the specified reference. | public SQuery | le(SFieldMeta field, Object value) | public SQuery | le(SFieldMeta field, int value) | public SQuery | le(SFieldMeta field, long value) | public SQuery | le(SFieldMeta field, double value) | public SQuery | le(SFieldMeta field1, SFieldMeta field2) | public SQuery | like(SFieldMeta field, Object value) | public SQuery | lt(SFieldMeta field, Object value) | public SQuery | lt(SFieldMeta field, int value) | public SQuery | lt(SFieldMeta field, long value) | public SQuery | lt(SFieldMeta field, double value) | public SQuery | lt(SFieldMeta field1, SFieldMeta field2) | public SQuery | ne(SFieldMeta field, int value) | public SQuery | ne(SFieldMeta field, Object value) Just adds fieldRelopParameter(field, "<>", value) .
Note that there are few methods ne(SfieldMeta, int)
etc. | public SQuery | ne(SFieldMeta field, long value) | public SQuery | ne(SFieldMeta field, double value) | public SQuery | ne(SFieldMeta field1, SFieldMeta field2) | public SQuery | not() | SQuery | nullAux(SFieldMeta field, boolean isNull) | public SQuery | or() | public SQuery | rawClause(String term) Add term to the where clase, eg. | public SQuery | rawField(SFieldMeta field) Adds the field's columnName. | public SQuery | rawOrderBy(String orderBy) Add a clause to the OrderBy statement, eg. | public SQuery | rawOrderBy(SFieldMeta field, boolean ascending) Add a clause to the OrderBy statement. | public SQuery | rawParameter(Object parameter) Add a parameter value to the internal array. | static String | substitute(String qry, SArrayList parameters) Substitute '?' in the sql query with the values in the parameters array. | public String | toString() |
needsConjunction | boolean needsConjunction(Code) | | |
orderByJoinedColumn | boolean orderByJoinedColumn(Code) | | |
reverseJoin | boolean reverseJoin(Code) | | |
sqy_bitSet | long sqy_bitSet(Code) | | |
SQuery | protected SQuery(SRecordMeta record, long sqy_bitSet, SFieldMeta[] selectList)(Code) | | SQueries are created by SRecordMeta.newQuery().
|
and | public SQuery and()(Code) | | Adds rawClause("AND");
This method is optional. If you call neither and() nor or() between statements
(e.g. eq()), then a call to and() is assumed, and is done for you.
|
combineBegin | public SQuery combineBegin()(Code) | | Inserts "(" into the query
author: Pierre Awaragi |
combineEnd | public SQuery combineEnd()(Code) | | Inserts ")" into the query.
author: Pierre Awaragi |
eq | public SQuery eq(SFieldMeta field, Object value)(Code) | | Normally just adds fieldRelopParameter(field, "=",
value) .
If field is a reference it recursively expands the foreign
keys, and value must be an instance of the same record type.
value must not be null, you need the special case IS NULL test.
(It would be possible to optimize this here, but what about
field == field where one of them is null -- that would be
inconsistent.)
|
equivalent | public SQuery equivalent(SFieldBoolean field, boolean value)(Code) | | True if boolean field == writeFieldValue(value).
Ie. value is converted from bool to "Y"/"N" etc.
|
execute | public SResultSet execute()(Code) | | Execute the query, set the previously specified query values,
and return the result set.
|
fieldQuery | public SQuery fieldQuery(SFieldMeta field, String clause)(Code) | | Generates field clause , ie the clause string is poked
literally into the query. Eg.
fieldQuery(Employee.Name, "= 'Fred'")
Use fieldRelopParameter instead for
parameters determined at run time as blindly concatenating
strings is dangerous.
|
fieldRelopParameter | public SQuery fieldRelopParameter(SFieldMeta field, String relop, Object value)(Code) | | Generates field relop ? and then subsequently sets the
parameter value. Eg.
fieldRelopParameter(Employee.Name, "=", myName)
|
fieldRelopParameter | public SQuery fieldRelopParameter(SFieldMeta field1, String relop, SFieldMeta field2)(Code) | | Generates field1 relop field2
E.g.
fieldRelopParameter(Order.QuantityRequired, "=", Order.QuantityReceived)
Mainly useful for Joins.
|
isDistinct | public boolean isDistinct()(Code) | | Return true if this query will use 'SELECT DISTINCT'. This is automatically
added if the following two conditions are true:
- This query uses a join on a reference FROM another table. For example,
a query on department that has a join to employee (note that without the
'DISTINCT' keyword, multiple rows would typically be returned for each department)
- There are no order-by columns on fields other than the query table. SQL
syntax forces all order-by columns to also be in the select list, if DISTINCT
is used.
|
join | public SQuery join(SFieldReference reference)(Code) | | Join to another table, based on the specified reference.
The INNER join works in both directions. So either reference may
be from a table that is already in the query to a new table, or
from a new table to a table that is already in the query.
This method updates both the from and where claues. After a call
to this method is made, you can simply refer to fields from the
joined table in subsequent calls to the relative operators such
as eq().
This provides an alternative to just using subselects. The effect is
almost the same except for inner join issues.
You can call join() multiple times to join several tables together. This will
work as long as each subsequent join() call makes a connection from the
existing set of tables to a new table.
A single table may not be joined multiple times, so there can be
no ambiguity as to which join path to use to access a field.
There is an example in BasicTests.queryTest().
|
ne | public SQuery ne(SFieldMeta field, Object value)(Code) | | Just adds fieldRelopParameter(field, "<>", value) .
Note that there are few methods ne(SfieldMeta, int)
etc. This is because 5 relops * 5 data types would require 25
methods! Java 1.5 boxing will (finally) make this unnecessary anyway.
|
rawClause | public SQuery rawClause(String term)(Code) | | Add term to the where clase, eg. "BUDGET > ?". Not normally called directly.
|
rawOrderBy | public SQuery rawOrderBy(String orderBy)(Code) | | Add a clause to the OrderBy statement, eg. "NAME DESC". Commas
are added automatically.
|
rawOrderBy | public SQuery rawOrderBy(SFieldMeta field, boolean ascending)(Code) | | Add a clause to the OrderBy statement. Commas
are added automatically.
|
rawParameter | public SQuery rawParameter(Object parameter)(Code) | | Add a parameter value to the internal array. These will be
setObject later after the prepared statement is
created but before it is executed.
|
substitute | static String substitute(String qry, SArrayList parameters)(Code) | | Substitute '?' in the sql query with the values in the parameters array.
Used to create meaningful SQL strings for logging purposes ONLY.
|
|
|