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

📄 sqlparser.java

📁 java 数据库 功能强大 效率高 SmallSQL Database is a free DBMS library for the Java(tm) platform. It runs on
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    }
    
    
    final private CommandSelect select() throws SQLException{
		CommandSelect selCmd = singleSelect();
		SQLToken token = nextToken();
		   		
    	UnionAll union = null; 
	
		while(token != null && token.value == SQLTokenizer.UNION){
			if(union == null){
				union = new UnionAll();
				union.addDataSource(new ViewResult( con, selCmd ));
				selCmd = new CommandSelect(con.log);
				selCmd.setSource( union );
				DataSources from = new DataSources();
				from.add(union);
				selCmd.setFrom( from );
				selCmd.addColumnExpression( new ExpressionName("*") );
			}
			nextToken(MISSING_ALL);
			nextToken(MISSING_SELECT);
			union.addDataSource( new ViewResult( con, singleSelect() ) );
			token = nextToken();
		}
		if(token != null && token.value == SQLTokenizer.ORDER){
			order( selCmd );
		}else{
			previousToken();
		}
		return selCmd;
    }


    private Command delete() throws SQLException{
    	CommandDelete cmd = new CommandDelete(con.log);
    	nextToken(MISSING_FROM);
    	from(cmd);
		SQLToken token = nextToken();
		if(token != null){
			if(token.value != SQLTokenizer.WHERE)
				throw this.createSyntaxError(token, MISSING_WHERE);
			where(cmd);
		}
		return cmd;
    }


	private Command truncate() throws SQLException{
		CommandDelete cmd = new CommandDelete(con.log);
		nextToken(MISSING_TABLE);
		from(cmd);
		return cmd;
	}


    private Command insert() throws SQLException{
        SQLToken token = nextToken( MISSING_INTO );
        CommandInsert cmd = new CommandInsert( con.log, nextIdentifier() );

		int parthesisCount = 0;

		token = nextToken(MISSING_PARENTHESIS_VALUES_SELECT);
        if(token.value == SQLTokenizer.PARENTHESIS_L){
        	token = nextToken(MISSING_EXPRESSION);
        	if(token.value == SQLTokenizer.SELECT){
				parthesisCount++;
				cmd.noColumns = true;
        	}else{
				previousToken();
	            Expressions list = expressionParenthesisList(cmd);
	            for(int i=0; i<list.size(); i++){
	                cmd.addColumnExpression( list.get( i ) );
	            }
	            token = nextToken(MISSING_PARENTHESIS_VALUES_SELECT);
        	}
        }else cmd.noColumns = true;
        
Switch: while(true)
        switch(token.value){
        	case SQLTokenizer.VALUES:{
	            token = nextToken(MISSING_PARENTHESIS_L);
	            cmd.addValues( expressionParenthesisList(cmd) );
	            return cmd;
	        }
        	case SQLTokenizer.SELECT:
        		cmd.addValues( select() );
        		while(parthesisCount-- > 0){
        			nextToken(MISSING_PARENTHESIS_R);
        		}
        		return cmd;
        	case SQLTokenizer.PARENTHESIS_L:
        		token = nextToken(MISSING_PARENTHESIS_VALUES_SELECT);
        		parthesisCount++;
        		continue Switch;
        	default:
        		throw new Error();
        }
    }


    private Command update() throws SQLException{
		CommandUpdate cmd = new CommandUpdate(con.log);
		// read table name
		DataSources tables = new DataSources();
		cmd.setFrom( tables );
		cmd.setSource( rowSource( cmd, tables, 0 ) );
		
		SQLToken token = nextToken(MISSING_SET);
		while(true){
			token = nextToken();
			Expression dest = expressionSingle( cmd, token);
			if(dest.getType() != Expression.NAME) throw createSyntaxError( token, MISSING_IDENTIFIER );
			nextToken(MISSING_EQUALS);
			Expression src = expression(cmd, 0);
			cmd.addSetting( dest, src);
			token = nextToken();
			if(token == null) break;
			switch(token.value){
				case SQLTokenizer.WHERE:
					where(cmd);
					return cmd;				
				case SQLTokenizer.COMMA:
					continue;
				default: throw createSyntaxError( token, MISSING_WHERE_COMMA );
			}
		}
		return cmd;
    }


    private Command create() throws SQLException{
        while(true){
            SQLToken token = nextToken(COMMANDS_CREATE);
            switch(token.value){
                case SQLTokenizer.DATABASE:
                    return createDatabase();
                case SQLTokenizer.TABLE:
                    return createTable();
                case SQLTokenizer.VIEW:
                    return createView();
                case SQLTokenizer.INDEX:
                    return createIndex(false);
                case SQLTokenizer.PROCEDURE:
                    return createProcedure();
                case SQLTokenizer.UNIQUE:
                    do{
                        token = nextToken(COMMANDS_CREATE_UNIQUE);
                    }while(token.value == SQLTokenizer.INDEX);
                    return createIndex(true);
                case SQLTokenizer.NONCLUSTERED:
                case SQLTokenizer.CLUSTERED:
                    continue;
                default:
                    throw createSyntaxError( token, COMMANDS_CREATE );
            }
        }
    }
	

    private CommandCreateDatabase createDatabase() throws SQLException{
        SQLToken token = nextToken();
        if(token == null) throw createSyntaxError( token, MISSING_EXPRESSION );
        return new CommandCreateDatabase( con.log, token.getName(sql));
    }
	
    
    private CommandTable createTable() throws SQLException{
        String catalog;
        String tableName = catalog = nextIdentifier();
        tableName = nextIdentiferPart(tableName);
        if(tableName == catalog) catalog = null;
        CommandTable cmdCreate = new CommandTable( con.log, catalog, tableName, SQLTokenizer.CREATE );
        SQLToken token = nextToken( MISSING_PARENTHESIS_L );

        nextCol:
        while(true){
            token = nextToken( MISSING_EXPRESSION );
			
			String constraintName;
            if(token.value == SQLTokenizer.CONSTRAINT){
            	// reading a CONSTRAINT with name
		    	constraintName = nextIdentifier();
				token = nextToken( MISSING_KEYTYPE );
            }else{
				constraintName = null;
            }
			switch(token.value){
				case SQLTokenizer.PRIMARY:
				case SQLTokenizer.UNIQUE:
				case SQLTokenizer.FOREIGN:
					IndexDescription index = index(cmdCreate, token.value, tableName, constraintName, null);
                    if(token.value == SQLTokenizer.FOREIGN){
                        nextToken( MISSING_REFERENCES );
                        String pk = nextIdentifier();
                        Expressions expressions = new Expressions();
                        Strings columns = new Strings();
                        expressionDefList( cmdCreate, expressions, columns );
                        IndexDescription pkIndex = new IndexDescription( null, pk, SQLTokenizer.UNIQUE, expressions, columns);
                        ForeignKey foreignKey = new ForeignKey(pk, pkIndex, tableName, index);
                        cmdCreate.addForeingnKey(foreignKey);
                    }else{
                        cmdCreate.addIndex( index );
                    }
	
					token = nextToken( MISSING_COMMA_PARENTHESIS );
					switch(token.value){
						case SQLTokenizer.PARENTHESIS_R:
							return cmdCreate;
						case SQLTokenizer.COMMA:
							continue nextCol;
					}
            }
            // the token is a column name
			token = addColumn( token, cmdCreate );
            if(token == null){
                throw createSyntaxError(token, MISSING_COMMA_PARENTHESIS);
            }
            switch(token.value){
                case SQLTokenizer.PARENTHESIS_R:
                    return cmdCreate;
                case SQLTokenizer.COMMA:
                    continue nextCol;
                default:
                    throw createSyntaxError(token, MISSING_COMMA_PARENTHESIS);
            }
        }
    }
    
	
    /**
     * Parse a Column and add it to the Command. If the column is unique or primary
     * then an index is added.
     * @param token the SQLToken with the columnname
     * @return the token of the delimiter
     */
    private SQLToken addColumn(SQLToken token, CommandTable cmdCreate) throws SQLException{
        String colName = getIdentifier( token );
        Column col = datatype(false);
        col.setName( colName );

		token = nextToken();
        boolean nullableWasSet = false;
        boolean defaultWasSet = col.isAutoIncrement(); // with data type COUNTER already this value is set
        while(true){
            if(token == null){
                cmdCreate.addColumn( col );
                return null;
            }
            switch(token.value){
                case SQLTokenizer.PARENTHESIS_R:
                case SQLTokenizer.COMMA:
                    cmdCreate.addColumn( col );
                    return token;
                case SQLTokenizer.DEFAULT:
                    if(defaultWasSet) throw createSyntaxError( token, MISSING_COMMA_PARENTHESIS );
					int offset = token.offset + token.length;
                    token = nextToken();
                    if(token != null) offset = token.offset;
					previousToken();                    
					Expression expr = expression(cmdCreate, 0);
					SQLToken last = lastToken();
					int length = last.offset + last.length - offset;
					String def = new String( sql, offset, length );
                    col.setDefaultValue( expr, def );
                    defaultWasSet = true;
                    break;
                case SQLTokenizer.IDENTITY:
                    if(defaultWasSet) throw createSyntaxError( token, MISSING_COMMA_PARENTHESIS );
                    col.setAutoIncrement(true);
                    defaultWasSet = true;
                    break;
                case SQLTokenizer.NULL:
                    if(nullableWasSet) throw createSyntaxError( token, MISSING_COMMA_PARENTHESIS );
                    //col.setNullable(true); ist bereits default
                    nullableWasSet = true;
                    break;
                case SQLTokenizer.NOT:
                    if(nullableWasSet) throw createSyntaxError( token, MISSING_COMMA_PARENTHESIS );
                    token = nextToken( MISSING_NULL );
                    col.setNullable(false);
                    nullableWasSet = true;
                    break;
				case SQLTokenizer.PRIMARY:
				case SQLTokenizer.UNIQUE:
					IndexDescription index = index(cmdCreate, token.value, cmdCreate.name, null, colName);
					cmdCreate.addIndex( index );
					break;
                default:
                    throw createSyntaxError(token, MISSING_OPTIONS_DATATYPE);
            }
            token = nextToken();
        }
    }
    

	/**
	 * Parse construct like:<br>
	 * <li>PRIMARY KEY (col1)
	 * <li>UNIQUE (col1, col2)
	 * <li>FOREIGN KEY REFERENCES ref_table(col1)
	 * @param cmd
	 * @param constraintType one of SQLTokenizer.PRIMARY, SQLTokenizer.UNIQUE or SQLTokenizer.FOREIGN.
	 * @param if it a constrain of the current column else null
	 * @return a new IndexDescription
	 */
	private IndexDescription index(Command cmd, int constraintType, String tableName, String contrainName, String columnName) throws SQLException{
		if(constraintType != SQLTokenizer.UNIQUE) nextToken( MISSING_KEY );
		SQLToken token = nextToken();
        if(token != null){
    		switch(token.value){
    			case SQLTokenizer.CLUSTERED:
    			case SQLTokenizer.NONCLUSTERED:
    				// ignoring, this tokens form MS SQL Server are ignored
    				break;
                default:
                    previousToken();
    		}
        }else{
            previousToken();
        }
		Strings columns = new Strings();
		Expressions expressions = new Expressions();
		if(columnName != null){
			//Constraint for a single column together with the column defination
			columns.add(columnName);
			expressions.add(new ExpressionName(columnName));
		}else{
			//Constraint as addition definition
            expressionDefList( cmd, expressions, columns );
		}
		return new IndexDescription( contrainName, tableName, constraintType, expressions, columns);
	}


    /**
     * Read a DataTpe description. This is used for CREATE TABLE and CONVERT function. 
     * @param isEscape true then the datatypes start with "SQL_". This is used for the Escape Syntax.
     */

⌨️ 快捷键说明

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