more sql and models

This commit is contained in:
theBreadCompany 2025-06-02 07:53:13 +02:00
parent 2a1d6ca031
commit 07b3c3821f
12 changed files with 201 additions and 46 deletions

View file

@ -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<PreparedStatementCase, PreparedStatement> {
return PreparedStatementCase.entries.associateWith { case ->
conn.prepareStatement(
File(
"prepare/${
case.toString().lowercase()
}.sql"
).readText()
javaClass.getResource("/prepare/${
case.toString().lowercase()
}.sql")!!.readText()
)
}
}

View file

@ -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<T : Model<T>> {
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<PreparedStatementCase, PreparedStatement>? = null
var stmts: Map<PreparedStatementCase, PreparedStatement>? = 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<PreparedStatementCase, PreparedStatement> {
return stmts!!
}
fun setStatements(stmts: Map<PreparedStatementCase, PreparedStatement>) {
Common.stmts = stmts
}
abstract fun get(id: Int): Model?
abstract fun getAll(): Set<Model>
abstract fun create(): Model
abstract fun get(id: Int): T?
abstract fun getAll(): Set<T>
abstract fun create(): T
}
class Models {
class User {
val name: String? = null
val discord: Int? = null
val google: Int? = null
}
class User() : Model<User>() {
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<User> {
val stmt = assureStatements()[PreparedStatementCase.GET_USERS]!!
val result = stmt.executeQuery()
val users = mutableSetOf<User>()
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<List>() {
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<List> {
val stmt = assureStatements()[PreparedStatementCase.GET_LISTS]!!
val result = stmt.executeQuery()
val lists = mutableSetOf<List>()
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<Stage>() {
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<Stage> {
val stmt = assureStatements()[PreparedStatementCase.GET_STAGES]!!
val result = stmt.executeQuery()
val stages = mutableSetOf<Models.Stage>()
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<Task>() {
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<Task> {
val stmt = assureStatements()[PreparedStatementCase.GET_TASKS]!!
val result = stmt.executeQuery()
val tasks = mutableSetOf<Task>()
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
}
}
}

View file

@ -0,0 +1,2 @@
insert into task_stages
values (?1, ?2, ?3)

View file

@ -0,0 +1,2 @@
insert into tasks
values (?1, ?2, ?3, ?4, ?5, ?6);

View file

@ -1 +1 @@
insert into users values (?1);
insert into users values (?1, ?2, ?3, ?4);

View file

@ -0,0 +1 @@
select * from lists where id = ?1;

View file

@ -0,0 +1 @@
select * from task_stages where id = ?1;

View file

@ -0,0 +1 @@
select * from task_stages;

View file

@ -0,0 +1,2 @@
select * from tasks
where id = ?1;

View file

@ -0,0 +1 @@
select * from tasks;

View file

@ -0,0 +1 @@
select * from users where id = ?1;

View file

@ -6,7 +6,5 @@ import org.junit.jupiter.api.Test;
import java.sql.Connection;
class DatabaseTests {
private val app : KlistApp = KlistApp(null);
}