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

📄 mysqlparser.java

📁 mysql集群
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* Generated By:JJTree&JavaCC: Do not edit this line. MysqlParser.java */
/*
 * Copyright (C) 2008 Struct chen <piratebase@sina.com>
 * 	This program 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 3 of the License, 
 * or (at your option) any later version. 
 * 
 * 	This program 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 General Public License for more details. 
 * 	You should have received a copy of the GNU General Public License along with this program; 
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.meidusa.amoeba.mysql.parser.sql;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.meidusa.amoeba.parser.dbobject.*;
import com.meidusa.amoeba.parser.expression.*;
import com.meidusa.amoeba.parser.function.*;
import com.meidusa.amoeba.parser.Parser;
import com.meidusa.amoeba.parser.statment.*;

import java.math.BigDecimal;
import java.util.Map;
import java.util.HashMap;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
import java.util.Calendar;

import java.sql.Connection;
import com.meidusa.amoeba.sqljep.function.Comparative;

public class MysqlParser implements/*@bgen(jjtree)*/ MysqlParserTreeConstants,com.meidusa.amoeba.parser.Parser, MysqlParserConstants {/*@bgen(jjtree)*/
  protected JJTMysqlParserState jjtree = new JJTMysqlParserState();private final static String metachars = "tnrbf\\\"";
        private final static String chars = "\t\n\r\b\f\\\"";

        private int parameterIndex = 0;
        private Map<String,Table> tableAliasMap = new HashMap<String,Table>();
        private Stack<Table> tableStack = new Stack<Table>();
        private Statment statment;
        private Schema defaultSchema;
        private Map<String,Function> functionMap;
        private static TimeConverter timeConverter = new TimeConverter();
        public void setFunctionMap(Map<String,Function> map){
                this.functionMap = map;
        }

        public void setDefaultSchema(Schema schema){
                defaultSchema = schema;
        }


    /**
     * main method to test parser
     */
    public static void main( String args[] )
        throws com.meidusa.amoeba.parser.ParseException
        {

        MysqlParser p = null ;
        if ( args.length < 1  ) {
            System.out.println("Reading from stdin") ;
            p = new MysqlParser(System.in) ;
        }
        else {
            try {
                p = new MysqlParser(new DataInputStream(
                                new FileInputStream(args[0]))) ;
            }
            catch (FileNotFoundException e) {
                System.out.println("File " + args[0] +
                                " not found. Reading from stdin") ;
                p = new MysqlParser(System.in) ;
            }
        } // else ends here

        if ( args.length > 0 ) {
            System.out.println(args[0]) ;
        }
        Statment statment = p.doParse();
        System.out.println(statment.getExpression());

    } // main ends here

   void jjtreeOpenNodeScope(Node n){
      //((SimpleNode)n).setFirstToken(getToken(1));
    }

   void jjtreeCloseNodeScope(Node n){
       // ((SimpleNode)n).setLastToken(token);
    }

        Function getFunction(String indent){
                if(functionMap == null){
                        return null;
                }else{
                        return functionMap.get(indent);
                }
                //return funMap.get(indent);
        }



        public Statment doParse() throws com.meidusa.amoeba.parser.ParseException{
                try{
                        Statment statment = this.parse();
                        if(statment != null){
                                statment.setParameterCount(parameterIndex);
                        }
                        return statment;
                }catch(Exception e){
                        throw new com.meidusa.amoeba.parser.ParseException(e);
                }
        }
        public Statment getParsedStatment(){
                return statment;
        }

        private Expression reverseExpression(boolean not,Expression expression){
                if(not){
                        if(expression != null){
                                return expression.reverse();
                        }else{
                                return null;
                        }
                }else{
                        return expression;
                }
        }

