forked from dork/tarantool-java
-
Notifications
You must be signed in to change notification settings - Fork 19
Add a simple DataSource implementation #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) { | ||
nicktorwald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.