From 07b3c3821f90b37efbb539fbf1d3e90543599555 Mon Sep 17 00:00:00 2001 From: theBreadCompany Date: Mon, 2 Jun 2025 07:53:13 +0200 Subject: [PATCH] more sql and models --- src/main/kotlin/Main.kt | 10 +- src/main/kotlin/Models.kt | 222 ++++++++++++++++++---- src/main/resources/prepare/add_stage.sql | 2 + src/main/resources/prepare/add_task.sql | 2 + src/main/resources/prepare/add_user.sql | 2 +- src/main/resources/prepare/get_list.sql | 1 + src/main/resources/prepare/get_stage.sql | 1 + src/main/resources/prepare/get_stages.sql | 1 + src/main/resources/prepare/get_task.sql | 2 + src/main/resources/prepare/get_tasks.sql | 1 + src/main/resources/prepare/get_user.sql | 1 + src/test/kotlin/DatabaseTests.kt | 2 - 12 files changed, 201 insertions(+), 46 deletions(-) create mode 100644 src/main/resources/prepare/add_stage.sql create mode 100644 src/main/resources/prepare/add_task.sql create mode 100644 src/main/resources/prepare/get_list.sql create mode 100644 src/main/resources/prepare/get_stage.sql create mode 100644 src/main/resources/prepare/get_stages.sql create mode 100644 src/main/resources/prepare/get_task.sql create mode 100644 src/main/resources/prepare/get_tasks.sql create mode 100644 src/main/resources/prepare/get_user.sql diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 8012d92..40a0c71 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -18,18 +18,16 @@ class KlistApp { } private fun dbSetup(conn: Connection) { - val initSql = File("init.sql").readText() + val initSql = javaClass.getResource("/init.sql")!!.readText() conn.createStatement().executeUpdate(initSql) } private fun dbPrepare(conn: Connection): Map { return PreparedStatementCase.entries.associateWith { case -> conn.prepareStatement( - File( - "prepare/${ - case.toString().lowercase() - }.sql" - ).readText() + javaClass.getResource("/prepare/${ + case.toString().lowercase() + }.sql")!!.readText() ) } } diff --git a/src/main/kotlin/Models.kt b/src/main/kotlin/Models.kt index a4961e4..7beb431 100644 --- a/src/main/kotlin/Models.kt +++ b/src/main/kotlin/Models.kt @@ -1,13 +1,17 @@ package dev.thebread +import dev.thebread.Model.PreparedStatementCase +import dev.thebread.Models.Stage import java.sql.PreparedStatement +import java.sql.ResultSet import java.time.LocalDate +import java.util.Date -abstract class Model { +abstract class Model> { enum class PreparedStatementCase { - GET_USERS, GET_LIST_FOR_USER, GET_LISTS, GET_STAGES_FOR_LIST, - ADD_LIST, ADD_USER, ADD_LIST_FOR_USER, ADD_STAGE_FOR_LIST, ADD_TASK_TO_LIST, + GET_USER, GET_USERS, GET_LIST_FOR_USER, GET_LIST, GET_LISTS, GET_STAGE, GET_STAGES, GET_STAGES_FOR_LIST, GET_TASK, GET_TASKS, + ADD_LIST, ADD_USER, ADD_LIST_FOR_USER, ADD_STAGE_FOR_LIST, ADD_TASK_TO_LIST, ADD_STAGE, ADD_TASK, MOVE_TASK_TO_STAGE, REMOVE_TASK, REMOVE_USER, REMOVE_LIST_FOR_USER, REG_LOCAL_FOR_USER, REG_DISCORD_FOR_USER, REG_GOOGLE_FOR_USER, @@ -15,53 +19,197 @@ abstract class Model { } companion object Common { - public var stmts: Map? = null + var stmts: Map? = null } - abstract var id: Int? + constructor() {} + var id: Int? = null - fun id(): Int? { - if(id == null) { - this.id = create().id - } - return id + fun assureStatements(): Map { + return stmts!! } fun setStatements(stmts: Map) { Common.stmts = stmts } - - abstract fun get(id: Int): Model? - abstract fun getAll(): Set - abstract fun create(): Model + abstract fun get(id: Int): T? + abstract fun getAll(): Set + abstract fun create(): T } class Models { - class User { - val name: String? = null - val discord: Int? = null - val google: Int? = null - } + class User() : Model() { + lateinit var name: String + var local: Int = -1 + var discord: Int = -1 + var google: Int = -1 - class List { - val id: Int? = null - val name: String? = null - val description: String? = null - } - - class Task { - class Stage { - val id: Int? = null - val name: String? = null - val description: String? = null - val list: List? = null + constructor(result: ResultSet) : this() { + this.id = result.getInt("id") + this.name = result.getString("name") + this.local = result.getInt("local_id") + this.discord = result.getInt("discord_id") + this.google = result.getInt("google_id") } - val id: Int? = null - val name: String? = null - val description: String? = null - val stage: Stage? = null - val due: LocalDate? = null - val dueStage: Stage? = null + override fun get(id: Int): User? { + val stmt = assureStatements()[PreparedStatementCase.GET_USER]!! + stmt.setInt(1, id) + val result = stmt.executeQuery() + val user = User(result) + stmt.clearParameters() + return user + } + + override fun getAll(): Set { + val stmt = assureStatements()[PreparedStatementCase.GET_USERS]!! + val result = stmt.executeQuery() + val users = mutableSetOf() + do { users.add(User(result)) } while (result.next()) + return users + } + + override fun create(): User { + val stmt = assureStatements()[PreparedStatementCase.ADD_USER]!! + stmt.setString(1, this.name) + if(this.local != null) stmt.setInt(2, this.local!!) + if(this.discord != null) stmt.setInt(3, this.discord!!) + if(this.google != null) stmt.setInt(4, this.google!!) + val result = stmt.executeQuery() + val user = User(result) + stmt.clearParameters() + return user + } + } + + class List() : Model() { + lateinit var name: String + lateinit var description: String + + constructor(result: ResultSet) : this() { + this.id = result.getInt("id") + this.name = result.getString("name") + this.description = result.getString("description") + } + + override fun get(id: Int): List? { + val stmt = assureStatements()[PreparedStatementCase.GET_LIST]!! + stmt.setInt(1, id) + val result = stmt.executeQuery() + val list = List(result) + stmt.clearParameters() + return list + } + + override fun getAll(): Set { + val stmt = assureStatements()[PreparedStatementCase.GET_LISTS]!! + val result = stmt.executeQuery() + val lists = mutableSetOf() + do { lists.add(List(result)) } while (result.next()) + return lists + } + override fun create(): List { + val stmt = assureStatements()[PreparedStatementCase.ADD_LIST]!! + stmt.setString(1, this.name) + if(this.description != null) stmt.setString(2, this.description) + val result = stmt.executeQuery() + val list = List(result) + stmt.clearParameters() + return list + } + } + + class Stage() : Model() { + lateinit var name: String + lateinit var description: String + var list: List? = null + + constructor(result: ResultSet) : this() { + this.id = result.getInt("id") + this.name = result.getString("name") + this.description = result.getString("description") + val listId = result.getInt("list_id") + this.list = List().get(listId) + } + + override fun get(id: Int): Stage? { + val stmt = assureStatements()[PreparedStatementCase.GET_STAGE]!! + stmt.setInt(1, id) + val result = stmt.executeQuery() + val stage = Stage(result) + stmt.clearParameters() + return stage + } + + override fun getAll(): Set { + val stmt = assureStatements()[PreparedStatementCase.GET_STAGES]!! + val result = stmt.executeQuery() + val stages = mutableSetOf() + do { + stages.add(Stage(result)) + } while (result.next()) + return stages + } + + override fun create(): Stage { + val stmt = assureStatements()[PreparedStatementCase.ADD_STAGE]!! + stmt.setString(1, this.name) + if (this.description != null) stmt.setString(2, this.description) + val result = stmt.executeQuery() + val stage = Stage(result) + stmt.clearParameters() + return stage + } + } + + class Task() : Model() { + lateinit var name: String + lateinit var description: String + var stage: Stage? = null + var due: Date? = null + var dueStage: Stage? = null + + constructor(result: ResultSet) : this() { + this.id = result.getInt("id") + this.name = result.getString("name") + this.description = result.getString("description") + val stageId = result.getInt("stage_id") + this.stage = Stage().get(stageId) + this.due = result.getDate("due") + val dueStageId = result.getInt("due_stage_id") + this.dueStage = Stage().get(dueStageId) + } + + override fun get(id: Int): Task? { + val stmt = assureStatements()[PreparedStatementCase.GET_TASK]!! + stmt.setInt(0, id) + val result = stmt.executeQuery() + val task = Task(result) + stmt.clearParameters() + return task + } + + override fun getAll(): Set { + val stmt = assureStatements()[PreparedStatementCase.GET_TASKS]!! + val result = stmt.executeQuery() + val tasks = mutableSetOf() + do { + tasks.add(Task(result)) + } while (result.next()) + return tasks + } + + override fun create(): Task { + val stmt = assureStatements()[PreparedStatementCase.ADD_TASK]!! + stmt.setString(1, this.name) + stmt.setString(2, this.description) + if (this.stage != null) stmt.setInt(3, this.stage!!.id!!) + if (this.due != null) stmt.setDate(4, this.due!! as java.sql.Date?) + if (this.dueStage != null) stmt.setInt(5, this.dueStage!!.id!!) + val result = stmt.executeQuery() + val task = Task(result) + stmt.clearParameters() + return task + } } } \ No newline at end of file diff --git a/src/main/resources/prepare/add_stage.sql b/src/main/resources/prepare/add_stage.sql new file mode 100644 index 0000000..2704f3e --- /dev/null +++ b/src/main/resources/prepare/add_stage.sql @@ -0,0 +1,2 @@ +insert into task_stages +values (?1, ?2, ?3) \ No newline at end of file diff --git a/src/main/resources/prepare/add_task.sql b/src/main/resources/prepare/add_task.sql new file mode 100644 index 0000000..8d368ec --- /dev/null +++ b/src/main/resources/prepare/add_task.sql @@ -0,0 +1,2 @@ +insert into tasks +values (?1, ?2, ?3, ?4, ?5, ?6); \ No newline at end of file diff --git a/src/main/resources/prepare/add_user.sql b/src/main/resources/prepare/add_user.sql index fbe5b9a..bc664ae 100644 --- a/src/main/resources/prepare/add_user.sql +++ b/src/main/resources/prepare/add_user.sql @@ -1 +1 @@ -insert into users values (?1); \ No newline at end of file +insert into users values (?1, ?2, ?3, ?4); \ No newline at end of file diff --git a/src/main/resources/prepare/get_list.sql b/src/main/resources/prepare/get_list.sql new file mode 100644 index 0000000..9131d06 --- /dev/null +++ b/src/main/resources/prepare/get_list.sql @@ -0,0 +1 @@ +select * from lists where id = ?1; \ No newline at end of file diff --git a/src/main/resources/prepare/get_stage.sql b/src/main/resources/prepare/get_stage.sql new file mode 100644 index 0000000..73e0a17 --- /dev/null +++ b/src/main/resources/prepare/get_stage.sql @@ -0,0 +1 @@ +select * from task_stages where id = ?1; \ No newline at end of file diff --git a/src/main/resources/prepare/get_stages.sql b/src/main/resources/prepare/get_stages.sql new file mode 100644 index 0000000..61867cf --- /dev/null +++ b/src/main/resources/prepare/get_stages.sql @@ -0,0 +1 @@ +select * from task_stages; \ No newline at end of file diff --git a/src/main/resources/prepare/get_task.sql b/src/main/resources/prepare/get_task.sql new file mode 100644 index 0000000..3104ddd --- /dev/null +++ b/src/main/resources/prepare/get_task.sql @@ -0,0 +1,2 @@ +select * from tasks +where id = ?1; \ No newline at end of file diff --git a/src/main/resources/prepare/get_tasks.sql b/src/main/resources/prepare/get_tasks.sql new file mode 100644 index 0000000..8fdcd01 --- /dev/null +++ b/src/main/resources/prepare/get_tasks.sql @@ -0,0 +1 @@ +select * from tasks; \ No newline at end of file diff --git a/src/main/resources/prepare/get_user.sql b/src/main/resources/prepare/get_user.sql new file mode 100644 index 0000000..7c9d91d --- /dev/null +++ b/src/main/resources/prepare/get_user.sql @@ -0,0 +1 @@ +select * from users where id = ?1; \ No newline at end of file diff --git a/src/test/kotlin/DatabaseTests.kt b/src/test/kotlin/DatabaseTests.kt index a09c472..dab0e46 100644 --- a/src/test/kotlin/DatabaseTests.kt +++ b/src/test/kotlin/DatabaseTests.kt @@ -6,7 +6,5 @@ import org.junit.jupiter.api.Test; import java.sql.Connection; class DatabaseTests { - private val app : KlistApp = KlistApp(null); - } \ No newline at end of file