more sql and models
This commit is contained in:
parent
2a1d6ca031
commit
07b3c3821f
12 changed files with 201 additions and 46 deletions
|
@ -18,18 +18,16 @@ class KlistApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun dbSetup(conn: Connection) {
|
private fun dbSetup(conn: Connection) {
|
||||||
val initSql = File("init.sql").readText()
|
val initSql = javaClass.getResource("/init.sql")!!.readText()
|
||||||
conn.createStatement().executeUpdate(initSql)
|
conn.createStatement().executeUpdate(initSql)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun dbPrepare(conn: Connection): Map<PreparedStatementCase, PreparedStatement> {
|
private fun dbPrepare(conn: Connection): Map<PreparedStatementCase, PreparedStatement> {
|
||||||
return PreparedStatementCase.entries.associateWith { case ->
|
return PreparedStatementCase.entries.associateWith { case ->
|
||||||
conn.prepareStatement(
|
conn.prepareStatement(
|
||||||
File(
|
javaClass.getResource("/prepare/${
|
||||||
"prepare/${
|
|
||||||
case.toString().lowercase()
|
case.toString().lowercase()
|
||||||
}.sql"
|
}.sql")!!.readText()
|
||||||
).readText()
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,17 @@
|
||||||
package dev.thebread
|
package dev.thebread
|
||||||
|
|
||||||
|
import dev.thebread.Model.PreparedStatementCase
|
||||||
|
import dev.thebread.Models.Stage
|
||||||
import java.sql.PreparedStatement
|
import java.sql.PreparedStatement
|
||||||
|
import java.sql.ResultSet
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
|
import java.util.Date
|
||||||
|
|
||||||
|
|
||||||
abstract class Model {
|
abstract class Model<T : Model<T>> {
|
||||||
enum class PreparedStatementCase {
|
enum class PreparedStatementCase {
|
||||||
GET_USERS, GET_LIST_FOR_USER, GET_LISTS, GET_STAGES_FOR_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_LIST, ADD_USER, ADD_LIST_FOR_USER, ADD_STAGE_FOR_LIST, ADD_TASK_TO_LIST, ADD_STAGE, ADD_TASK,
|
||||||
MOVE_TASK_TO_STAGE,
|
MOVE_TASK_TO_STAGE,
|
||||||
REMOVE_TASK, REMOVE_USER, REMOVE_LIST_FOR_USER,
|
REMOVE_TASK, REMOVE_USER, REMOVE_LIST_FOR_USER,
|
||||||
REG_LOCAL_FOR_USER, REG_DISCORD_FOR_USER, REG_GOOGLE_FOR_USER,
|
REG_LOCAL_FOR_USER, REG_DISCORD_FOR_USER, REG_GOOGLE_FOR_USER,
|
||||||
|
@ -15,53 +19,197 @@ abstract class Model {
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object Common {
|
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? {
|
fun assureStatements(): Map<PreparedStatementCase, PreparedStatement> {
|
||||||
if(id == null) {
|
return stmts!!
|
||||||
this.id = create().id
|
|
||||||
}
|
|
||||||
return id
|
|
||||||
}
|
}
|
||||||
fun setStatements(stmts: Map<PreparedStatementCase, PreparedStatement>) {
|
fun setStatements(stmts: Map<PreparedStatementCase, PreparedStatement>) {
|
||||||
Common.stmts = stmts
|
Common.stmts = stmts
|
||||||
}
|
}
|
||||||
|
abstract fun get(id: Int): T?
|
||||||
abstract fun get(id: Int): Model?
|
abstract fun getAll(): Set<T>
|
||||||
abstract fun getAll(): Set<Model>
|
abstract fun create(): T
|
||||||
abstract fun create(): Model
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Models {
|
class Models {
|
||||||
class User {
|
class User() : Model<User>() {
|
||||||
val name: String? = null
|
lateinit var name: String
|
||||||
val discord: Int? = null
|
var local: Int = -1
|
||||||
val google: Int? = null
|
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")
|
||||||
}
|
}
|
||||||
|
|
||||||
class List {
|
override fun get(id: Int): User? {
|
||||||
val id: Int? = null
|
val stmt = assureStatements()[PreparedStatementCase.GET_USER]!!
|
||||||
val name: String? = null
|
stmt.setInt(1, id)
|
||||||
val description: String? = null
|
val result = stmt.executeQuery()
|
||||||
|
val user = User(result)
|
||||||
|
stmt.clearParameters()
|
||||||
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
class Task {
|
override fun getAll(): Set<User> {
|
||||||
class Stage {
|
val stmt = assureStatements()[PreparedStatementCase.GET_USERS]!!
|
||||||
val id: Int? = null
|
val result = stmt.executeQuery()
|
||||||
val name: String? = null
|
val users = mutableSetOf<User>()
|
||||||
val description: String? = null
|
do { users.add(User(result)) } while (result.next())
|
||||||
val list: List? = null
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
val id: Int? = null
|
override fun create(): User {
|
||||||
val name: String? = null
|
val stmt = assureStatements()[PreparedStatementCase.ADD_USER]!!
|
||||||
val description: String? = null
|
stmt.setString(1, this.name)
|
||||||
val stage: Stage? = null
|
if(this.local != null) stmt.setInt(2, this.local!!)
|
||||||
val due: LocalDate? = null
|
if(this.discord != null) stmt.setInt(3, this.discord!!)
|
||||||
val dueStage: Stage? = null
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
2
src/main/resources/prepare/add_stage.sql
Normal file
2
src/main/resources/prepare/add_stage.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
insert into task_stages
|
||||||
|
values (?1, ?2, ?3)
|
2
src/main/resources/prepare/add_task.sql
Normal file
2
src/main/resources/prepare/add_task.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
insert into tasks
|
||||||
|
values (?1, ?2, ?3, ?4, ?5, ?6);
|
|
@ -1 +1 @@
|
||||||
insert into users values (?1);
|
insert into users values (?1, ?2, ?3, ?4);
|
1
src/main/resources/prepare/get_list.sql
Normal file
1
src/main/resources/prepare/get_list.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
select * from lists where id = ?1;
|
1
src/main/resources/prepare/get_stage.sql
Normal file
1
src/main/resources/prepare/get_stage.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
select * from task_stages where id = ?1;
|
1
src/main/resources/prepare/get_stages.sql
Normal file
1
src/main/resources/prepare/get_stages.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
select * from task_stages;
|
2
src/main/resources/prepare/get_task.sql
Normal file
2
src/main/resources/prepare/get_task.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
select * from tasks
|
||||||
|
where id = ?1;
|
1
src/main/resources/prepare/get_tasks.sql
Normal file
1
src/main/resources/prepare/get_tasks.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
select * from tasks;
|
1
src/main/resources/prepare/get_user.sql
Normal file
1
src/main/resources/prepare/get_user.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
select * from users where id = ?1;
|
|
@ -6,7 +6,5 @@ import org.junit.jupiter.api.Test;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
|
||||||
class DatabaseTests {
|
class DatabaseTests {
|
||||||
|
|
||||||
private val app : KlistApp = KlistApp(null);
|
private val app : KlistApp = KlistApp(null);
|
||||||
|
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue