Skip to content

Commit

Permalink
docs: Fix Enumeration topic examples
Browse files Browse the repository at this point in the history
- Switch manual create with plain SQL to mock an already existing table
- Add insert function to test that Exposed table object successfully works with
database object not created by Exposed
- Move these existing enum column functions to runH2Examples()
- Fix detekt issues in changed files
  • Loading branch information
bog-walk committed Feb 20, 2025
1 parent 4a2dbd7 commit 8dbc5b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package org.example

import org.example.examples.EnumerationExamples
import org.example.examples.JSONandJSONBExamples
import org.example.examples.TeamsTable
import org.example.examples.TeamProjectsTable
import org.example.examples.TeamsTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.DatabaseConfig
import org.jetbrains.exposed.sql.SchemaUtils
Expand Down Expand Up @@ -36,7 +36,6 @@ fun runMySQLExamples() {
jsonExamples.useContains()
jsonExamples.useContainsWithPath()
jsonArraysExamples()
enumExamples.createTableWithExistingEnumColumn()
}
}

Expand All @@ -53,6 +52,8 @@ fun runH2Examples() {
jsonExamples.example()
jsonExamples.useExtract()
jsonExamples.useExists()
enumExamples.createTableWithExistingEnumColumn()
enumExamples.insertEnumIntoTableWithExistingEnumColumn()
}
}

Expand All @@ -79,4 +80,3 @@ fun jsonArraysExamples() {
SchemaUtils.create(TeamProjectsTable)
jsonExamples.insertJSONArrays()
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.example.examples

import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction
import org.postgresql.util.PGobject

Expand All @@ -15,21 +16,40 @@ class PGEnum<T : Enum<T>>(enumTypeName: String, enumValue: T?) : PGobject() {
}

object EnumTable : Table() {
val enumColumn = customEnumeration("enumColumn", "FooEnum", { value -> Foo.valueOf(value as String) }, { PGEnum("FooEnum", it) })
val enumColumn: Column<Foo> = customEnumeration(
name = "enumColumn",
sql = "FooEnum",
fromDb = { value -> Foo.valueOf(value as String) },
toDb = { PGEnum("FooEnum", it) }
)
}

object NewEnumTable : Table() {
val newEnumColumn = customEnumeration("enumColumn", "ENUM('BAR', 'BAZ')", { value -> Foo.valueOf(value as String) }, { it.name })
val newEnumColumn: Column<Foo> = customEnumeration(
name = "enumColumn",
sql = "ENUM('BAR', 'BAZ')",
fromDb = { value -> Foo.valueOf(value as String) },
toDb = { it.name }
)
}

object ExistingEnumTable : Table() {
val existingEnumColumn = customEnumeration("enumColumn", { value -> Foo.valueOf(value as String) }, { it.name })
val existingEnumColumn: Column<Foo> = customEnumeration(
name = "enumColumn",
fromDb = { value -> Foo.valueOf(value as String) },
toDb = { it.name }
)
}

class EnumerationExamples {
fun createTableWithExistingEnumColumn() {
transaction {
SchemaUtils.create(MySQLEnumTable)
exec("""CREATE TABLE IF NOT EXISTS EXISTINGENUM ("enumColumn" ENUM('BAR', 'BAZ') NOT NULL)""")
}
}
fun insertEnumIntoTableWithExistingEnumColumn() {
ExistingEnumTable.insert {
it[existingEnumColumn] = Foo.BAZ
}
}
fun createTableWithEnumColumn() {
Expand Down

0 comments on commit 8dbc5b9

Please sign in to comment.