Skip to content

Commit ddd1a9a

Browse files
committed
mybatis #355 PL/SQL is not fully supported
declarative changing of delimiter support using standard SQL comment characters, e.g.: --/@delimiter / --/@delimiter ;
1 parent e99abe6 commit ddd1a9a

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

src/main/java/org/apache/ibatis/jdbc/ScriptRunner.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,12 @@ private void checkForMissingLineTerminator(StringBuilder command) {
186186

187187
private StringBuilder handleLine(StringBuilder command, String line) throws SQLException, UnsupportedEncodingException {
188188
String trimmedLine = line.trim();
189-
if (trimmedLine.toUpperCase().startsWith("DELIMITER")) {
190-
delimiter = trimmedLine.substring(10).trim();
191-
return command;
192-
}
193189
if (lineIsComment(trimmedLine)) {
190+
final String cleanedString = trimmedLine.substring(2).trim();
191+
if(cleanedString.toUpperCase().startsWith("//@DELIMITER")) {
192+
delimiter = cleanedString.substring(13,14);
193+
return command;
194+
}
194195
println(trimmedLine);
195196
} else if (commandReadyToExecute(trimmedLine)) {
196197
command.append(line.substring(0, line.lastIndexOf(delimiter)));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- Copyright 2009-2012 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
drop table signon;
18+
19+
--//@DELIMITER /
20+
create table signon (
21+
username varchar(25) not null,
22+
password varchar(25) not null,
23+
constraint pk_signon primary key (username)
24+
)
25+
/
26+
--//@DELIMITER ;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Copyright 2009-2012 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
drop table signon;
18+
19+
--//@DELIMITER /
20+
create table signon (
21+
username varchar(25) not null,
22+
password varchar(25) not null,
23+
constraint pk_signon primary key (username)
24+
);
25+
--//@DELIMITER ;

src/test/java/org/apache/ibatis/jdbc/ScriptRunnerTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,48 @@ public void commentAferStatementDelimiterShouldNotCauseRunnerFail() throws Excep
121121
fail(e.getMessage());
122122
}
123123
}
124+
125+
@Test
126+
public void shouldReturnWarningIfNotTheCurrentDelimiterUsed() throws Exception {
127+
DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
128+
Connection conn = ds.getConnection();
129+
ScriptRunner runner = new ScriptRunner(conn);
130+
runner.setAutoCommit(false);
131+
runner.setStopOnError(true);
132+
runner.setErrorLogWriter(null);
133+
runner.setLogWriter(null);
134+
135+
String resource = "org/apache/ibatis/jdbc/ScriptChangingDelimiterMissingDelimiter.sql";
136+
Reader reader = Resources.getResourceAsReader(resource);
137+
138+
try {
139+
runner.runScript(reader);
140+
fail("Expected script runner to fail due to the usage of invalid delimiter.");
141+
} catch (Exception e) {
142+
assertTrue(e.getMessage().contains("end-of-line terminator"));
143+
}
144+
}
145+
146+
@Test
147+
public void changingDelimiterShouldNotCauseRunnerFail() throws Exception {
148+
DataSource ds = createUnpooledDataSource(JPETSTORE_PROPERTIES);
149+
Connection conn = ds.getConnection();
150+
ScriptRunner runner = new ScriptRunner(conn);
151+
runner.setAutoCommit(false);
152+
runner.setStopOnError(true);
153+
runner.setErrorLogWriter(null);
154+
runner.setLogWriter(null);
155+
runJPetStoreScripts(runner);
156+
157+
String resource = "org/apache/ibatis/jdbc/ScriptChangingDelimiter.sql";
158+
Reader reader = Resources.getResourceAsReader(resource);
159+
160+
try {
161+
runner.runScript(reader);
162+
} catch (Exception e) {
163+
fail(e.getMessage());
164+
}
165+
}
124166

125167
private void runJPetStoreScripts(ScriptRunner runner) throws IOException, SQLException {
126168
runScript(runner, JPETSTORE_DDL);

0 commit comments

Comments
 (0)