Skip to content

Commit

Permalink
Tutorial for selective JOIN.
Browse files Browse the repository at this point in the history
  • Loading branch information
urosjarc committed Apr 3, 2024
1 parent 55cc412 commit f6924a8
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 93 deletions.
114 changes: 68 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<h1 align="center">db-messiah</h1>
<h3 align="center">Kotlin lib. for enterprise database development</h3>
<h3 align="center">
Similar to JetBrains/Exposed, but with data class reflection.<br>
No need for annotations, DSLs or DAOs, reflection does that for you!<br>
Strong focus on clean architecture, simplicity and minimal developer interference.<br>
Build huge databases with 100+ tables with ease!
</h3>
<br>
<p align="center">
+<b>290</b> <a href="https://github.com/urosjarc/db-messiah/blob/master/src/test">unit</a>,
+<b>210</b> <a href="https://github.com/urosjarc/db-messiah/blob/master/src/e2e">e2e</a>,
Expand All @@ -11,6 +17,20 @@
<a href="https://github.com/urosjarc/db-messiah/blob/master/src/tutorials/kotlin/Test_README.kt">tested</a><br>
+<b>86%</b> instruction coverage
</p>
<br>

```kotlin
val result = it.query.get(output = Out::class, input = parent) {
"""
SELECT ${it.column(Child::value)} AS ${it.name(Out::child_value)},
${it.column(Parent::value)} AS ${it.name(Out::parent_value)}
FROM ${it.table<Child>()}
JOIN ${it.table<Parent>()} ON ${it.column(Parent::pk)} = ${it.column(Child::parent_pk)}
WHERE ${it.column(Child::value)} = ${it.input(Parent::value)}
"""
}
```

<br>
<br>
<table width="100%" border="0">
Expand Down Expand Up @@ -40,8 +60,8 @@
```kotlin
/** DEPENDENCIES */

implementation("com.urosjarc:db-messiah:0.0.1") // Required
implementation("com.urosjarc:db-messiah-extra:0.0.1") // Optional
implementation("com.urosjarc:db-messiah:0.0.2") // Required
implementation("com.urosjarc:db-messiah-extra:0.0.2") // Optional
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:2.20.0") //Optional

/** DRIVERS */
Expand Down Expand Up @@ -102,6 +122,10 @@ data class Unsafe(
val pk: UUID = UUID.randomUUID(), // Unsafe UUID manual primary key
val created: Instant = Instant.now() // Support for java.time.*
)

/** QUERY DTO */

data class Out(val child_value: String, val parent_value: String)
```

<br><h3 align="center">Database</h3>
Expand Down Expand Up @@ -130,12 +154,13 @@ val sqliteSerializer = SqliteSerializer(
)
),
),
globalOutputs = listOf(Out::class)
)

/** POSTGRES */

