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

📄 expressionarithmetic.java

📁 java 数据库 功能强大 效率高 SmallSQL Database is a free DBMS library for the Java(tm) platform. It runs on
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* =============================================================
 * SmallSQL : a free Java DBMS library for the Java(tm) platform
 * =============================================================
 *
 * (C) Copyright 2004-2006, by Volker Berlin.
 *
 * Project Info:  http://www.smallsql.de/
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library 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. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ---------------
 * ExpressionArithmethic.java
 * ---------------
 * Author: Volker Berlin
 * 
 */
package smallsql.database;

import java.sql.*;



public class ExpressionArithmetic extends Expression {

    private Expression left;
    private Expression right;
    private Expression right2;
    private Expression[] inList;
    final private int operation;

    /**
     * Constructor for NOT, NEGATIVE, BIT_NOT, ISNULL and ISNOTNULL
     */
    ExpressionArithmetic( Expression left, int operation){
    	super(FUNCTION);
        this.left  = left;
        this.operation = operation;
        super.setParams( new Expression[]{ left });
    }

    ExpressionArithmetic( Expression left, Expression right, int operation){
		super(FUNCTION);
        this.left   = left;
        this.right  = right;
        this.operation = operation;
        super.setParams( new Expression[]{ left, right });
    }

    /**
     * Constructor for BETWEEN
     */
    ExpressionArithmetic( Expression left, Expression right, Expression right2, int operation){
		super(FUNCTION);
        this.left   = left;
        this.right  = right;
        this.right2 = right2;
        this.operation = operation;
        super.setParams( new Expression[]{ left, right, right2 });
    }
    
    /**
     * Constructor for IN
     */
    ExpressionArithmetic( Expression left, Expressions inList, int operation){
		super(FUNCTION);
        this.left   = left;
        this.operation = operation;
		Expression[] params;
        if(inList != null){
	        this.inList = inList.toArray();
	        params = new Expression[this.inList.length+1];
	        params[0] = left;
	        System.arraycopy(this.inList, 0, params, 1, this.inList.length);
        }else{
            //Occur with ExpressionInSelect, in this case the method isInList() is overridden
			params = new Expression[]{ left };
        }
        super.setParams( params );
    }
      
    
    private Expression convertExpressionIfNeeded( Expression expr, Expression other ){
        if(expr == null || other == null){
            return expr;
        }
        switch(expr.getDataType()){
        case SQLTokenizer.CHAR:
        case SQLTokenizer.NCHAR:
        case SQLTokenizer.BINARY:
            switch(other.getDataType()){
            case SQLTokenizer.VARCHAR:
            case SQLTokenizer.NVARCHAR:
            case SQLTokenizer.CLOB:
            case SQLTokenizer.NCLOB:
            case SQLTokenizer.LONGNVARCHAR:
            case SQLTokenizer.LONGVARCHAR:
            case SQLTokenizer.VARBINARY:
                ExpressionFunctionRTrim trim = new ExpressionFunctionRTrim();
                trim.setParams(new Expression[]{expr});
                return trim;
            case SQLTokenizer.CHAR:
            case SQLTokenizer.NCHAR:
            case SQLTokenizer.BINARY:
                if(other.getPrecision() > expr.getPrecision()){
                    return new ExpressionFunctionConvert(new ColumnExpression(other), expr, null );
                }
                break; 
            }
            break;
        }
        return expr;
    }
    

	final void setParamAt( Expression param, int idx){
		switch(idx){
			case 0:
				left = param;
				break;
			case 1:
                if(right != null){
                    right = param;
                }
				break;
			case 2:
                if(right != null){
                    right2 = param;
                }
				break;
		}
		if(inList != null && idx>0 && idx<=inList.length){
			inList[idx-1] = param;
		}
		super.setParamAt( param, idx );
	}


