Collapse into top-level module
This commit is contained in:
62
src/main/kotlin/Configuration.kt
Normal file
62
src/main/kotlin/Configuration.kt
Normal file
@@ -0,0 +1,62 @@
|
||||
package solutions.bitbadger.documents
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.sql.Connection
|
||||
import java.sql.DriverManager
|
||||
|
||||
object Configuration {
|
||||
|
||||
/**
|
||||
* JSON serializer; replace to configure with non-default options
|
||||
*
|
||||
* The default sets `encodeDefaults` to `true` and `explicitNulls` to `false`; see
|
||||
* https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md for all configuration options
|
||||
*/
|
||||
var json = Json {
|
||||
encodeDefaults = true
|
||||
explicitNulls = false
|
||||
}
|
||||
|
||||
/** The field in which a document's ID is stored */
|
||||
var idField = "id"
|
||||
|
||||
/** The automatic ID strategy to use */
|
||||
var autoIdStrategy = AutoId.DISABLED
|
||||
|
||||
/** The length of automatic random hex character string */
|
||||
var idStringLength = 16
|
||||
|
||||
/** The derived dialect value from the connection string */
|
||||
private var dialectValue: Dialect? = null
|
||||
|
||||
/** The connection string for the JDBC connection */
|
||||
var connectionString: String? = null
|
||||
set(value) {
|
||||
field = value
|
||||
dialectValue = if (value.isNullOrBlank()) null else Dialect.deriveFromConnectionString(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a new connection to the configured database
|
||||
*
|
||||
* @return A new connection to the configured database
|
||||
* @throws IllegalArgumentException If the connection string is not set before calling this
|
||||
*/
|
||||
fun dbConn(): Connection {
|
||||
if (connectionString == null) {
|
||||
throw IllegalArgumentException("Please provide a connection string before attempting data access")
|
||||
}
|
||||
return DriverManager.getConnection(connectionString)
|
||||
}
|
||||
|
||||
/**
|
||||
* The dialect in use
|
||||
*
|
||||
* @param process The process being attempted
|
||||
* @return The dialect for the current connection
|
||||
* @throws DocumentException If the dialect has not been set
|
||||
*/
|
||||
fun dialect(process: String? = null): Dialect =
|
||||
dialectValue ?: throw DocumentException(
|
||||
"Database mode not set" + if (process == null) "" else "; cannot $process")
|
||||
}
|
||||
Reference in New Issue
Block a user