refactoring: change select method names
This commit is contained in:
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
@@ -4,4 +4,7 @@
|
|||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
|
<component name="TaskProjectConfiguration">
|
||||||
|
<server type="GitHub" url="https://github.com" />
|
||||||
|
</component>
|
||||||
</project>
|
</project>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/postgres-json.iml" filepath="$PROJECT_DIR$/.idea/postgres-json.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
9
.idea/postgres-json.iml
generated
Normal file
9
.idea/postgres-json.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
6
.idea/sqldialects.xml
generated
Normal file
6
.idea/sqldialects.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="SqlDialectMappings">
|
||||||
|
<file url="PROJECT" dialect="PostgreSQL" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -10,13 +10,20 @@ import fr.postgresjson.entity.EntityI
|
|||||||
import fr.postgresjson.serializer.Serializer
|
import fr.postgresjson.serializer.Serializer
|
||||||
import java.util.concurrent.CompletableFuture
|
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(
|
class Connection(
|
||||||
private val database: String,
|
private val database: String,
|
||||||
private val username: String,
|
private val username: String,
|
||||||
private val password: String,
|
private val password: String,
|
||||||
private val host: String = "localhost",
|
private val host: String = "localhost",
|
||||||
private val port: Int = 5432
|
private val port: Int = 5432
|
||||||
) {
|
): Executable {
|
||||||
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
|
private lateinit var connection: ConnectionPool<PostgreSQLConnection>
|
||||||
private val serializer = Serializer()
|
private val serializer = Serializer()
|
||||||
|
|
||||||
@@ -31,7 +38,7 @@ class Connection(
|
|||||||
|
|
||||||
fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>) = connect().inTransaction(f)
|
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 future = connect().sendPreparedStatement(sql, compileArgs(values))
|
||||||
val json = future.get().rows[0].getString(0)
|
val json = future.get().rows[0].getString(0)
|
||||||
return if (json === null) {
|
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 future = connect().sendPreparedStatement(sql, compileArgs(values))
|
||||||
val json = future.get().rows[0].getString(0)
|
val json = future.get().rows[0].getString(0)
|
||||||
return if (json === null) {
|
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)
|
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))
|
return connect().sendPreparedStatement(sql, compileArgs(values))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,11 +79,11 @@ class Requester (
|
|||||||
return sql
|
return sql
|
||||||
}
|
}
|
||||||
|
|
||||||
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? {
|
||||||
return connection.selectOne(this.toString(), typeReference, values)
|
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? {
|
override fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||||
return connection.select(this.toString(), typeReference, values)
|
return connection.select(this.toString(), typeReference, values)
|
||||||
@@ -101,14 +101,14 @@ class Requester (
|
|||||||
return definition.name
|
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 args = compileArgs(values)
|
||||||
val sql = "SELECT * FROM ${definition.name} ($args)"
|
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? {
|
override fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: List<Any?>): R? {
|
||||||
val args = compileArgs(values)
|
val args = compileArgs(values)
|
||||||
@@ -143,7 +143,7 @@ class Requester (
|
|||||||
val connection : Connection
|
val connection : Connection
|
||||||
override fun toString(): String
|
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?
|
fun <T, R : List<EntityI<T?>?>> select(typeReference: TypeReference<R>, values: List<Any?> = emptyList()): R?
|
||||||
|
|
||||||
|
|||||||
@@ -15,9 +15,7 @@ class MigrationEntity(
|
|||||||
val up: String,
|
val up: String,
|
||||||
val down: String,
|
val down: String,
|
||||||
val version: Int
|
val version: Int
|
||||||
): Entity<String?>(filename) {
|
): Entity<String?>(filename)
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Migration {
|
interface Migration {
|
||||||
var executedAt: Date?
|
var executedAt: Date?
|
||||||
@@ -177,16 +175,16 @@ class Migrations(directory: File, private val connection: Connection) {
|
|||||||
|
|
||||||
fun test(): Map<Pair<String, Direction>, Migration.Status> {
|
fun test(): Map<Pair<String, Direction>, Migration.Status> {
|
||||||
var list: MutableMap<Pair<String, Direction>, Migration.Status> = mutableMapOf()
|
var list: MutableMap<Pair<String, Direction>, Migration.Status> = mutableMapOf()
|
||||||
connection.inTransaction {
|
connection.connect().let {
|
||||||
|
it.sendQuery("BEGIN").join()
|
||||||
up().map {
|
up().map {
|
||||||
list.set(Pair(it.key, Direction.UP), it.value)
|
list.set(Pair(it.key, Direction.UP), it.value)
|
||||||
}
|
}
|
||||||
down().map {
|
down().map {
|
||||||
list.set(Pair(it.key, Direction.DOWN), it.value)
|
list.set(Pair(it.key, Direction.DOWN), it.value)
|
||||||
}
|
}
|
||||||
|
it.sendQuery("ROLLBACK").join()
|
||||||
it.sendQuery("ROLLBACK");
|
}
|
||||||
}.join()
|
|
||||||
|
|
||||||
return list.toMap()
|
return list.toMap()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,14 +12,14 @@ class Query(
|
|||||||
override var executedAt: Date? = null
|
override var executedAt: Date? = null
|
||||||
): Migration, Entity<String?>(name) {
|
): Migration, Entity<String?>(name) {
|
||||||
override fun up(): Migration.Status {
|
override fun up(): Migration.Status {
|
||||||
connection.exec(up)
|
connection.exec(up).join()
|
||||||
// TODO insert to migration Table
|
// TODO insert to migration Table
|
||||||
|
|
||||||
return Migration.Status.OK
|
return Migration.Status.OK
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun down(): Migration.Status {
|
override fun down(): Migration.Status {
|
||||||
connection.exec(down)
|
connection.exec(down).join()
|
||||||
// TODO insert to migration Table
|
// TODO insert to migration Table
|
||||||
|
|
||||||
return Migration.Status.OK
|
return Migration.Status.OK
|
||||||
@@ -29,7 +29,7 @@ class Query(
|
|||||||
connection.inTransaction {
|
connection.inTransaction {
|
||||||
up()
|
up()
|
||||||
down()
|
down()
|
||||||
it.sendQuery("ROLLBACK");
|
it.sendQuery("ROLLBACK")
|
||||||
}.join()
|
}.join()
|
||||||
|
|
||||||
return Migration.Status.OK // TODO
|
return Migration.Status.OK // TODO
|
||||||
|
|||||||
Reference in New Issue
Block a user