Skip to content

Commit d0b57fe

Browse files
author
dmitriy.grytsovets
committed
JDBC Integration with idea database tool
1 parent bbdf983 commit d0b57fe

10 files changed

+1172
-89
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ First you should add snaphost repository and dependency to your pom file
1212
## Spring NamedParameterJdbcTemplate usage example.
1313

1414
To configure sockets you should implements SQLSocketProvider and add socketProvider=abc.xyz.MySocketProvider to connect url.
15-
For example tarantool://localhost:3301?username=test&password=test&socketProvider=abc.xyz.MySocketProvider
15+
For example tarantool://localhost:3301?user=test&password=test&socketProvider=abc.xyz.MySocketProvider
1616

1717
```java
18-
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(new DriverManagerDataSource("tarantool://localhost:3301?username=test&password=test"));
18+
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(new DriverManagerDataSource("tarantool://localhost:3301?user=test&password=test"));
1919

2020
RowMapper<Object> rowMapper = new RowMapper<Object>() {
2121
@Override

src/main/java/org/tarantool/JDBCBridge.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package org.tarantool;
22

3+
import java.util.ArrayList;
4+
import java.util.Collections;
35
import java.util.LinkedHashMap;
46
import java.util.List;
57
import java.util.ListIterator;
68
import java.util.Map;
79

10+
import org.tarantool.jdbc.SQLResultSet;
11+
812
public class JDBCBridge {
13+
public static final JDBCBridge EMPTY = new JDBCBridge(Collections.<TarantoolBase.SQLMetaData>emptyList(), Collections.<List<Object>>emptyList());
14+
915
final List<TarantoolBase.SQLMetaData> sqlMetadata;
1016
final Map<String,Integer> columnsByName;
1117
final List<List<Object>> rows;
@@ -19,7 +25,7 @@ protected JDBCBridge(List<TarantoolBase.SQLMetaData> sqlMetadata, List<List<Obje
1925
this.rows = rows;
2026
columnsByName = new LinkedHashMap<String, Integer>((int) Math.ceil(sqlMetadata.size() / 0.75), 0.75f);
2127
for (int i = 0; i < sqlMetadata.size(); i++) {
22-
columnsByName.put(sqlMetadata.get(i).getName(), i);
28+
columnsByName.put(sqlMetadata.get(i).getName(), i + 1);
2329
}
2430
}
2531

@@ -32,26 +38,34 @@ public static int update(TarantoolConnection connection, String sql, Object ...
3238
return connection.update(sql, params).intValue();
3339
}
3440

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+
3549
public static Object execute(TarantoolConnection connection, String sql, Object ... params) {
3650
connection.sql(sql, params);
3751
Long rowCount = connection.getSqlRowCount();
3852
if(rowCount == null) {
39-
return new JDBCBridge(connection);
53+
return new SQLResultSet(new JDBCBridge(connection));
4054
}
4155
return rowCount.intValue();
4256
}
4357

4458

4559
public String getColumnName(int columnIndex) {
46-
return sqlMetadata.get(columnIndex).getName();
60+
return columnIndex > sqlMetadata.size() ? null : sqlMetadata.get(columnIndex - 1).getName();
4761
}
4862

4963
public Integer getColumnIndex(String columnName) {
5064
return columnsByName.get(columnName);
5165
}
5266

5367
public int getColumnCount() {
54-
return sqlMetadata.size();
68+
return columnsByName.size();
5569
}
5670

5771
public ListIterator<List<Object>> iterator() {

src/main/java/org/tarantool/TarantoolBase.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.util.concurrent.atomic.AtomicLong;
1818

1919
public abstract class TarantoolBase<Result> extends AbstractTarantoolOps<Integer, List<?>, Object, Result> {
20+
protected static final String WELCOME = "Tarantool ";
21+
protected String serverVersion;
2022
/**
2123
* Connection state
2224
*/
@@ -42,10 +44,11 @@ public TarantoolBase(String username, String password, Socket socket) {
4244
byte[] bytes = new byte[64];
4345
is.readFully(bytes);
4446
String firstLine = new String(bytes);
45-
if (!firstLine.startsWith("Tarantool")) {
47+
if (!firstLine.startsWith(WELCOME)) {
4648
close();
4749
throw new CommunicationException("Welcome message should starts with tarantool but starts with '" + firstLine + "'", new IllegalStateException("Invalid welcome packet"));
4850
}
51+
serverVersion = firstLine.substring(WELCOME.length());
4952
is.readFully(bytes);
5053
this.salt = new String(bytes);
5154
if (username != null && password != null) {
@@ -230,4 +233,8 @@ protected void validateArgs(Object[] args) {
230233
public void setInitialRequestSize(int initialRequestSize) {
231234
this.initialRequestSize = initialRequestSize;
232235
}
236+
237+
public String getServerVersion() {
238+
return serverVersion;
239+
}
233240
}

src/main/java/org/tarantool/jdbc/SqlConnection.java renamed to src/main/java/org/tarantool/jdbc/SQLConnection.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@
2323
import org.tarantool.TarantoolConnection;
2424

2525
@SuppressWarnings("Since15")
26-
public class SqlConnection implements Connection {
26+
public class SQLConnection implements Connection {
2727
final TarantoolConnection connection;
28+
final String url;
29+
final Properties properties;
2830

29-
public SqlConnection(TarantoolConnection connection) {
31+
public SQLConnection(TarantoolConnection connection, String url, Properties properties) {
3032
this.connection = connection;
33+
this.url = url;
34+
this.properties = properties;
3135
}
3236

3337
@Override
@@ -85,12 +89,12 @@ public boolean isClosed() throws SQLException {
8589

8690
@Override
8791
public DatabaseMetaData getMetaData() throws SQLException {
88-
throw new SQLFeatureNotSupportedException();
92+
return new SQLDatabaseMetadata(this);
8993
}
9094

9195
@Override
9296
public void setReadOnly(boolean readOnly) throws SQLException {
93-
throw new SQLFeatureNotSupportedException();
97+
9498
}
9599

96100
@Override
@@ -100,12 +104,11 @@ public boolean isReadOnly() throws SQLException {
100104

101105
@Override
102106
public void setCatalog(String catalog) throws SQLException {
103-
throw new SQLFeatureNotSupportedException();
104107
}
105108

106109
@Override
107110
public String getCatalog() throws SQLException {
108-
throw new SQLFeatureNotSupportedException();
111+
return null;
109112
}
110113

111114
@Override
@@ -276,12 +279,11 @@ public Struct createStruct(String typeName, Object[] attributes) throws SQLExcep
276279

277280
@Override
278281
public void setSchema(String schema) throws SQLException {
279-
throw new SQLFeatureNotSupportedException();
280282
}
281283

282284
@Override
283285
public String getSchema() throws SQLException {
284-
throw new SQLFeatureNotSupportedException();
286+
return null;
285287
}
286288

287289
@Override

0 commit comments

Comments
 (0)