summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/storage/Hikari.kt
blob: 480d533af94f62def190ec71cc6e7e99ec80a5f1 (plain)
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
package io.dico.parcels2.storage

import com.zaxxer.hikari.HikariConfig
import io.dico.parcels2.options.DataConnectionOptions

fun getHikariConfig(dialectName: String,
                    dco: DataConnectionOptions): HikariConfig = HikariConfig().apply {

    val (address, port) = dco.splitAddressAndPort() ?: throw IllegalArgumentException("Invalid address: ${dco.address}")

    when (dialectName) {
        "postgresql" -> run {
            dataSourceClassName = "org.postgresql.ds.PGSimpleDataSource"
            dataSourceProperties["serverName"] = address
            dataSourceProperties["portNumber"] = port.toString()
            dataSourceProperties["databaseName"] = dco.database
        }

        "mariadb" -> run {
            dataSourceClassName = "org.mariadb.jdbc.MariaDbDataSource"
            dataSourceProperties["serverName"] = address
            dataSourceProperties["port"] = port.toString()
            dataSourceProperties["databaseName"] = dco.database
            dataSourceProperties["properties"] = "useUnicode=true;characterEncoding=utf8"
        }

        else -> throw IllegalArgumentException("Unsupported dialect: $dialectName")
    }

    poolName = "parcels"
    maximumPoolSize = dco.poolSize
    username = dco.username
    password = dco.password
    connectionTimeout = 15000
    leakDetectionThreshold = 10000
    connectionTestQuery = "SELECT 1"


    /*

    addDataSourceProperty("serverName", address)
    addDataSourceProperty("port", port.toString())
    addDataSourceProperty("databaseName", dco.database)

    // copied from github.com/lucko/LuckPerms
    if (dialectName.toLowerCase() == "mariadb") {
        addDataSourceProperty("properties", "useUnicode=true;characterEncoding=utf8")
    } else if (dialectName.toLowerCase() == "h2") {
        dataSourceProperties.remove("serverName")
        dataSourceProperties.remove("port")
        dataSourceProperties.remove("databaseName")
        addDataSourceProperty("url", "jdbc:h2:${if (address.isBlank()) "" else "tcp://$address/"}~/${dco.database}")
    } else if (dialectName.toLowerCase() == "mysql") {
        // doesn't exist on the MariaDB driver
        addDataSourceProperty("cachePrepStmts", "true")
        addDataSourceProperty("alwaysSendSetIsolation", "false")
        addDataSourceProperty("cacheServerConfiguration", "true")
        addDataSourceProperty("elideSetAutoCommits", "true")
        addDataSourceProperty("useLocalSessionState", "true")

        // already set as default on mariadb
        addDataSourceProperty("useServerPrepStmts", "true")
        addDataSourceProperty("prepStmtCacheSize", "250")
        addDataSourceProperty("prepStmtCacheSqlLimit", "2048")
        addDataSourceProperty("cacheCallableStmts", "true")

        // make sure unicode characters can be used.
        addDataSourceProperty("characterEncoding", "utf8")
        addDataSourceProperty("useUnicode", "true")
    } else {

    }*/
}