	/**
	 * Is used in GroupResult.
	 */
	public boolean equals(Object expr){
		if(!super.equals(expr)) return false;
		if(!(expr instanceof ExpressionArithmetic)) return false;
		if( ((ExpressionArithmetic)expr).operation != operation) return false;
		return true;
	}


	
    int getInt() throws java.lang.Exception {
        if(isNull()) return 0;
        int dataType = getDataType();
        switch(dataType){
            case SQLTokenizer.BIT:
            case SQLTokenizer.BOOLEAN:
				return getBoolean() ? 1 : 0;
            case SQLTokenizer.TINYINT:
            case SQLTokenizer.SMALLINT:
            case SQLTokenizer.INT:
				return getIntImpl();
            case SQLTokenizer.BIGINT:
                return (int)getLongImpl();
			case SQLTokenizer.REAL:
                return (int)getFloatImpl();
			case SQLTokenizer.FLOAT:
			case SQLTokenizer.DOUBLE:
            case SQLTokenizer.MONEY:
            case SQLTokenizer.SMALLMONEY:
            case SQLTokenizer.NUMERIC:
            case SQLTokenizer.DECIMAL:
                return (int)getDoubleImpl();
        }
        throw createUnspportedConversion( SQLTokenizer.INT);
    }
    
    
    private int getIntImpl() throws java.lang.Exception {
        switch(operation){
            case ADD:       return left.getInt() + right.getInt();
            case SUB:       return left.getInt() - right.getInt();
            case MUL:       return left.getInt() * right.getInt();
            case DIV:       return left.getInt() / right.getInt();
            case NEGATIVE:  return               - left.getInt();
            case MOD:		return left.getInt() % right.getInt();
            case BIT_NOT:   return               ~ left.getInt();
        }
        throw createUnspportedConversion( SQLTokenizer.INT);
    }
    
    
	long getLong() throws java.lang.Exception {
        if(isNull()) return 0;
        int dataType = getDataType();
        switch(dataType){
            case SQLTokenizer.BIT:
            case SQLTokenizer.BOOLEAN:
				return getBoolean() ? 1 : 0;
            case SQLTokenizer.TINYINT:
            case SQLTokenizer.SMALLINT:
            case SQLTokenizer.INT:
				return getIntImpl();
            case SQLTokenizer.BIGINT:
                return getLongImpl();
			case SQLTokenizer.REAL:
                return (long)getFloatImpl();
			case SQLTokenizer.FLOAT:
			case SQLTokenizer.DOUBLE:
            case SQLTokenizer.MONEY:
            case SQLTokenizer.SMALLMONEY:
            case SQLTokenizer.NUMERIC:
            case SQLTokenizer.DECIMAL:
                return (long)getDoubleImpl();
        }
		throw createUnspportedConversion( SQLTokenizer.LONG);
    }
	
	
	private long getLongImpl() throws java.lang.Exception {
        if(isNull()) return 0;
        switch(operation){
            case ADD: return left.getLong() + right.getLong();
            case SUB: return left.getLong() - right.getLong();
            case MUL: return left.getLong() * right.getLong();
            case DIV: return left.getLong() / right.getLong();
            case NEGATIVE:  return          - left.getLong();
            case MOD:		return left.getLong() % right.getLong();
            case BIT_NOT:   return          ~ right.getInt();
        }
		throw createUnspportedConversion( SQLTokenizer.LONG);
    }
	
	
    double getDouble() throws java.lang.Exception {
        if(isNull()) return 0;
        int dataType = getDataType();
        switch(dataType){
            case SQLTokenizer.BIT:
            case SQLTokenizer.BOOLEAN:
				return getBoolean() ? 1 : 0;
            case SQLTokenizer.TINYINT:
            case SQLTokenizer.SMALLINT:
            case SQLTokenizer.INT:
				return getIntImpl();
            case SQLTokenizer.BIGINT:
                return getLongImpl();
			case SQLTokenizer.REAL:
                return getFloatImpl();
			case SQLTokenizer.FLOAT:
			case SQLTokenizer.DOUBLE:
            case SQLTokenizer.MONEY:
            case SQLTokenizer.SMALLMONEY:
            case SQLTokenizer.NUMERIC:
            case SQLTokenizer.DECIMAL:
                return getDoubleImpl();
        }
		throw createUnspportedConversion( SQLTokenizer.DOUBLE);
    }
	
	
    private double getDoubleImpl() throws java.lang.Exception{
		if(operation == NEGATIVE)
			return getDoubleImpl(0, left.getDouble());
		return getDoubleImpl(left.getDouble(), right.getDouble());
	}
	
	
    private double getDoubleImpl( double lVal, double rVal) throws java.lang.Exception{
        switch(operation){
            case ADD: return lVal + rVal;
            case SUB: return lVal - rVal;
            case MUL: return lVal * rVal;
            case DIV: return lVal / rVal;
            case NEGATIVE: return - rVal;
            case MOD:		return lVal % rVal;
        }
        throw createUnspportedConversion( SQLTokenizer.DOUBLE);
    }
	

    float getFloat() throws java.lang.Exception {
        if(isNull()) return 0;
        int dataType = getDataType();
        switch(dataType){
            case SQLTokenizer.BIT:
            case SQLTokenizer.BOOLEAN:
				return getBoolean() ? 1 : 0;
            case SQLTokenizer.TINYINT:
            case SQLTokenizer.SMALLINT:
            case SQLTokenizer.INT:
				return getIntImpl();
            case SQLTokenizer.BIGINT:
                return getLongImpl();
			case SQLTokenizer.REAL:
                return getFloatImpl();
			case SQLTokenizer.FLOAT:
			case SQLTokenizer.DOUBLE:
            case SQLTokenizer.MONEY:
            case SQLTokenizer.SMALLMONEY:
            case SQLTokenizer.NUMERIC:
            case SQLTokenizer.DECIMAL:
                return (float)getDoubleImpl();
        }
		throw createUnspportedConversion( SQLTokenizer.DOUBLE);
    }
    
    
    private float getFloatImpl() throws java.lang.Exception {
        switch(operation){
            case ADD: return left.getFloat() + right.getFloat();
            case SUB: return left.getFloat() - right.getFloat();
            case MUL: return left.getFloat() * right.getFloat();
            case DIV: return left.getFloat() / right.getFloat();
            case NEGATIVE:  return           - left.getFloat();
            case MOD:		return left.getFloat() % right.getFloat();
        }
        throw createUnspportedConversion( SQLTokenizer.REAL );
    }
    

⌨️ 快捷键说明

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