⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commandselect.java

📁 這是一個油Java實作的資料庫系統 是個入門的好材料
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */
    void beforeFirst() throws Exception{
		from.beforeFirst();
    }
    
    
	/**
	 * Is used from ResultSet.isBeforeFirst().
	 */
	boolean isBeforeFirst() throws SQLException{
		return from.isBeforeFirst();
	}
    

	/**
	 * Is used from ResultSet.isFirst().
	 */
	boolean isFirst() throws SQLException{
		return from.isFirst();
	}
    

	/**
     * Is used from ResultSet.first().
     */
    boolean first() throws Exception{
		return from.first();
    }


	/**
	 * Is used from ResultSet.previous().
	 */
	boolean previous() throws Exception{
		return from.previous();
	}


	/**
	 * move to the next row.
	 * @return true if the next row valid
	 * @throws Exception
	 */
    boolean next() throws Exception{
        if(maxRows >= 0 && from.getRow() >= maxRows){
        	from.afterLast();
        	return false;
        }
		return from.next();
    }


	/**
	 * Is used from ResultSet.last().
	 */
	final boolean last() throws Exception{
		if(maxRows >= 0){
            if(maxRows == 0){
                from.beforeFirst();
                return false;
            }
			return from.absolute(maxRows);
		}
		return from.last();
	}


	/**
	 * Is used from ResultSet.afterLast().
	 */
	final void afterLast() throws Exception{
		from.afterLast();
	}


	/**
	 * Is used from ResultSet.isLast().
	 */
	boolean isLast() throws Exception{
		return from.isLast();
	}
    

	/**
	 * Is used from ResultSet.isAfterLast().
	 */
	boolean isAfterLast() throws Exception{
		return from.isAfterLast();
	}
    

	/**
	 * Is used from ResultSet.absolute().
	 */
	final boolean absolute(int row) throws Exception{
		return from.absolute(row);
	}


	/**
	 * Is used from ResultSet.relative().
	 */
	final boolean relative(int rows) throws Exception{
		return from.relative(rows);
	}


	/**
	 * Is used from ResultSet.afterLast().
	 */
	final int getRow() throws Exception{
		int row = from.getRow();
		if(maxRows >= 0 && row > maxRows) return 0;
		return row;
	}


	final void updateRow(SSConnection con, Expression[] newRowSources) throws SQLException{
		int savepoint = con.getSavepoint();
		try{
			//loop through all tables of this ResultSet 
			for(int t=0; t<tables.size(); t++){
				TableViewResult result = TableViewResult.getTableViewResult( tables.get(t) );
				TableView table = result.getTableView();
				Columns tableColumns = table.columns;
				int count = tableColumns.size();
				
				// order the new Values after it position in the table
				Expression[] updateValues = new Expression[count];
				boolean isUpdateNeeded = false;
				for(int i=0; i<columnExpressions.size(); i++){
					Expression src = newRowSources[i];
					if(src != null && (!(src instanceof ExpressionValue) || !((ExpressionValue)src).isEmpty())){	
						Expression col = columnExpressions.get(i);
						if(!col.isDefinitelyWritable())
							throw SmallSQLException.create(Language.COL_READONLY, new Integer(i));
						ExpressionName exp = (ExpressionName)col;
						if(table == exp.getTable()){
							updateValues[exp.getColumnIndex()] = src;
							isUpdateNeeded = true;
							continue;
						}
					}
				}
				
				// save the new values if there are new value for this table
				if(isUpdateNeeded){
					result.updateRow(updateValues);
				}
			}
		}catch(Throwable e){
			con.rollback(savepoint);
			throw SmallSQLException.createFromException(e);
		}finally{
			if(con.getAutoCommit()) con.commit();
		}
	}
	
	final void insertRow(SSConnection con, Expression[] newRowSources) throws SQLException{
		if(tables.size() > 1)
			throw SmallSQLException.create(Language.JOIN_INSERT);
		if(tables.size() == 0)
			throw SmallSQLException.create(Language.INSERT_WO_FROM);
		
		int savepoint = con.getSavepoint();
		try{
			TableViewResult result = TableViewResult.getTableViewResult( tables.get(0) );
			TableView table = result.getTableView();
			Columns tabColumns = table.columns;
			int count = tabColumns.size();
					
			// order the new Values after it position in the table
			Expression[] updateValues = new Expression[count];
			if(newRowSources != null){
				for(int i=0; i<columnExpressions.size(); i++){
					Expression src = newRowSources[i];
					if(src != null && (!(src instanceof ExpressionValue) || !((ExpressionValue)src).isEmpty())){	
						Expression rsColumn = columnExpressions.get(i); // Column of the ResultSet
						if(!rsColumn.isDefinitelyWritable())
							throw SmallSQLException.create(Language.COL_READONLY, new Integer(i));
						ExpressionName exp = (ExpressionName)rsColumn;
						if(table == exp.getTable()){
							updateValues[exp.getColumnIndex()] = src;
							continue;
						}
					}
					updateValues[i] = null;
				}
			}
					
			// save the new values if there are new value for this table
			result.insertRow(updateValues);
		}catch(Throwable e){
			con.rollback(savepoint);
			throw SmallSQLException.createFromException(e);
		}finally{
			if(con.getAutoCommit()) con.commit();
		}
	}
	
	final void deleteRow(SSConnection con) throws SQLException{
		int savepoint = con.getSavepoint();
		try{
			if(tables.size() > 1)
				throw SmallSQLException.create(Language.JOIN_DELETE);
			if(tables.size() == 0)
				throw SmallSQLException.create(Language.DELETE_WO_FROM);
			TableViewResult.getTableViewResult( tables.get(0) ).deleteRow();
		}catch(Throwable e){
			con.rollback(savepoint);
			throw SmallSQLException.createFromException(e);
		}finally{
			if(con.getAutoCommit()) con.commit();
		}
	}
	
	
	/**
	 * The returning index start at 0.
	 */
	public int findColumn(String columnName) throws SQLException {
		Expressions columns = columnExpressions;
		// FIXME performance
		for(int i=0; i<columns.size(); i++){
			if(columnName.equalsIgnoreCase(columns.get(i).getAlias()))
				return i;
		}
		throw SmallSQLException.create(Language.COL_MISSING, columnName);
	}
	
	
	/**
	 * Set if the keyword DISTINCT occur in the SELECT expression. 
	 */
	final void setDistinct(boolean distinct){
		this.isDistinct = distinct;
	}
	
	
	/**
	 * Set the RowSource expression from the FROM clause. 
	 * The Simples case is only a Table (TableResult)
	 */
    final void setSource(RowSource join){
        this.from = join;
    }

	/**
	 * List of all Tables and Views. 
	 * This is needed to replace the table aliases in the columnExpressions with the real sources.
	 */
    final void setTables( DataSources from ){
        this.tables = from;
    }

	/**
	 * Is used from CommandSelect, CommandDelete and CommandUpdate
	 * @param where
	 */
	final void setWhere( Expression where ){
		this.where = where;
	}

	final void setGroup(Expressions group){
        this.groupBy = group;
    }

	final void setHaving(Expression having){
        this.having = having;
    }

	final void setOrder(Expressions order){
        this.orderBy = order;
    }
    

	final void setMaxRows(int max){
		maxRows = max;
	}
    
    
    final int getMaxRows(){
        return maxRows;
    }
}

⌨️ 快捷键说明

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