summaryrefslogtreecommitdiff
path: root/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt
diff options
context:
space:
mode:
authormjkwiatkowski <mati.rewa@gmail.com>2026-02-20 16:17:39 +0100
committermjkwiatkowski <mati.rewa@gmail.com>2026-02-20 16:17:39 +0100
commitf5da60e4275ca1172128c3994298691e12d5e1f8 (patch)
tree189804251bf88bf390e1c9ffb4472b7a798d7f22 /opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt
parent2f16cb0f48eca4453e3e894b3d45a3aa09e6dcc0 (diff)
fix: changed the syntex to slowly get rid of the Config classHEADmaster
Diffstat (limited to 'opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt')
-rw-r--r--opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt63
1 files changed, 22 insertions, 41 deletions
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
index 69314ef3..361925ee 100644
--- a/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt
+++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt
@@ -1,71 +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 and creates empty tables.
+ * On setup cleans the entire database.
*
- * @author Mateusz
+ * @author Mateusz Kwiatkowski
*
- * @param address ipv4 address
- * @param port postgres post
- * @param dbName database name
- * @param user
- * @param password
+ * @see <a href=https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/DriverManager.html>
+ * https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/DriverManager.html</a>
*/
-public class PostgresqlDB(
- address : String,
- port : Int,
- dbName : String,
- private var user : String,
- private var password : String,
-) {
+@Suppress("DEPRECATION")
+public class PostgresqlDB {
+ private var properties = Properties()
private var connection : Connection? = null
- private var dbUrl : String = ""
init {
- dbUrl = "jdbc:postgresql://$address:$port/$dbName"
- println(dbUrl)
try {
- connection = DriverManager.getConnection(dbUrl, user, password)
+ properties = TomlMapper().readerFor(Properties().javaClass)
+ .readValue(PostgresqlDB::class.java.getResource("/database.toml"))
+ connection = DriverManager.getConnection(
+ properties.getProperty("address").asJdbc(properties.getProperty("table")),
+ properties.getProperty("user"),
+ properties.getProperty("password"))
clear()
- setup()
} catch (e: SQLException) {
print("${e.message}")
}
}
- public fun setup(){
- 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)
+ val st = connection?.createStatement()
+ st?.executeQuery(DELETE_ALL_TABLES)
} catch (e: SQLException){
println("${e.message}")
}
}
+
+ private fun String.asJdbc(table : String) : String {
+ return "jdbc:postgresql://$this/$table"
+ }
+
} \ No newline at end of file