Skip to content

Commit 3c477bd

Browse files
authored
Merge pull request tarantool#99 from tarantool/connector-1.8.jdbc-rebased
Initial JDBC support
2 parents 1a1dff5 + 8cd3b79 commit 3c477bd

37 files changed

+5749
-58
lines changed

.travis.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,16 @@ before_script:
1212
- src/test/travis.pre.sh
1313

1414
script:
15-
- mvn verify
15+
- |
16+
if [ "${TRAVIS_JDK_VERSION}" = openjdk11 ]; then
17+
mvn verify jacoco:report
18+
else
19+
mvn verify
20+
fi
1621
- cat testroot/jdk-testing.log
22+
23+
after_success:
24+
- |
25+
if [ "${TRAVIS_JDK_VERSION}" = openjdk11 ]; then
26+
mvn coveralls:report -DrepoToken=${COVERALLS_TOKEN}
27+
fi

README.md

+37-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ align="right">
55

66
# Java connector for Tarantool 1.7.4+
77

8+
[![Coverage Status][coveralls-badge]][coveralls-page]
9+
10+
[coveralls-badge]: https://coveralls.io/repos/github/tarantool/tarantool-java/badge.svg?branch=master
11+
[coveralls-page]: https://coveralls.io/github/tarantool/tarantool-java?branch=master
12+
813
[![Join the chat at https://gitter.im/tarantool/tarantool-java](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/tarantool/tarantool-java?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
914

1015
To get the Java connector for Tarantool 1.6.9, visit
@@ -115,7 +120,37 @@ Feel free to override any method of `TarantoolClientImpl`. For example, to hook
115120
all the results, you could override this:
116121

117122
```java
118-
protected void complete(long code, FutureImpl<List> q);
123+
protected void complete(long code, FutureImpl<?> q);
124+
```
125+
126+
## Spring NamedParameterJdbcTemplate usage example
127+
128+
To configure sockets you should implements SQLSocketProvider and add socketProvider=abc.xyz.MySocketProvider to connect url.
129+
For example tarantool://localhost:3301?user=test&password=test&socketProvider=abc.xyz.MySocketProvider
130+
131+
```java
132+
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(new DriverManagerDataSource("tarantool://localhost:3301?user=test&password=test"));
133+
RowMapper<Object> rowMapper = new RowMapper<Object>() {
134+
@Override
135+
public Object mapRow(ResultSet resultSet, int i) throws SQLException {
136+
return Arrays.asList(resultSet.getInt(1), resultSet.getString(2));
137+
}
138+
};
139+
140+
try {
141+
System.out.println(template.update("drop table hello_world", Collections.<String, Object>emptyMap()));
142+
} catch (Exception ignored) {
143+
}
144+
145+
System.out.println(template.update("create table hello_world(hello int not null PRIMARY KEY, world varchar(255) not null)", Collections.<String, Object>emptyMap()));
146+
Map<String, Object> params = new LinkedHashMap<String, Object>();
147+
params.put("text", "hello world");
148+
params.put("id", 1);
149+
150+
System.out.println(template.update("insert into hello_world(hello, world) values(:id,:text)", params));
151+
System.out.println(template.query("select * from hello_world", rowMapper));
152+
153+
System.out.println(template.query("select * from hello_world where hello=:id", Collections.singletonMap("id", 1), rowMapper));
119154
```
120155

121156
For more implementation details, see [API documentation](http://tarantool.github.io/tarantool-java/apidocs/index.html).
@@ -131,4 +166,4 @@ base for possible answers and solutions.
131166
To run tests
132167
```
133168
./mvnw clean test
134-
```
169+
```

pom.xml

+27
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,33 @@
6363
</execution>
6464
</executions>
6565
</plugin>
66+
<plugin>
67+
<groupId>org.jacoco</groupId>
68+
<artifactId>jacoco-maven-plugin</artifactId>
69+
<version>0.8.2</version>
70+
<executions>
71+
<execution>
72+
<id>prepare-agent</id>
73+
<goals>
74+
<goal>prepare-agent</goal>
75+
</goals>
76+
</execution>
77+
</executions>
78+
</plugin>
79+
<plugin>
80+
<groupId>org.eluder.coveralls</groupId>
81+
<artifactId>coveralls-maven-plugin</artifactId>
82+
<version>4.3.0</version>
83+
<dependencies>
84+
<!-- Support reporting to coveralls on Java 9. -->
85+
<!-- https://github.com/trautonen/coveralls-maven-plugin/issues/112 -->
86+
<dependency>
87+
<groupId>javax.xml.bind</groupId>
88+
<artifactId>jaxb-api</artifactId>
89+
<version>2.3.1</version>
90+
</dependency>
91+
</dependencies>
92+
</plugin>
6693
</plugins>
6794
</build>
6895

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//package org.tarantool;
2+
//
3+
//import java.io.IOException;
4+
//import java.net.InetSocketAddress;
5+
//import java.net.Socket;
6+
//import java.net.URI;
7+
//import java.sql.ResultSet;
8+
//import java.sql.SQLException;
9+
//import java.util.Arrays;
10+
//import java.util.Collections;
11+
//import java.util.LinkedHashMap;
12+
//import java.util.Map;
13+
//import java.util.Properties;
14+
//
15+
//import org.springframework.jdbc.core.RowMapper;
16+
//import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
17+
//import org.springframework.jdbc.datasource.DriverManagerDataSource;
18+
//import org.tarantool.jdbc.SQLSocketProvider;
19+
//
20+
//public class TestSql {
21+
//
22+
// public static class TestSocketProvider implements SQLSocketProvider {
23+
//
24+
// @Override
25+
// public Socket getConnectedSocket(URI uri, Properties params) {
26+
// Socket socket;
27+
// socket = new Socket();
28+
// try {
29+
// socket.connect(new InetSocketAddress(params.getProperty("host","localhost"), Integer.parseInt(params.getProperty("port", "3301"))));
30+
// } catch (Exception e) {
31+
// throw new RuntimeException("Couldn't connect to tarantool using" + params, e);
32+
// }
33+
// return socket;
34+
// }
35+
// }
36+
//
37+
// public static void main(String[] args) throws IOException, SQLException {
38+
//
39+
// NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(new DriverManagerDataSource("tarantool://localhost:3301?username=test&password=test&socketProvider=org.tarantool.TestSql$TestSocketProvider"));
40+
// RowMapper<Object> rowMapper = new RowMapper<Object>() {
41+
// @Override
42+
// public Object mapRow(ResultSet resultSet, int i) throws SQLException {
43+
// return Arrays.asList(resultSet.getInt(1), resultSet.getString(2));
44+
// }
45+
// };
46+
//
47+
// try {
48+
// System.out.println(template.update("drop table hello_world", Collections.<String, Object>emptyMap()));
49+
// } catch (Exception ignored) {
50+
// }
51+
// System.out.println(template.update("create table hello_world(hello int not null PRIMARY KEY, world varchar(255) not null)", Collections.<String, Object>emptyMap()));
52+
// Map<String, Object> params = new LinkedHashMap<String, Object>();
53+
// params.put("text", "hello world");
54+
// params.put("id", 1);
55+
//
56+
// System.out.println(template.update("insert into hello_world(hello, world) values(:id,:text)", params));
57+
// System.out.println(template.query("select * from hello_world", rowMapper));
58+
//
59+
// System.out.println(template.query("select * from hello_world where hello=:id", Collections.singletonMap("id", 1), rowMapper));
60+
// }
61+
//}

src/it/java/org/tarantool/TestTarantoolClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected void reconnect(int retry, Throwable lastError) {
8181
}
8282

8383
@Override
84-
protected void complete(long code, FutureImpl<List<?>> q) {
84+
protected void complete(long code, FutureImpl<?> q) {
8585
super.complete(code, q);
8686
if (code != 0) {
8787
System.out.println(code);

src/main/java/org/tarantool/AbstractTarantoolOps.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
public abstract class AbstractTarantoolOps<Space, Tuple, Operation, Result> implements TarantoolClientOps<Space, Tuple, Operation, Result> {
55
private Code callCode = Code.OLD_CALL;
66

7-
public abstract Result exec(Code code, Object... args);
7+
protected abstract Result exec(Code code, Object... args);
88

99
public Result select(Space space, Space index, Tuple key, int offset, int limit, Iterator iterator) {
1010
return select(space, index, key, offset, limit, iterator.getValue());

src/main/java/org/tarantool/Code.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
public enum Code {
55
SELECT(1), INSERT(2), REPLACE(3), UPDATE(4),
6-
DELETE(5), OLD_CALL(6), AUTH(7), EVAL(8), UPSERT(9), CALL(10), PING(64), SUBSCRIBE(66);
6+
DELETE(5), OLD_CALL(6), AUTH(7), EVAL(8), UPSERT(9), CALL(10), EXECUTE(11) , PING(64), SUBSCRIBE(66),;
77

88
int id;
99

src/main/java/org/tarantool/FutureImpl.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99

1010
public class FutureImpl<V> extends AbstractQueuedSynchronizer implements Future<V> {
1111
protected final long id;
12+
protected Code code;
1213
protected V value;
1314
protected Exception error;
1415

15-
public FutureImpl(long id) {
16+
public FutureImpl(long id, Code code) {
1617
this.id = id;
18+
this.code = code;
1719
setState(1);
1820
}
1921

@@ -91,4 +93,7 @@ public Long getId() {
9193
return id;
9294
}
9395

96+
public Code getCode() {
97+
return code;
98+
}
9499
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.tarantool;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.LinkedHashMap;
6+
import java.util.List;
7+
import java.util.ListIterator;
8+
import java.util.Map;
9+
10+
import org.tarantool.jdbc.SQLResultSet;
11+
12+
public class JDBCBridge {
13+
public static final JDBCBridge EMPTY = new JDBCBridge(Collections.<TarantoolBase.SQLMetaData>emptyList(), Collections.<List<Object>>emptyList());
14+
15+
final List<TarantoolBase.SQLMetaData> sqlMetadata;
16+
final Map<String,Integer> columnsByName;
17+
final List<List<Object>> rows;
18+
19+
protected JDBCBridge(TarantoolConnection connection) {
20+
this(connection.getSQLMetadata(),connection.getSQLData());
21+
}
22+
23+
protected JDBCBridge(List<TarantoolBase.SQLMetaData> sqlMetadata, List<List<Object>> rows) {
24+
this.sqlMetadata = sqlMetadata;
25+
this.rows = rows;
26+
columnsByName = new LinkedHashMap<String, Integer>((int) Math.ceil(sqlMetadata.size() / 0.75), 0.75f);
27+
for (int i = 0; i < sqlMetadata.size(); i++) {
28+
columnsByName.put(sqlMetadata.get(i).getName(), i + 1);
29+
}
30+
}
31+
32+
public static JDBCBridge query(TarantoolConnection connection, String sql, Object ... params) {
33+
connection.sql(sql, params);
34+
return new JDBCBridge(connection);
35+
}
36+
37+
public static int update(TarantoolConnection connection, String sql, Object ... params) {
38+
return connection.update(sql, params).intValue();
39+
}
40+
41+
public static JDBCBridge mock(List<String> fields, List<List<Object>> values) {
42+
List<TarantoolBase.SQLMetaData> meta = new ArrayList<TarantoolBase.SQLMetaData>(fields.size());
43+
for(String field:fields) {
44+
meta.add(new TarantoolBase.SQLMetaData(field));
45+
}
46+
return new JDBCBridge(meta, values);
47+
}
48+
49+
public static Object execute(TarantoolConnection connection, String sql, Object ... params) {
50+
connection.sql(sql, params);
51+
Long rowCount = connection.getSqlRowCount();
52+
if(rowCount == null) {
53+
return new SQLResultSet(new JDBCBridge(connection));
54+
}
55+
return rowCount.intValue();
56+
}
57+
58+
59+
public String getColumnName(int columnIndex) {
60+
return columnIndex > sqlMetadata.size() ? null : sqlMetadata.get(columnIndex - 1).getName();
61+
}
62+
63+
public Integer getColumnIndex(String columnName) {
64+
return columnsByName.get(columnName);
65+
}
66+
67+
public int getColumnCount() {
68+
return columnsByName.size();
69+
}
70+
71+
public ListIterator<List<Object>> iterator() {
72+
return rows.listIterator();
73+
}
74+
75+
public int size() {
76+
return rows.size();
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return "JDBCBridge{" +
82+
"sqlMetadata=" + sqlMetadata +
83+
", columnsByName=" + columnsByName +
84+
", rows=" + rows +
85+
'}';
86+
}
87+
}

src/main/java/org/tarantool/Key.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ public enum Key implements Callable<Integer> {
1616
TUPLE(0x21), FUNCTION(0x22),
1717
USER_NAME(0x23),EXPRESSION(0x27),
1818
UPSERT_OPS(0x28),
19-
DATA(0x30), ERROR(0x31);
19+
DATA(0x30), ERROR(0x31),
20+
21+
SQL_FIELD_NAME(0),
22+
SQL_METADATA(0x32),
23+
SQL_TEXT(0x40),
24+
SQL_BIND(0x41),
25+
SQL_OPTIONS(0x42),
26+
SQL_INFO(0x42),
27+
SQL_ROW_COUNT(0);
2028

2129
int id;
2230

0 commit comments

Comments
 (0)