29
29
import org .springframework .core .io .support .EncodedResource ;
30
30
31
31
import static org .assertj .core .api .Assertions .assertThat ;
32
+ import static org .springframework .r2dbc .connection .init .ScriptUtils .DEFAULT_BLOCK_COMMENT_END_DELIMITER ;
33
+ import static org .springframework .r2dbc .connection .init .ScriptUtils .DEFAULT_BLOCK_COMMENT_START_DELIMITER ;
34
+ import static org .springframework .r2dbc .connection .init .ScriptUtils .DEFAULT_COMMENT_PREFIXES ;
35
+ import static org .springframework .r2dbc .connection .init .ScriptUtils .DEFAULT_STATEMENT_SEPARATOR ;
32
36
import static org .springframework .r2dbc .connection .init .ScriptUtils .containsSqlScriptDelimiters ;
37
+ import static org .springframework .r2dbc .connection .init .ScriptUtils .splitSqlScript ;
33
38
34
39
/**
35
40
* Unit tests for {@link ScriptUtils}.
@@ -56,7 +61,7 @@ public void splitSqlScriptDelimitedWithSemicolon() {
56
61
";" );
57
62
58
63
List <String > statements = new ArrayList <>();
59
- ScriptUtils . splitSqlScript (script , ";" , statements );
64
+ splitSqlScript (script , ";" , statements );
60
65
61
66
assertThat (statements ).hasSize (3 ).containsSequence (cleanedStatement1 ,
62
67
cleanedStatement2 , cleanedStatement3 );
@@ -71,7 +76,7 @@ public void splitSqlScriptDelimitedWithNewLine() {
71
76
String script = Strings .join (statement1 , statement2 , statement3 ).with ("\n " );
72
77
73
78
List <String > statements = new ArrayList <>();
74
- ScriptUtils . splitSqlScript (script , "\n " , statements );
79
+ splitSqlScript (script , "\n " , statements );
75
80
76
81
assertThat (statements ).hasSize (3 ).containsSequence (statement1 , statement2 ,
77
82
statement3 );
@@ -87,7 +92,7 @@ public void splitSqlScriptDelimitedWithNewLineButDefaultDelimiterSpecified() {
87
92
88
93
List <String > statements = new ArrayList <>();
89
94
90
- ScriptUtils . splitSqlScript (script , ScriptUtils . DEFAULT_STATEMENT_SEPARATOR ,
95
+ splitSqlScript (script , DEFAULT_STATEMENT_SEPARATOR ,
91
96
statements );
92
97
93
98
assertThat (statements ).hasSize (1 ).contains (script .replace ('\n' , ' ' ));
@@ -102,7 +107,7 @@ public void splitScriptWithSingleQuotesNestedInsideDoubleQuotes() {
102
107
String script = statement1 + delim + statement2 + delim ;
103
108
104
109
List <String > statements = new ArrayList <>();
105
- ScriptUtils . splitSqlScript (script , ';' , statements );
110
+ splitSqlScript (script , ';' , statements );
106
111
107
112
assertThat (statements ).hasSize (2 ).containsSequence (statement1 , statement2 );
108
113
}
@@ -111,7 +116,7 @@ public void splitScriptWithSingleQuotesNestedInsideDoubleQuotes() {
111
116
public void readAndSplitScriptWithMultipleNewlinesAsSeparator () {
112
117
String script = readScript ("db-test-data-multi-newline.sql" );
113
118
List <String > statements = new ArrayList <>();
114
- ScriptUtils . splitSqlScript (script , "\n \n " , statements );
119
+ splitSqlScript (script , "\n \n " , statements );
115
120
116
121
String statement1 = "insert into users (last_name) values ('Walter')" ;
117
122
String statement2 = "insert into users (last_name) values ('Jesse')" ;
@@ -126,19 +131,26 @@ public void readAndSplitScriptWithMultipleNewlinesAsSeparator() {
126
131
@ Test
127
132
public void readAndSplitScriptContainingComments () {
128
133
String script = readScript ("test-data-with-comments.sql" );
129
- splitScriptContainingComments (script );
134
+ splitScriptContainingComments (script , DEFAULT_COMMENT_PREFIXES );
130
135
}
131
136
132
137
@ Test
133
138
public void readAndSplitScriptContainingCommentsWithWindowsLineEnding () {
134
139
String script = readScript ("test-data-with-comments.sql" ).replaceAll ("\n " ,
135
140
"\r \n " );
136
- splitScriptContainingComments (script );
141
+ splitScriptContainingComments (script , DEFAULT_COMMENT_PREFIXES );
137
142
}
138
143
139
- private void splitScriptContainingComments (String script ) {
144
+ @ Test
145
+ public void readAndSplitScriptContainingCommentsWithMultiplePrefixes () throws Exception {
146
+ String script = readScript ("test-data-with-multi-prefix-comments.sql" );
147
+ splitScriptContainingComments (script , "--" , "#" , "^" );
148
+ }
149
+
150
+ private void splitScriptContainingComments (String script , String ... commentPrefixes ) {
140
151
List <String > statements = new ArrayList <>();
141
- ScriptUtils .splitSqlScript (script , ';' , statements );
152
+ splitSqlScript (null , script , ";" , commentPrefixes , DEFAULT_BLOCK_COMMENT_START_DELIMITER ,
153
+ DEFAULT_BLOCK_COMMENT_END_DELIMITER , statements );
142
154
143
155
String statement1 = "insert into customer (id, name) values (1, 'Rod; Johnson'), (2, 'Adrian Collier')" ;
144
156
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)" ;
@@ -153,7 +165,7 @@ private void splitScriptContainingComments(String script) {
153
165
public void readAndSplitScriptContainingCommentsWithLeadingTabs () {
154
166
String script = readScript ("test-data-with-comments-and-leading-tabs.sql" );
155
167
List <String > statements = new ArrayList <>();
156
- ScriptUtils . splitSqlScript (script , ';' , statements );
168
+ splitSqlScript (script , ';' , statements );
157
169
158
170
String statement1 = "insert into customer (id, name) values (1, 'Walter White')" ;
159
171
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2013-06-08', 1)" ;
@@ -167,7 +179,7 @@ public void readAndSplitScriptContainingCommentsWithLeadingTabs() {
167
179
public void readAndSplitScriptContainingMultiLineComments () {
168
180
String script = readScript ("test-data-with-multi-line-comments.sql" );
169
181
List <String > statements = new ArrayList <>();
170
- ScriptUtils . splitSqlScript (script , ';' , statements );
182
+ splitSqlScript (script , ';' , statements );
171
183
172
184
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Walter', 'White')" ;
173
185
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Jesse' , 'Pinkman' )" ;
@@ -179,7 +191,7 @@ public void readAndSplitScriptContainingMultiLineComments() {
179
191
public void readAndSplitScriptContainingMultiLineNestedComments () {
180
192
String script = readScript ("test-data-with-multi-line-nested-comments.sql" );
181
193
List <String > statements = new ArrayList <>();
182
- ScriptUtils . splitSqlScript (script , ';' , statements );
194
+ splitSqlScript (script , ';' , statements );
183
195
184
196
String statement1 = "INSERT INTO users(first_name, last_name) VALUES('Walter', 'White')" ;
185
197
String statement2 = "INSERT INTO users(first_name, last_name) VALUES( 'Jesse' , 'Pinkman' )" ;
0 commit comments