        private static String replaceEscape(String inputStr) {
                int len = inputStr.length();
                int p = 0;
                int i;

                StringBuilder output = new StringBuilder();

                while ((i = inputStr.indexOf('\\', p)) != -1) {
                        output.append(inputStr.substring(p, i));

                        if (i+1 == len) break;

                        // find metacharacter
                        char metac = inputStr.charAt(i+1);

                        // find the index of the metac
                        int k = metachars.indexOf(metac);
                        if (k == -1) {
                                // didn't find the metachar, leave sequence as found.
                                // This code should be unreachable if the parser
                                // is functioning properly because strings containing
                                // unknown escape characters should not be accepted.
                                output.append('\\');
                                output.append(metac);
                        } else {
                                // its corresponding true char
                                output.append(chars.charAt(k));
                        }

                        // skip over both escape character & metacharacter
                        p = i + 2;
                }

                // add the end of the input string to the output
                if (p < len)
                        output.append(inputStr.substring(p));
                return output.toString();
        }

  final public Statment parse() throws ParseException {
        Expression expression = null;
        Statment statment = null;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case K_DELETE:
      statment = DeleteQuery();
      break;
    case K_INSERT:
    case K_REPLACE:
      statment = InsertQuery();
      break;
    case K_SELECT:
    case 129:
      statment = SelectQuery();
      break;
    case K_UPDATE:
      statment = UpdateQuery();
      break;
    case K_SET:
      jj_consume_token(K_SET);
      statment = PropertySetQuery(new PropertyStatment());
      break;
    case K_USE:
      statment = SelectSchema();
      break;
    case K_COMMIT:
      jj_consume_token(K_COMMIT);
                                                                                     statment = new CommitStatment();
      break;
    case K_ROLLBACK:
      jj_consume_token(K_ROLLBACK);
                                                                                      statment = new RollbackStatment();
      break;
    case K_START_TRANSACTION:
      jj_consume_token(K_START_TRANSACTION);
                             statment = new StartTansactionStatment();
      break;
    default:
      jj_la1[0] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case 125:
      jj_consume_token(125);
      break;
    case 0:
      jj_consume_token(0);
      break;
    default:
      jj_la1[1] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
        if(statment instanceof DMLStatment){
                List<Table> list = new ArrayList<Table>();
                        for(Map.Entry<String,Table> entry : tableAliasMap.entrySet()){
                                if(!list.contains(entry.getValue())){
                                        list.add(entry.getValue());
                                }
                        }

                        Table[] tables = new Table[list.size()];
                        list.toArray(tables);
                        ((DMLStatment)statment).setTables(tables);
        }
        {if (true) return statment;}
    throw new Error("Missing return statement in function");
  }

