refactoring: change select method names

This commit is contained in:
2019-07-02 23:40:29 +02:00
parent f48b06b596
commit 6cc3152215
8 changed files with 53 additions and 22 deletions

View File

@@ -10,13 +10,20 @@ import fr.postgresjson.entity.EntityI
import fr.postgresjson.serializer.Serializer
import java.util.concurrent.CompletableFuture
interface Executable {
fun <T, R : EntityI<T?>?> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
fun <T, R : List<EntityI<T?>?>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
fun exec(sql: String, values: List<Any?> = emptyList()): CompletableFuture<QueryResult>
}
class Connection(
private val database: String,
private val username: String,
private val password: String,
private val host: String = "localhost",
private val port: Int = 5432
) {
): Executable {
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
private val serializer = Serializer()
@@ -31,7 +38,7 @@ class Connection(
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
fun <T, R : EntityI<T?>?> selectOne(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R? {
override fun <T, R : EntityI<T?>?> select(sql: String, typeReference: TypeReference<R>, values: List<Any?>): R? {
val future = connect().sendPreparedStatement(sql, compileArgs(values))
val json = future.get().rows[0].getString(0)
return if (json === null) {
@@ -41,9 +48,9 @@ class Connection(
}
}
inline fun <T, reified R : EntityI<T?>?> selectOne(sql: String, values: List<Any?> = emptyList()): R? = selectOne(sql, object: TypeReference<R>() {}, values)
inline fun <T, reified R : EntityI<T?>?> selectOne(sql: String, values: List<Any?> = emptyList()): R? = select(sql, object: TypeReference<R>() {}, values)
fun <T, R : List<EntityI<T?>?>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R {
override fun <T, R : List<EntityI<T?>?>> select(sql: String, typeReference: TypeReference<R>, values: List<Any?>): R {
val future = connect().sendPreparedStatement(sql, compileArgs(values))
val json = future.get().rows[0].getString(0)
return if (json === null) {
@@ -55,7 +62,7 @@ class Connection(
inline fun <T, reified R : List<EntityI<T?>?>> select(sql: String, values: List<Any?> = emptyList()): R = select(sql, object : TypeReference<R>() {}, values)
fun exec(sql: String, values: List<Any?> = emptyList()): CompletableFuture<QueryResult> {
override fun exec(sql: String, values: List<Any?>): CompletableFuture<QueryResult> {
return connect().sendPreparedStatement(sql, compileArgs(values))
}

View File

@@ -79,11 +79,11 @@ class Requester (
return sql
}
override fun <T, R : EntityI<T?>?> selectOne(typeReference: TypeReference<R>, values: List<Any?>): R? {
return connection.selectOne(this.toString(), typeReference, values)
override fun <T, R : EntityI<T?>?> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
return connection.select(this.toString(), typeReference, values)
}
inline fun <T, reified R : EntityI<T?>?> selectOne(values: List<Any?> = emptyList()): R? = selectOne(object: TypeReference<R>() {}, values)
inline fun <T, reified R : EntityI<T?>?> selectOne(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
override fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
return connection.select(this.toString(), typeReference, values)
@@ -101,14 +101,14 @@ class Requester (
return definition.name
}
override fun <T, R : EntityI<T?>?> selectOne(typeReference: TypeReference<R>, values: List<Any?>): R? {
override fun <T, R : EntityI<T?>?> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
val args = compileArgs(values)
val sql = "SELECT * FROM ${definition.name} ($args)"
return connection.selectOne(sql, typeReference, values)
return connection.select(sql, typeReference, values)
}
inline fun <T, reified R: EntityI<T?>?> selectOne(values: List<String?> = emptyList()): R? = selectOne(object: TypeReference<R>() {}, values)
inline fun <T, reified R: EntityI<T?>?> selectOne(values: List<Any?> = emptyList()): R? = select(object: TypeReference<R>() {}, values)
override fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
val args = compileArgs(values)
@@ -143,7 +143,7 @@ class Requester (
val connection : Connection
override fun toString(): String
fun <T, R : EntityI<T?>?> selectOne(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
fun <T, R : EntityI<T?>?> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?

View File

@@ -15,9 +15,7 @@ class MigrationEntity(
val up: String,
val down: String,
val version: Int
): Entity<String?>(filename) {
}
): Entity<String?>(filename)
interface Migration {
var executedAt: Date?
@@ -177,16 +175,16 @@ class Migrations(directory: File, private val connection: Connection) {
fun test(): Map<Pair<String, Direction>, Migration.Status> {
var list: MutableMap<Pair<String, Direction>, Migration.Status> = mutableMapOf()
connection.inTransaction {
connection.connect().let {
it.sendQuery("BEGIN").join()
up().map {
list.set(Pair(it.key, Direction.UP), it.value)
}
down().map {
list.set(Pair(it.key, Direction.DOWN), it.value)
}
it.sendQuery("ROLLBACK");
}.join()
it.sendQuery("ROLLBACK").join()
}
return list.toMap()
}

View File

@@ -12,14 +12,14 @@ class Query(
override var executedAt: Date? = null
): Migration, Entity<String?>(name) {
override fun up(): Migration.Status {
connection.exec(up)
connection.exec(up).join()
// TODO insert to migration Table
return Migration.Status.OK
}
override fun down(): Migration.Status {
connection.exec(down)
connection.exec(down).join()
// TODO insert to migration Table
return Migration.Status.OK
@@ -29,7 +29,7 @@ class Query(
connection.inTransaction {
up()
down()
it.sendQuery("ROLLBACK");
it.sendQuery("ROLLBACK")
}.join()
return Migration.Status.OK // TODO