1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package org.opendc.common.utils
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException
/**
* Represents the Postgresql database.
* On setup cleans the entire database and creates empty tables.
*
* @author Mateusz
*
* @param address ipv4 address
* @param port postgres post
* @param dbName database name
* @param user
* @param password
*/
public class PostgresqlDB(
address : String,
port : Int,
dbName : String,
private var user : String,
private var password : String,
) {
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)
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)
} catch (e: SQLException){
println("${e.message}")
}
}
}
|