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 320690a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 90 deletions.
111 changes: 66 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@
<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>
<h3 align="center">
Lib. similar to JetBrains/Exposed BUT without annotations, DSLs or DAOs... <br>
Library has a strong focus on clean architecture and simplicity.
</h3>

```kotlin
val outputs = 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>()}
INNER JOIN ${it.table<Parent>()} ON ${it.column(Parent::pk)} = ${it.column(Child::parent_pk)}
WHERE ${it.column(Child::value)} = ${it.input(Parent::value)}
"""
}
assert(outputs == listOf(Out(child_value="value_3", parent_value="value_3")))
```

<br>
<br>
<table width="100%" border="0">
Expand Down Expand Up @@ -40,8 +59,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 +121,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 +153,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 +295,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 +419,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 +474,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
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
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
23 changes: 20 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,22 @@ fun main() {
WHERE ${it.column(Child::value)} = ${it.input(Parent::value)}
"""
}
println(moreChildren)
assert(moreChildren == listOf(children[3]))

// START 'Selective join'
val outputs = 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>()}
INNER JOIN ${it.table<Parent>()} ON ${it.column(Parent::pk)} = ${it.column(Child::parent_pk)}
WHERE ${it.column(Child::value)} = ${it.input(Parent::value)}
"""
}
assert(outputs == listOf(Out(child_value="value_3", parent_value="value_3")))
// STOP
}
// STOP

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

0 comments on commit 320690a

Please sign in to comment.