forked from duckdb/duckdb-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcUtils.java
More file actions
78 lines (68 loc) · 2.42 KB
/
JdbcUtils.java
File metadata and controls
78 lines (68 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.duckdb;
import static org.duckdb.DuckDBDriver.DUCKDB_URL_PREFIX;
import static org.duckdb.DuckDBDriver.MEMORY_DB;
import java.sql.SQLException;
import java.util.Properties;
final class JdbcUtils {
private JdbcUtils() {
}
@SuppressWarnings("unchecked")
static <T> T unwrap(Object obj, Class<T> iface) throws SQLException {
if (!iface.isInstance(obj)) {
throw new SQLException(obj.getClass().getName() + " not unwrappable from " + iface.getName());
}
return (T) obj;
}
static String removeOption(Properties props, String opt) {
return removeOption(props, opt, null);
}
static String removeOption(Properties props, String opt, String defaultVal) {
Object obj = props.remove(opt);
if (null != obj) {
return obj.toString().trim();
}
return defaultVal;
}
static void setDefaultOptionValue(Properties props, String opt, Object value) {
if (props.contains(opt)) {
return;
}
props.put(opt, value);
}
static boolean isStringTruish(String val, boolean defaultVal) throws SQLException {
if (null == val) {
return defaultVal;
}
String valLower = val.toLowerCase().trim();
if (valLower.equals("true") || valLower.equals("1") || valLower.equals("yes") || valLower.equals("on")) {
return true;
}
if (valLower.equals("false") || valLower.equals("0") || valLower.equals("no") || valLower.equals("off")) {
return false;
}
throw new SQLException("Invalid boolean option value: " + val);
}
static String dbNameFromUrl(String url) throws SQLException {
if (null == url) {
throw new SQLException("Invalid null URL specified");
}
if (!url.startsWith(DUCKDB_URL_PREFIX)) {
throw new SQLException("DuckDB JDBC URL needs to start with 'jdbc:duckdb:'");
}
final String shortUrl;
if (url.contains(";")) {
String[] parts = url.split(";");
shortUrl = parts[0].trim();
} else {
shortUrl = url;
}
String dbName = shortUrl.substring(DUCKDB_URL_PREFIX.length()).trim();
if (dbName.length() == 0) {
dbName = MEMORY_DB;
}
if (dbName.startsWith(MEMORY_DB.substring(1))) {
dbName = ":" + dbName;
}
return dbName;
}
}