query.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 821 行 · 第 1/2 页

JAVA
821
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.db.sql;import com.caucho.db.Database;import com.caucho.db.store.Transaction;import com.caucho.db.table.Column;import com.caucho.db.table.Table;import com.caucho.db.table.TableIterator;import com.caucho.log.Log;import com.caucho.sql.SQLExceptionWrapper;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import java.io.IOException;import java.io.InputStream;import java.sql.SQLException;import java.util.ArrayList;import java.util.logging.Level;import java.util.logging.Logger;abstract public class Query {  private static final Logger log = Log.open(Query.class);  private static final L10N L = new L10N(Query.class);  private Database _db;  private String _sql;  private FromItem []_fromItems;  private ParamExpr []_params;  private boolean _isGroup;  private int _dataFieldCount;  private Query _parent;  private SubSelectExpr _parentSubSelect;  private Expr []_whereExprs;  protected Expr _whereExpr;  private RowIterateExpr []_indexExprs;  private ArrayList<SubSelectParamExpr> _paramExprs    = new ArrayList<SubSelectParamExpr>();  protected Query(Database db, String sql)  {    _db = db;    _sql = sql;  }  protected Query(Database db, String sql, FromItem []fromItems)  {    _db = db;    _sql = sql;    _fromItems = fromItems;  }  /**   * Returns the owning database.   */  public Database getDatabase()  {    return _db;  }  /**   * Sets the parent query   */  public void setParent(Query query)  {    _parent = query;  }  /**   * Gets the parent query   */  public Query getParent()  {    return _parent;  }  /**   * Sets the parent sub-select.   */  public void setSubSelect(SubSelectExpr subSelect)  {    _parentSubSelect = subSelect;  }  /**   * Gets the parent sub-select.   */  public SubSelectExpr getSubSelect()  {    return _parentSubSelect;  }  /**   * Returns the number of temporary data fields.   */  public int getDataFields()  {    return _dataFieldCount;  }  /**   * Sets the number of temporary data fields.   */  public void setDataFields(int fieldCount)  {    _dataFieldCount = fieldCount;  }  /**   * Sets the maximum entires   */  public void setLimit(int limit)  {  }    /**   * Returns any from items.   */  public FromItem []getFromItems()  {    return _fromItems;  }  /**   * Sets from items.   */  protected void setFromItems(FromItem []fromItems)  {    _fromItems = fromItems;  }  /**   * Sets from items.   */  protected void setFromItems(ArrayList<FromItem> fromItems)  {    _fromItems = new FromItem [fromItems.size()];    fromItems.toArray(_fromItems);  }  /**   * Sets the where expr.   */  public void setWhereExpr(Expr expr)  {    _whereExpr = expr;  }  /**   * Returns the where exprs   */  public Expr []getWhereExprs()  {    return _whereExprs;  }  /**   * Sets the where exprs.   */  protected void setWhereExprs(Expr []whereExprs)  {    _whereExprs = whereExprs;  }  /**   * Sets the params.   */  public void setParams(ParamExpr []params)  {    _params = params;  }  /**   * Returns the param exprs.   */  public ArrayList<SubSelectParamExpr> getParamExprs()  {    return _paramExprs;  }  /**   * Returns the SQL.   */  String getSQL()  {    return _sql;  }  /**   * Returns true for select queries.   */  public boolean isSelect()  {    return false;  }  public boolean isReadOnly()  {    return true;  }  /**   * Sets the current number of group fields.   */  public void setGroup(boolean isGroup)  {    _isGroup = isGroup;  }  /**   * Sets true for group operations   */  public boolean isGroup()  {    return _isGroup;  }  /**   * Binds the query.   */  protected void bind()    throws SQLException  {    if (_whereExpr != null) {      generateWhere(_whereExpr);      for (int i = 0; i < _whereExprs.length; i++) {	Expr expr = _whereExprs[i];	if (expr != null)	  _whereExprs[i] = expr.bind(this);      }    }    else if (_fromItems != null) {      _whereExprs = new Expr[_fromItems.length + 1];      _indexExprs = new RowIterateExpr[_fromItems.length];    }    else {      _whereExprs = new Expr[2];      _indexExprs = new RowIterateExpr[1];    }    for (int i = 0; i < _indexExprs.length; i++) {      Expr expr = _indexExprs[i];      if (expr == null)	_indexExprs[i] = new RowIterateExpr();      else	_indexExprs[i] = (RowIterateExpr) _indexExprs[i].bind(this);    }    for (int i = 0; i < _paramExprs.size(); i++) {      SubSelectParamExpr expr = _paramExprs.get(i);      expr = (SubSelectParamExpr) expr.bind(_parent);      _paramExprs.set(i, expr);    }  }  /**   * Optimize the where and order the from items.   */  protected void generateWhere(Expr whereExpr)    throws SQLException  {    ArrayList<Expr> andProduct = new ArrayList<Expr>();    whereExpr.splitAnd(andProduct);    FromItem []fromItems = getFromItems();    Expr []whereExprs = new Expr[fromItems.length + 1];    RowIterateExpr []indexExprs = new RowIterateExpr[fromItems.length];    _whereExprs = whereExprs;    _indexExprs = indexExprs;    ArrayList<FromItem> costItems = new ArrayList<FromItem>();    orderFromItems(costItems, andProduct);    costItems.clear();    for (int i = fromItems.length; i >= 0; i--) {      if (i < fromItems.length)	costItems.add(fromItems[i]);      AndExpr subWhereExpr = null;      boolean hasExpr = false;      int bestIndex = -1;      long bestCost;      do {	bestCost = Long.MAX_VALUE;	for (int j = andProduct.size() - 1; j >= 0; j--) {	  Expr subExpr = andProduct.get(j);	  long cost = subExpr.cost(costItems);	  if (Integer.MAX_VALUE <= cost && i != 0) {	  }	  else if (cost < bestCost) {	    bestCost = cost;	    bestIndex = j;	  }	}	if (bestCost < Long.MAX_VALUE) {	  Expr expr = andProduct.remove(bestIndex);	  RowIterateExpr indexExpr = null;	  if (i < fromItems.length)	    indexExpr = expr.getIndexExpr(fromItems[i]);	  if (indexExpr != null && indexExprs[i] == null) {	    indexExprs[i] = indexExpr;	  }	  else {	    // XXX: check if really need to add	    if (subWhereExpr == null)	      subWhereExpr = new AndExpr();	    subWhereExpr.add(expr);	  }	}      } while (bestCost < Long.MAX_VALUE);      if (subWhereExpr != null)	whereExprs[i] = subWhereExpr.getSingleExpr();    }    for (int i = 0; i < whereExprs.length; i++) {      Expr expr = whereExprs[i];      /*      if (expr != null)	expr = expr.bind(this);      */      whereExprs[i] = expr;    }    _whereExprs = whereExprs;    if (log.isLoggable(Level.FINE)) {      log.fine("where-" + (whereExprs.length - 1) +  ": static " + whereExprs[whereExprs.length - 1]);      for (int i = whereExprs.length - 2; i >= 0; i--) {	if (_indexExprs[i] != null)	  log.fine("index-" + i + ": " + _fromItems[i] + " " + _indexExprs[i]);	log.fine("where-" + i + ": " + _fromItems[i] + " " + whereExprs[i]);      }    }  }  private void orderFromItems(ArrayList<FromItem> costItems,			      ArrayList<Expr> topAndProduct)  {    FromItem []fromItems = getFromItems();    ArrayList<Expr> andProduct = new ArrayList<Expr>(topAndProduct);    for (int i = fromItems.length - 1; i >= 0; i--) {      costItems.clear();      for (int j = i + 1; j < fromItems.length; j++)	costItems.add(fromItems[j]);      int bestIndex = i;      long bestCost = Expr.COST_INVALID;      loop:      for (int j = 0; j <= i; j++) {	FromItem item = fromItems[j];	costItems.add(item);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?