📄 selectcommand.java
字号:
// Create a references to the RowIterator for this table. RowIterator tableiter = null; // And create the set of WhereNodes that apply to this table. Set whereNodesForTable = null; if (col != null && pos == 1) { Index index = table.getIndexForColumn(table.getColumn(col.getName())); if (index != null) { tableiter = new ChangingIndexedRowIterator(index, table, ComparisonOperator.EQUAL); } } if (_applyWhereNodesAfterJoin == false) { for (Iterator whereNodeIter = _unappliedWhereNodes.iterator();whereNodeIter.hasNext();) { WhereNode node = (WhereNode) (whereNodeIter.next()); // If the node only references this table... if (onlyReferencesTable(tableident,node)) { // .. and we haven't yet applied an index,... if (null == tableiter) { // ...then try to find an index for this node. tableiter = table.getIndexedRows(node,true); // If we still don't have an iterator, // then no index is available, so add the node // to the whereNodesForTable set... if(null == tableiter) { if(null == whereNodesForTable) { whereNodesForTable = new HashSet(); } whereNodesForTable.add(node); } } else { // Else if we've already applied an index, // then add the node to the whereNodesForTable if(null == whereNodesForTable) { whereNodesForTable = new HashSet(); } whereNodesForTable.add(node); } // Remove the WhereNode from the unapplied where nodes, // since we've either added it to the whereNodesForTable set, // or we applied it via the index. whereNodeIter.remove(); } } } // If we still don't have a RowIterator for this table, // then we'll use a table scan. if(null == tableiter) { tableiter = table.getRowIterator(true); } // Apply any unapplied whereNodesForTable. if(null != whereNodesForTable && !whereNodesForTable.isEmpty()) { Map localmap = new HashMap(); populateColumnIdToFieldMap(localmap,tableident,0,db); Iterator whereNodesForTableIter = whereNodesForTable.iterator(); while(whereNodesForTableIter.hasNext()) { WhereNode node = (WhereNode)(whereNodesForTableIter.next()); tableiter = new FilteringRowIterator(tableiter, new RowDecorator(localmap), node); } } populateColumnIdToFieldMap(_colIdToFieldMap, tableident, _indexOffset, db); _indexOffset += table.getColumnCount(); return (tableiter); } private Selectable[] generateSelectArrayForResultSet(Database db) throws AxionException { List list = new ArrayList(getSelectCount()); TableIdentifier[] tables = getFromArray(); for(int i=0;i<getSelectCount();i++) { Selectable sel = getSelect(i); if(sel instanceof ColumnIdentifier) { ColumnIdentifier colid = (ColumnIdentifier)sel; if("*".equals(colid.getName())) { if(null == colid.getTableName()) { for(int j=0;j<getFromCount();j++) { TableIdentifier tableID = tables[j]; Table table = db.getTable(tableID); for(Iterator iter = table.getColumnIdentifiers(); iter.hasNext();) { ColumnIdentifier colId = (ColumnIdentifier)iter.next(); colId.setTableIdentifier(tableID); list.add(colId); } } } else { Table table = db.getTable(colid.getTableIdentifier()); for(Iterator iter = table.getColumnIdentifiers();iter.hasNext();) { list.add(iter.next()); } } } else { list.add(colid); } } else { list.add(sel); } } return(Selectable[])(list.toArray(new Selectable[list.size()])); } /** Unsupported */ public int executeUpdate(Database database) throws AxionException { throw new UnsupportedOperationException("Use executeQuery, not executeUpdate."); } public boolean execute(Database database) throws AxionException { setResultSet(executeQuery(database)); return (getResultSet() != null); } private void resolve(Database db) throws AxionException { if(!_resolved) { // resolve the Seletables TableIdentifier[] tables = getFromArray(); // resolve SELECT part for(int i=0;i<getSelectCount();i++) { setSelect(i,db.resolveSelectable(getSelect(i),tables)); } db.resolveFromNode(getFrom(), tables); // resolve WHERE part db.resolveWhereNode(getWhere(),tables); // resolve ORDER BY part if(null != _orderBy) { for(int i=0;i<_orderBy.size();i++) { OrderNode ob = (OrderNode)(_orderBy.get(i)); ob.setSelectable(db.resolveSelectable(ob.getSelectable(),tables)); } } _resolved = true; // check for aggregate functions boolean foundScalar = false; for(int i=0;i<getSelectCount();i++) { if(getSelect(i) instanceof AggregateFunction) { if(foundScalar) { throw new AxionException("Can't select both scalar values and aggregate functions."); } else if(_foundAggregateFunction) { throw new AxionException("Currently can't select more than one aggregate function at a time."); } else { _foundAggregateFunction = true; } } else { if(_foundAggregateFunction) { throw new AxionException("Can't select both scalar values and aggregate functions."); } foundScalar = true; } } } } public String toString() { StringBuffer buf = new StringBuffer(); buf.append("SELECT "); if(_distinct) { buf.append("DISTINCT "); } { Iterator iter = _select.iterator(); while(iter.hasNext()) { buf.append(iter.next()); if(iter.hasNext()) { buf.append(", "); } } } if (null != _from) { buf.append(" FROM "); buf.append(_from); } if(null != _where) { buf.append(" WHERE "); buf.append(_where); } if(null != _orderBy && !_orderBy.isEmpty()) { buf.append(" ORDER BY "); { Iterator iter = _orderBy.iterator(); while(iter.hasNext()) { buf.append(iter.next()); if(iter.hasNext()) { buf.append(", "); } } } } return buf.toString(); } /** * Create a list of all the literals that have been selected, * returning null if there aren't any. */ private List createLiteralList() throws AxionException{ List literals = null; for(int i=0;i<this.getSelectCount();i++) { if(getSelect(i) instanceof Literal) { if(null == literals) { literals = new ArrayList(); } literals.add(this.getSelect(i)); } } return literals; } private boolean onlyReferencesTable(TableIdentifier table, WhereNode node) { ReferencesOtherTablesWhereNodeVisitor v = new ReferencesOtherTablesWhereNodeVisitor(table); v.visit(node); return v.getResult(); } private ComparatorChain generateOrderChain(Map indexMap) { ComparatorChain chain = new ComparatorChain(); for(int i=0;i<getOrderByCount();i++) { if(getOrderBy(i).isDescending()) { chain.setReverseSort(i); } chain.addComparator(new RowComparator(getOrderBy(i).getSelectable(),new RowDecorator(indexMap))); } return chain; } private void populateColumnIdToFieldMap(Map indexMap, TableIdentifier tableIdent, int offset, Database db) throws AxionException { Table table = db.getTable(tableIdent); if(null == table) { throw new AxionException("Table " + tableIdent + " not found."); } for(int j=0,J=table.getColumnCount();j<J;j++) { ColumnIdentifier id = null; // determine which selected column id matches, if any for(int k=0,K=getSelectCount();k<K;k++) { Selectable sel = getSelect(k); if(sel instanceof ColumnIdentifier) { ColumnIdentifier cSel = (ColumnIdentifier)sel; if(tableIdent.equals(cSel.getTableIdentifier()) && cSel.getName().equals(table.getColumn(j).getName())) { id = cSel; break; } } } if(null == id) { id = new ColumnIdentifier(tableIdent, table.getColumn(j).getName()); } indexMap.put(id, new Integer(offset + j)); } } //----------------------------------------------------------------- Members private List _select = new ArrayList(); private FromNode _from = null; private WhereNode _where = null; private List _orderBy = new ArrayList(); private boolean _distinct = false; private boolean _resolved = false; private boolean _foundAggregateFunction = false; private Literal _limit = null; private Literal _offset = null; private Database _currentDatabase = null; //--- private Map _colIdToFieldMap = null; private int _indexOffset = 0; private Set _unappliedWhereNodes = null; private List _literals = null; private RowIterator _rows = null; private Selectable[] _selected = null; private boolean _applyWhereNodesAfterJoin = false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -