From 0731bd58889df127ef87aba2590d505d79e6646f Mon Sep 17 00:00:00 2001 From: mjkwiatkowski Date: Mon, 15 Jun 2026 23:48:44 +0200 Subject: feat: migrated the past project to the sunfish repo --- .../kotlin/org/opendc/common/utils/PostgresqlDB.kt | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt (limited to 'opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt') diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt new file mode 100644 index 00000000..35d03feb --- /dev/null +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt @@ -0,0 +1,52 @@ +package org.opendc.common.utils + +import com.fasterxml.jackson.dataformat.toml.TomlMapper +import java.sql.Connection +import java.sql.DriverManager +import java.sql.SQLException +import java.util.Properties +/** + * Represents the Postgresql database. + * On setup cleans the entire database. + * + * @author Mateusz Kwiatkowski + * + * @see + * https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/DriverManager.html + */ +@Suppress("DEPRECATION") +public class PostgresqlDB { + private var properties = Properties() + private var connection : Connection? = null + + init { + try { + properties = TomlMapper().readerFor(Properties().javaClass) + .readValue(PostgresqlDB::class.java.getResource("/database.toml")) + connection = DriverManager.getConnection( + properties.getProperty("address").asJdbc(properties.getProperty("database")), + properties.getProperty("user"), + properties.getProperty("password")) + clear() + } catch (e: SQLException) { + print("${e.message}") + } + } + public fun clear(){ + val DELETE_ALL_TABLES = """ + DROP SCHEMA public CASCADE; + CREATE SCHEMA public; + """.trimIndent() + try { + val st = connection?.createStatement() + st?.executeQuery(DELETE_ALL_TABLES) + } catch (e: SQLException){ + println("${e.message}") + } + } + + private fun String.asJdbc(database : String) : String { + return "jdbc:postgresql://$this/$database" + } + +} \ No newline at end of file -- cgit v1.2.3