1 16 package scriptella.jdbc; 17 18 import scriptella.configuration.ConfigurationException; 19 import scriptella.expression.PropertiesSubstitutor; 20 import scriptella.util.IOUtils; 21 import scriptella.util.StringUtils; 22 23 import java.io.IOException ; 24 import java.io.Reader ; 25 import java.util.regex.Matcher ; 26 27 28 59 public class SqlParserBase { 60 65 public void parse(final Reader reader) { 66 parse(new SqlReaderTokenizer(reader)); 67 } 68 69 public void parse(final SqlTokenizer tok) { 70 try { 71 for (String s;(s=tok.nextStatement())!=null;) { 72 handleStatement(s, tok.getInjections()); 73 } 74 } catch (IOException e) { 75 throw new ConfigurationException("Failed to read element content", e); 76 } finally { 77 IOUtils.closeSilently(tok); 78 } 79 80 } 81 82 private final Matcher m = PropertiesSubstitutor.PROP_PTR.matcher(""); 83 private final Matcher extM = PropertiesSubstitutor.EXPR_PTR.matcher(""); 84 private final StringBuilder tmpBuf = new StringBuilder (); 85 86 private void handleStatement(final String sql, 87 final int[] injections) { 88 if (StringUtils.isAsciiWhitespacesOnly(sql)) { 89 return; 90 } 91 92 if (injections != null && injections.length>0) { 93 m.reset(sql); 94 extM.reset(sql); 95 96 tmpBuf.setLength(0); int lastPos = 0; 98 99 for (int index : injections) { 100 int ind = index + 1; 101 Matcher found = null; 102 boolean expr = false; 103 if (m.find(ind) && (m.start() == ind)) { found = m; 105 106 } else if (extM.find(ind) && (extM.start() == ind)) { found = extM; 108 expr = true; 109 } 110 if (found != null) { 111 boolean jdbcParam = sql.charAt(index) == '?'; 113 tmpBuf.append(sql.substring(lastPos, index)); 114 lastPos = found.end(); 115 tmpBuf.append(handleParameter(found.group(1), expr, jdbcParam)); 116 } 117 118 } 119 120 if (lastPos < sql.length()) { tmpBuf.append(sql.substring(lastPos, sql.length())); 122 } 123 statementParsed(tmpBuf.toString()); 124 } else { 125 statementParsed(sql); 126 } 127 128 129 } 130 131 140 protected String handleParameter(final String name, final boolean expression, final boolean jdbcParam) { 141 return expression ? ((jdbcParam ? "?{" : "${") + name + '}') : ((jdbcParam ? "?{" : "$") + name); 142 } 143 144 149 protected void statementParsed(final String sql) { 150 } 151 152 } 153 | Popular Tags |