diff options
author | Marvin Borner | 2019-04-07 13:01:13 +0200 |
---|---|---|
committer | Marvin Borner | 2019-04-07 13:01:13 +0200 |
commit | ea6d0b7176fb2133f170421666f410dfce20ca31 (patch) | |
tree | 34ef84d06ef6f1687c6f09b3cd7bb187120c3579 /src/main/kotlin/DatabaseController.kt | |
parent | 05f73150bc060dc9dc52e34da6323a38c433e118 (diff) |
Updated documentation
Diffstat (limited to 'src/main/kotlin/DatabaseController.kt')
-rw-r--r-- | src/main/kotlin/DatabaseController.kt | 41 |
1 files changed, 15 insertions, 26 deletions
diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt index 32e33ef..86d20a8 100644 --- a/src/main/kotlin/DatabaseController.kt +++ b/src/main/kotlin/DatabaseController.kt @@ -6,8 +6,8 @@ import java.sql.* import java.util.logging.* class DatabaseController(dbFileLocation: String = "main.db") { - val db: Database - val LOG = Logger.getLogger(this.javaClass.name) + val db: Database = Database.connect("jdbc:sqlite:$dbFileLocation", "org.sqlite.JDBC") + private val log = Logger.getLogger(this.javaClass.name) /** * Database table for the file location indexing @@ -21,9 +21,8 @@ class DatabaseController(dbFileLocation: String = "main.db") { * Database table to index the users with their regarding passwords */ object UserData : Table() { - // only for multiple users: - // val id = integer("id").autoIncrement().primaryKey() - val username = varchar("username", 24).primaryKey() // remove .primaryKey(), if id column is used + val id = integer("id").autoIncrement().primaryKey() + val username = varchar("username", 24) val password = varchar("password", 64) val role = varchar("role", 64).default("USER") } @@ -32,20 +31,22 @@ class DatabaseController(dbFileLocation: String = "main.db") { * Database table storing general data/states */ object General : Table() { - val initialUse = integer("initialUse").default(1).primaryKey() // boolean -> 0:1 + val initialUse = integer("initialUse").default(1).primaryKey() } init { - // create connection - this.db = Database.connect("jdbc:sqlite:$dbFileLocation", "org.sqlite.JDBC") + // Create connection TransactionManager.manager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE - // add tables + // Add tables transaction { SchemaUtils.createMissingTablesAndColumns(FileLocation, UserData, General) } } + /** + * Creates the user in the database using username, password and the role + */ fun createUser(usernameString: String, passwordHash: String, roleString: String) { transaction { try { @@ -55,12 +56,15 @@ class DatabaseController(dbFileLocation: String = "main.db") { it[role] = roleString } } catch (_: org.jetbrains.exposed.exceptions.ExposedSQLException) { - LOG.warning("User already exists!") + log.warning("User already exists!") } } } + /** + * Returns a list of the username paired with the corresponding role using [usernameString] + */ fun getUser(usernameString: String): List<Pair<String, Roles>> { return transaction { return@transaction UserData.select { UserData.username eq usernameString }.map { @@ -69,20 +73,5 @@ class DatabaseController(dbFileLocation: String = "main.db") { } } - /* - fun selectUser(uname: String) :String { - var pwd :String - - transaction { - UserData.select{UserData.username eq uname}.forEach { - pwd = it[UserData.password] - } - } - - // return pwd - } - - */ - - // TODO add functions for database usage + // TODO: Add more functions for database interaction } |