*** SQLExec.java.original Thu Aug 19 10:56:21 2004 --- SQLExec.java Fri Sep 10 10:13:47 2004 *************** *** 74,79 **** --- 74,85 ---- */ public class SQLExec extends JDBCTask { + private static final String BEGIN = "BEGIN"; + private static final String END = "END"; + + private boolean inBlockStatement; + private int blockDepth; + /** * delimiters we support, "normal" and "row" */ *************** *** 429,450 **** BufferedReader in = new BufferedReader(reader); while ((line = in.readLine()) != null) { if (!keepformat) { line = line.trim(); } line = getProject().replaceProperties(line); - if (!keepformat) { if (line.startsWith("//")) { continue; } if (line.startsWith("--")) { continue; } StringTokenizer st = new StringTokenizer(line); if (st.hasMoreTokens()) { String token = st.nextToken(); if ("REM".equalsIgnoreCase(token)) { continue; } } } --- 435,470 ---- BufferedReader in = new BufferedReader(reader); while ((line = in.readLine()) != null) { + // flag whether the current line is a comment + // or not. + boolean isComment = false; + if (!keepformat) { line = line.trim(); } line = getProject().replaceProperties(line); if (line.startsWith("//")) { + if (!keepformat) { continue; + }else{ + isComment = true; + } } if (line.startsWith("--")) { + if (!keepformat) { continue; + }else{ + isComment = true; + } } StringTokenizer st = new StringTokenizer(line); if (st.hasMoreTokens()) { String token = st.nextToken(); if ("REM".equalsIgnoreCase(token)) { + if (!keepformat) { continue; + }else{ + isComment = true; } } } *************** *** 463,470 **** sql.append("\n"); } } if ((delimiterType.equals(DelimiterType.NORMAL) ! && sql.toString().endsWith(delimiter)) || (delimiterType.equals(DelimiterType.ROW) && line.equals(delimiter))) { --- 483,497 ---- sql.append("\n"); } } + + // determine if a block statement has started or ended. + if(!isComment){ + containsBlockStatement(line); + } + if ((delimiterType.equals(DelimiterType.NORMAL) ! && sql.toString().endsWith(delimiter) ! && !inBlockStatement) || (delimiterType.equals(DelimiterType.ROW) && line.equals(delimiter))) { *************** *** 479,484 **** --- 506,548 ---- } } + /** + * Determines if the supplied line starts or ends + * a block statement. + *

+ * The start of a block statement is considered to be + * a line that trims to "BEGIN" or "BEGIN" + delimiter (case insensitive) + *
+ * The end of a block statement is considered to be + * a line that trims to "END" or "END" + delimiter (case insensitive) + * + * @param line The line to test. + */ + protected void containsBlockStatement (String line) + { + if(line != null && line.trim().length() > 0){ + // trim the line and remove all spaces to simplify string compare. + String ln = line.trim().replaceAll("\\s\\+", ""); + if(ln.equalsIgnoreCase(BEGIN) || ln.equalsIgnoreCase(BEGIN + delimiter)){ + // if a new block is beginning then increment the depth. + if(inBlockStatement){ + blockDepth++; + }else{ + inBlockStatement = true; + } + } + if(ln.equalsIgnoreCase(END) || ln.equalsIgnoreCase(END + delimiter)){ + // this end statement is in an outer block, so decrement the depth + // and continue in the outer block. + if(blockDepth > 0){ + blockDepth--; + }else{ + inBlockStatement = false; + } + } + } + } + /** * Exec the sql statement.