1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.impl.sql.compile.QueryTreeNode; 25 import org.apache.derby.iapi.sql.compile.Parser; 26 import org.apache.derby.iapi.sql.Statement; 27 import org.apache.derby.iapi.sql.compile.CompilerContext; 28 29 import org.apache.derby.iapi.reference.SQLState; 30 import org.apache.derby.iapi.error.StandardException; 31 import org.apache.derby.iapi.services.sanity.SanityManager; 32 33 public class ParserImpl implements Parser 34 { 35 56 static final int LARGE_TOKEN_SIZE = 128; 57 58 59 protected Object cachedParser; 60 protected Object cachedTokenManager; 61 62 protected CharStream charStream; 63 protected String SQLtext; 64 65 protected final CompilerContext cc; 66 67 70 71 public ParserImpl(CompilerContext cc) 72 { 73 this.cc = cc; 74 } 75 76 public QueryTreeNode parseStatement(String statementSQLText) 77 throws StandardException 78 { 79 return parseStatement(statementSQLText, (Object [])null); 80 } 81 82 86 protected Object getTokenManager() 87 { 88 89 SQLParserTokenManager tm = (SQLParserTokenManager) cachedTokenManager; 90 if (tm == null) { 91 tm = new SQLParserTokenManager(charStream); 92 cachedTokenManager = tm; 93 } else { 94 tm.ReInit(charStream); 95 } 96 return tm; 97 } 98 99 102 protected Object getParser() 103 { 104 SQLParserTokenManager tm = (SQLParserTokenManager) getTokenManager(); 105 106 SQLParser p = (SQLParser) cachedParser; 107 if (p == null) { 108 p = new SQLParser(tm); 109 p.setCompilerContext(cc); 110 cachedParser = p; 111 } else { 112 p.ReInit(tm); 113 } 114 return p; 115 } 116 117 128 129 public QueryTreeNode parseStatement(String statementSQLText, Object [] paramDefaults) 130 throws StandardException 131 { 132 133 java.io.Reader sqlText = new java.io.StringReader (statementSQLText); 134 135 136 if (charStream == null) 137 { 138 charStream = new UCode_CharStream(sqlText, 1, 1, LARGE_TOKEN_SIZE); 139 } 140 else 141 { 142 charStream.ReInit(sqlText, 1, 1, LARGE_TOKEN_SIZE); 143 } 144 145 146 SQLtext = statementSQLText; 147 148 149 try 150 { 151 return parseGoalProduction( statementSQLText, paramDefaults); 152 } 153 catch (ParseException e) 154 { 155 throw StandardException.newException(SQLState.LANG_SYNTAX_ERROR, e.getMessage()); 156 } 157 catch (TokenMgrError e) 158 { 159 throw StandardException.newException(SQLState.LANG_LEXICAL_ERROR, e.getMessage()); 160 } 161 } 162 163 175 protected QueryTreeNode parseGoalProduction( String statementSQLText, 176 Object [] paramDefaults) 177 throws ParseException, TokenMgrError, StandardException 178 { 179 SQLParser p = (SQLParser) getParser(); 180 return p.Statement( statementSQLText, paramDefaults); 181 } 183 189 public String getSQLtext() 190 { return SQLtext; } 191 } 192 | Popular Tags |