|
||||||||||
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);
Method Summary | |
void |
addAll(DbAbstractTable table)
Add all the columns from the given table to the select list. |
void |
addAllExcept(DbAbstractTable table,
DbColumn o)
Add all the columns from the given table to the select list. |
void |
addAllExcept(DbAbstractTable 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 |
addGroupBy(DbExpr column)
Adds a GROUP BY column to this select. |
void |
addJoinedTable(DbJoinedTable joinedTable)
Adds a joined table. |
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. |
void |
executeToResultSet(DbConnection dbcon)
Execute and get a JDBC ResultSet. |
DbColumn |
getColumn(int index)
Gets a column in the column list. |
java.lang.String |
getQueryString()
Get the query string represented by this query. |
DbExpr |
getWhere()
Returns the where condition. |
void |
selectTables(java.util.Set c)
Creates a SELECT instruction with given tables. |
void |
setDistinct(boolean pDistinct)
Sets the value to control whether to put the DISTINCT clause or not. |
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)
Puts data into statement. |
void |
setWhere(DbExpr where)
Set the where condition for this query. |
java.lang.String |
toString()
Converts this selector in a string. |
Methods inherited from class javatools.db.DbExpr |
and, containsAllStrings, containsAllStrings, count, dateTrunc, equal, greaterThan, greaterThanOrEqual, in, in, isNotNull, isNull, lessThan, lessThanOrEqual, like, lower, max, min, notEqual, notIn, notIn, or, upper, usesTables, usesTables |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Method Detail |
public void setWhere(DbExpr where)
where
- The new where valuepublic DbExpr getWhere()
public void setOrderBy(java.util.List l)
l
- The new orderBy valuepublic int setSqlValues(java.sql.PreparedStatement stmt, int i) throws DbException, java.sql.SQLException
setSqlValues
in class DbExpr
stmt
- The statement to use.i
- An index (obscure).
DbException
- If something goes wrong.
java.sql.SQLException
- If something goes wrong.public void setLimit(int n) throws DbException
n
- The new limit value
DbException
- If something goes wrong.public void setOffset(int n) throws DbException
n
- The new offset value
DbException
- If something goes wrong.public java.lang.String getQueryString() throws DbException
getQueryString
in class DbExpr
DbException
- If something goes wrong.public DbColumn addColumn(java.lang.Object col) throws DbException
col
- A DbColumn, DbExpr or literal value
DbException
- If something goes wrong.public DbColumn addColumn(java.lang.Object col, java.lang.String as) throws DbException
col
- a DbColumn, DbExpr or literal valueas
- a column alias
DbException
- If something goes wrong.public DbColumn getColumn(int index)
index
- The column to be got.
public void addAll(DbAbstractTable table) throws DbException
table
- the table whose columns we wish to add
DbException
- If something goes wrong.public void addAllExcept(DbAbstractTable table, DbColumn o) throws DbException
table
- the table whose columns we wish to addo
- The feature to be added to the AllExcept attribute
DbException
- If something goes wrong.public void addAllExcept(DbAbstractTable 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
- If something goes wrong.public void addOrderBy(DbExpr column, boolean desc)
column
- the column to order bydesc
- whether to sort in descending orderpublic void addGroupBy(DbExpr column)
column
- The column to add in the GROUP BY clause.public void setDistinct(boolean pDistinct)
pDistinct
- true
: clause DISTINCT will be put;
false
: clause DISTINCT will not be put.public void addJoinedTable(DbJoinedTable joinedTable)
joinedTable
- The joined table to add.public void executeToResultSet(DbConnection dbcon) throws DbException
dbcon
- The connection to use.
DbException
- If something goes wrong.public DbTable execute(DbConnection dbcon) throws DbException
dbcon
- The connection to use.
DbException
- If something goes wrong.public DbTable execute() throws DbException
DbException
- If something goes wrong.public java.lang.String toString()
toString
in class java.lang.Object
public void selectTables(java.util.Set c)
c
- A set of tables.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |