|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javatools.db.DbExpr | +--javatools.db.DbSelector
A class used to select tabular data from an SQL database. The constructor is not public. To obtain a DbSelector call DbDatabase.selector(); Example: To select FRED's record from the people table...
DbDatabase db = ...; DbTable people = db.getTable("PEOPLE"); DbSelector selector = db.selector(); selector.addColumn(people.getColumn("NAME")); selector.addColumn(people.getColumn("AGE")); selector.setWhere(people.getColumn("NAME").equal("FRED")); DbTable result = selector.execute(); DbIterator it = result.iterator(); while (it.hasNextRow()) { DbRow row = it.nextRow(); System.out.println(row.getValue("NAME") + " " + row.getValue("AGE")); }This is equivilent to...
SELECT NAME, AGE FROM PEOPLE WHERE PEOPLE.NAME='FRED';To get more fancy we can join the people table with the team table to find the captain of the person's favourite team. Then we can also order by the person's name, while igoring upper/lower case distinctions...
DbDatabase db = ...; DbSelector selector = db.selector(); DbTable people = db.getTable("PEOPLE"); DbTable team = db.getTable("TEAM"); DbSelector selector = db.selector(); selector.addColumn(people.getColumn("NAME")); selector.addColumn(team.getColumn("CAPTAIN")); selector.setWhere(team.getColumn("NAME").equal(people.getColumn("FAVOURITE_TEAM")); selector.addOrderBy(people.getColumn("NAME").lower(), false) // Order by NAME ignoring case. DbTable result = selector.execute(); DbIterator it = result.iterator(); while (it.hasNextRow()) { DbRow row = it.nextRow(); System.out.println(row.getValue("NAME") + " " + row.getValue("CAPTAIN")); }This is equivilent to...
SELECT PEOPLE.NAME, TEAM.CAPTAIN FROM PEOPLE, TEAM WHERE TEAM.NAME = PEOPLE.FAVOURITE_TEAM ORDER BY LOWER(PEOPLE.NAME)To get fancier still, we can make use of sub-selects. To find all the people who happen to be captains of teams...
DbDatabase db = ...; DbTable people = db.getTable("PEOPLE"); DbTable team = db.getTable("TEAM"); DbSelector subselector = db.selector(); subselector.addColumn(team.getColumn("CAPTAIN")); DbSelector selector = db.selector(); selector.addAll(people); selector.setWhere(people.getColumn("NAME").in(subselector)); DbTable result = selector.execute(); DbIterator it = result.iterator(); while (it.hasNextRow()) { DbRow row = it.nextRow(); System.out.println(row.toString()); }This is equivilent to...
SELECT * from PEOPLE WHERE PEOPLE.NAME IN (SELECT CAPTAIN FROM TEAM);
Field Summary | |
(package private) java.util.Map |
asMap
|
(package private) java.util.List |
columnList
|
(package private) DbExpr |
limit
|
(package private) DbExpr |
offset
|
(package private) java.util.List |
orderBy
|
(package private) DbTable |
result
|
(package private) java.sql.ResultSet |
resultSet
|
(package private) DbExpr |
where
|
Fields inherited from class javatools.db.DbExpr |
db |
Constructor Summary | |
(package private) |
DbSelector(DbDatabase db)
|
(package private) |
DbSelector(DbDatabase db,
DbTable result)
|
Method Summary | |
void |
addAll(DbTable table)
Add all the columns from the given table to the select list. |
void |
addAllExcept(DbTable table,
DbColumn o)
Add all the columns from the given table to the select list. |
void |
addAllExcept(DbTable table,
java.util.Set set)
Add all the columns from the given table to the select list. |
DbColumn |
addColumn(java.lang.Object col)
Add the given object to the select column list. |
DbColumn |
addColumn(java.lang.Object col,
java.lang.String as)
Add the given object to the select column list with an "AS" alias. |
void |
addOrderBy(DbExpr column,
boolean desc)
Add an ORDER BY clause to this select. |
DbTable |
execute()
Execute and return a DbTable with the default DbConnection. |
DbTable |
execute(DbConnection dbcon)
Execute and return a DbTable. |
java.sql.PreparedStatement |
executeToResultSet(DbConnection dbcon)
Execute and get a JDBC ResultSet. |
java.lang.String |
getQueryString()
Get the query string represented by this query. |
(package private) java.lang.String |
orderByClause(java.util.List orderBy)
Generate the order by clause. |
void |
selectTables(java.util.Set c)
|
void |
setLimit(int n)
Don't get the whole result set, get only a limited number of rows. |
void |
setOffset(int n)
Don't get the first results, but skip n result rows. |
void |
setOrderBy(java.util.List l)
Set the entire orderby list in one go. |
int |
setSqlValues(java.sql.PreparedStatement stmt,
int i)
Any DbExpr needs to be able to substitute any parameters as per JDBC "?" substitutions. |
(package private) int |
setSqlValues(java.sql.PreparedStatement stmt,
int i,
java.util.List intoList)
Substitute the literal values in the Prepared Statement. |
void |
setWhere(DbExpr where)
Set the where condition for this query. |
java.lang.String |
toString()
|
Methods inherited from class javatools.db.DbExpr |
and, dateTrunc, equal, getString, greaterThan, greaterThanOrEqual, in, isNotNull, isNull, lessThan, lessThanOrEqual, like, lower, max, min, notEqual, notIn, or, setSqlValue, upper, usesTables, usesTables |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
DbTable result
java.util.Map asMap
java.util.List columnList
DbExpr where
java.util.List orderBy
java.sql.ResultSet resultSet
DbExpr limit
DbExpr offset
Constructor Detail |
DbSelector(DbDatabase db) throws DbException
DbSelector(DbDatabase db, DbTable result) throws DbException
Method Detail |
public void setWhere(DbExpr where)
where
- The new where valuepublic void setOrderBy(java.util.List l)
l
- The new orderBy valuepublic int setSqlValues(java.sql.PreparedStatement stmt, int i) throws DbException, java.sql.SQLException
DbExpr
setSqlValues
in class DbExpr
stmt
- The new sqlValues valuei
- The new sqlValues value
DbException
- Description of Exception
java.sql.SQLException
- Description of Exceptionpublic void setLimit(int n) throws DbException
n
- The new limit value
DbException
- Description of Exceptionpublic void setOffset(int n) throws DbException
n
- The new offset value
DbException
- Description of Exceptionpublic java.lang.String getQueryString() throws DbException
getQueryString
in class DbExpr
DbException
- Description of Exceptionpublic DbColumn addColumn(java.lang.Object col) throws DbException
col
- A DbColumn, DbExpr or literal value
DbException
- Description of Exceptionpublic DbColumn addColumn(java.lang.Object col, java.lang.String as) throws DbException
col
- a DbColumn, DbExpr or literal valueas
- a column alias
DbException
- Description of Exceptionpublic void addAll(DbTable table) throws DbException
table
- the table whose columns we wish to add
DbException
- Description of Exceptionpublic void addAllExcept(DbTable table, DbColumn o) throws DbException
table
- the table whose columns we wish to addo
- The feature to be added to the AllExcept attribute
DbException
- Description of Exceptionpublic void addAllExcept(DbTable table, java.util.Set set) throws DbException
table
- the table whose columns we wish to addset
- The feature to be added to the AllExcept attribute
DbException
- Description of Exceptionpublic void addOrderBy(DbExpr column, boolean desc)
column
- the column to order bydesc
- whether to sort in descending orderpublic java.sql.PreparedStatement executeToResultSet(DbConnection dbcon) throws DbException
dbcon
- Description of Parameter
DbException
- Description of Exceptionpublic DbTable execute(DbConnection dbcon) throws DbException
dbcon
- Description of Parameter
DbException
- Description of Exceptionpublic DbTable execute() throws DbException
DbException
- Description of Exceptionpublic java.lang.String toString()
toString
in class java.lang.Object
public void selectTables(java.util.Set c)
int setSqlValues(java.sql.PreparedStatement stmt, int i, java.util.List intoList) throws DbException, java.sql.SQLException
stmt
- the PreparedStatementi
- the parameter number we are up tointoList
- The new sqlValues value
DbException
- Description of Exception
java.sql.SQLException
- Description of Exceptionjava.lang.String orderByClause(java.util.List orderBy) throws DbException
orderBy
- Description of Parameter
DbException
- Description of Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |