diff options
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.kt | 56 |
1 files changed, 56 insertions, 0 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 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 |
