Commit 4d89c61
authored
Add comment parsing for shouldReturnResultSet and countParameters (#1207)
## Description
Closes #1201
Closes #1254
Goes through each character in the sql string while keeping track of the
current state
(normal, comment, string literal, identifier). Provides an interface for
consumers to iterate over the non-comment characters.
## Testing
See unit tests. I was also testing with this script:
<details>
<summary>Main.java</summary>
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
String url = System.getenv("DATABRICKS_URL");
if (url == null) {
System.err.println("DATABRICKS_URL environment variable is not set");
System.exit(1);
}
Class.forName("com.databricks.client.jdbc.Driver");
List<String> examples;
examples = List.of(
// "/* This is a comment */",
"SELECT 1; /* This is also a comment */",
"""
SELECT /* This is a comment
that spans multiple lines */ 1;
""",
"SELECT /* Comments are not limited to Latin characters: 评论 😊 */ 1;",
"SELECT /* Comments /* can be */ nested */ 1;",
"SELECT /* Quotes in '/*' comments \"/*\" are not special */ */ */ 1;",
"/* A prefixed comment */ SELECT 1;",
"""
SELECT '/* This is not a comment */';
/* This is not a comment */
"""
);
for (int i = 0; i < examples.size(); i++) {
String example = examples.get(i);
System.out.println("==============================");
try {
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(example);
while (rs.next()) {
System.out.println("Result " + i + ": " + rs.getObject(1));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("==============================");
System.out.println("");
System.out.println("");
}
System.out.println("");
System.out.println("");
System.out.println("==============================");
try {
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("""
/*
hi */
SELECT 1;
""");
while (rs.next()) {
System.out.println("Multi line block: " + rs.getObject(1));
}
} catch (Exception e) {
System.out.println("Multi line block: " + e.getMessage());
}
System.out.println("==============================");
System.out.println("");
System.out.println("");
System.out.println("==============================");
try {
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("""
/* /* */ */
SELECT 2;
""");
while (rs.next()) {
System.out.println("Nested: " + rs.getObject(1));
}
} catch (Exception e) {
System.out.println("Nested: " + e.getMessage());
}
System.out.println("==============================");
System.out.println("");
System.out.println("");
System.out.println("==============================");
try {
Connection conn = DriverManager.getConnection(url);
PreparedStatement pstmt = conn.prepareStatement("""
/* ? /* ? */ ? */
SELECT ? /* ? /* ? */ ? */
/* ? /* ? */ ? */;
""");
pstmt.setString(1, "hello");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("Nested param: " + rs.getObject(1));
}
} catch (Exception e) {
System.out.println("Nested param: " + e.getMessage());
}
System.out.println("==============================");
}
}
```
</details>
## Additional Notes to the Reviewer
---------
Signed-off-by: rileythomp <[email protected]>1 parent c7291f5 commit 4d89c61
7 files changed
Lines changed: 587 additions & 69 deletions
File tree
- src
- main/java/com/databricks/jdbc
- api/impl
- common/util
- test/java/com/databricks/jdbc
- api/impl
- common/util
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
Lines changed: 10 additions & 63 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
136 | 137 | | |
137 | 138 | | |
138 | 139 | | |
139 | | - | |
140 | | - | |
| 140 | + | |
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
192 | 156 | | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
| 157 | + | |
| 158 | + | |
212 | 159 | | |
213 | 160 | | |
Lines changed: 1 addition & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
717 | 717 | | |
718 | 718 | | |
719 | 719 | | |
720 | | - | |
721 | | - | |
722 | | - | |
723 | | - | |
724 | | - | |
725 | | - | |
| 720 | + | |
726 | 721 | | |
727 | 722 | | |
728 | 723 | | |
| |||
Lines changed: 147 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
Lines changed: 21 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
252 | 273 | | |
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
700 | 700 | | |
701 | 701 | | |
702 | 702 | | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
703 | 715 | | |
704 | 716 | | |
705 | 717 | | |
| |||
0 commit comments