-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathConfigDataSource.kt
53 lines (45 loc) · 1.91 KB
/
ConfigDataSource.kt
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
package klite.jdbc
import klite.Config
import klite.info
import klite.logger
import java.io.PrintWriter
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLFeatureNotSupportedException
import java.util.*
import javax.sql.DataSource
/** Non-pooled basic datasource, see also [PooledDataSource] */
open class ConfigDataSource(
val url: String = Config["DB_URL"],
val user: String? = Config.optional("DB_USER"),
pass: String? = Config.optional("DB_PASS"),
val isReadOnly: Boolean = Config.optional("DB_READONLY") == "true",
val props: Properties = Properties()
): DataSource {
init {
user?.let { props["user"] = it }
pass?.let { props["password"] = it }
if (url.startsWith("jdbc:postgresql") && "autosave" !in props) {
// handle "cached plan must not change result type" Postgres error if schema changes
// https://stackoverflow.com/questions/2783813/postgres-error-cached-plan-must-not-change-result-type
props["autosave"] = "conservative"
}
if (isReadOnly) {
props["readOnly"] = "true"
props["readOnlyMode"] = "always"
}
}
init {
logger().info("Connecting to $url${user?.let { ", user: $user" } ?: ""}")
}
override fun getConnection(): Connection = DriverManager.getConnection(url, props)
override fun getConnection(user: String?, pass: String?) = throw SQLFeatureNotSupportedException("Use getConnection()")
override fun getLogWriter(): PrintWriter? = null
override fun setLogWriter(out: PrintWriter?) = throw SQLFeatureNotSupportedException()
override fun getLoginTimeout() = 0
override fun setLoginTimeout(seconds: Int) = throw SQLFeatureNotSupportedException()
override fun <T: Any?> unwrap(iface: Class<T>?) = null
override fun isWrapperFor(iface: Class<*>?) = false
override fun getParentLogger() = throw SQLFeatureNotSupportedException()
override fun createConnectionBuilder() = throw SQLFeatureNotSupportedException()
}