Skip to content

Commit 6ab0753

Browse files
author
Olha Virolainen
authored
Merge pull request #20 from elasticio/sprint-19-master
Sprint 19 master
2 parents edf1f7d + 344758d commit 6ab0753

File tree

2 files changed

+55
-207
lines changed

2 files changed

+55
-207
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class Utils {
3232
public static final String CFG_HOST = "host";
3333
public static final String CFG_USER = "user";
3434
public static final String VARS_REGEXP = "\\B@([\\w_$][\\d\\w_$]*(:(string|boolean|date|number|bigint|float|real))?)";
35-
public static final String TEMPLATE_REGEXP = "\\B@\\S+";
35+
public static final String TEMPLATE_REGEXP = "\\B@(?:(?![=\\)\\(])[\\S])+";
3636
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
3737
public static Map<String, String> columnTypes = null;
3838

@@ -102,31 +102,31 @@ public static void setStatementParam(PreparedStatement statement, int paramNumbe
102102
JsonObject body) throws SQLException {
103103
try {
104104
if (isNumeric(colName)) {
105-
if (body.get(colName) != null) {
105+
if ((body.get(colName) != null) && (body.get(colName) != JsonValue.NULL)) {
106106
statement.setBigDecimal(paramNumber, body.getJsonNumber(colName).bigDecimalValue());
107107
} else {
108108
statement.setBigDecimal(paramNumber, null);
109109
}
110110
} else if (isTimestamp(colName)) {
111-
if (body.get(colName) != null) {
111+
if ((body.get(colName) != null) && (body.get(colName) != JsonValue.NULL)) {
112112
statement.setTimestamp(paramNumber, Timestamp.valueOf(body.getString(colName)));
113113
} else {
114114
statement.setTimestamp(paramNumber, null);
115115
}
116116
} else if (isDate(colName)) {
117-
if (body.get(colName) != null) {
117+
if ((body.get(colName) != null) && (body.get(colName) != JsonValue.NULL)) {
118118
statement.setDate(paramNumber, Date.valueOf(body.getString(colName)));
119119
} else {
120120
statement.setDate(paramNumber, null);
121121
}
122122
} else if (isBoolean(colName)) {
123-
if (body.get(colName) != null) {
123+
if ((body.get(colName) != null) && (body.get(colName) != JsonValue.NULL)) {
124124
statement.setBoolean(paramNumber, body.getBoolean(colName));
125125
} else {
126126
statement.setBoolean(paramNumber, false);
127127
}
128128
} else {
129-
if (body.get(colName) != null) {
129+
if ((body.get(colName) != null) && (body.get(colName) != JsonValue.NULL)) {
130130
statement.setString(paramNumber, body.getString(colName));
131131
} else {
132132
statement.setNull(paramNumber, Types.VARCHAR);
@@ -227,8 +227,18 @@ public static Map<String, String> getVariableTypes(String sqlQuery) {
227227
if (matcher.find()) {
228228
do {
229229
String result[] = matcher.group().split(":");
230-
String name = result[0].substring(1);
231-
String type = result[1];
230+
String name;
231+
String type;
232+
if (result.length > 0 && result.length < 3){
233+
name = result[0].substring(1);
234+
if (result.length == 1){
235+
type = "string";
236+
} else {
237+
type = result[1];
238+
}
239+
} else {
240+
throw new RuntimeException("Incorrect prepared statement" + matcher.group());
241+
}
232242
columnTypes.put(name, type);
233243
isEmpty = false;
234244
} while (matcher.find());

src/test/groovy/io/elastic/jdbc/actions/SelectMySQLSpec.groovy

Lines changed: 37 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package io.elastic.jdbc.actions
33
import io.elastic.api.EventEmitter
44
import io.elastic.api.ExecutionParameters
55
import io.elastic.api.Message
6-
import io.elastic.jdbc.actions.SelectAction
76
import spock.lang.Ignore
87
import spock.lang.Shared
98
import spock.lang.Specification
@@ -12,7 +11,6 @@ import javax.json.Json
1211
import javax.json.JsonObject
1312
import java.sql.Connection
1413
import java.sql.DriverManager
15-
import java.sql.ResultSet
1614

1715
@Ignore
1816
class SelectMySQLSpec extends Specification {
@@ -27,6 +25,8 @@ class SelectMySQLSpec extends Specification {
2725
def databaseName = System.getenv("CONN_DBNAME_MYSQL")
2826
@Shared
2927
def host = System.getenv("CONN_HOST_MYSQL")
28+
@Shared
29+
def port = System.getenv("CONN_PORT_MYSQL")
3030

3131
@Shared
3232
Connection connection
@@ -38,6 +38,8 @@ class SelectMySQLSpec extends Specification {
3838
@Shared
3939
EventEmitter.Callback dataCallback
4040
@Shared
41+
EventEmitter.Callback onHttpReplyCallback
42+
@Shared
4143
EventEmitter.Callback reboundCallback
4244
@Shared
4345
EventEmitter emitter
@@ -53,226 +55,62 @@ class SelectMySQLSpec extends Specification {
5355
}
5456

5557
def createAction() {
56-
errorCallback = Mock(EventEmitter.Callback)
57-
snapshotCallback = Mock(EventEmitter.Callback)
58-
dataCallback = Mock(EventEmitter.Callback)
59-
reboundCallback = Mock(EventEmitter.Callback)
60-
emitter = new EventEmitter.Builder().onData(dataCallback).onSnapshot(snapshotCallback).onError(errorCallback).onRebound(reboundCallback).build()
61-
action = new SelectAction(emitter)
58+
action = new SelectAction()
6259
}
6360

6461
def runAction(JsonObject config, JsonObject body, JsonObject snapshot) {
6562
Message msg = new Message.Builder().body(body).build()
66-
ExecutionParameters params = new ExecutionParameters(msg, config, snapshot)
63+
errorCallback = Mock(EventEmitter.Callback)
64+
snapshotCallback = Mock(EventEmitter.Callback)
65+
dataCallback = Mock(EventEmitter.Callback)
66+
reboundCallback = Mock(EventEmitter.Callback)
67+
onHttpReplyCallback = Mock(EventEmitter.Callback)
68+
emitter = new EventEmitter.Builder()
69+
.onData(dataCallback)
70+
.onSnapshot(snapshotCallback)
71+
.onError(errorCallback)
72+
.onRebound(reboundCallback)
73+
.onHttpReplyCallback(onHttpReplyCallback).build()
74+
ExecutionParameters params = new ExecutionParameters(msg, emitter, config, snapshot)
6775
action.execute(params);
6876
}
6977

7078
def getStarsConfig() {
71-
JsonObject config = Json.createObjectBuilder().build();
72-
73-
config.addProperty("idColumn", "id")
74-
config.addProperty("tableName", "stars")
75-
config.addProperty("user", user)
76-
config.addProperty("password", password)
77-
config.addProperty("dbEngine", "mssql")
78-
config.addProperty("host", host)
79-
config.addProperty("databaseName", databaseName)
79+
JsonObject config = Json.createObjectBuilder()
80+
.add("sqlQuery", "SELECT * from stars where @id:number =id AND name=@name")
81+
.add("user", user)
82+
.add("password", password)
83+
.add("dbEngine", "mysql")
84+
.add("host", host)
85+
.add("port", port)
86+
.add("databaseName", databaseName)
87+
.build()
8088
return config;
8189
}
82-
8390
def prepareStarsTable() {
84-
String sql = "DROP TABLE IF EXISTS stars;"
91+
String sql = "DROP TABLE IF EXISTS stars"
8592
connection.createStatement().execute(sql);
8693
connection.createStatement().execute("CREATE TABLE stars (id int, name varchar(255) NOT NULL, date datetime, radius int, destination int)");
87-
}
88-
89-
def getRecords(tableName) {
90-
ArrayList<String> records = new ArrayList<String>();
91-
String sql = "SELECT * FROM " + tableName;
92-
ResultSet rs = connection.createStatement().executeQuery(sql);
93-
while (rs.next()) {
94-
records.add(rs.toRowResult().toString());
95-
}
96-
rs.close();
97-
return records;
94+
connection.createStatement().execute("INSERT INTO stars (id, name) VALUES (1,'Hello')");
9895
}
9996

10097
def cleanupSpec() {
101-
String sql = "DROP TABLE IF EXISTS persons;"
102-
103-
connection.createStatement().execute(sql)
104-
sql = "DROP TABLE IF EXISTS stars;"
98+
String sql = "DROP TABLE IF EXISTS stars"
10599
connection.createStatement().execute(sql)
106100
connection.close()
107101
}
108102

109-
def "one insert"() {
110-
103+
def "one select"() {
111104
prepareStarsTable();
112-
113105
JsonObject snapshot = Json.createObjectBuilder().build();
114-
115-
JsonObject body = Json.createObjectBuilder().build();
116-
body.addProperty("id", "1")
117-
body.addProperty("name", "Taurus")
118-
body.addProperty("date", "2015-02-19 10:10:10.0")
119-
body.addProperty("radius", "123")
120-
106+
JsonObject body = Json.createObjectBuilder()
107+
.add("id", 1)
108+
.add("name", "Hello")
109+
.build()
110+
when:
121111
runAction(getStarsConfig(), body, snapshot)
122-
123-
ArrayList<String> records = getRecords("stars")
124-
125-
expect:
126-
records.size() == 1
127-
records.get(0) == '{id=1, name=Taurus, date=2015-02-19 10:10:10.0, radius=123, destination=null}'
128-
}
129-
130-
def "one insert, incorrect value: string in integer field"() {
131-
132-
prepareStarsTable();
133-
134-
JsonObject snapshot = Json.createObjectBuilder().build();
135-
136-
JsonObject body = Json.createObjectBuilder().build();
137-
body.addProperty("id", "1")
138-
body.addProperty("name", "Taurus")
139-
body.addProperty("radius", "test")
140-
141-
String exceptionClass = "";
142-
143-
try {
144-
runAction(getStarsConfig(), body, snapshot)
145-
} catch (Exception e) {
146-
exceptionClass = e.getClass().getName();
147-
}
148-
149-
expect:
150-
exceptionClass.contains("Exception")
151-
}
152-
153-
def "two inserts"() {
154-
155-
prepareStarsTable();
156-
157-
JsonObject snapshot = Json.createObjectBuilder().build()
158-
159-
JsonObject body1 = Json.createObjectBuilder().build()
160-
body1.addProperty("id", "1")
161-
body1.addProperty("name", "Taurus")
162-
body1.addProperty("radius", "123")
163-
164-
runAction(getStarsConfig(), body1, snapshot)
165-
166-
JsonObject body2 = Json.createObjectBuilder().build()
167-
body2.addProperty("id", "2")
168-
body2.addProperty("name", "Eridanus")
169-
body2.addProperty("radius", "456")
170-
171-
runAction(getStarsConfig(), body2, snapshot)
172-
173-
ArrayList<String> records = getRecords("stars")
174-
175-
expect:
176-
records.size() == 2
177-
records.get(0) == '{id=1, name=Taurus, date=null, radius=123, destination=null}'
178-
records.get(1) == '{id=2, name=Eridanus, date=null, radius=456, destination=null}'
179-
}
180-
181-
def "one insert, one update by ID"() {
182-
183-
prepareStarsTable();
184-
185-
JsonObject snapshot = Json.createObjectBuilder().build()
186-
187-
JsonObject body1 = Json.createObjectBuilder().build()
188-
body1.addProperty("id", "1")
189-
body1.addProperty("name", "Taurus")
190-
body1.addProperty("radius", "123")
191-
192-
runAction(getStarsConfig(), body1, snapshot)
193-
194-
JsonObject body2 = Json.createObjectBuilder().build()
195-
body2.addProperty("id", "1")
196-
body2.addProperty("name", "Eridanus")
197-
198-
runAction(getStarsConfig(), body2, snapshot)
199-
200-
ArrayList<String> records = getRecords("stars")
201-
202-
expect:
203-
records.size() == 1
204-
records.get(0) == '{id=1, name=Eridanus, date=null, radius=123, destination=null}'
112+
then:
113+
0 * errorCallback.receive(_)
205114
}
206115

207-
208-
def getPersonsConfig() {
209-
JsonObject config = Json.createObjectBuilder().build()
210-
config.addProperty("idColumn", "email")
211-
config.addProperty("tableName", "persons")
212-
config.addProperty("user", user)
213-
config.addProperty("password", password)
214-
config.addProperty("dbEngine", "mssql")
215-
config.addProperty("host", host)
216-
config.addProperty("databaseName", databaseName)
217-
return config;
218-
}
219-
220-
def preparePersonsTable() {
221-
String sql = "DROP TABLE IF EXISTS persons;"
222-
connection.createStatement().execute(sql);
223-
connection.createStatement().execute("CREATE TABLE persons (id int, name varchar(255) NOT NULL, email varchar(255) NOT NULL)");
224-
}
225-
226-
def "one insert, name with quote"() {
227-
228-
preparePersonsTable();
229-
230-
JsonObject snapshot = Json.createObjectBuilder().build()
231-
232-
JsonObject body1 = Json.createObjectBuilder().build()
233-
body1.addProperty("id", "1")
234-
body1.addProperty("name", "O'Henry")
235-
body1.addProperty("email", "[email protected]")
236-
runAction(getPersonsConfig(), body1, snapshot)
237-
238-
ArrayList<String> records = getRecords("persons")
239-
240-
expect:
241-
records.size() == 1
242-
records.get(0) == '{id=1, name=O\'Henry, [email protected]}'
243-
}
244-
245-
def "two inserts, one update by email"() {
246-
247-
preparePersonsTable();
248-
249-
JsonObject snapshot = Json.createObjectBuilder().build()
250-
251-
JsonObject body1 = Json.createObjectBuilder().build()
252-
body1.addProperty("id", "1")
253-
body1.addProperty("name", "User1")
254-
body1.addProperty("email", "[email protected]")
255-
runAction(getPersonsConfig(), body1, snapshot)
256-
257-
JsonObject body2 = Json.createObjectBuilder().build()
258-
body2.addProperty("id", "2")
259-
body2.addProperty("name", "User2")
260-
body2.addProperty("email", "[email protected]")
261-
runAction(getPersonsConfig(), body2, snapshot)
262-
263-
JsonObject body3 = Json.createObjectBuilder().build()
264-
body3.addProperty("id", "3")
265-
body3.addProperty("name", "User3")
266-
body3.addProperty("email", "[email protected]")
267-
runAction(getPersonsConfig(), body3, snapshot)
268-
269-
ArrayList<String> records = getRecords("persons")
270-
271-
expect:
272-
records.size() == 2
273-
records.get(0) == '{id=1, name=User1, [email protected]}'
274-
records.get(1) == '{id=3, name=User3, [email protected]}'
275-
}
276-
277-
278116
}

0 commit comments

Comments
 (0)