From d72012dd27081667a37df284e73f447e2aad6ff4 Mon Sep 17 00:00:00 2001 From: Roman Makeev Date: Sat, 6 Aug 2022 12:33:12 +0300 Subject: [PATCH] Fixed inline call and added query selector --- .../astrainteractive/astratemplate/api/DatabaseApi.kt | 4 ++-- .../astratemplate/sqldatabase/lib/DatabaseCore.kt | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/astrainteractive/astratemplate/api/DatabaseApi.kt b/src/main/kotlin/com/astrainteractive/astratemplate/api/DatabaseApi.kt index cb7e773..b182249 100644 --- a/src/main/kotlin/com/astrainteractive/astratemplate/api/DatabaseApi.kt +++ b/src/main/kotlin/com/astrainteractive/astratemplate/api/DatabaseApi.kt @@ -20,11 +20,11 @@ object DatabaseApi { * @return null or T */ suspend fun createUserTable() = catching { - SQLDatabase.createTable(User::class.java) + SQLDatabase.createTable() } suspend fun createRatingTable() = catching { - SQLDatabase.createTable(RatingRelation::class.java) + SQLDatabase.createTable() } /** diff --git a/src/main/kotlin/com/astrainteractive/astratemplate/sqldatabase/lib/DatabaseCore.kt b/src/main/kotlin/com/astrainteractive/astratemplate/sqldatabase/lib/DatabaseCore.kt index 3b1d214..2afe440 100644 --- a/src/main/kotlin/com/astrainteractive/astratemplate/sqldatabase/lib/DatabaseCore.kt +++ b/src/main/kotlin/com/astrainteractive/astratemplate/sqldatabase/lib/DatabaseCore.kt @@ -28,6 +28,8 @@ abstract class DatabaseCore() { inline fun select(where: String = "") = select(T::class.java, where) inline fun insert(instance: T) = insert(T::class.java, instance) inline fun createTable() = createTable(T::class.java) + inline fun querySelect(query: String, noinline rsBuilder: (ResultSet) -> T?) = + querySelect(T::class.java, query, rsBuilder) fun update(clazz: Class, instance: T): Boolean? { val info = AnnotationUtils.EntityInfo.create(clazz, instance) val primaryKeyInfo = @@ -64,6 +66,14 @@ abstract class DatabaseCore() { } + + fun querySelect(clazz: Class, query: String = "", rsBuilder: (ResultSet) -> T?): List? { + return connection?.createStatement()?.executeQuery(query)?.mapNotNull { rs -> + rsBuilder.invoke(rs) + } + } + + fun insert(clazz: Class, instance: T): Long? { val info = AnnotationUtils.EntityInfo.create(clazz, instance) val keys = info?.columns?.mapNotNull {