forked from dork/tarantool-java
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
e32b3d8
commit 2c9cd80
Showing
14 changed files
with
416 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.tarantool.jdbc; | ||
|
||
public class SQLConstant { | ||
|
||
private SQLConstant() { | ||
} | ||
|
||
public static final String DRIVER_NAME = "Tarantool JDBC Driver"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package org.tarantool.jdbc.ds; | ||
|
||
import org.tarantool.jdbc.SQLConnection; | ||
import org.tarantool.jdbc.SQLConstant; | ||
import org.tarantool.jdbc.SQLProperty; | ||
|
||
import java.io.PrintWriter; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.sql.SQLFeatureNotSupportedException; | ||
import java.sql.SQLNonTransientException; | ||
import java.util.Properties; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.logging.Logger; | ||
import javax.sql.DataSource; | ||
|
||
/** | ||
* Simple {@code java.sql.DataSource} implementation. | ||
*/ | ||
public class SQLDataSource implements TarantoolDataSource, DataSource { | ||
|
||
private PrintWriter logWriter; | ||
private String name = "Tarantool basic data source"; | ||
|
||
private Properties properties = new Properties(); | ||
|
||
@Override | ||
public Connection getConnection() throws SQLException { | ||
return new SQLConnection(makeUrl(), new Properties(properties)); | ||
} | ||
|
||
@Override | ||
public Connection getConnection(String username, String password) throws SQLException { | ||
Properties copyProperties = new Properties(properties); | ||
SQLProperty.USER.setString(copyProperties, username); | ||
SQLProperty.PASSWORD.setString(copyProperties, password); | ||
return new SQLConnection(makeUrl(), copyProperties); | ||
} | ||
|
||
@Override | ||
public PrintWriter getLogWriter() { | ||
return logWriter; | ||
} | ||
|
||
@Override | ||
public void setLogWriter(PrintWriter out) { | ||
logWriter = out; | ||
} | ||
|
||
@Override | ||
public void setLoginTimeout(int seconds) { | ||
SQLProperty.LOGIN_TIMEOUT.setInt(properties, (int) TimeUnit.SECONDS.toMillis(seconds)); | ||
} | ||
|
||
@Override | ||
public int getLoginTimeout() throws SQLException { | ||
return (int) TimeUnit.MILLISECONDS.toSeconds(SQLProperty.LOGIN_TIMEOUT.getInt(properties)); | ||
} | ||
|
||
@Override | ||
public Logger getParentLogger() throws SQLFeatureNotSupportedException { | ||
throw new SQLFeatureNotSupportedException(); | ||
} | ||
|
||
@Override | ||
public <T> T unwrap(Class<T> type) throws SQLException { | ||
if (isWrapperFor(type)) { | ||
return type.cast(this); | ||
} | ||
throw new SQLNonTransientException("SQLDataSource does not wrap " + type.getName()); | ||
} | ||
|
||
@Override | ||
public boolean isWrapperFor(Class<?> type) { | ||
return type.isAssignableFrom(this.getClass()); | ||
} | ||
|
||
@Override | ||
public String getServerName() { | ||
return SQLProperty.HOST.getString(properties); | ||
} | ||
|
||
@Override | ||
public void setServerName(String serverName) { | ||
SQLProperty.HOST.setString(properties, serverName); | ||
} | ||
|
||
@Override | ||
public int getPortNumber() throws SQLException { | ||
return SQLProperty.PORT.getInt(properties); | ||
} | ||
|
||
@Override | ||
public void setPortNumber(int port) { | ||
SQLProperty.PORT.setInt(properties, port); | ||
} | ||
|
||
@Override | ||
public String getUser() { | ||
return SQLProperty.USER.getString(properties); | ||
} | ||
|
||
@Override | ||
public void setUser(String userName) { | ||
SQLProperty.USER.setString(properties, userName); | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return SQLProperty.PASSWORD.getString(properties); | ||
} | ||
|
||
@Override | ||
public void setPassword(String password) { | ||
SQLProperty.PASSWORD.setString(properties, password); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Basic DataSource implementation - produces a standard Connection object. " + | ||
SQLConstant.DRIVER_NAME + "."; | ||
} | ||
|
||
@Override | ||
public String getDataSourceName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public void setDataSourceName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String getSocketChannelProvider() { | ||
return SQLProperty.SOCKET_CHANNEL_PROVIDER.getString(properties); | ||
} | ||
|
||
@Override | ||
public void setSocketChannelProvider(String classFqdn) { | ||
SQLProperty.SOCKET_CHANNEL_PROVIDER.setString(properties, classFqdn); | ||
} | ||
|
||
@Override | ||
public int getQueryTimeout() throws SQLException { | ||
return (int) TimeUnit.MILLISECONDS.toSeconds(SQLProperty.QUERY_TIMEOUT.getInt(properties)); | ||
} | ||
|
||
@Override | ||
public void setQueryTimeout(int seconds) { | ||
SQLProperty.QUERY_TIMEOUT.setInt(properties, (int) TimeUnit.SECONDS.toMillis(seconds)); | ||
} | ||
|
||
private String makeUrl() { | ||
return "jdbc:tarantool://" + | ||
SQLProperty.HOST.getString(properties) + ":" + SQLProperty.PORT.getString(properties); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/org/tarantool/jdbc/ds/TarantoolDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.tarantool.jdbc.ds; | ||
|
||
import java.sql.SQLException; | ||
|
||
/** | ||
* JDBC standard Tarantool specific data source properties. | ||
*/ | ||
public interface TarantoolDataSource { | ||
|
||
String getServerName() throws SQLException; | ||
|
||
void setServerName(String serverName) throws SQLException; | ||
|
||
int getPortNumber() throws SQLException; | ||
|
||
void setPortNumber(int port) throws SQLException; | ||
|
||
String getUser() throws SQLException; | ||
|
||
void setUser(String userName) throws SQLException; | ||
|
||
String getPassword() throws SQLException; | ||
|
||
void setPassword(String password) throws SQLException; | ||
|
||
String getDescription() throws SQLException; | ||
|
||
String getDataSourceName() throws SQLException; | ||
|
||
void setDataSourceName(String name) throws SQLException; | ||
|
||
String getSocketChannelProvider() throws SQLException; | ||
|
||
void setSocketChannelProvider(String classFqdn) throws SQLException; | ||
|
||
int getQueryTimeout() throws SQLException; | ||
|
||
void setQueryTimeout(int seconds) throws SQLException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.