blist/src/main/kotlin/Models.kt

210 lines
No EOL
7.1 KiB
Kotlin

package dev.thebread
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.util.Date
abstract class Model<T : Model<T>> {
enum class PreparedStatementCase {
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,
CLEANUP,
}
companion object Common {
var stmts: Map<PreparedStatementCase, PreparedStatement>? = null
}
constructor() {}
var id: Int? = null
fun assureStatements(): Map<PreparedStatementCase, PreparedStatement> {
return stmts!!
}
abstract fun get(id: Int): T?
abstract fun getAll(): Set<T>
abstract fun create(): T
}
class Models {
class User() : Model<User>() {
lateinit var name: String
var local: Int = -1
var discord: Int = -1
var google: Int = -1
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")
}
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 != -1) stmt.setInt(2, this.local)
if(this.discord != -1) stmt.setInt(3, this.discord)
if(this.google != -1) stmt.setInt(4, this.google)
val result = stmt.executeQuery()
val user = User(result)
stmt.clearParameters()
this.id = user.id
return user
}
}
class List() : Model<List>() {
lateinit var name: String
var description: String? = null
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
var description: String? = null
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
var description: String? = null
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
}
}
}