Skip to content

Commit 9b3976e

Browse files
committed
Added support for static values within batched insert.
1 parent 0a0bb56 commit 9b3976e

File tree

3 files changed

+66
-20
lines changed

3 files changed

+66
-20
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group 'com.softwareverde'
2-
version '3.2.2'
2+
version '3.2.3'
33

44
apply plugin: 'java'
55
apply plugin: 'java-library'

src/main/java/com/softwareverde/database/query/BatchedInsertQuery.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,33 +86,41 @@ public String getQueryString() {
8686
throw new RuntimeException("Invalid parameter count for batched query: " + _query);
8787
}
8888

89-
final int writeIndex;
89+
final int firstParamParenthesisIndex;
90+
final int lastParamParenthesisIndex;
9091
{
91-
final Pattern pattern = BatchedInsertQuery.getMatcher(parameterCountPerBatch);
92-
final Matcher matcher = pattern.matcher(_query);
93-
if (matcher.find()) {
94-
writeIndex = (matcher.start() - 1);
92+
final int firstParamIndex = _query.indexOf('?');
93+
final int lastParamIndex = _query.lastIndexOf('?');
94+
95+
{
96+
int index = (firstParamIndex - 1);
97+
while (_query.charAt(index) != '(') {
98+
index -= 1;
99+
}
100+
firstParamParenthesisIndex = index;
95101
}
96-
else {
97-
writeIndex = _query.length();
102+
103+
{
104+
int index = (lastParamIndex + 1);
105+
while (_query.charAt(index) != ')') {
106+
index += 1;
107+
}
108+
lastParamParenthesisIndex = index;
98109
}
99110
}
100111

101112
final StringBuilder stringBuilder = new StringBuilder();
102-
stringBuilder.append(_query, 0, writeIndex);
103-
104-
for (int i = 1; i < batchCount; ++i) {
105-
stringBuilder.append("(");
106-
for (int j = 0; j < parameterCountPerBatch; ++j) {
107-
stringBuilder.append("?");
108-
if (j < parameterCountPerBatch - 1) {
109-
stringBuilder.append(", ");
110-
}
111-
}
112-
stringBuilder.append("), ");
113+
stringBuilder.append(_query, 0, firstParamParenthesisIndex);
114+
115+
String delimiter = "";
116+
final String parameterList = _query.substring(firstParamParenthesisIndex, (lastParamParenthesisIndex + 1));
117+
for (int i = 0; i < batchCount; ++i) {
118+
stringBuilder.append(delimiter);
119+
stringBuilder.append(parameterList);
120+
delimiter = ", ";
113121
}
114122

115-
stringBuilder.append(_query.substring(writeIndex));
123+
stringBuilder.append(_query.substring(lastParamParenthesisIndex + 1));
116124

117125
return stringBuilder.toString();
118126
}

src/test/java/com/softwareverde/database/query/BatchedInsertQueryTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,42 @@ public void should_create_batched_query_supporting_on_duplicate_key_update() {
7878
// Assert
7979
Assert.assertEquals("INSERT INTO table (column0, column1) VALUES (?, ?), (?, ?), (?, ?) ON DUPLICATE KEY UPDATE column1 = NULL", query);
8080
}
81+
82+
@Test
83+
public void should_create_batched_query_with_static_parameter() {
84+
// Setup
85+
final int batchSize = 3;
86+
87+
final BatchedInsertQuery batchedInsertQuery = new BatchedInsertQuery("INSERT INTO table (column0, column1, column2) VALUES (?, ?, 1)");
88+
89+
for (int i = 0; i < batchSize; ++i) {
90+
batchedInsertQuery.setParameter("valueA" + i);
91+
batchedInsertQuery.setParameter("valueB" + i);
92+
}
93+
94+
// Action
95+
final String query = batchedInsertQuery.getQueryString();
96+
97+
// Assert
98+
Assert.assertEquals("INSERT INTO table (column0, column1, column2) VALUES (?, ?, 1), (?, ?, 1), (?, ?, 1)", query);
99+
}
100+
101+
@Test
102+
public void should_create_batched_query_with_batch_size_7_with_static_parameter_and_duplicate_update_clause() {
103+
// Setup
104+
final int batchSize = 7;
105+
106+
final BatchedInsertQuery batchedInsertQuery = new BatchedInsertQuery("INSERT INTO table (column0, column1, column2) VALUES (?, 1, ?) ON DUPLICATE KEY UPDATE column1 = VALUE (column1)");
107+
108+
for (int i = 0; i < batchSize; ++i) {
109+
batchedInsertQuery.setParameter("valueA" + i);
110+
batchedInsertQuery.setParameter("valueC" + i);
111+
}
112+
113+
// Action
114+
final String query = batchedInsertQuery.getQueryString();
115+
116+
// Assert
117+
Assert.assertEquals("INSERT INTO table (column0, column1, column2) VALUES (?, 1, ?), (?, 1, ?), (?, 1, ?), (?, 1, ?), (?, 1, ?), (?, 1, ?), (?, 1, ?) ON DUPLICATE KEY UPDATE column1 = VALUE (column1)", query);
118+
}
81119
}

0 commit comments

Comments
 (0)