Skip to content
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

[APIS-869] Administrative setting of CUBRID jdbc property #15

Merged
merged 2 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 98 additions & 12 deletions src/jdbc/cubrid/jdbc/driver/CUBRIDDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import cubrid.jdbc.jci.UJCIManager;
import cubrid.jdbc.jci.UJCIUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.Driver;
Expand Down Expand Up @@ -86,6 +88,7 @@ public class CUBRIDDriver implements Driver {
"jdbc:cubrid(-oracle|-mysql)?:([a-zA-Z_0-9\\.-]*):([0-9]*):([^:]+):([^:]*):([^:]*):(\\?[a-zA-Z_0-9]+=[^&=?]+(&[a-zA-Z_0-9]+=[^&=?]+)*)?";
private static final String CUBRID_JDBC_URL_HEADER = "jdbc:cubrid";
private static final String JDBC_DEFAULT_CONNECTION = "jdbc:default:connection";
private static final String ENV_JDBC_PROP_NAME = "CUBRID_JDBC_PROP";

static {
try {
Expand Down Expand Up @@ -123,6 +126,17 @@ public static void printDebug(String msg) {
*/

public Connection connect(String url, Properties info) throws SQLException {
Connection conn = null;
String holdability = null;
String dummy = null;
String host = null;
String portString = null;
String db = null;
String user = null;
String pass = null;
String prop = null;
int port = default_port;

if (!acceptsURL(url)) {
return null;
}
Expand All @@ -142,14 +156,10 @@ public Connection connect(String url, Properties info) throws SQLException {
throw new CUBRIDException(CUBRIDJDBCErrorCode.invalid_url, url, null);
}

String dummy;
String host = matcher.group(2);
String portString = matcher.group(3);
String db = matcher.group(4);
String user;
String pass;
String prop = matcher.group(7);
int port = default_port;
host = matcher.group(2);
portString = matcher.group(3);
db = matcher.group(4);
prop = matcher.group(7);

UClientSideConnection u_con;
String resolvedUrl;
Expand All @@ -165,9 +175,6 @@ public Connection connect(String url, Properties info) throws SQLException {
port = Integer.parseInt(portString);
}

connProperties = new ConnectionProperties();
connProperties.setProperties(prop);

user = info.getProperty("user");
if (user == null) {
user = matcher.group(5);
Expand All @@ -178,11 +185,80 @@ public Connection connect(String url, Properties info) throws SQLException {
pass = matcher.group(6);
}

String filePath = System.getenv(ENV_JDBC_PROP_NAME);

if (filePath != null) {
if (existPropertiesFile(filePath)) {
Properties temp_prop = new Properties();
FileInputStream in;
try {
in = new FileInputStream(filePath);
temp_prop.load(in);
in.close();

StringBuilder sbProp = new StringBuilder();
String value = null;
for (String key : temp_prop.stringPropertyNames()) {
value = temp_prop.getProperty(key);
if (sbProp.length() == 0) {
sbProp.append("?");
} else {
sbProp.append("&");
}
sbProp.append(key + "=" + value);
}

if (sbProp == null || sbProp.length() <= 0) {
throw new CUBRIDException(
CUBRIDJDBCErrorCode.invalid_prop_file, filePath, null);
}

prop = sbProp.toString();
url =
"jdbc:cubrid:"
+ host
+ ":"
+ port
+ ":"
+ db
+ ":"
+ user
+ ":"
+ pass
+ ":"
+ prop;

Matcher propMatcher = pattern.matcher(url);
if (!propMatcher.find()) {
throw new CUBRIDException(
CUBRIDJDBCErrorCode.invalid_prop_file, filePath, null);
}

String propMatch = propMatcher.group();
if (!propMatch.equals(url)) {
throw new CUBRIDException(
CUBRIDJDBCErrorCode.invalid_prop_file, filePath, null);
}

} catch (FileNotFoundException e) {
throw new CUBRIDException(
CUBRIDJDBCErrorCode.file_not_found_prop, filePath, null);
} catch (IOException e) {
throw new CUBRIDException(
CUBRIDJDBCErrorCode.invalid_prop_file, filePath, null);
}
} else {
throw new CUBRIDException(CUBRIDJDBCErrorCode.file_not_found_prop, filePath, null);
}
}

resolvedUrl = "jdbc:cubrid:" + host + ":" + port + ":" + db + ":" + user + ":********:";
if (prop != null) {
resolvedUrl += prop;
}

connProperties = new ConnectionProperties();
connProperties.setProperties(prop);
connProperties.setProperties(info);

dummy = connProperties.getAltHosts();
Expand Down Expand Up @@ -221,7 +297,12 @@ public Connection connect(String url, Properties info) throws SQLException {

u_con.setConnectionProperties(connProperties);
u_con.tryConnect();
return new CUBRIDConnection(u_con, url, user);

conn = new CUBRIDConnection(u_con, url, user);
if (conn != null) {
conn.setHoldability(connProperties.getHoldCursor());
}
return conn;
}

public Connection defaultConnection() throws SQLException {
Expand Down Expand Up @@ -302,4 +383,9 @@ public boolean jdbcCompliant() {
public Logger getParentLogger() {
throw new java.lang.UnsupportedOperationException();
}

private boolean existPropertiesFile(String filePath) {
File file = new File(filePath);
return file.exists();
}
}
4 changes: 4 additions & 0 deletions src/jdbc/cubrid/jdbc/driver/CUBRIDJDBCErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class CUBRIDJDBCErrorCode {
public static int lob_pos_invalid = -21139;
public static int lob_is_not_writable = -21140;
public static int request_timeout = -21141;
public static int invalid_prop_file = -21142;
public static int file_not_found_prop = -21143;

private static Hashtable<Integer, String> messageString;

Expand Down Expand Up @@ -169,6 +171,8 @@ private static void setMessageHash() {
messageString.put(new Integer(lob_pos_invalid), "Lob position to write is invalid.");
messageString.put(new Integer(lob_is_not_writable), "Lob is not writable.");
messageString.put(new Integer(request_timeout), "Request timed out.");
messageString.put(new Integer(invalid_prop_file), "Invalid file - ");
messageString.put(new Integer(file_not_found_prop), "File not found - ");
}

public static String getMessage(int code) {
Expand Down
11 changes: 11 additions & 0 deletions src/jdbc/cubrid/jdbc/driver/ConnectionProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import cubrid.jdbc.jci.UConnection;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
Expand Down Expand Up @@ -392,6 +393,8 @@ private int getDefaultConnectTimeout() {
IntegerConnectionProperty clientCacheSize =
new IntegerConnectionProperty("clientCacheSize", 1, 1, 1024);

BooleanConnectionProperty holdCursor = new BooleanConnectionProperty("hold_cursor", false);

public boolean getLogOnException() {
return logOnException.getValueAsBoolean();
}
Expand Down Expand Up @@ -459,4 +462,12 @@ public boolean getUseSSL() {
public int getClientCacheSize() {
return clientCacheSize.getValueAsInteger();
}

public int getHoldCursor() {
int holdability = ResultSet.HOLD_CURSORS_OVER_COMMIT;
if (holdCursor.getValueAsBoolean() == true) {
holdability = ResultSet.CLOSE_CURSORS_AT_COMMIT;
}
return holdability;
}
}