From 6cc315221579d8cc3b453cde49375f5e3cbcddf0 Mon Sep 17 00:00:00 2001 From: flecomte Date: Tue, 2 Jul 2019 23:40:29 +0200 Subject: [PATCH] refactoring: change select method names --- .idea/misc.xml | 3 +++ .idea/modules.xml | 8 ++++++++ .idea/postgres-json.iml | 9 +++++++++ .idea/sqldialects.xml | 6 ++++++ .../fr/postgresjson/connexion/Connection.kt | 17 ++++++++++++----- .../fr/postgresjson/connexion/Requester.kt | 14 +++++++------- .../fr/postgresjson/migration/Migrations.kt | 12 +++++------- .../kotlin/fr/postgresjson/migration/Query.kt | 6 +++--- 8 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 .idea/modules.xml create mode 100644 .idea/postgres-json.iml create mode 100644 .idea/sqldialects.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 29af3ee..fcc1aa4 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,4 +4,7 @@ + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c2af880 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/postgres-json.iml b/.idea/postgres-json.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/postgres-json.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..6df4889 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt index deeaa30..307489d 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Connection.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Connection.kt @@ -10,13 +10,20 @@ import fr.postgresjson.entity.EntityI import fr.postgresjson.serializer.Serializer import java.util.concurrent.CompletableFuture + +interface Executable { + fun ?> select(sql: String, typeReference: TypeReference, values: List = emptyList()): R? + fun ?>> select(sql: String, typeReference: TypeReference, values: List = emptyList()): R? + fun exec(sql: String, values: List = emptyList()): CompletableFuture +} + 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 private val serializer = Serializer() @@ -31,7 +38,7 @@ class Connection( fun inTransaction(f: (Connection) -> CompletableFuture) = connect().inTransaction(f) - fun ?> selectOne(sql: String, typeReference: TypeReference, values: List = emptyList()): R? { + override fun ?> select(sql: String, typeReference: TypeReference, values: List): 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 ?> selectOne(sql: String, values: List = emptyList()): R? = selectOne(sql, object: TypeReference() {}, values) + inline fun ?> selectOne(sql: String, values: List = emptyList()): R? = select(sql, object: TypeReference() {}, values) - fun ?>> select(sql: String, typeReference: TypeReference, values: List = emptyList()): R { + override fun ?>> select(sql: String, typeReference: TypeReference, values: List): 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 ?>> select(sql: String, values: List = emptyList()): R = select(sql, object : TypeReference() {}, values) - fun exec(sql: String, values: List = emptyList()): CompletableFuture { + override fun exec(sql: String, values: List): CompletableFuture { return connect().sendPreparedStatement(sql, compileArgs(values)) } diff --git a/src/main/kotlin/fr/postgresjson/connexion/Requester.kt b/src/main/kotlin/fr/postgresjson/connexion/Requester.kt index 5f2d432..713f339 100644 --- a/src/main/kotlin/fr/postgresjson/connexion/Requester.kt +++ b/src/main/kotlin/fr/postgresjson/connexion/Requester.kt @@ -79,11 +79,11 @@ class Requester ( return sql } - override fun ?> selectOne(typeReference: TypeReference, values: List): R? { - return connection.selectOne(this.toString(), typeReference, values) + override fun ?> select(typeReference: TypeReference, values: List): R? { + return connection.select(this.toString(), typeReference, values) } - inline fun ?> selectOne(values: List = emptyList()): R? = selectOne(object: TypeReference() {}, values) + inline fun ?> selectOne(values: List = emptyList()): R? = select(object: TypeReference() {}, values) override fun ?>> select(typeReference: TypeReference, values: List): R? { return connection.select(this.toString(), typeReference, values) @@ -101,14 +101,14 @@ class Requester ( return definition.name } - override fun ?> selectOne(typeReference: TypeReference, values: List): R? { + override fun ?> select(typeReference: TypeReference, values: List): 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 ?> selectOne(values: List = emptyList()): R? = selectOne(object: TypeReference() {}, values) + inline fun ?> selectOne(values: List = emptyList()): R? = select(object: TypeReference() {}, values) override fun ?>> select(typeReference: TypeReference, values: List): R? { val args = compileArgs(values) @@ -143,7 +143,7 @@ class Requester ( val connection : Connection override fun toString(): String - fun ?> selectOne(typeReference: TypeReference, values: List = emptyList()): R? + fun ?> select(typeReference: TypeReference, values: List = emptyList()): R? fun ?>> select(typeReference: TypeReference, values: List = emptyList()): R? diff --git a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt index d517415..69400f3 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt @@ -15,9 +15,7 @@ class MigrationEntity( val up: String, val down: String, val version: Int -): Entity(filename) { - -} +): Entity(filename) interface Migration { var executedAt: Date? @@ -177,16 +175,16 @@ class Migrations(directory: File, private val connection: Connection) { fun test(): Map, Migration.Status> { var list: MutableMap, 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() } diff --git a/src/main/kotlin/fr/postgresjson/migration/Query.kt b/src/main/kotlin/fr/postgresjson/migration/Query.kt index 3924014..38736aa 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Query.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Query.kt @@ -12,14 +12,14 @@ class Query( override var executedAt: Date? = null ): Migration, Entity(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