Browse Source

work on PR 26459 - in progress (fighting with the tests and my environment)

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@675949 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 17 years ago
parent
commit
e1f227ae3d
3 changed files with 89 additions and 3 deletions
  1. +10
    -0
      docs/manual/CoreTasks/sql.html
  2. +61
    -3
      src/main/org/apache/tools/ant/taskdefs/SQLExec.java
  3. +18
    -0
      src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java

+ 10
- 0
docs/manual/CoreTasks/sql.html View File

@@ -204,6 +204,16 @@ and <b>abort</b> execution and transaction and fail task.</p>
</tr> </tr>
</table> </table>


<tr>
<td width="12%" valign="top">strictDelimiterMatching</td>
<td width="78%" valign="top">If false, delimiters will be searched
for in a case-insesitive manner (i.e. delimer="go" matches "GO")
and surrounding whitespace will be ignored (delimter="go" matches
"GO "). <em>Since Ant 1.8.0</em>.</td>
<td width="10%" valign="top">No, default <em>true</em></td>
</tr>
</table>

<h3>Parameters specified as nested elements</h3> <h3>Parameters specified as nested elements</h3>
<h4>transaction</h4> <h4>transaction</h4>
<p>Use nested <code>&lt;transaction&gt;</code> <p>Use nested <code>&lt;transaction&gt;</code>


+ 61
- 3
src/main/org/apache/tools/ant/taskdefs/SQLExec.java View File

@@ -40,6 +40,7 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Iterator; import java.util.Iterator;
import java.util.Locale;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Vector; import java.util.Vector;


@@ -198,6 +199,12 @@ public class SQLExec extends JDBCTask {
*/ */
private boolean rawBlobs; private boolean rawBlobs;


/**
* delimers must match in case and whitespace is significant.
* @since Ant 1.8.0
*/
private boolean strictDelimiterMatching = true;

/** /**
* Set the name of the SQL file to be run. * Set the name of the SQL file to be run.
* Required unless statements are enclosed in the build file * Required unless statements are enclosed in the build file
@@ -396,6 +403,16 @@ public class SQLExec extends JDBCTask {
this.rawBlobs = rawBlobs; this.rawBlobs = rawBlobs;
} }


/**
* If false, delimiters will be searched for in a case-insesitive
* manner (i.e. delimer="go" matches "GO") and surrounding
* whitespace will be ignored (delimter="go" matches "GO ").
* @since Ant 1.8.0
*/
public void setStrictDelimiterMatching(boolean b) {
strictDelimiterMatching = b;
}

/** /**
* Load the sql file and then execute it * Load the sql file and then execute it
* @throws BuildException on error. * @throws BuildException on error.
@@ -542,9 +559,9 @@ public class SQLExec extends JDBCTask {
if (!keepformat && line.indexOf("--") >= 0) { if (!keepformat && line.indexOf("--") >= 0) {
sql.append("\n"); sql.append("\n");
} }
if ((delimiterType.equals(DelimiterType.NORMAL) && StringUtils.endsWith(sql, delimiter))
|| (delimiterType.equals(DelimiterType.ROW) && line.equals(delimiter))) {
execSQL(sql.substring(0, sql.length() - delimiter.length()), out);
int lastDelimPos = lastDelimiterPosition(sql, line);
if (lastDelimPos > -1) {
execSQL(sql.substring(0, lastDelimPos), out);
sql.replace(0, sql.length(), ""); sql.replace(0, sql.length(), "");
} }
} }
@@ -833,4 +850,45 @@ public class SQLExec extends JDBCTask {
} }
} }
} }

public int lastDelimiterPosition(StringBuffer buf, String currentLine) {
if (strictDelimiterMatching) {
if (delimiterType.equals(DelimiterType.NORMAL)
&& StringUtils.endsWith(buf, delimiter)) {
return buf.length() - delimiter.length();
} else if (delimiterType.equals(DelimiterType.ROW)
&& currentLine.equals(delimiter)) {
return 0;
}
// no match
return -1;
} else {
String d = delimiter.trim().toLowerCase(Locale.US);
if (delimiterType.equals(DelimiterType.NORMAL)) {
// still trying to avoid wasteful copying, see
// StringUtils.endsWith
int endIndex = delimiter.length() - 1;
int bufferIndex = buf.length() - 1;
while (bufferIndex >= 0
&& Character.isWhitespace(buf.charAt(bufferIndex))) {
--bufferIndex;
}
if (bufferIndex < endIndex) {
return -1;
}
while (endIndex >= 0) {
if (buf.substring(bufferIndex, 1).toLowerCase(Locale.US)
.charAt(0) != d.charAt(endIndex)) {
return -1;
}
bufferIndex--;
endIndex--;
}
return bufferIndex;
} else {
return currentLine.trim().toLowerCase(Locale.US).equals(d)
? 0 : -1;
}
}
}
} }

+ 18
- 0
src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java View File

@@ -234,4 +234,22 @@ public class SQLExecTest extends TestCase {
} }
} }


public void testLastDelimiterPositionNormalModeStrict() {
SQLExec s = new SQLExec();
assertEquals(-1,
s.lastDelimiterPosition(new StringBuffer(), null));
assertEquals(-1,
s.lastDelimiterPosition(new StringBuffer("GO"), null));
assertEquals(-1,
s.lastDelimiterPosition(new StringBuffer("; "), null));
assertEquals(2,
s.lastDelimiterPosition(new StringBuffer("ab;"), null));
s.setDelimiter("GO");
assertEquals(-1,
s.lastDelimiterPosition(new StringBuffer("GO "), null));
assertEquals(-1,
s.lastDelimiterPosition(new StringBuffer("go"), null));
assertEquals(0,
s.lastDelimiterPosition(new StringBuffer("GO"), null));
}
} }

Loading…
Cancel
Save