|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.perdues.db.Query
Class that holds some clauses of a SQL select and helps you build select statements. Serves a couple of different purposes.
First, this helps you build the syntax of the statement by letting you add information to any part of the statement at any time, and by inserting punctuation and logical connectives between the parts.
Second, this class builds SQL with properly escaped and quoted literals, so characters such as "'" can appear in your data.
Finally, if you add columns using methods that take ColumnInfo objects, this will automatically add the tables of those columns to the tables list. If you request equijoins for a table, it will use foreign key information in the Schema to generate the conditions that the primary keys of tables refer to match the foreign key into it from the given table.
Field Summary | |
static boolean |
debug
|
Constructor Summary | |
Query(Query template)
You can build a select statement from another Query. |
|
Query(Schema schema)
|
Method Summary | |
void |
addAliasedTable(java.lang.String name,
java.lang.String alias)
Add a table to the statement, giving an alias. |
void |
addColumn(ColumnInfo col)
Add a column to the list of columns to select, and its table to the set of tables for the query. |
void |
addColumn(java.lang.String colName)
Add a column to the list of columns to select, given its name; and adds the column's table to the set of tables for the query. |
void |
addColumns(java.lang.String[] colIds)
Adds each column ID String in the given array to this Query, as by addColumn. |
void |
addCondition(java.lang.String expr)
Add a condition to the "where" part of the select. |
void |
addEquiJoins(java.lang.String tableName)
Add a table to the join across known tables. |
void |
addEquiJoins(TableInfo table)
Adds equijoin conditions to this Query, requiring that foreign keys of the given TableInfo must be equal to the corresponding primary key of the table referred to. |
void |
addFieldTest(java.lang.String field,
java.lang.String op,
java.lang.String value)
Adds a condition comparing the value of a field to a specific String value. |
void |
addHaving(java.lang.String expr)
Add a condition to the "having" part of the select. |
void |
addSearchContainsCondition(java.lang.String field,
java.lang.String value)
Adds a condition that the value of the named field must contain the given value, or if the field is an empty string or null no condition is added. |
void |
addSearchEqualCondition(java.lang.String field,
java.lang.String value)
Adds a condition that the value of the named field must be equal to the given value, or if the field is an empty string or null no condition is added. |
void |
addTable(java.lang.String tableName)
Add a table by name to the list of tables to select from. |
void |
addTable(TableInfo table)
Add a table to the statement, using a TableInfo object. |
void |
addWhat(java.lang.String what)
Add a result column that is not a table column. |
static int |
getLastInsertID(java.sql.Statement stmt)
Returns the last "insert ID" automatically generated by the database for an insert request on the given Statement's connection. |
java.lang.String |
render()
Returns a string containing the complete select statement. |
void |
setGroupBy(java.lang.String clause)
Sets contents for the "group by" clause of the statement. |
void |
setLimit(java.lang.String clause)
Sets contents for the (MySQL-specific) "limit" clause. |
void |
setOrderBy(java.lang.String clause)
Sets contents for the "order by" clause of the statement. |
static java.lang.String |
sqlEscape(java.lang.String s)
Escape the String so it is appropriate as part of a SQL string literal. |
static java.lang.String |
sqlFormat(java.lang.Object o)
Formats an object as a literal for SQL. |
java.lang.String |
toString()
Returns a string containing the complete select statement. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static boolean debug
Constructor Detail |
public Query(Schema schema)
public Query(Query template)
Method Detail |
public static int getLastInsertID(java.sql.Statement stmt) throws java.sql.SQLException
public static java.lang.String sqlEscape(java.lang.String s)
public static java.lang.String sqlFormat(java.lang.Object o)
public void addWhat(java.lang.String what)
public void addColumn(java.lang.String colName)
Same as addColumn(ColumnInfo col), but takes a String naming the column. (A string naming the column is a qualified name with the table name or shorthand, and the name of the column, separated by ".".)
public void addColumn(ColumnInfo col)
public void addColumns(java.lang.String[] colIds)
public void addTable(java.lang.String tableName)
public void addTable(TableInfo table)
public void addAliasedTable(java.lang.String name, java.lang.String alias)
public void addCondition(java.lang.String expr)
public void addHaving(java.lang.String expr)
public void addSearchEqualCondition(java.lang.String field, java.lang.String value)
public void addSearchContainsCondition(java.lang.String field, java.lang.String value)
public void addFieldTest(java.lang.String field, java.lang.String op, java.lang.String value)
public void addEquiJoins(java.lang.String tableName)
public void addEquiJoins(TableInfo table)
Better to do this just once per table before rendering the query. Probably buggy if the given table has more than one column that is a foreign key into the the same table.
public void setGroupBy(java.lang.String clause)
public void setOrderBy(java.lang.String clause)
public void setLimit(java.lang.String clause)
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String render()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |