📄 query.java
字号:
} } // Now if we have any columns, init those as well... if (!this.retObjs) { int aic = 0; int si = this.cols.size (); this.aliases = new HashMap (); for (int i = 0; i < si; i++) { SelectItemExpression exp = (SelectItemExpression) this.cols.get (i); exp.init (this); if (exp.isAddItemsFromCollectionOrMap ()) { aic++; } String alias = exp.getAlias (); if (alias != null) { this.aliases.put (alias, new Integer (i + 1)); } this.aliases.put ((i + 1) + "", new Integer (i + 1)); } if ((aic > 0) && (aic != si) ) { throw new QueryParseException ("If one or more SELECT clause columns is set to add the items returned from a: " + Map.class.getName () + " or: " + java.util.Collection.class.getName () + " then ALL columns must be marked to return the items as well."); } } // Now init the where clause (where possible)... if (this.where != null) { this.where.init (this); } // Now init the having clause (where possible)... if (this.having != null) { this.having.init (this); } // See if we have order by columns, if so init the comparator. this.initOrderByComparator (); // See if we have order by columns, if so init the comparator. if (this.groupBys != null) { this.grouper = new Grouper (this); int si = this.groupBys.size (); for (int i = 0; i < si; i++) { OrderBy ob = (OrderBy) this.groupBys.get (i); // Get the expression... Expression e = (Expression) ob.getExpression (); if (e == null) { // Now expect an integer that refers to a column // in the select... int ci = ob.getIndex (); if (ci == 0) { throw new QueryParseException ("Order by column indices should start at 1."); } if (this.retObjs) { throw new QueryParseException ("Cannot sort on a select column index when the objects are to be returned."); } if (ci > this.cols.size ()) { throw new QueryParseException ("Invalid order by column index: " + ci + ", only: " + this.cols.size () + " columns are selected to be returned."); } // Get the SelectItemExpression. SelectItemExpression sei = (SelectItemExpression) this.cols.get (ci - 1); // Get the expression... e = sei.getExpression (); } else { // Init the expression... e.init (this); } this.grouper.addExpression (e); } } if (this.groupOrderBys != null) { if (this.grouper == null) { throw new QueryParseException ("Group Order Bys are only valid if 1 or more Group By columns have been specified."); } // Here we "override" the from class because when dealing with the order bys the // current object will be a List, NOT the class defined in the FROM clause. Class c = this.objClass; this.objClass = List.class; // No caching, this may need to change in the future. this.groupOrderByComp = new GroupByExpressionComparator (this, false); GroupByExpressionComparator lec = (GroupByExpressionComparator) this.groupOrderByComp; List grouperExps = this.grouper.getExpressions (); // Need to check the type of each order by, if we have // any "column" indexes check to see if they are an accessor... int si = this.groupOrderBys.size (); for (int i = 0; i < si; i++) { Integer k = new Integer (i); OrderBy ob = (OrderBy) this.groupOrderBys.get (i); if (ob.getIndex () > -1) { int ci = ob.getIndex (); if (ci == 0) { throw new QueryParseException ("Group Order by column indices should start at 1."); } if (ci > grouperExps.size ()) { throw new QueryParseException ("Invalid Group Order By column index: " + ci + ", only: " + grouperExps.size () + " Group By columns are selected to be returned."); } lec.addSortItem (null, // Remember the -1! Column indices start at 1 but // List indices start at 0 ;) ci - 1, ob.getType ()); continue; } // Get the expression... Expression e = (Expression) ob.getExpression (); // See if the expression is a "direct" match for any of the // group by columns. boolean cont = true; for (int j = 0; j < grouperExps.size (); j++) { Expression exp = (Expression) grouperExps.get (j); if (e.equals (exp)) { // This is a match, add to the comparator. lec.addSortItem (null, j, ob.getType ()); cont = false; } } if (!cont) { continue; } if ((e instanceof Function) || (e instanceof BindVariable) || (e instanceof SaveValue) ) { e.init (this); lec.addSortItem (e, -1, ob.getType ()); continue; } // If we are here then we haven't been able to deal with the // order by... so barf. throw new QueryParseException ("If the Group Order By: " + ob + " is not a function, a bind variable or a save value then it must be present in the Group By list."); } // Restore the FROM object class. this.objClass = c; } if (this.groupByLimit != null) { this.groupByLimit.init (this); } if (this.limit != null) { this.limit.init (this); } if (this.executeOn != null) { // Get the supported types. List allF = (List) this.executeOn.get (Query.ALL); if (allF != null) { // We have some, so init them... int si = allF.size (); for (int i = 0; i < si; i++) { AliasedExpression f = (AliasedExpression) allF.get (i); f.init (this); } } List resultsF = (List) this.executeOn.get (Query.RESULTS); if (resultsF != null) { // We have some, so init them... int si = resultsF.size (); for (int i = 0; i < si; i++) { AliasedExpression f = (AliasedExpression) resultsF.get (i); f.init (this); } } resultsF = (List) this.executeOn.get (Query.GROUP_BY_RESULTS); if (resultsF != null) { // We have some, so init them... int si = resultsF.size (); for (int i = 0; i < si; i++) { AliasedExpression f = (AliasedExpression) resultsF.get (i); f.init (this); } } } this.addTiming ("Time to init Query objects", System.currentTimeMillis () - s); } /** * Set the "FROM" object class. It is advised that you NEVER call this method, do so * at your own risk, dragons will swoop from the sky and crisp your innards if you do so!!! * Seriously though ;), this method should ONLY be called by those who know what they * are doing, whatever you think you know about how this method operates is irrelevant * which is why the dangers of calling this method are not documented... * <p> * YOU HAVE BEEN WARNED!!! NO BUGS WILL BE ACCEPTED THAT ARISE FROM THE CALLING OF * THIS METHOD!!! * * @param c The FROM class. */ public void setFromObjectClass (Class c) { this.objClass = c; } public Class getFromObjectClass () { return this.objClass; } public void removeBindVariableChangedListener (BindVariableChangedListener bvl) { List l = (List) this.listeners.get ("bvs"); if (l == null) { return; } l.remove (bvl); } public void addBindVariableChangedListener (BindVariableChangedListener bvl) { List l = (List) this.listeners.get ("bvs"); if (l == null) { l = new ArrayList (); this.listeners.put ("bvs", l); } if (!l.contains (bvl)) { l.add (bvl); } } public void removeSaveValueChangedListener (SaveValueChangedListener svl) { List l = (List) this.listeners.get ("svs"); if (l == null) { return; } l.remove (svl); } public void addSaveValueChangedListener (SaveValueChangedListener svl) { List l = (List) this.listeners.get ("svs"); if (l == null) { l = new ArrayList (); this.listeners.put ("svs", l); } if (!l.contains (svl)) { l.add (svl); } } /* public class ExecutionInfo { private long timeStamp = System.currentTimeMillis (); private String message = null; private Object creator = null; public ExecutionInfo (Object creator, String message) { this.message = message; this.creator = creator; } public String getMessage () { return this.message; } public Object getCreator () { return this.creator; } public long getTimestamp () { return this.timeStamp; } } */ public Map getAliases () { return this.aliases; } public boolean isWantObjects () { return this.retObjs; } public void setWantObjects (boolean v) { this.retObjs = v; } public char getWildcardCharacter () { return this.wildcardChar; } public void setWildcardCharacter (char c) { this.wildcardChar = c; } public void setLimit (Limit l) { this.limit = l; } public Limit getLimit () { return this.limit; } /** * Return whether this Query object has had a statement applied to it * and has been parsed. * * @return Whether the query is associated with a statement. */ public boolean parsed () { return this.isParsed; } /** * Indicate whether "distinct" results are required. * * @param v Set to <code>true</code> to make the results distinct. */ public void setWantDistinctResults (boolean v) { this.distinctResults = v; } public QueryResults getQueryResults () { return this.qd; } /** * Get the "order bys". This will return a List of {@link OrderBy} objects. * This is generally only useful when you want to {@link #reorder(List,String)} * the search and wish to get access to the textual representation of the order bys. * <p> * It is therefore possible to modify the orderbys in place, perhaps by using a different * expression or changing the direction (since the objects are not cloned before being * returned). However do so at <b>YOUR OWN RISK</b>. If you do so, then ensure you * call: {@link #setOrderByColumns(List)}, then: {@link #initOrderByComparator()} * before re-executing the statement, otherwise nothing will happen! * * @return The order bys. */ public List getOrderByColumns () { return new ArrayList (this.orderBys); } public void setParent (Query q) { this.parent = q; } public Query getParent () { return this.parent; } public Query getTopLevelQuery () { Query q = this; Query par = null; while (true) { par = q.getParent (); if (par == null) { break; } q = par; } return q; } public String toString () { StringBuffer buf = new StringBuffer ("SELECT "); if (this.distinctResults) { buf.append ("DISTINCT "); } if (this.retObjs) { buf.append ("*"); } else { for (int i = 0; i < this.cols.size (); i++) { buf.append (" "); buf.append (this.cols.get (i)); if (i < (this.cols.size () - 1)) { buf.append (","); } } } buf.append (" FROM "); buf.append (this.from); if (this.where != null) { buf.append (" WHERE "); buf.append (this.where); } return buf.toString (); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -