Skip to content

Commit 1bc0bf2

Browse files
committed
Add a simple DataSource implementation
Implement the DataSource interface to be more compatible with JDBC spec. Add JDBC standard and Tarantool specific properties (getters/setters) to follow enterprise features (according to JavaBeans spec). Fix a JDBC URL scheme. Now correct scheme has the format like 'jdbc:tarantool://' where `tarantool` is JDBC sub-protocol. Old scheme version `tarantool://` was not JDBC-compatible. Closes: #175
1 parent fb368d2 commit 1bc0bf2

14 files changed

+416
-25
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ To configure socket channel provider you should implements SocketChannelProvider
138138
For example:
139139

140140
```
141-
tarantool://localhost:3301?user=test&password=test&socketProvider=abc.xyz.MySocketProvider
141+
jdbc:tarantool://localhost:3301?user=test&password=test&socketProvider=abc.xyz.MySocketProvider
142142
```
143143

144144
Here is an example how you can use the driver covered by Spring `DriverManagerDataSource`:
145145

146146
```java
147-
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(new DriverManagerDataSource("tarantool://localhost:3301?user=test&password=test"));
147+
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(new DriverManagerDataSource("jdbc:tarantool://localhost:3301?user=test&password=test"));
148148
RowMapper<Object> rowMapper = new RowMapper<Object>() {
149149
@Override
150150
public Object mapRow(ResultSet resultSet, int i) throws SQLException {

Diff for: src/it/java/org/tarantool/TestSql.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
//
3737
// public static void main(String[] args) throws IOException, SQLException {
3838
//
39-
// NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(new DriverManagerDataSource("tarantool://localhost:3301?username=test&password=test&socketProvider=org.tarantool.TestSql$TestSocketProvider"));
39+
// NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(new DriverManagerDataSource("jdbc:tarantool://localhost:3301?username=test&password=test&socketProvider=org.tarantool.TestSql$TestSocketProvider"));
4040
// RowMapper<Object> rowMapper = new RowMapper<Object>() {
4141
// @Override
4242
// public Object mapRow(ResultSet resultSet, int i) throws SQLException {

Diff for: src/main/java/org/tarantool/jdbc/SQLConnection.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class SQLConnection implements Connection {
5858
private DatabaseMetaData cachedMetadata;
5959
private int resultSetHoldability = UNSET_HOLDABILITY;
6060

61-
SQLConnection(String url, Properties properties) throws SQLException {
61+
public SQLConnection(String url, Properties properties) throws SQLException {
6262
this.url = url;
6363
this.properties = properties;
6464

Diff for: src/main/java/org/tarantool/jdbc/SQLConstant.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.tarantool.jdbc;
2+
3+
public class SQLConstant {
4+
5+
private SQLConstant() {
6+
}
7+
8+
public static final String DRIVER_NAME = "Tarantool JDBC Driver";
9+
10+
}

Diff for: src/main/java/org/tarantool/jdbc/SQLDatabaseMetadata.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public String getDatabaseProductVersion() throws SQLException {
9696

9797
@Override
9898
public String getDriverName() throws SQLException {
99-
return "tarantool-java";
99+
return SQLConstant.DRIVER_NAME;
100100
}
101101

102102
@Override

Diff for: src/main/java/org/tarantool/jdbc/SQLDriver.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ protected SQLTarantoolClientImpl makeSqlClient(String address, TarantoolClientCo
5757
protected Properties parseQueryString(URI uri, Properties info) throws SQLException {
5858
Properties urlProperties = new Properties();
5959

60+
// get scheme specific part (after the scheme part "jdbc:")
61+
// to correct parse remaining URL
62+
uri = URI.create(uri.getSchemeSpecificPart());
6063
String userInfo = uri.getUserInfo();
6164
if (userInfo != null) {
6265
// Get user and password from the corresponding part of the URI, i.e. before @ sign.
@@ -140,7 +143,7 @@ protected SocketChannelProvider getSocketProviderInstance(String className) thro
140143

141144
@Override
142145
public boolean acceptsURL(String url) throws SQLException {
143-
return url.toLowerCase().startsWith("tarantool:");
146+
return url.toLowerCase().startsWith("jdbc:tarantool:");
144147
}
145148

146149
@Override

Diff for: src/main/java/org/tarantool/jdbc/SQLProperty.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public enum SQLProperty {
4444
LOGIN_TIMEOUT(
4545
"loginTimeout",
4646
"The number of milliseconds to wait for connection establishment. " +
47-
"The default value is 60000 (1 minute).",
47+
"The default system value is 60000 (1 minute).",
4848
"60000",
4949
null,
5050
false
@@ -84,6 +84,10 @@ public String getDefaultValue() {
8484
return defaultValue;
8585
}
8686

87+
public int getDefaultIntValue() {
88+
return Integer.parseInt(defaultValue);
89+
}
90+
8791
public boolean isRequired() {
8892
return required;
8993
}
+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package org.tarantool.jdbc.ds;
2+
3+
import org.tarantool.jdbc.SQLConnection;
4+
import org.tarantool.jdbc.SQLConstant;
5+
import org.tarantool.jdbc.SQLProperty;
6+
7+
import java.io.PrintWriter;
8+
import java.sql.Connection;
9+
import java.sql.SQLException;
10+
import java.sql.SQLFeatureNotSupportedException;
11+
import java.sql.SQLNonTransientException;
12+
import java.util.Properties;
13+
import java.util.concurrent.TimeUnit;
14+
import java.util.logging.Logger;
15+
import javax.sql.DataSource;
16+
17+
/**
18+
* Simple {@code java.sql.DataSource} implementation.
19+
*/
20+
public class SQLDataSource implements TarantoolDataSource, DataSource {
21+
22+
private PrintWriter logWriter;
23+
private String name = "Tarantool basic data source";
24+
25+
private Properties properties = new Properties();
26+
27+
@Override
28+
public Connection getConnection() throws SQLException {
29+
return new SQLConnection(makeUrl(), new Properties(properties));
30+
}
31+
32+
@Override
33+
public Connection getConnection(String username, String password) throws SQLException {
34+
Properties copyProperties = new Properties(properties);
35+
SQLProperty.USER.setString(copyProperties, username);
36+
SQLProperty.PASSWORD.setString(copyProperties, password);
37+
return new SQLConnection(makeUrl(), copyProperties);
38+
}
39+
40+
@Override
41+
public PrintWriter getLogWriter() {
42+
return logWriter;
43+
}
44+
45+
@Override
46+
public void setLogWriter(PrintWriter out) {
47+
logWriter = out;
48+
}
49+
50+
@Override
51+
public void setLoginTimeout(int seconds) {
52+
SQLProperty.LOGIN_TIMEOUT.setInt(properties, (int) TimeUnit.SECONDS.toMillis(seconds));
53+
}
54+
55+
@Override
56+
public int getLoginTimeout() throws SQLException {
57+
return (int) TimeUnit.MILLISECONDS.toSeconds(SQLProperty.LOGIN_TIMEOUT.getInt(properties));
58+
}
59+
60+
@Override
61+
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
62+
throw new SQLFeatureNotSupportedException();
63+
}
64+
65+
@Override
66+
public <T> T unwrap(Class<T> type) throws SQLException {
67+
if (isWrapperFor(type)) {
68+
return type.cast(this);
69+
}
70+
throw new SQLNonTransientException("Statement does not wrap " + type.getName());
71+
}
72+
73+
@Override
74+
public boolean isWrapperFor(Class<?> type) {
75+
return type.isAssignableFrom(this.getClass());
76+
}
77+
78+
@Override
79+
public String getServerName() {
80+
return SQLProperty.HOST.getString(properties);
81+
}
82+
83+
@Override
84+
public void setServerName(String serverName) {
85+
SQLProperty.HOST.setString(properties, serverName);
86+
}
87+
88+
@Override
89+
public int getPortNumber() throws SQLException {
90+
return SQLProperty.PORT.getInt(properties);
91+
}
92+
93+
@Override
94+
public void setPortNumber(int port) {
95+
SQLProperty.PORT.setInt(properties, port);
96+
}
97+
98+
@Override
99+
public String getUser() {
100+
return SQLProperty.USER.getString(properties);
101+
}
102+
103+
@Override
104+
public void setUser(String userName) {
105+
SQLProperty.USER.setString(properties, userName);
106+
}
107+
108+
@Override
109+
public String getPassword() {
110+
return SQLProperty.PASSWORD.getString(properties);
111+
}
112+
113+
@Override
114+
public void setPassword(String password) {
115+
SQLProperty.PASSWORD.setString(properties, password);
116+
}
117+
118+
@Override
119+
public String getDescription() {
120+
return "Basic DataSource implementation - produces a standard Connection object. " +
121+
SQLConstant.DRIVER_NAME + ".";
122+
}
123+
124+
@Override
125+
public String getDataSourceName() {
126+
return name;
127+
}
128+
129+
@Override
130+
public void setDataSourceName(String name) {
131+
this.name = name;
132+
}
133+
134+
@Override
135+
public String getSocketChannelProvider() {
136+
return SQLProperty.SOCKET_CHANNEL_PROVIDER.getString(properties);
137+
}
138+
139+
@Override
140+
public void setSocketChannelProvider(String classFqdn) {
141+
SQLProperty.SOCKET_CHANNEL_PROVIDER.setString(properties, classFqdn);
142+
}
143+
144+
@Override
145+
public int getQueryTimeout() throws SQLException {
146+
return (int) TimeUnit.MILLISECONDS.toSeconds(SQLProperty.QUERY_TIMEOUT.getInt(properties));
147+
}
148+
149+
@Override
150+
public void setQueryTimeout(int seconds) {
151+
SQLProperty.QUERY_TIMEOUT.setInt(properties, (int) TimeUnit.SECONDS.toMillis(seconds));
152+
}
153+
154+
private String makeUrl() {
155+
return "jdbc:tarantool://" +
156+
SQLProperty.HOST.getString(properties) + ":" + SQLProperty.PORT.getString(properties);
157+
}
158+
159+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.tarantool.jdbc.ds;
2+
3+
import java.sql.SQLException;
4+
5+
/**
6+
* JDBC standard Tarantool specific data source properties.
7+
*/
8+
public interface TarantoolDataSource {
9+
10+
String getServerName() throws SQLException;
11+
12+
void setServerName(String serverName) throws SQLException;
13+
14+
int getPortNumber() throws SQLException;
15+
16+
void setPortNumber(int port) throws SQLException;
17+
18+
String getUser() throws SQLException;
19+
20+
void setUser(String userName) throws SQLException;
21+
22+
String getPassword() throws SQLException;
23+
24+
void setPassword(String password) throws SQLException;
25+
26+
String getDescription() throws SQLException;
27+
28+
String getDataSourceName() throws SQLException;
29+
30+
void setDataSourceName(String name) throws SQLException;
31+
32+
String getSocketChannelProvider() throws SQLException;
33+
34+
void setSocketChannelProvider(String classFqdn) throws SQLException;
35+
36+
int getQueryTimeout() throws SQLException;
37+
38+
void setQueryTimeout(int seconds) throws SQLException;
39+
40+
}

Diff for: src/test/java/org/tarantool/jdbc/AbstractJdbcIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public abstract class AbstractJdbcIT {
2626
private static final Integer port = Integer.valueOf(System.getProperty("tntPort", "3301"));
2727
private static final String user = System.getProperty("tntUser", "test_admin");
2828
private static final String pass = System.getProperty("tntPass", "4pWBZmLEgkmKK5WP");
29-
private static String URL = String.format("tarantool://%s:%d?user=%s&password=%s", host, port, user, pass);
29+
private static String URL = String.format("jdbc:tarantool://%s:%d?user=%s&password=%s", host, port, user, pass);
3030

3131
protected static final String LUA_FILE = "jdk-testing.lua";
3232
protected static final int LISTEN = 3301;

Diff for: src/test/java/org/tarantool/jdbc/JdbcDatabaseMetaDataIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public void testGetDriverNameVersion() throws SQLException {
229229
String version = meta.getDriverVersion();
230230

231231
// Verify driver name.
232-
assertEquals("tarantool-java", name);
232+
assertEquals(SQLConstant.DRIVER_NAME, name);
233233

234234
// Verify driver version format.
235235
// E.g. 1.7.6 or 1.7.6-SNAPSHOT.

0 commit comments

Comments
 (0)