From 86d35fcec83057e346e4982b5a6908f25342a392 Mon Sep 17 00:00:00 2001 From: mjkwiatkowski Date: Sun, 15 Feb 2026 18:26:59 +0100 Subject: feat: opendc -> postgresql connection works --- .../kotlin/org/opendc/common/utils/PostgresqlDB.kt | 56 ++++++++++++++++++++++ 1 file changed, 56 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..3dc7a0e4 --- /dev/null +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt @@ -0,0 +1,56 @@ +package org.opendc.common.utils + +import java.sql.Connection +import java.sql.DriverManager +import java.sql.SQLException + +public class PostgresqlDB { + public var connection : Connection? = null + public var dbUrl : String = "" + public var user : String = "" + public var password : String = "" + + public fun setupDatabase(address : String, port : Int, dbUser : String, dbPassword : String, db : String){ + dbUrl = "jdbc:postgresql://$address:$port/$db" + user = dbUser + password = dbPassword + println(dbUrl) + try { + connection = DriverManager.getConnection(dbUrl, dbUser, dbPassword) + } catch (e: SQLException) { + print("${e.message}") + } + } + + public fun create(){ + val CREATE_TABLE = """ + CREATE TABLE metrics ( + id SERIAL PRIMARY KEY, + timestamp bigint, + tasksActive integer, + clusterName varchar(10)); + """.trimIndent() + + try { + val conn = DriverManager.getConnection(dbUrl, user, password) + val st = conn.createStatement() + st.executeQuery(CREATE_TABLE) + } catch (e: SQLException){ + println("${e.message}") + } + } + + public fun clear(){ + val DELETE_ALL_TABLES = """ + DROP SCHEMA public CASCADE; + CREATE SCHEMA public; + """.trimIndent() + try { + val conn = DriverManager.getConnection(dbUrl, user, password) + val st = conn.createStatement() + st.executeQuery(DELETE_ALL_TABLES) + } catch (e: SQLException){ + println("${e.message}") + } + } +} \ No newline at end of file -- cgit v1.2.3