val pgSerializer = PgSerializer(
globalSerializers = BasicTS.sqlite + JavaTimeTS.postgresql,
globalSerializers = BasicTS.postgresql + JavaTimeTS.postgresql,
schemas = listOf(
PgSchema(
name = "name", tables = listOf(
Expand Down Expand Up @@ -271,9 +296,8 @@ sqlite.autocommit {
WHERE ${it.column(Child::value)} = ${it.input(Parent::value)}
"""
}
println(moreChildren)
assert(moreChildren == listOf(children[3]))
}

```

<br><h3 align="center">Transactions</h3>
Expand Down Expand Up @@ -396,52 +420,51 @@ For detailed explanation read about <a href="https://logging.apache.org/log4j/2.

<br><br><h2 align="center">Specifications</h3>

| | Schema | Serializer | Service | Basic types | java.time.* types |
|:--------:|:-------------:|:----------------:|:-------------:|:-------------------:|:----------------------:|
| DB2 | Db2Schema | Db2Serializer | Db2Service | BasicTS.db2 | JavaTimeTS.db2 |
| Derby | DerbySchema | DerbySerializer | DerbyService | BasicTS.derby | JavaTimeTS.derby |
| H2 | H2Schema | H2Serializer | H2Service | BasicTS.h2 | JavaTimeTS.h2 |
| Maria | MariaSchema | MariaSerializer | MariaService | BasicTS.maria | JavaTimeTS.maria |
| MS SQL | MssqlSchema | MssqlSerializer | MssqlService | BasicTS.mssql | JavaTimeTS.mssql |
| MySQL | MysqlSchema | MysqlSerializer | MysqlService | BasicTS.mysql | JavaTimeTS.mysql |
| Oracle | OracleSchema | OracleSerializer | OracleService | BasicTS.oracle | JavaTimeTS.oracle |
| Postgres | PgSchema | PgSerializer | PgService | BasicTS. postgresql | JavaTimeTS. postgresql |
| Sqlite | :x: | SqliteSerializer | SqliteService | BasicTS.sqlite | JavaTimeTS.sqlite |
| | Schema | Serializer | Service | Basic types | java.time.* types |
|:--------:|:------------:|:----------------:|:-------------:|:-------------------:|:----------------------:|
| DB2 | Db2Schema | Db2Serializer | Db2Service | BasicTS.db2 | JavaTimeTS.db2 |
| Derby | DerbySchema | DerbySerializer | DerbyService | BasicTS.derby | JavaTimeTS.derby |
| H2 | H2Schema | H2Serializer | H2Service | BasicTS.h2 | JavaTimeTS.h2 |
| Maria | MariaSchema | MariaSerializer | MariaService | BasicTS.maria | JavaTimeTS.maria |
| MS SQL | MssqlSchema | MssqlSerializer | MssqlService | BasicTS.mssql | JavaTimeTS.mssql |
| MySQL | MysqlSchema | MysqlSerializer | MysqlService | BasicTS.mysql | JavaTimeTS.mysql |
| Oracle | OracleSchema | OracleSerializer | OracleService | BasicTS.oracle | JavaTimeTS.oracle |
| Postgres | PgSchema | PgSerializer | PgService | BasicTS. postgresql | JavaTimeTS. postgresql |
| Sqlite | :x: | SqliteSerializer | SqliteService | BasicTS.sqlite | JavaTimeTS.sqlite |

<br><h3 align="center">Features</h3>

| | Escape | Schema | Auto INT PK | Auto UUID PK | UUID column | Many queries | Cascade | Procedure |
| | Escape | Schema | Auto INT PK | Auto UUID PK | UUID column | Many queries | Cascade | Procedure |
|:--------:|:------:|:----------------------:|:------------------:|:------------------:|:------------:|:------------------:|:-------------------------------------:|:----------------------:|
| DB2 | "%s" | :large_orange_diamond: | :white_check_mark: | :x: | CHAR(36) | :x: | :x: | :large_orange_diamond: |
| Derby | "%s" | :white_check_mark: | :white_check_mark: | :x: | CHAR(36) | :x: | :x: | :x: |
| H2 | "%s" | :white_check_mark: | :white_check_mark: | :white_check_mark: | UUID | :x: | :white_check_mark: | :x: |
| Maria | \`%s\` | :white_check_mark: | :white_check_mark: | :x: | UUID | :x: | :x: | :white_check_mark: |
| MS SQL | [%s] | :white_check_mark: | :white_check_mark: | :x: | UNIQUEIDE... | :white_check_mark: | :x: | :white_check_mark: |
| MySQL | \`%s\` | :white_check_mark: | :white_check_mark: | :x: | CHAR(36) | :x: | :x: | :white_check_mark: |
| Oracle | "%s" | :large_orange_diamond: | :white_check_mark: | :x: | VARCHAR2(36) | :x: | :white_check_mark: | :white_check_mark: |
| Postgres | "%s" | :white_check_mark: | :white_check_mark: | :white_check_mark: | UUID | :white_check_mark: | :white_check_mark: :white_check_mark: | :x: |
| Sqlite | "%s" | :x: | :white_check_mark: | :x: | CHAR(36) | :x: | :x: | :x: |
| DB2 | "%s" | :large_orange_diamond: | :white_check_mark: | :x: | CHAR(36) | :x: | :x: | :large_orange_diamond: |
| Derby | "%s" | :white_check_mark: | :white_check_mark: | :x: | CHAR(36) | :x: | :x: | :x: |
| H2 | "%s" | :white_check_mark: | :white_check_mark: | :white_check_mark: | UUID | :x: | :white_check_mark: | :x: |
| Maria | \`%s\` | :white_check_mark: | :white_check_mark: | :x: | UUID | :x: | :x: | :white_check_mark: |
| MS SQL | [%s] | :white_check_mark: | :white_check_mark: | :x: | UNIQUEIDE... | :white_check_mark: | :x: | :white_check_mark: |
| MySQL | \`%s\` | :white_check_mark: | :white_check_mark: | :x: | CHAR(36) | :x: | :x: | :white_check_mark: |
| Oracle | "%s" | :large_orange_diamond: | :white_check_mark: | :x: | VARCHAR2(36) | :x: | :white_check_mark: | :white_check_mark: |
| Postgres | "%s" | :white_check_mark: | :white_check_mark: | :white_check_mark: | UUID | :white_check_mark: | :white_check_mark: :white_check_mark: | :x: |
| Sqlite | "%s" | :x: | :white_check_mark: | :x: | CHAR(36) | :x: | :x: | :x: |

<br><br><h3 align="center">Type system</h3>

| KClass | COLUMN | Databases | db-messiah | db-messiah-extra |
|:---------------:|:------------:|:------------------------------------:|:-----------------------:|:--------------------------:|
| Boolean | BOOL | :white_check_mark: | :white_check_mark: | :x: |
| Char | CHAR | :white_check_mark: | :white_check_mark: | :x: |
| String | VARCHAR(100) | :white_check_mark: | :white_check_mark: | :x: |
| Float | FLOAT | :white_check_mark: | :white_check_mark: | :x: |
| Double | DOUBLE | :white_check_mark: | :white_check_mark: | :x: |
| Byte / UByte | TINYINT | :white_check_mark: | :white_check_mark: | :x: |
| Short / UShort | SMALLINT | :white_check_mark: | :white_check_mark: | :x: |
| Int / Uint | INTEGER | :white_check_mark: | :white_check_mark: | :x: |
| Long / ULong | BIGINT | :white_check_mark: | :white_check_mark: | :x: |
| Instant | DATETIME | Sqlite, Mysql, MSSql, Maria, H2, DB2 | Java :white_check_mark: | kotlinx :white_check_mark: |
| Instant | TIMESTAMP | Derby, Postgres, Oracle | Java :white_check_mark: | kotlinx :white_check_mark: |
| LocalDateTime | :x: | :x: | Java :x: | kotlinx :x: |
| LocalDate | DATE | :white_check_mark: | Java :white_check_mark: | kotlinx :white_check_mark: |
| LocalTime | TIME | :white_check_mark: but Oracle | Java :white_check_mark: | kotlinx :white_check_mark: |
| LocalTime | NUMBER(5, 0) | Oracle | Java :white_check_mark: | kotlinx :white_check_mark: |>

| KClass | COLUMN | Databases | db-messiah | db-messiah-extra |
|:--------------:|:------------:|:------------------------------------:|:-----------------------:|:--------------------------:|
| Boolean | BOOL | :white_check_mark: | :white_check_mark: | :x: |
| Char | CHAR | :white_check_mark: | :white_check_mark: | :x: |
| String | VARCHAR(100) | :white_check_mark: | :white_check_mark: | :x: |
| Float | FLOAT | :white_check_mark: | :white_check_mark: | :x: |
| Double | DOUBLE | :white_check_mark: | :white_check_mark: | :x: |
| Byte / UByte | TINYINT | :white_check_mark: | :white_check_mark: | :x: |
| Short / UShort | SMALLINT | :white_check_mark: | :white_check_mark: | :x: |
| Int / Uint | INTEGER | :white_check_mark: | :white_check_mark: | :x: |
| Long / ULong | BIGINT | :white_check_mark: | :white_check_mark: | :x: |
| Instant | DATETIME | Sqlite, Mysql, MSSql, Maria, H2, DB2 | Java :white_check_mark: | kotlinx :white_check_mark: |
| Instant | TIMESTAMP | Derby, Postgres, Oracle | Java :white_check_mark: | kotlinx :white_check_mark: |
| LocalDateTime | :x: | :x: | Java :x: | kotlinx :x: |
| LocalDate | DATE | :white_check_mark: | Java :white_check_mark: | kotlinx :white_check_mark: |
| LocalTime | TIME | :white_check_mark: but Oracle | Java :white_check_mark: | kotlinx :white_check_mark: |
| LocalTime | NUMBER(5, 0) | Oracle | Java :white_check_mark: | kotlinx :white_check_mark: |>

<br><br><h3 align="center">PRIMARY KEY</h3>
<img width="100%" src="https://github.com/urosjarc/db-messiah/blob/master/docs/constraints/PRIMARY_KEY.png">
Expand All @@ -452,7 +475,6 @@ For detailed explanation read about <a href="https://logging.apache.org/log4j/2.
<br><br><h3 align="center">INSERT BATCH</h3>
<img width="100%" src="https://github.com/urosjarc/db-messiah/blob/master/docs/queries/BATCH_INSERT.png">


<br><br><h3 align="center">UPDATE / DELETE</h3>
<table align="center" width="100%">
<tr>
Expand Down
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ publishing {
artifact(javadocJar)
pom {
name = "Db Messiah"
description = "Kotlin lib. for enterprise database development"
description = "Db Messiah, kotlin lib. for enterprise database development "
url = github
issueManagement {
system = "Github"
Expand Down Expand Up @@ -203,8 +203,8 @@ tasks.register<GradleBuild>("readme") {
var readme = File("./src/tutorials/kotlin/Test_README.md").readText()

val dependencies = mutableListOf(
"implementation(\"${project.group}:${project.name}:$version\") // Required",
"implementation(\"${project.group}:${project.name}-extra:$version\") // Optional",
"implementation(\"${project.group}:${project.name}:${project.version}\") // Required",
"implementation(\"${project.group}:${project.name}-extra:${project.version}\") // Optional",
)
dependencies += optionals.map { "implementation(\"$it\") //Optional" }

Expand Down
22 changes: 19 additions & 3 deletions src/tutorials/kotlin/Test_README.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ data class Unsafe(
val pk: UUID = UUID.randomUUID(), // Unsafe UUID manual primary key
val created: Instant = Instant.now() // Support for java.time.*
)

/** QUERY DTO */

data class Out(val child_value: String, val parent_value: String)
// STOP

// START 'Database'
Expand All @@ -82,12 +86,13 @@ val sqliteSerializer = SqliteSerializer(
)
),
),
globalOutputs = listOf(Out::class)
)

/** POSTGRES */

val pgSerializer = PgSerializer(
globalSerializers = BasicTS.sqlite + JavaTimeTS.postgresql,
globalSerializers = BasicTS.postgresql + JavaTimeTS.postgresql,
schemas = listOf(
PgSchema(
name = "name", tables = listOf(
Expand Down Expand Up @@ -202,10 +207,21 @@ fun main() {
WHERE ${it.column(Child::value)} = ${it.input(Parent::value)}
"""
}
println(moreChildren)
assert(moreChildren == listOf(children[3]))

// START 'Selective join'
val result = it.query.get(output = Out::class, input = parent) {
"""
SELECT ${it.column(Child::value)} AS ${it.name(Out::child_value)},
${it.column(Parent::value)} AS ${it.name(Out::parent_value)}
FROM ${it.table<Child>()}
JOIN ${it.table<Parent>()} ON ${it.column(Parent::pk)} = ${it.column(Child::parent_pk)}
WHERE ${it.column(Child::value)} = ${it.input(Parent::value)}
"""
}
// STOP
assert(result == listOf(Out(child_value = "value_3", parent_value = "value_3")))
}
// STOP

// START 'Transactions'
sqlite.transaction { // Any exception inside will trigger rollback ALL!
Expand Down
Loading

0 comments on commit f6924a8

Please sign in to comment.