fetch sql files and save Queries into List
This commit is contained in:
@@ -5,6 +5,7 @@ import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
|
|||||||
import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
|
import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
|
||||||
import fr.postgresjson.entity.EntityI
|
import fr.postgresjson.entity.EntityI
|
||||||
import fr.postgresjson.serializer.Serializer
|
import fr.postgresjson.serializer.Serializer
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
|
||||||
class Connection(
|
class Connection(
|
||||||
@@ -12,11 +13,24 @@ class Connection(
|
|||||||
private val port: Int = 5432,
|
private val port: Int = 5432,
|
||||||
private val database: String = "dc-project",
|
private val database: String = "dc-project",
|
||||||
private val username: String = "dc-project",
|
private val username: String = "dc-project",
|
||||||
private val password: String = "dc-project"
|
private val password: String = "dc-project",
|
||||||
|
queriesDirectory: File? = null
|
||||||
) {
|
) {
|
||||||
|
private val queries = mutableMapOf<String, MutableMap<String, String>>()
|
||||||
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
|
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
|
||||||
val serializer = Serializer()
|
val serializer = Serializer()
|
||||||
|
|
||||||
|
init {
|
||||||
|
if (queriesDirectory === null) {
|
||||||
|
val resource = this::class.java.getResource("/sql/query")
|
||||||
|
if (resource !== null) {
|
||||||
|
fetchQueries(File(resource.toURI()))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fetchQueries(queriesDirectory)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun connect(): ConnectionPool<PostgreSQLConnection> {
|
fun connect(): ConnectionPool<PostgreSQLConnection> {
|
||||||
if (!::connection.isInitialized || !connection.isConnected()) {
|
if (!::connection.isInitialized || !connection.isConnected()) {
|
||||||
connection = PostgreSQLConnectionBuilder.createConnectionPool(
|
connection = PostgreSQLConnectionBuilder.createConnectionPool(
|
||||||
@@ -26,7 +40,13 @@ class Connection(
|
|||||||
return connection
|
return connection
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <T, reified R : EntityI<T?>> selectOne(sql: String, values: List<Any?> = emptyList()): R? {
|
inline fun <T, reified R : EntityI<T?>?> selectOne(group: String, name: String, values: List<Any?> = emptyList()): R? {
|
||||||
|
val sql: String = getQuery(group, name)
|
||||||
|
|
||||||
|
return selectOne<T, R>(sql, values)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T, reified R : EntityI<T?>?> selectOne(sql: String, values: List<Any?> = emptyList()): R? {
|
||||||
val future = connect().sendPreparedStatement(sql, values)
|
val future = connect().sendPreparedStatement(sql, values)
|
||||||
val json = future.get().rows[0].getString(0)
|
val json = future.get().rows[0].getString(0)
|
||||||
if (json === null) {
|
if (json === null) {
|
||||||
@@ -49,4 +69,24 @@ class Connection(
|
|||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun fetchQueries(queriesDirectory: File) {
|
||||||
|
queriesDirectory.walk().filter{it.isDirectory}.forEach { directory ->
|
||||||
|
val group = directory.name
|
||||||
|
directory.walk().filter{it.isFile}.forEach { file ->
|
||||||
|
val sql = file.readText()
|
||||||
|
if (queries[group] === null) {
|
||||||
|
queries[group] = mutableMapOf()
|
||||||
|
}
|
||||||
|
queries[group]!![file.nameWithoutExtension] = sql
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getQuery(group: String, name: String): String {
|
||||||
|
if (queries[group] === null || queries[group]!![name] === null) {
|
||||||
|
throw Exception("No query defined for $group/$name")
|
||||||
|
}
|
||||||
|
return queries[group]!![name]!!
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -32,8 +32,8 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
|||||||
return mapper.writeValueAsString(source)
|
return mapper.writeValueAsString(source)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <T, reified E : EntityI<T?>> deserialize(json: String): E {
|
inline fun <T, reified E : EntityI<T?>?> deserialize(json: String): E {
|
||||||
return this.mapper.readValue<E>(json)
|
return this.mapper.readValue(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified E> deserializeList(json: String): E {
|
inline fun <reified E> deserializeList(json: String): E {
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import org.junit.jupiter.api.BeforeEach
|
|||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
class ConnectionTest() {
|
class ConnectionTest() {
|
||||||
|
private class ObjTest(var name: String): IdEntity()
|
||||||
|
private class ObjTest2(var title: String, var test: ObjTest?): IdEntity()
|
||||||
|
|
||||||
private lateinit var connection: Connection
|
private lateinit var connection: Connection
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
@@ -41,7 +44,4 @@ class ConnectionTest() {
|
|||||||
assertTrue(objs[0].id == 1)
|
assertTrue(objs[0].id == 1)
|
||||||
assertTrue(objs[0].test!!.id == 1)
|
assertTrue(objs[0].test!!.id == 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ObjTest(var name: String): IdEntity()
|
|
||||||
class ObjTest2(var title: String, var test: ObjTest?): IdEntity()
|
|
||||||
19
src/test/kotlin/fr/postgresjson/serializer/RequestTest.kt
Normal file
19
src/test/kotlin/fr/postgresjson/serializer/RequestTest.kt
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package fr.postgresjson.serializer
|
||||||
|
|
||||||
|
import fr.postgresjson.connexion.Connection
|
||||||
|
import fr.postgresjson.entity.IdEntity
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class RequestTest {
|
||||||
|
class ObjTest(var name:String): IdEntity(1)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun getRequestFromFile() {
|
||||||
|
val resources = File(this::class.java.getResource("/sql/query").toURI())
|
||||||
|
val objTest: ObjTest? = Connection(queriesDirectory = resources).selectOne("Test", "test")
|
||||||
|
assertTrue(objTest!!.id == 2)
|
||||||
|
assertTrue(objTest.name == "test")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ import org.junit.jupiter.api.TestInstance
|
|||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
internal class SerializerTest {
|
internal class SerializerTest {
|
||||||
class ObjTest(var val1: String, var val2: Int) : IdEntity(1)
|
private class ObjTest(var val1: String, var val2: Int) : IdEntity(1)
|
||||||
|
|
||||||
private val serializer = Serializer()
|
private val serializer = Serializer()
|
||||||
|
|
||||||
|
|||||||
1
src/test/resources/sql/query/Test/test.sql
Normal file
1
src/test/resources/sql/query/Test/test.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
select json_build_object('id', 2, 'name', 'test');
|
||||||
Reference in New Issue
Block a user