  final public Statment SelectSchema() throws ParseException {
        Token t = null;
        String schema = null;
    jj_consume_token(K_USE);
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case STRING_LITERAL:
      token = jj_consume_token(STRING_LITERAL);
                                        schema = (t.image).substring(1,t.image.length()-1);
      break;
    case S_QUOTED_IDENTIFIER:
      t = jj_consume_token(S_QUOTED_IDENTIFIER);
                                                 schema = (t.image).substring(1,t.image.length()-1);
      break;
    case S_COMMA_IDENTIFIER:
      t = jj_consume_token(S_COMMA_IDENTIFIER);
                                                schema = (t.image).substring(1,t.image.length()-1);
      break;
    case IDENTIFIER:
      t = jj_consume_token(IDENTIFIER);
                                        schema = t.image;
      break;
    default:
      jj_la1[2] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
                PropertyStatment statment = new PropertyStatment();
                statment.addProperty("schema",new ConstantExpression(schema));
                {if (true) return statment;}
    throw new Error("Missing return statement in function");
  }

/*
Statment ShowQuery() :{
	Expression expression = null;
	Column column = null;
	Table table = null;
}{
	"SHOW"{return ShowStatment.STATMENT;}//[(<K_CREATE> [<K_TABLE>|<IDENTIFIER>])|<K_FULL>] column = ColumnName() [<K_FROM> table = TableReference()]{
		ColumnExpression columnExpression = new ColumnExpression();
		columnExpression.setColumn(column);
		ShowStatment show = new ShowStatment();
		show.setExpression(columnExpression);
		return show;
	}
}
	
*/
  final public Statment PropertySetQuery(PropertyStatment statment) throws ParseException {
        Expression expression = null;

        Token name = null;
        Token valueToken = null;
        Comparable value = null;
        String propertyName = null;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case K_OPTION:
    case K_SESSION:
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case K_SESSION:
        jj_consume_token(K_SESSION);
        break;
      case K_OPTION:
        jj_consume_token(K_OPTION);
        break;
      default:
        jj_la1[3] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      break;
    default:
      jj_la1[4] = jj_gen;
      ;
    }
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case K_TRANSACTION_ISOLATION_LEVEL:
      jj_consume_token(K_TRANSACTION_ISOLATION_LEVEL);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case K_TRANSACTION_READ_COMMITTED:
        jj_consume_token(K_TRANSACTION_READ_COMMITTED);
                                                               value=Connection.TRANSACTION_READ_COMMITTED;
        break;
      case K_TRANSACTION_READ_UNCOMMITTED:
        jj_consume_token(K_TRANSACTION_READ_UNCOMMITTED);
                                                                  value=Connection.TRANSACTION_READ_UNCOMMITTED;
        break;
      case K_TRANSACTION_SERIALIZABLE:
        jj_consume_token(K_TRANSACTION_SERIALIZABLE);
                                                              value=Connection.TRANSACTION_SERIALIZABLE;
        break;
      case K_TRANSACTION_REPEATABLE_READ:
        jj_consume_token(K_TRANSACTION_REPEATABLE_READ);
                                                                 value=Connection.TRANSACTION_REPEATABLE_READ;
        break;
      default:
        jj_la1[5] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
                                   propertyName = "TRANSACTIONISOLATION";
      break;
    case K_CLIENT_CHARSET:
      jj_consume_token(K_CLIENT_CHARSET);
                                            propertyName = "charset";
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case IDENTIFIER:
        valueToken = jj_consume_token(IDENTIFIER);
                                                            value = valueToken.image;
        break;
      case STRING_LITERAL:
        valueToken = jj_consume_token(STRING_LITERAL);
                                                                value = valueToken.image;
        break;
      case S_QUOTED_IDENTIFIER:
        valueToken = jj_consume_token(S_QUOTED_IDENTIFIER);
                                                                    value = valueToken.image;
        break;
      default:
        jj_la1[6] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      break;
    case IDENTIFIER:
      name = jj_consume_token(IDENTIFIER);
                                             propertyName = name.image;
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 126:
        jj_consume_token(126);
        name = jj_consume_token(IDENTIFIER);
                                                                                               propertyName = propertyName+"."+name.image;
        break;
      default:
        jj_la1[7] = jj_gen;
        ;
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case 127:
        jj_consume_token(127);
        break;
      default:
        jj_la1[8] = jj_gen;
        ;
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case K_NULL:
        valueToken = jj_consume_token(K_NULL);
                                              value = "null";
        break;
      case K_TRUE:
        valueToken = jj_consume_token(K_TRUE);
                                                value = Boolean.TRUE;
        break;
      case K_FALSE:
        valueToken = jj_consume_token(K_FALSE);
                                                 value = Boolean.FALSE;
        break;
      case IDENTIFIER:
        valueToken = jj_consume_token(IDENTIFIER);
                                                     value = valueToken.image;
        break;
      case INTEGER_LITERAL:
        valueToken = jj_consume_token(INTEGER_LITERAL);
                                                         value = Long.valueOf(valueToken.image);
        break;
      case STRING_LITERAL:
        valueToken = jj_consume_token(STRING_LITERAL);
                                                        value = valueToken.image;
        break;
      case S_QUOTED_IDENTIFIER:
        valueToken = jj_consume_token(S_QUOTED_IDENTIFIER);
                                                             value = valueToken.image;
        break;
      default:
        jj_la1[9] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      break;
    default:
      jj_la1[10] = jj_gen;
      jj_consume_token(-1);

⌨️ 快捷键说明

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