Skip to content

Commit a7ec45b

Browse files
committed
Add support for comments to Parameters.
1 parent 9dfe60e commit a7ec45b

File tree

9 files changed

+126
-20
lines changed

9 files changed

+126
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ the values of the "first_name" and "last_name" columns would be returned in a ne
850850

851851
```java
852852
public void attach(String key, String subquery) { ... }
853+
public void attach(String key, Parameters subquery) { ... }
853854
```
854855

855856
Each attached query is executed once per row in the result set. The resulting rows are returned in a list that is associated with the corresponding key.

httprpc-test/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ dependencies {
3131
implementation project(':httprpc')
3232
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
3333
implementation 'org.mongodb:mongo-java-driver:3.6.3'
34-
implementation 'mysql:mysql-connector-java:8.0.15'
3534
}
3635

3736
compileJava {

httprpc-test/src/main/java/org/httprpc/test/mysql/EmployeeService.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616

1717
import java.io.IOException;
1818
import java.sql.Connection;
19-
import java.sql.DriverManager;
2019
import java.sql.PreparedStatement;
2120
import java.sql.ResultSet;
2221
import java.sql.SQLException;
2322
import java.util.HashMap;
2423
import java.util.List;
2524

25+
import javax.naming.Context;
26+
import javax.naming.InitialContext;
27+
import javax.naming.NamingException;
2628
import javax.servlet.ServletException;
2729
import javax.servlet.annotation.WebServlet;
30+
import javax.sql.DataSource;
2831

2932
import org.httprpc.RequestMethod;
3033
import org.httprpc.ResourcePath;
@@ -41,15 +44,18 @@
4144
public class EmployeeService extends WebService {
4245
private static final long serialVersionUID = 0;
4346

44-
private static final String DB_URL = "jdbc:mysql://vm.local:3306/employees?user=root&password=password&serverTimezone=UTC&useSSL=false";
47+
private DataSource dataSource = null;
4548

4649
@Override
4750
public void init() throws ServletException {
4851
super.init();
4952

5053
try {
51-
Class.forName("com.mysql.cj.jdbc.Driver");
52-
} catch (ClassNotFoundException exception) {
54+
Context initialCtx = new InitialContext();
55+
Context environmentContext = (Context) initialCtx.lookup("java:comp/env");
56+
57+
dataSource = (DataSource) environmentContext.lookup("jdbc/EmployeesDB");
58+
} catch (NamingException exception) {
5359
throw new ServletException(exception);
5460
}
5561
}
@@ -72,7 +78,7 @@ public void getEmployees(String name) throws SQLException, IOException {
7278

7379
arguments.put("name", (name == null) ? "%" : name.replace('*', '%'));
7480

75-
try (Connection connection = DriverManager.getConnection(DB_URL);
81+
try (Connection connection = dataSource.getConnection();
7682
PreparedStatement statement = connection.prepareStatement(parameters.getSQL())) {
7783
parameters.apply(statement, arguments);
7884

@@ -117,7 +123,7 @@ public void getEmployee(List<String> details) throws SQLException, IOException {
117123

118124
arguments.put("employeeNumber", employeeNumber);
119125

120-
try (Connection connection = DriverManager.getConnection(DB_URL);
126+
try (Connection connection = dataSource.getConnection();
121127
PreparedStatement statement = connection.prepareStatement(parameters.getSQL())) {
122128
parameters.apply(statement, arguments);
123129

httprpc-test/src/main/java/org/httprpc/test/mysql/PetService.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import java.io.IOException;
1818
import java.sql.Connection;
19-
import java.sql.DriverManager;
2019
import java.sql.PreparedStatement;
2120
import java.sql.ResultSet;
2221
import java.sql.SQLException;
@@ -28,8 +27,12 @@
2827
import java.util.stream.Stream;
2928
import java.util.stream.StreamSupport;
3029

30+
import javax.naming.Context;
31+
import javax.naming.InitialContext;
32+
import javax.naming.NamingException;
3133
import javax.servlet.ServletException;
3234
import javax.servlet.annotation.WebServlet;
35+
import javax.sql.DataSource;
3336

3437
import org.httprpc.WebService;
3538
import org.httprpc.io.CSVEncoder;
@@ -48,6 +51,8 @@
4851
public class PetService extends WebService {
4952
private static final long serialVersionUID = 0;
5053

54+
private DataSource dataSource = null;
55+
5156
/**
5257
* Pet interface.
5358
*/
@@ -59,15 +64,16 @@ public interface Pet {
5964
public Date getBirth();
6065
}
6166

62-
private static final String DB_URL = "jdbc:mysql://vm.local:3306/menagerie?user=root&password=password&serverTimezone=UTC&useSSL=false";
63-
6467
@Override
6568
public void init() throws ServletException {
6669
super.init();
6770

6871
try {
69-
Class.forName("com.mysql.cj.jdbc.Driver");
70-
} catch (ClassNotFoundException exception) {
72+
Context initialCtx = new InitialContext();
73+
Context environmentContext = (Context) initialCtx.lookup("java:comp/env");
74+
75+
dataSource = (DataSource) environmentContext.lookup("jdbc/MenagerieDB");
76+
} catch (NamingException exception) {
7177
throw new ServletException(exception);
7278
}
7379
}
@@ -87,7 +93,7 @@ public void getPets(String owner, String format) throws SQLException, IOExceptio
8793

8894
arguments.put("owner", owner);
8995

90-
try (Connection connection = DriverManager.getConnection(DB_URL);
96+
try (Connection connection = dataSource.getConnection();
9197
PreparedStatement statement = connection.prepareStatement(parameters.getSQL())) {
9298
parameters.apply(statement, arguments);
9399

@@ -123,7 +129,7 @@ public void getPets(String owner, String format) throws SQLException, IOExceptio
123129
@ResourcePath("average-age")
124130
public double getAverageAge() throws SQLException {
125131
double averageAge;
126-
try (Connection connection = DriverManager.getConnection(DB_URL);
132+
try (Connection connection = dataSource.getConnection();
127133
Statement statement = connection.createStatement();
128134
ResultSet resultSet = statement.executeQuery("SELECT birth FROM pet")) {
129135
ResultSetAdapter resultSetAdapter = new ResultSetAdapter(resultSet);

httprpc-test/src/main/webapp/WEB-INF/web.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,19 @@
44
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
55
version="3.1">
66
<display-name>HTTP-RPC Server Test</display-name>
7+
8+
<resource-ref>
9+
<description>Employees DB</description>
10+
<res-ref-name>jdbc/EmployeesDB</res-ref-name>
11+
<res-type>javax.sql.DataSource</res-type>
12+
<res-auth>Container</res-auth>
13+
</resource-ref>
14+
15+
<resource-ref>
16+
<description>Menagerie DB</description>
17+
<res-ref-name>jdbc/MenagerieDB</res-ref-name>
18+
<res-type>javax.sql.DataSource</res-type>
19+
<res-auth>Container</res-auth>
20+
</resource-ref>
721
</web-app>
822

httprpc/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ plugins {
1919
}
2020

2121
group = 'org.httprpc'
22-
version = '6.3.3'
22+
version = '6.3.4'
2323

2424
repositories {
2525
mavenCentral()

httprpc/src/main/java/org/httprpc/sql/Parameters.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,55 @@ public static Parameters parse(Reader sqlReader) throws IOException {
111111

112112
StringBuilder sqlBuilder = new StringBuilder();
113113

114+
boolean singleLineComment = false;
115+
boolean multiLineComment = false;
116+
114117
boolean quoted = false;
115118

116119
int c = sqlReader.read();
117120

118121
while (c != EOF) {
119-
if (c == ':' && !quoted) {
122+
if (c == '-') {
123+
sqlBuilder.append((char) c);
124+
125+
c = sqlReader.read();
126+
127+
singleLineComment = (c == '-') && !multiLineComment;
128+
129+
sqlBuilder.append((char) c);
130+
131+
c = sqlReader.read();
132+
} else if (c == '\r' || c == '\n') {
133+
sqlBuilder.append((char) c);
134+
135+
singleLineComment = false;
136+
137+
c = sqlReader.read();
138+
} else if (c == '/') {
139+
sqlBuilder.append((char)c);
140+
141+
c = sqlReader.read();
142+
143+
multiLineComment = (c == '*');
144+
145+
sqlBuilder.append((char)c);
146+
147+
c = sqlReader.read();
148+
} else if (c == '*') {
149+
sqlBuilder.append((char) c);
150+
151+
c = sqlReader.read();
152+
153+
multiLineComment = (c == '/');
154+
155+
sqlBuilder.append((char) c);
156+
157+
c = sqlReader.read();
158+
} else if (singleLineComment || multiLineComment) {
159+
sqlBuilder.append((char)c);
160+
161+
c = sqlReader.read();
162+
} else if (c == ':' && !quoted) {
120163
c = sqlReader.read();
121164

122165
if (c == ':') {

httprpc/src/main/java/org/httprpc/sql/ResultSetAdapter.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ResultSetAdapter implements Iterable<Map<String, Object>> {
3333
private ResultSet resultSet;
3434
private ResultSetMetaData resultSetMetaData;
3535

36-
private LinkedHashMap<String, String> subqueries = new LinkedHashMap<>();
36+
private LinkedHashMap<String, Parameters> subqueries = new LinkedHashMap<>();
3737

3838
private Iterator<Map<String, Object>> iterator = new Iterator<Map<String, Object>>() {
3939
private Boolean hasNext = null;
@@ -89,14 +89,14 @@ public Map<String, Object> next() {
8989
throw new RuntimeException(exception);
9090
}
9191

92-
for (Map.Entry<String, String> entry : subqueries.entrySet()) {
93-
Parameters parameters = Parameters.parse(entry.getValue());
94-
92+
for (Map.Entry<String, Parameters> entry : subqueries.entrySet()) {
9593
LinkedList<Map<String, Object>> results = new LinkedList<>();
9694

9795
try {
9896
Connection connection = resultSet.getStatement().getConnection();
9997

98+
Parameters parameters = entry.getValue();
99+
100100
try (PreparedStatement statement = connection.prepareStatement(parameters.getSQL())) {
101101
parameters.apply(statement, row);
102102

@@ -149,6 +149,19 @@ public ResultSetAdapter(ResultSet resultSet) {
149149
* The subquery to attach.
150150
*/
151151
public void attach(String key, String subquery) {
152+
attach(key, Parameters.parse(subquery));
153+
}
154+
155+
/**
156+
* Attaches a subquery to the result set.
157+
*
158+
* @param key
159+
* The key to associate with the subquery results.
160+
*
161+
* @param subquery
162+
* The subquery to attach.
163+
*/
164+
public void attach(String key, Parameters subquery) {
152165
if (key == null) {
153166
throw new IllegalArgumentException();
154167
}

httprpc/src/test/java/org/httprpc/sql/ParametersTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,28 @@ public void testDoubleColon() {
3838

3939
Assertions.assertEquals("select 'ab:c'::varchar(16) as abc", parameters.getSQL());
4040
}
41+
42+
@Test
43+
public void testSingleLineComment() {
44+
Parameters parameters = Parameters.parse("-- this is a comment: hello\r\nselect * from xyz where foo = :foo");
45+
46+
Assertions.assertEquals("-- this is a comment: hello\r\nselect * from xyz where foo = ?",
47+
parameters.getSQL());
48+
}
49+
50+
@Test
51+
public void testMultiLineComment() {
52+
Parameters parameters = Parameters.parse("/* this is a comment: hello\r\nand so is this: goodbye */ select * from xyz where foo = :foo");
53+
54+
Assertions.assertEquals("/* this is a comment: hello\r\nand so is this: goodbye */ select * from xyz where foo = ?",
55+
parameters.getSQL());
56+
}
57+
58+
@Test
59+
public void testSingleAndMultiLineComment() {
60+
Parameters parameters = Parameters.parse("/* this is a comment: hello -- and so is this: goodbye */ select * from xyz where foo = :foo");
61+
62+
Assertions.assertEquals("/* this is a comment: hello -- and so is this: goodbye */ select * from xyz where foo = ?",
63+
parameters.getSQL());
64+
}
4165
}

0 commit comments

Comments
 (0)