Compare commits
10 Commits
fix/functi
...
1.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07678afbe7 | ||
|
|
d3bcfdd627 | ||
|
|
deea5153f2 | ||
|
|
05fb868574 | ||
|
|
bbeec7bb60 | ||
|
|
012beb6884 | ||
|
|
45707f9734 | ||
|
|
4e89aa072d | ||
|
|
b65e82a52f | ||
|
|
898951e91a |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,4 +2,5 @@
|
||||
/out/
|
||||
/build/
|
||||
.gradle
|
||||
/var/log/
|
||||
/var/log/
|
||||
/.idea/*
|
||||
6
.idea/codeStyles/Project.xml
generated
6
.idea/codeStyles/Project.xml
generated
@@ -1,7 +1,13 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<JetCodeStyleSettings>
|
||||
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
|
||||
<value>
|
||||
<package name="java.util" withSubpackages="true" static="false" />
|
||||
</value>
|
||||
</option>
|
||||
<option name="SPACE_BEFORE_EXTEND_COLON" value="false" />
|
||||
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
</JetCodeStyleSettings>
|
||||
<codeStyleSettings language="kotlin">
|
||||
|
||||
2
.idea/gradle.xml
generated
2
.idea/gradle.xml
generated
@@ -8,13 +8,11 @@
|
||||
<option name="testRunner" value="PLATFORM" />
|
||||
<option name="distributionType" value="DEFAULT_WRAPPED" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="gradleHome" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="useAutoImport" value="true" />
|
||||
<option name="useQualifiedModuleNames" value="true" />
|
||||
</GradleProjectSettings>
|
||||
</option>
|
||||
|
||||
6
.idea/kotlinc.xml
generated
Normal file
6
.idea/kotlinc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Kotlin2JvmCompilerArguments">
|
||||
<option name="jvmTarget" value="11" />
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/misc.xml
generated
6
.idea/misc.xml
generated
@@ -1,7 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="adopt-openjdk-11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
<component name="TaskProjectConfiguration">
|
||||
|
||||
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@@ -1,8 +0,0 @@
|
||||
<?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>
|
||||
11
.idea/postgres-json.iml
generated
11
.idea/postgres-json.iml
generated
@@ -1,11 +0,0 @@
|
||||
<?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$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/var" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -27,7 +27,7 @@
|
||||
</extension>
|
||||
<GradleScriptDebugEnabled>true</GradleScriptDebugEnabled>
|
||||
<method v="2">
|
||||
<option name="RunConfigurationTask" enabled="true" run_configuration_name="ktlint" run_configuration_type="GradleRunConfiguration" />
|
||||
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Lint" run_configuration_type="GradleRunConfiguration" />
|
||||
<option name="RunConfigurationTask" enabled="true" run_configuration_name="tests" run_configuration_type="JUnit" />
|
||||
</method>
|
||||
</configuration>
|
||||
|
||||
3
.idea/runConfigurations/tests.xml
generated
3
.idea/runConfigurations/tests.xml
generated
@@ -1,6 +1,7 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="tests" type="JUnit" factoryName="JUnit" singleton="false">
|
||||
<module name="postgresjson.test" />
|
||||
<useClassPathOnly />
|
||||
<extension name="net.ashald.envfile">
|
||||
<option name="IS_ENABLED" value="false" />
|
||||
<option name="IS_SUBST" value="false" />
|
||||
@@ -12,7 +13,7 @@
|
||||
</ENTRIES>
|
||||
</extension>
|
||||
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
|
||||
<option name="ALTERNATIVE_JRE_PATH" value="11" />
|
||||
<option name="ALTERNATIVE_JRE_PATH" value="$USER_HOME$/.jdks/adopt-openjdk-11.0.5" />
|
||||
<option name="MAIN_CLASS_NAME" value="" />
|
||||
<option name="METHOD_NAME" value="" />
|
||||
<option name="TEST_OBJECT" value="directory" />
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
group = "fr.postgresjson"
|
||||
version = "0.1"
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
plugins {
|
||||
jacoco
|
||||
@@ -9,13 +8,23 @@ plugins {
|
||||
|
||||
id("org.jlleitschuh.gradle.ktlint") version "8.2.0"
|
||||
id("org.owasp.dependencycheck") version "5.1.0"
|
||||
id("fr.coppernic.versioning") version "3.1.2"
|
||||
}
|
||||
|
||||
group = "flecomte"
|
||||
version = versioning.info.tag
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
jcenter()
|
||||
}
|
||||
|
||||
tasks.withType<KotlinCompile> {
|
||||
kotlinOptions {
|
||||
jvmTarget = "11"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
|
||||
implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.31")
|
||||
@@ -31,14 +40,27 @@ dependencies {
|
||||
testImplementation("org.amshove.kluent:kluent:1.47")
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("maven") {
|
||||
groupId = "fr.postgresjson"
|
||||
artifactId = "postgresjson"
|
||||
version = "0.1"
|
||||
val sourcesJar by tasks.creating(Jar::class) {
|
||||
archiveClassifier.set("sources")
|
||||
from(sourceSets.getByName("main").allSource)
|
||||
}
|
||||
|
||||
from(components["java"])
|
||||
publishing {
|
||||
repositories {
|
||||
maven {
|
||||
name = "postgres-json"
|
||||
url = uri("https://maven.pkg.github.com/flecomte/postgres-json")
|
||||
credentials {
|
||||
username = System.getenv("GITHUB_USERNAME")
|
||||
password = System.getenv("GITHUB_TOKEN")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
publications {
|
||||
create<MavenPublication>("postgres-json") {
|
||||
from(components["java"])
|
||||
artifact(sourcesJar)
|
||||
}
|
||||
}
|
||||
}
|
||||
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,5 +1,5 @@
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
|
||||
@@ -1 +1 @@
|
||||
rootProject.name = 'postgresjson'
|
||||
rootProject.name = 'postgres-json'
|
||||
|
||||
@@ -6,12 +6,13 @@ import com.github.jasync.sql.db.QueryResult
|
||||
import com.github.jasync.sql.db.pool.ConnectionPool
|
||||
import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
|
||||
import com.github.jasync.sql.db.postgresql.PostgreSQLConnectionBuilder
|
||||
import com.github.jasync.sql.db.util.length
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.entity.Serializable
|
||||
import fr.postgresjson.serializer.Serializer
|
||||
import fr.postgresjson.utils.LoggerDelegate
|
||||
import org.slf4j.Logger
|
||||
import java.util.concurrent.CompletableFuture
|
||||
import java.util.concurrent.*
|
||||
|
||||
typealias SelectOneCallback<T> = QueryResult.(T?) -> Unit
|
||||
typealias SelectCallback<T> = QueryResult.(List<T>) -> Unit
|
||||
@@ -204,7 +205,7 @@ class Connection(
|
||||
|
||||
private fun compileArgs(values: List<Any?>): List<Any?> {
|
||||
return values.map {
|
||||
if (it is Serializable) {
|
||||
if (it is Serializable || (it is List<*> && it.firstOrNull() is Serializable)) {
|
||||
serializer.serialize(it)
|
||||
} else {
|
||||
it
|
||||
@@ -248,15 +249,33 @@ class Connection(
|
||||
data class ParametersQuery(val sql: String, val parameters: List<Any?>)
|
||||
|
||||
private fun <T> stopwatchQuery(sql: String, values: List<Any?> = emptyList(), callback: () -> T): T {
|
||||
val sqlForLog = "\n${sql.prependIndent()}"
|
||||
try {
|
||||
val start = System.currentTimeMillis()
|
||||
val result = callback()
|
||||
val duration = (System.currentTimeMillis() - start)
|
||||
logger?.debug("$duration ms for query: $sqlForLog \n {}", values.joinToString(", "))
|
||||
val resultText = when (result) {
|
||||
null -> "with no result"
|
||||
is QueryResult -> result.rows.firstOrNull()?.joinToString(", ")?.let { text ->
|
||||
if (text.length > 100) "${text.take(100)}... (size: ${text.length})" else text
|
||||
} ?: "with no result"
|
||||
else -> "unknown"
|
||||
}
|
||||
val args = """
|
||||
|Query ($duration ms):
|
||||
|${sql.trimIndent().prependIndent()}
|
||||
|Arguments (${values.length}):
|
||||
|${values.joinToString("\n").ifBlank { "No arguments" }.prependIndent()}
|
||||
|Result:
|
||||
|${resultText.trimIndent().prependIndent()}
|
||||
""".trimMargin().prependIndent(" > ")
|
||||
logger?.debug("Query executed in $duration ms \n{}", args)
|
||||
return result
|
||||
} catch (e: Throwable) {
|
||||
logger?.info("Query Error: $sqlForLog, $values", e)
|
||||
logger?.info("""
|
||||
Query Error:
|
||||
${sql.prependIndent()},
|
||||
${values.joinToString(", ").prependIndent()}
|
||||
""".trimIndent(), e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,12 @@ interface EmbedExecutable {
|
||||
|
||||
fun exec(values: List<Any?> = emptyList()): QueryResult
|
||||
fun exec(values: Map<String, Any?>): QueryResult
|
||||
fun exec(vararg values: Pair<String, Any?>): QueryResult = exec(values.toMap())
|
||||
|
||||
fun perform(values: List<Any?>) { exec(values) }
|
||||
fun perform(values: Map<String, Any?>) { exec(values) }
|
||||
fun perform(vararg values: Pair<String, Any?>) = perform(values.toMap())
|
||||
|
||||
fun sendQuery(values: List<Any?> = emptyList()): Int
|
||||
fun sendQuery(values: Map<String, Any?>): Int
|
||||
fun sendQuery(vararg values: Pair<String, Any?>): Int =
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package fr.postgresjson.connexion
|
||||
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import kotlin.math.ceil
|
||||
|
||||
data class Paginated<T : EntityI>(
|
||||
val result: List<T>,
|
||||
@@ -10,10 +11,15 @@ data class Paginated<T : EntityI>(
|
||||
) {
|
||||
val currentPage: Int = (offset / limit) + 1
|
||||
val count: Int = result.size
|
||||
val totalPages: Int = (total.toDouble() / limit.toDouble()).ceil()
|
||||
|
||||
init {
|
||||
if (offset < 0) error("offset must be greather or equal than 0")
|
||||
if (limit < 1) error("limit must be greather or equal than 1")
|
||||
if (total < 0) error("total must be greather or equal than 0")
|
||||
}
|
||||
|
||||
fun isLastPage(): Boolean = currentPage >= totalPages
|
||||
|
||||
private fun Double.ceil(): Int = ceil(this).toInt()
|
||||
}
|
||||
@@ -1,126 +1,5 @@
|
||||
package fr.postgresjson.entity
|
||||
|
||||
import org.joda.time.DateTime
|
||||
import java.util.*
|
||||
|
||||
interface Serializable
|
||||
interface EntityI : Serializable
|
||||
interface Parameter : Serializable
|
||||
|
||||
abstract class Entity<T>(open var id: T? = null) : EntityI
|
||||
open class UuidEntity(override var id: UUID? = UUID.randomUUID()) : Entity<UUID>(id)
|
||||
open class IdEntity(override var id: Int? = null) : Entity<Int>(id)
|
||||
|
||||
/* Version */
|
||||
interface EntityVersioning<ID, NUMBER> {
|
||||
var versionId: ID
|
||||
var versionNumber: NUMBER?
|
||||
}
|
||||
|
||||
class UuidEntityVersioning : EntityVersioning<UUID, Int> {
|
||||
override var versionId: UUID = UUID.randomUUID()
|
||||
override var versionNumber: Int? = null
|
||||
}
|
||||
|
||||
/* Dates */
|
||||
interface EntityCreatedAt {
|
||||
var createdAt: DateTime?
|
||||
}
|
||||
|
||||
interface EntityUpdatedAt {
|
||||
var updatedAt: DateTime?
|
||||
}
|
||||
|
||||
interface EntityDeletedAt {
|
||||
var deletedAt: DateTime?
|
||||
fun isDeleted(): Boolean {
|
||||
val deletedAt = deletedAt
|
||||
return deletedAt != null && deletedAt < DateTime.now()
|
||||
}
|
||||
}
|
||||
|
||||
class EntityCreatedAtImp : EntityCreatedAt {
|
||||
override var createdAt: DateTime? = null
|
||||
}
|
||||
|
||||
class EntityUpdatedAtImp : EntityUpdatedAt {
|
||||
override var updatedAt: DateTime? = null
|
||||
}
|
||||
|
||||
class EntityDeletedAtImp : EntityDeletedAt {
|
||||
override var deletedAt: DateTime? = null
|
||||
}
|
||||
|
||||
/* Author */
|
||||
interface EntityCreatedBy<T : EntityI> {
|
||||
var createdBy: T?
|
||||
}
|
||||
|
||||
interface EntityUpdatedBy<T : EntityI> {
|
||||
var updatedBy: T?
|
||||
}
|
||||
|
||||
interface EntityDeletedBy<T : EntityI> {
|
||||
var deletedBy: T?
|
||||
}
|
||||
|
||||
class EntityCreatedByImp<UserT : EntityI>(
|
||||
override var createdBy: UserT?
|
||||
) : EntityCreatedBy<UserT>
|
||||
|
||||
class EntityUpdatedByImp<UserT : EntityI>(
|
||||
override var updatedBy: UserT?
|
||||
) : EntityUpdatedBy<UserT>
|
||||
|
||||
class EntityDeletedByImp<UserT : EntityI>(
|
||||
override var deletedBy: UserT?
|
||||
) : EntityDeletedBy<UserT>
|
||||
|
||||
/* Mixed */
|
||||
class EntityDeletedImp<UserT : EntityI>(
|
||||
override var deletedBy: UserT? = null
|
||||
) : EntityDeletedBy<UserT>,
|
||||
EntityDeletedAt by EntityDeletedAtImp()
|
||||
|
||||
class EntityUpdatedImp<UserT : EntityI>(
|
||||
override var updatedAt: DateTime? = null,
|
||||
override var updatedBy: UserT? = null
|
||||
) : EntityUpdatedBy<UserT>,
|
||||
EntityUpdatedAt by EntityUpdatedAtImp()
|
||||
|
||||
class EntityCreatedImp<UserT : EntityI>(
|
||||
override var createdAt: DateTime? = null,
|
||||
override var createdBy: UserT? = null
|
||||
) : EntityCreatedBy<UserT>,
|
||||
EntityCreatedAt by EntityCreatedAtImp()
|
||||
|
||||
/* Published */
|
||||
interface Published<UserT : EntityI> {
|
||||
var publishedAt: DateTime?
|
||||
var publishedBy: UserT?
|
||||
}
|
||||
|
||||
class EntityPublishedImp<UserT : EntityI>(
|
||||
override var publishedBy: UserT?
|
||||
) : Published<UserT> {
|
||||
override var publishedAt: DateTime? = null
|
||||
}
|
||||
|
||||
/* Implementation */
|
||||
abstract class EntityImp<T, UserT : EntityI>(
|
||||
updatedBy: UserT?
|
||||
) : Entity<T>(),
|
||||
EntityCreatedAt by EntityCreatedAtImp(),
|
||||
EntityUpdatedAt by EntityUpdatedAtImp(),
|
||||
EntityDeletedAt by EntityDeletedAtImp(),
|
||||
EntityCreatedBy<UserT> by EntityCreatedByImp(updatedBy),
|
||||
EntityUpdatedBy<UserT> by EntityUpdatedByImp(updatedBy),
|
||||
EntityDeletedBy<UserT> by EntityDeletedByImp(updatedBy)
|
||||
|
||||
abstract class UuidEntityExtended<T, UserT : EntityI>(
|
||||
updatedBy: UserT?,
|
||||
publishedBy: UserT?
|
||||
) :
|
||||
EntityImp<T, UserT>(updatedBy),
|
||||
EntityVersioning<UUID, Int> by UuidEntityVersioning(),
|
||||
Published<UserT> by EntityPublishedImp(publishedBy)
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package fr.postgresjson.entity.immutable
|
||||
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.entity.mutable.EntityDeletedAt
|
||||
import fr.postgresjson.entity.mutable.EntityDeletedAtImp
|
||||
import fr.postgresjson.entity.mutable.EntityDeletedBy
|
||||
import fr.postgresjson.entity.mutable.EntityDeletedByImp
|
||||
import org.joda.time.DateTime
|
||||
import java.util.*
|
||||
|
||||
interface EntityRefI<T> : EntityI {
|
||||
val id: T
|
||||
}
|
||||
|
||||
interface UuidEntityI : EntityRefI<UUID> {
|
||||
override val id: UUID
|
||||
}
|
||||
|
||||
abstract class Entity<T>(override val id: T) : EntityRefI<T>
|
||||
open class UuidEntity(override val id: UUID = UUID.randomUUID()) : UuidEntityI, Entity<UUID>(id)
|
||||
|
||||
/* Version */
|
||||
interface EntityVersioning<ID, NUMBER> {
|
||||
val versionNumber: NUMBER
|
||||
val versionId: ID
|
||||
}
|
||||
|
||||
class UuidEntityVersioning(
|
||||
override var versionNumber: Int? = null,
|
||||
override val versionId: UUID = UUID.randomUUID()
|
||||
) : EntityVersioning<UUID, Int?>
|
||||
|
||||
/* Dates */
|
||||
interface EntityCreatedAt {
|
||||
val createdAt: DateTime
|
||||
}
|
||||
interface EntityUpdatedAt {
|
||||
var updatedAt: DateTime
|
||||
}
|
||||
|
||||
class EntityCreatedAtImp(
|
||||
override val createdAt: DateTime = DateTime.now()
|
||||
) : EntityCreatedAt
|
||||
|
||||
class EntityUpdatedAtImp(
|
||||
override var updatedAt: DateTime = DateTime.now()
|
||||
) : EntityUpdatedAt
|
||||
|
||||
/* Author */
|
||||
interface EntityCreatedBy<T : EntityI> {
|
||||
val createdBy: T
|
||||
}
|
||||
interface EntityUpdatedBy<T : EntityI> {
|
||||
var updatedBy: T
|
||||
}
|
||||
|
||||
class EntityCreatedByImp<UserT : EntityI>(
|
||||
override val createdBy: UserT
|
||||
) : EntityCreatedBy<UserT>
|
||||
|
||||
class EntityUpdatedByImp<UserT : EntityI>(
|
||||
override var updatedBy: UserT
|
||||
) : EntityUpdatedBy<UserT>
|
||||
|
||||
/* Mixed */
|
||||
class EntityCreatedImp<UserT : EntityI>(
|
||||
override val createdAt: DateTime = DateTime.now(),
|
||||
createdBy: UserT
|
||||
) : EntityCreatedBy<UserT> by EntityCreatedByImp(createdBy),
|
||||
EntityCreatedAt by EntityCreatedAtImp()
|
||||
|
||||
class EntityUpdatedImp<UserT : EntityI>(
|
||||
updatedAt: DateTime = DateTime.now(),
|
||||
override var updatedBy: UserT
|
||||
) : EntityUpdatedBy<UserT>,
|
||||
EntityUpdatedAt by EntityUpdatedAtImp(updatedAt)
|
||||
|
||||
/* Implementation */
|
||||
abstract class EntityImp<T, UserT : EntityI>(
|
||||
updatedBy: UserT,
|
||||
updatedAt: DateTime = DateTime.now()
|
||||
) : UuidEntity(),
|
||||
EntityCreatedAt by EntityCreatedAtImp(updatedAt),
|
||||
EntityUpdatedAt by EntityUpdatedAtImp(updatedAt),
|
||||
EntityDeletedAt by EntityDeletedAtImp(),
|
||||
EntityCreatedBy<UserT> by EntityCreatedByImp(updatedBy),
|
||||
EntityUpdatedBy<UserT> by EntityUpdatedByImp(updatedBy),
|
||||
EntityDeletedBy<UserT> by EntityDeletedByImp(updatedBy)
|
||||
135
src/main/kotlin/fr/postgresjson/entity/mutable/MutableEntity.kt
Normal file
135
src/main/kotlin/fr/postgresjson/entity/mutable/MutableEntity.kt
Normal file
@@ -0,0 +1,135 @@
|
||||
package fr.postgresjson.entity.mutable
|
||||
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import org.joda.time.DateTime
|
||||
import java.util.*
|
||||
|
||||
interface EntityRefI<T> : EntityI {
|
||||
var id: T?
|
||||
}
|
||||
|
||||
interface UuidEntityI : EntityRefI<UUID> {
|
||||
override var id: UUID?
|
||||
}
|
||||
|
||||
interface IdEntityI : EntityRefI<Int> {
|
||||
override var id: Int?
|
||||
}
|
||||
|
||||
abstract class Entity<T>(override var id: T? = null) : EntityRefI<T>
|
||||
open class UuidEntity(override var id: UUID? = UUID.randomUUID()) : UuidEntityI, Entity<UUID>(id)
|
||||
open class IdEntity(override var id: Int? = null) : IdEntityI, Entity<Int>(id)
|
||||
|
||||
/* Version */
|
||||
interface EntityVersioning<ID, NUMBER> {
|
||||
var versionId: ID
|
||||
var versionNumber: NUMBER?
|
||||
}
|
||||
|
||||
class UuidEntityVersioning(
|
||||
override var versionNumber: Int? = null,
|
||||
override var versionId: UUID = UUID.randomUUID()
|
||||
) : EntityVersioning<UUID, Int>
|
||||
|
||||
/* Dates */
|
||||
interface EntityCreatedAt {
|
||||
var createdAt: DateTime?
|
||||
}
|
||||
|
||||
interface EntityUpdatedAt {
|
||||
var updatedAt: DateTime?
|
||||
}
|
||||
|
||||
interface EntityDeletedAt {
|
||||
var deletedAt: DateTime?
|
||||
fun isDeleted(): Boolean {
|
||||
val deletedAt = deletedAt
|
||||
return deletedAt != null && deletedAt < DateTime.now()
|
||||
}
|
||||
}
|
||||
|
||||
class EntityCreatedAtImp : EntityCreatedAt {
|
||||
override var createdAt: DateTime? = null
|
||||
}
|
||||
|
||||
class EntityUpdatedAtImp : EntityUpdatedAt {
|
||||
override var updatedAt: DateTime? = null
|
||||
}
|
||||
|
||||
class EntityDeletedAtImp : EntityDeletedAt {
|
||||
override var deletedAt: DateTime? = null
|
||||
}
|
||||
|
||||
/* Author */
|
||||
interface EntityCreatedBy<T : EntityI> {
|
||||
var createdBy: T?
|
||||
}
|
||||
|
||||
interface EntityUpdatedBy<T : EntityI> {
|
||||
var updatedBy: T?
|
||||
}
|
||||
|
||||
interface EntityDeletedBy<T : EntityI> {
|
||||
var deletedBy: T?
|
||||
}
|
||||
|
||||
class EntityCreatedByImp<UserT : EntityI>(
|
||||
override var createdBy: UserT?
|
||||
) : EntityCreatedBy<UserT>
|
||||
|
||||
class EntityUpdatedByImp<UserT : EntityI>(
|
||||
override var updatedBy: UserT?
|
||||
) : EntityUpdatedBy<UserT>
|
||||
|
||||
class EntityDeletedByImp<UserT : EntityI>(
|
||||
override var deletedBy: UserT?
|
||||
) : EntityDeletedBy<UserT>
|
||||
|
||||
/* Mixed */
|
||||
class EntityDeletedImp<UserT : EntityI>(
|
||||
override var deletedBy: UserT? = null
|
||||
) : EntityDeletedBy<UserT>,
|
||||
EntityDeletedAt by EntityDeletedAtImp()
|
||||
|
||||
class EntityUpdatedImp<UserT : EntityI>(
|
||||
override var updatedAt: DateTime? = null,
|
||||
override var updatedBy: UserT? = null
|
||||
) : EntityUpdatedBy<UserT>,
|
||||
EntityUpdatedAt by EntityUpdatedAtImp()
|
||||
|
||||
class EntityCreatedImp<UserT : EntityI>(
|
||||
override var createdAt: DateTime? = null,
|
||||
override var createdBy: UserT? = null
|
||||
) : EntityCreatedBy<UserT>,
|
||||
EntityCreatedAt by EntityCreatedAtImp()
|
||||
|
||||
/* Published */
|
||||
interface Published<UserT : EntityI> {
|
||||
var publishedAt: DateTime?
|
||||
var publishedBy: UserT?
|
||||
}
|
||||
|
||||
class EntityPublishedImp<UserT : EntityI>(
|
||||
override var publishedBy: UserT?
|
||||
) : Published<UserT> {
|
||||
override var publishedAt: DateTime? = null
|
||||
}
|
||||
|
||||
/* Implementation */
|
||||
abstract class EntityImp<T, UserT : EntityI>(
|
||||
updatedBy: UserT?
|
||||
) : Entity<T>(),
|
||||
EntityCreatedAt by EntityCreatedAtImp(),
|
||||
EntityUpdatedAt by EntityUpdatedAtImp(),
|
||||
EntityDeletedAt by EntityDeletedAtImp(),
|
||||
EntityCreatedBy<UserT> by EntityCreatedByImp(updatedBy),
|
||||
EntityUpdatedBy<UserT> by EntityUpdatedByImp(updatedBy),
|
||||
EntityDeletedBy<UserT> by EntityDeletedByImp(updatedBy)
|
||||
|
||||
abstract class UuidEntityExtended<T, UserT : EntityI>(
|
||||
updatedBy: UserT?,
|
||||
publishedBy: UserT?
|
||||
) :
|
||||
EntityImp<T, UserT>(updatedBy),
|
||||
EntityVersioning<UUID, Int> by UuidEntityVersioning(),
|
||||
Published<UserT> by EntityPublishedImp(publishedBy)
|
||||
@@ -5,7 +5,7 @@ import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import fr.postgresjson.migration.Migration.Status
|
||||
import java.util.*
|
||||
import java.util.concurrent.CompletionException
|
||||
import java.util.concurrent.*
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
|
||||
data class Function(
|
||||
@@ -75,7 +75,7 @@ data class Function(
|
||||
}
|
||||
|
||||
override fun status(): Status {
|
||||
val result = connection.inTransaction {
|
||||
connection.inTransaction {
|
||||
up()
|
||||
down()
|
||||
it.sendQuery("ROLLBACK")
|
||||
|
||||
@@ -3,7 +3,7 @@ package fr.postgresjson.migration
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.definition.Function.FunctionNotFound
|
||||
import fr.postgresjson.entity.Entity
|
||||
import fr.postgresjson.entity.mutable.Entity
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import fr.postgresjson.migration.Migration.Status
|
||||
import fr.postgresjson.utils.LoggerDelegate
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package fr.postgresjson.migration
|
||||
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.entity.Entity
|
||||
import fr.postgresjson.entity.mutable.Entity
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import java.util.*
|
||||
|
||||
@@ -48,7 +48,7 @@ data class Query(
|
||||
}
|
||||
|
||||
override fun status(): Migration.Status {
|
||||
val result = connection.inTransaction {
|
||||
connection.inTransaction {
|
||||
up()
|
||||
down()
|
||||
it.sendQuery("ROLLBACK")
|
||||
|
||||
@@ -3,7 +3,7 @@ package fr.postgresjson.repository
|
||||
import fr.postgresjson.connexion.Requester
|
||||
|
||||
interface RepositoryI {
|
||||
var requester: Requester
|
||||
val requester: Requester
|
||||
|
||||
enum class Direction {
|
||||
asc,
|
||||
|
||||
@@ -49,5 +49,6 @@ class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
}
|
||||
|
||||
fun Serializable.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
||||
fun List<Serializable>.serialize(pretty: Boolean = false) = Serializer().serialize(this, pretty)
|
||||
inline fun <reified E : Serializable> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||
inline fun <reified E : Serializable> String.deserialize() = Serializer().deserialize<E>(this)
|
||||
@@ -1,7 +1,7 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.entity.IdEntity
|
||||
import fr.postgresjson.entity.mutable.IdEntity
|
||||
import fr.postgresjson.entity.Parameter
|
||||
import org.junit.Assert.*
|
||||
import org.junit.jupiter.api.Assertions
|
||||
@@ -38,8 +38,7 @@ class ConnectionTest() : TestAbstract() {
|
||||
""".trimIndent()
|
||||
)
|
||||
assertNotNull(objs)
|
||||
assertTrue(objs is List<ObjTest2>)
|
||||
assertEquals(objs!!.size, 2)
|
||||
assertEquals(objs.size, 2)
|
||||
assertEquals(objs[0].id, 1)
|
||||
assertEquals(objs[0].test!!.id, 1)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.entity.*
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.entity.mutable.*
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.TestInstance
|
||||
|
||||
@@ -2,7 +2,7 @@ package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.entity.IdEntity
|
||||
import fr.postgresjson.entity.mutable.IdEntity
|
||||
import org.junit.Assert
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package fr.postgresjson
|
||||
|
||||
import fr.postgresjson.entity.IdEntity
|
||||
import fr.postgresjson.entity.mutable.IdEntity
|
||||
import fr.postgresjson.serializer.Serializer
|
||||
import fr.postgresjson.serializer.deserialize
|
||||
import fr.postgresjson.serializer.serialize
|
||||
@@ -41,6 +41,14 @@ internal class SerializerTest {
|
||||
assertTrue(json.contains(""""val1":"plop","val2":123"""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun serializeList() {
|
||||
val list = listOf(ObjTest("one", 1), ObjTest("two", 2))
|
||||
val json = list.serialize()
|
||||
assertTrue(json.contains(""""val1":"one","val2":1"""))
|
||||
assertTrue(json.contains(""""val1":"two","val2":2"""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun serializeDate() {
|
||||
val objDate = ObjTestDate(DateTime.parse("2019-07-30T14:08:51.420108+04:00"))
|
||||
|
||||
Reference in New Issue
Block a user