Skip to content

Commit

Permalink
feat(101): Add postgresql database support (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarari authored Feb 11, 2024
1 parent 713ec9a commit 4080adc
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {
kapt(libs.moshi.codegen)
implementation(libs.commons.io)
implementation(libs.coroutines)
implementation("org.postgresql:postgresql:42.5.4")
}

tasks.withType<KotlinCompile>().configureEach {
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
[versions]
kotlin = "1.8.10"
detekt = "1.23.0"
sqlite_driver = "3.42.0.0"
sqlite_driver = "3.45.0.0"
mysql_driver = "8.0.33"
jetbrains_exposed = "0.41.1"
jetbrains_exposed = "0.46.0"
moshi = "1.15.0"
commons-io = "2.12.0"
mockk = "1.13.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import groovy.lang.Closure
import io.github.janbarari.gradle.ExcludeJacocoGenerated
import io.github.janbarari.gradle.analytics.database.DatabaseConnection
import io.github.janbarari.gradle.analytics.database.MySqlDatabaseConnection
import io.github.janbarari.gradle.analytics.database.PostgresDatabaseConnection
import io.github.janbarari.gradle.analytics.database.SqliteDatabaseConnection

@ExcludeJacocoGenerated
Expand Down Expand Up @@ -68,6 +69,27 @@ class DatabaseConfig @JvmOverloads constructor() : java.io.Serializable {
return temp
}

/**
* Factory method for create a new instance
* of [io.github.janbarari.gradle.analytics.database.PostgresDatabaseConnection].
*/
fun postgres(block: PostgresDatabaseConnection.() -> Unit): PostgresDatabaseConnection {
return PostgresDatabaseConnection {
also(block)
}
}

/**
* Factory method for create a new instance
* of [io.github.janbarari.gradle.analytics.database.PostgresDatabaseConnection].
*/
fun postgres(closure: Closure<*>): PostgresDatabaseConnection {
val temp = PostgresDatabaseConnection { }
closure.delegate = temp
closure.call()
return temp
}

/**
* Factory method for create a new instance
* of [io.github.janbarari.gradle.analytics.database.SqliteDatabaseConnection].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,26 @@ class Database(
when (databaseConfig) {
is MySqlDatabaseConnection -> {
LongTextColumnType.longTextType = LongTextColumnType.Companion.LongTextType.MEDIUMTEXT
connectToMysqlDatabase(databaseConfig as MySqlDatabaseConnection)
connectMysqlDatabase(databaseConfig as MySqlDatabaseConnection)
ResetAutoIncremental.dbType = MySqlDatabaseConnection::class.java
}
is SqliteDatabaseConnection -> {
LongTextColumnType.longTextType = LongTextColumnType.Companion.LongTextType.TEXT
connectSqliteDatabase(databaseConfig as SqliteDatabaseConnection)
ResetAutoIncremental.dbType = SqliteDatabaseConnection::class.java
}
is PostgresDatabaseConnection -> {
LongTextColumnType.longTextType = LongTextColumnType.Companion.LongTextType.TEXT
connectPostgresDatabase(databaseConfig as PostgresDatabaseConnection)
ResetAutoIncremental.dbType = PostgresDatabaseConnection::class.java
}
}

createTables(MetricTable, TemporaryMetricTable, SingleMetricTable)
}
}

private fun connectToMysqlDatabase(config: MySqlDatabaseConnection) {
private fun connectMysqlDatabase(config: MySqlDatabaseConnection) {
tower.i(clazz, "connectToMysqlDatabase()")
_database = Database.connect(
url = "jdbc:mysql://${config.host}:${config.port}/${config.name}",
Expand All @@ -103,6 +108,16 @@ class Database(
)
}

private fun connectPostgresDatabase(config: PostgresDatabaseConnection) {
tower.i(clazz, "connectPostgresDatabase()")
_database = Database.connect(
url = "jdbc:postgresql://${config.host}:${config.port}/${config.name}",
driver = "org.postgresql.Driver",
user = config.user,
password = config.password
)
}

/**
* Creates the database tables if not exist.
*/
Expand All @@ -116,11 +131,10 @@ class Database(
@ExcludeJacocoGenerated
fun <T> transaction(statement: Transaction.() -> T): T {
return transaction(
_database.transactionManager.defaultIsolationLevel,
_database.transactionManager.defaultRepetitionAttempts,
transactionIsolation = _database.transactionManager.defaultIsolationLevel,
readOnly = false,
_database,
statement
db = _database,
statement = statement
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import io.github.janbarari.gradle.ExcludeJacocoGenerated
class MySqlDatabaseConnection(block: MySqlDatabaseConnection.() -> Unit): DatabaseConnection() {

companion object {
const val DEFAULT_MYSQL_PORT = 3306
const val DEFAULT_PORT = 3306
}

var host: String? = null

var name: String? = null

var port: Int = DEFAULT_MYSQL_PORT
var port: Int = DEFAULT_PORT

var user: String = "root"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* MIT License
* Copyright (c) 2022 Mehdi Janbarari (@janbarari)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.github.janbarari.gradle.analytics.database

import io.github.janbarari.gradle.ExcludeJacocoGenerated

@ExcludeJacocoGenerated
class PostgresDatabaseConnection(block: PostgresDatabaseConnection.() -> Unit): DatabaseConnection() {

companion object {
const val DEFAULT_PORT = 5432
}

var host: String? = null

var name: String? = null

var port: Int = DEFAULT_PORT

var user: String = ""

var password: String = ""

init {
apply(block)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ object ResetAutoIncremental {
if (dbType == SqliteDatabaseConnection::class.java) {
return "UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='$tableName';"
}
if (dbType == PostgresDatabaseConnection::class.java) {
return "ALTER SEQUENCE $tableName RESTART WITH 0;"
}
}
return null
}
Expand Down

0 comments on commit 4080adc

Please sign in to comment.