Skip to content

Commit 32fcd7a

Browse files
committed
Fix issue 13 - update readme
1 parent fe19820 commit 32fcd7a

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Internally we use prepared statements, so all incoming data is
123123
validated against SQL injection, however we had to build a connection from JavaScript types to the SQL data types
124124
therefore when doing a prepared statements, you would need to add ``:type`` to **each prepared statement variable**.
125125

126+
**Note:** prepared statement variables name could contain: any word character, a digit and a character `_`.
127+
126128
For example if you have a following SQL statement:
127129

128130
```sql

src/main/java/io/elastic/jdbc/QueryColumnNamesProvider.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,37 @@ public JsonObject getMetaModel(JsonObject configuration) {
4444
public JsonObject getColumns(JsonObject configuration) {
4545
JsonObjectBuilder properties = Json.createObjectBuilder();
4646
String sqlQuery = configuration.getString("sqlQuery");
47-
Pattern patternPoint = Pattern.compile("\\B@\\S+");
48-
Matcher matcherPoint = patternPoint.matcher(sqlQuery);
49-
if (matcherPoint.find()) {
50-
do {
51-
if (matcherPoint.group().contains(".")){
52-
throw new RuntimeException(
53-
"The variable name of the prepared statement '"
54-
+ matcherPoint.group()
55-
+ "' should not contain '.' symbol");
56-
}
57-
} while (matcherPoint.find());
58-
}
47+
Pattern patternCheckCharacter = Pattern.compile("\\B@\\S+");
48+
Matcher matcherCheckCharacter = patternCheckCharacter.matcher(sqlQuery);
5949
Pattern pattern = Pattern.compile(Utils.VARS_REGEXP);
6050
Matcher matcher = pattern.matcher(sqlQuery);
6151
Boolean isEmpty = true;
6252
if (matcher.find()) {
6353
do {
54+
matcherCheckCharacter.find();
6455
LOGGER.info("Var = {}", matcher.group());
56+
LOGGER.info("Var matcherCheckCharacter = {}", matcherCheckCharacter.group());
57+
if (!matcher.group().equals(matcherCheckCharacter.group())){
58+
throw new RuntimeException(
59+
"Prepared statement variables name '"
60+
+ matcherCheckCharacter.group()
61+
+ "' contains a forbidden character. "
62+
+ "The name could contain: any word character, a digit and a character '_'");
63+
}
6564
JsonObjectBuilder field = Json.createObjectBuilder();
6665
String result[] = matcher.group().split(":");
67-
String name = result[0].substring(1);
68-
String type = result[1];
66+
String name;
67+
String type;
68+
if (result.length > 0 && result.length < 3){
69+
name = result[0].substring(1);
70+
if (result.length == 1){
71+
type = "string";
72+
} else {
73+
type = result[1];
74+
}
75+
} else {
76+
throw new RuntimeException("Incorrect prepared statement" + matcher.group());
77+
}
6978
field.add("title", name)
7079
.add("type", type);
7180
properties.add(name, field);

src/test/groovy/io/elastic/jdbc/QueryColumnNamesProviderSpec.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class QueryColumnNamesProviderSpec extends Specification {
1313
String wrongSqlQuery
1414

1515
def setup() {
16-
sqlQuery = "SELECT * FROM films WHERE watched = @watched:boolean AND created = @created:date"
16+
sqlQuery = "SELECT * FROM films WHERE watched = @watched:boolean AND created = @created:date AND name = @name"
1717
wrongSqlQuery = "SELECT * FROM films WHERE watched = @watched.name:boolean"
1818
}
1919

@@ -24,7 +24,7 @@ class QueryColumnNamesProviderSpec extends Specification {
2424
JsonObject meta = provider.getMetaModel(configuration.build())
2525
print meta
2626
expect:
27-
meta.toString() == "{\"out\":{\"type\":\"object\",\"properties\":{\"watched\":{\"title\":\"watched\",\"type\":\"boolean\"},\"created\":{\"title\":\"created\",\"type\":\"date\"}}},\"in\":{\"type\":\"object\",\"properties\":{\"watched\":{\"title\":\"watched\",\"type\":\"boolean\"},\"created\":{\"title\":\"created\",\"type\":\"date\"}}}}"
27+
meta.toString() == "{\"out\":{\"type\":\"object\",\"properties\":{\"watched\":{\"title\":\"watched\",\"type\":\"boolean\"},\"created\":{\"title\":\"created\",\"type\":\"date\"},\"name\":{\"title\":\"name\",\"type\":\"string\"}}},\"in\":{\"type\":\"object\",\"properties\":{\"watched\":{\"title\":\"watched\",\"type\":\"boolean\"},\"created\":{\"title\":\"created\",\"type\":\"date\"},\"name\":{\"title\":\"name\",\"type\":\"string\"}}}}"
2828
}
2929

3030
def "get metadata model, wrong sqlQuery"() {

0 commit comments

Comments
 (0)