query.java

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

JAVA
821
字号
	for (int k = 0; k < fromItems.length; k++) {	  if (! fromItems[k].isValid(costItems)) {	    costItems.remove(costItems.size() - 1);	    continue loop;	  }	}	long cost = Long.MAX_VALUE;	for (int k = 0; k < andProduct.size(); k++) {	  Expr expr = andProduct.get(k);	  long subCost = expr.cost(costItems);	  if (Expr.COST_INVALID <= subCost) {	    costItems.remove(costItems.size() - 1);	    continue loop;	  }	  if (subCost < cost)	    cost = subCost;	}	costItems.remove(costItems.size() - 1);	if (cost < bestCost) {	  bestCost = cost;	  bestIndex = j;	}      }      FromItem tempItem = fromItems[i];      fromItems[i] = fromItems[bestIndex];      fromItems[bestIndex] = tempItem;      costItems.add(fromItems[i]);      for (int k = andProduct.size() - 1; k >= 0; k--) {	Expr expr = andProduct.get(k);	long subCost = expr.cost(costItems);	if (subCost < Expr.COST_NO_TABLE) {	  andProduct.remove(k);	}      }    }  }  private String logWhere()  {    CharBuffer cb = CharBuffer.allocate();    cb.append("[");    for (int i = 0; i < _whereExprs.length; i++) {      if (i != 0)	cb.append(", ");      if (_whereExprs[i] != null)	cb.append(_whereExprs[i]);    }    cb.append("]");    return cb.close();  }  /**   * Returns a bound expression for the specified table.column.   */  protected Expr bind(String tableName, String columnName)    throws SQLException  {    FromItem []fromItems = getFromItems();    if (tableName == null) {      if ("resin_oid".equals(columnName))	return new OidExpr(fromItems[0].getTable(), 0);      for (int i = 0; i < fromItems.length; i++) {	Table table = fromItems[i].getTable();	int columnIndex = table.getColumnIndex(columnName);	if (columnIndex >= 0) {	  Column column = table.getColumn(columnName);	  return new IdExpr(fromItems[i], column);	}      }      Expr expr = bindParent(tableName, columnName);      if (expr != null) {	return expr;      }      throw new SQLException(L.l("`{0}' is an unknown column.", columnName));    }    else {      for (int i = 0; i < fromItems.length; i++) {	if (tableName.equals(fromItems[i].getName())) {	  Table table = fromItems[i].getTable();	  if ("resin_oid".equals(columnName))	    return new OidExpr(table, i);	  int columnIndex = table.getColumnIndex(columnName);	  if (columnIndex < 0) {	    Expr expr = bindParent(tableName, columnName);	    if (expr != null)	      return expr;	    throw new SQLException(L.l("`{0}' is an unknown column in \n  {1}.",				       columnName, _sql));	  }	  Column column = table.getColumn(columnName);	  return new IdExpr(fromItems[i], column);	}      }      Expr expr = bindParent(tableName, columnName);      if (expr != null)	return expr;      throw new SQLException(L.l("`{0}' is an unknown table.\n{1}",				 tableName, getSQL()));    }  }  /**   * Binds as a subselect.   */  private Expr bindParent(String tableName, String columnName)    throws SQLException  {    if (_parent != null) {      Expr expr = _parent.bind(tableName, columnName);      if (expr != null) {	SubSelectParamExpr paramExpr;	paramExpr = new SubSelectParamExpr(this, expr, _paramExprs.size());	_paramExprs.add(paramExpr);	return paramExpr;      }    }    return null;  }  /**   * Clears the paramters.   */  public void clearParameters()  {    for (int i = 0; i < _params.length; i++)      _params[i].clear();  }  /**   * Sets the indexed parameter as a boolean.   */  public void setBoolean(int index, boolean value)  {    if (index < 1 || _params.length < index) {      throw new IllegalArgumentException(L.l("{0}: column '{1}' out of bounds for {2}",					     this, index, _sql));    }        _params[index - 1].setBoolean(value);  }  /**   * Sets the indexed parameter as a string.   */  public void setString(int index, String value)  {    if (index < 1 || _params.length < index) {      throw new IllegalArgumentException(L.l("{0}: column '{1}' out of bounds for {2}",					     this, index, _sql));    }        _params[index - 1].setString(value);  }  /**   * Sets the indexed parameter as a long.   */  public void setLong(int index, long value)  {    if (index < 1 || _params.length < index) {      throw new IllegalArgumentException(L.l("{0}: column '{1}' out of bounds for {2}",					     this, index, _sql));    }        _params[index - 1].setLong(value);  }  /**   * Sets the indexed parameter as a double.   */  public void setDouble(int index, double value)  {    if (index < 1 || _params.length < index) {      throw new IllegalArgumentException(L.l("{0}: column '{1}' out of bounds for {2}",					     this, index, _sql));    }        _params[index - 1].setDouble(value);  }  /**   * Sets the indexed parameter as a date value.   */  public void setDate(int index, long value)  {    if (index < 1 || _params.length < index) {      throw new IllegalArgumentException(L.l("{0}: column '{1}' out of bounds for {2}",					     this, index, _sql));    }        _params[index - 1].setDate(value);  }  /**   * Sets the indexed parameter as a binary stream   */  public void setBinaryStream(int index, InputStream is, int length)  {    if (index < 1 || _params.length < index) {      throw new IllegalArgumentException(L.l("{0}: column '{1}' out of bounds for {2}",					     this, index, _sql));    }        _params[index - 1].setBinaryStream(is, length);  }  /**   * Sets the indexed parameter as a bytes   */  public void setBytes(int index, byte []bytes)  {    if (index < 1 || _params.length < index) {      throw new IllegalArgumentException(L.l("{0}: column '{1}' out of bounds for {2}",					     this, index, _sql));    }        _params[index - 1].setBytes(bytes);  }  /**   * Executes the query.   */  abstract public void execute(QueryContext queryCtx, Transaction xa)    throws SQLException;  /**   * Starts the query.   */  protected boolean start(TableIterator []rows, int rowLength,			  QueryContext queryContext, Transaction xa)    throws SQLException  {    try {      Expr []whereExprs = _whereExprs;      // Test the constant expression      if (whereExprs == null || whereExprs[rowLength] == null) {      }      else if (! whereExprs[rowLength].isSelect(queryContext)) {	return false;      }      if (rowLength == 0)	return true;      for (int i = rowLength - 1; i >= 0; i--) {	TableIterator row = rows[i];	RowIterateExpr iterExpr = _indexExprs[i];	if (! iterExpr.init(queryContext, row)) {	  return false;	}	// XXX: check to make sure others actually lock this properly	//if (! xa.isAutoCommit())	//  xa.lockRead(row.getTable().getLock());      }      return (initBlockRow(rowLength - 1, rows, queryContext)	      || nextBlock(rowLength - 1, rows, rowLength, queryContext));    } catch (IOException e) {      throw new SQLExceptionWrapper(e);    }  }  /**   * Returns the next tuple from the query.   */  protected boolean nextTuple(TableIterator []rows, int rowLength,			      QueryContext queryContext, Transaction xa)    throws SQLException  {    try {      if (rowLength == 0)	return false;      RowIterateExpr []indexExprs = _indexExprs;      Expr []whereExprs = _whereExprs;      for (int i = 0; i < rowLength; i++) {	TableIterator table = rows[i];	RowIterateExpr indexExpr = indexExprs[i];	Expr whereExpr = whereExprs == null ? null : whereExprs[i];	while (indexExpr.nextRow(queryContext, table)) {	  if (whereExpr == null || whereExpr.isSelect(queryContext)) {	    if (i == 0 || initBlockRow(i - 1, rows, queryContext)) {	      return true;	    }	  }	}      }      return nextBlock(rowLength - 1, rows, rowLength, queryContext);    } catch (IOException e) {      throw new SQLExceptionWrapper(e);    }  }  /**   * Initialize this row and all previous rows within this block group.   */  private boolean nextBlock(int i,			    TableIterator []rows,			    int rowLength,			    QueryContext queryContext)    throws IOException, SQLException  {    TableIterator rowIter = rows[i];    RowIterateExpr iterExpr = _indexExprs[i];    while (true) {      if (i > 0 && nextBlock(i - 1, rows, rowLength, queryContext)) {	return true;      }      if (! iterExpr.nextBlock(queryContext, rowIter)) {	return false;      }      if (! iterExpr.allowChildRowShift(queryContext, rows[i]))	return false;      for (int j = i - 1; j >= 0; j--) {	if (! iterExpr.init(queryContext, rows[j]))	  return false;      }      if (initBlockRow(rowLength - 1, rows, queryContext))	return true;    }  }  /**   * Initialize this row and all previous rows within this block group.   */  private boolean initBlockRow(int i,			       TableIterator []rows,			       QueryContext queryContext)    throws IOException, SQLException  {    RowIterateExpr iterExpr = _indexExprs[i];    Expr []whereExprs = _whereExprs;    Expr expr = whereExprs == null ? null : whereExprs[i];    TableIterator rowIter = rows[i];    if (! iterExpr.initRow(queryContext, rowIter)) {      return false;    }    while (expr != null && ! expr.isSelect(queryContext)	   || i > 0 && ! initBlockRow(i - 1, rows, queryContext)) {      if (! iterExpr.nextRow(queryContext, rowIter)) {	return false;      }    }    return true;  }  /**   * Frees any blocks for the rows.   */  protected void freeRows(TableIterator []rows, int rowLength)  {    for (rowLength--; rowLength >= 0; rowLength--) {      if (rows[rowLength] != null)	rows[rowLength].free();    }  }}

⌨️ 快捷键说明

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