diff --git a/example/src/main/kotlin/com/github/andrewoma/kwery/example/film/dao/ActorDao.kt b/example/src/main/kotlin/com/github/andrewoma/kwery/example/film/dao/ActorDao.kt index 451fca5..343fbd7 100644 --- a/example/src/main/kotlin/com/github/andrewoma/kwery/example/film/dao/ActorDao.kt +++ b/example/src/main/kotlin/com/github/andrewoma/kwery/example/film/dao/ActorDao.kt @@ -37,13 +37,13 @@ object actorTable : Table("actor", tableConfig, "actor_seq"), VersionedW // @formatter:off val Id by col(A::id, id = true) val FirstName by col(N::first, { it.name }) - val LastName by col(N::last, { it.name }) + val LastName by col(N::last, { it.name }) val Version by col(A::version, version = true) // @formatter:on override fun idColumns(id: Int) = setOf(Id of id) - override fun create(value: Value) = A(value.of(Id), N(value.of(FirstName), value.of(LastName)), + override fun create(value: Value) = A(value of Id, N(value of FirstName, value of LastName), value.of(Version)) } diff --git a/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Column.kt b/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Column.kt new file mode 100644 index 0000000..933e07e --- /dev/null +++ b/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Column.kt @@ -0,0 +1,67 @@ +package com.github.andrewoma.kwery.mapper + +/** + * Column defines a how to map an SQL column to and from an object property of type `T` + * within a containing class `C`. + * + * While columns can be added directly it is more common to use the `col` methods on `Table` + * to provide sensible defaults. + */ +data class Column( + /** + * A function to extract the property value from the containing object + */ + val property: (C) -> T, + + /** + * If a value is not `nullable` a default value must be provided to allow construction + * of partially selected objects + */ + val defaultValue: T, + + /** + * A converter between the SQL type and `T` + */ + val converter: Converter, + + /** + * The name of the SQL column + */ + val name: String, + + /** + * True if the column is part of the primary key + */ + val id: Boolean, + + /** + * True if the column is used for versioning using optimistic locking + */ + val version: Boolean, + + /** + * True if the column is selected in queries by default. + * Generally true, but is useful to exclude `BLOBs` and `CLOBs` in some cases. + */ + val selectByDefault: Boolean, + + /** + * True if the column is nullable + */ + val isNullable: Boolean +) { + /** + * A type-safe variant of `to` + */ + infix fun of(value: T): Pair, T> = Pair(this, value) + + /** + * A type-safe variant of `to` with an optional value + */ + @Suppress("BASE_WITH_NULLABLE_UPPER_BOUND") + infix fun optional(value: T?): Pair, T?> = Pair(this, value) + + override fun toString(): String { + return "Column($name id=$id version=$version nullable=$isNullable)" // Prevent NPE in debugger on "property" + } +} \ No newline at end of file diff --git a/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Table.kt b/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Table.kt index e3efd73..2233e34 100644 --- a/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Table.kt +++ b/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Table.kt @@ -25,108 +25,12 @@ package com.github.andrewoma.kwery.mapper import com.github.andrewoma.kommon.collection.hashMapOfExpectedSize import com.github.andrewoma.kwery.core.Row import com.github.andrewoma.kwery.core.Session -import com.github.andrewoma.kwery.mapper.util.camelToLowerUnderscore import java.lang.reflect.ParameterizedType import java.util.* import kotlin.properties.ReadOnlyProperty import kotlin.reflect.* import kotlin.reflect.jvm.javaType -/** - * Column defines a how to map an SQL column to and from an object property of type `T` - * within a containing class `C`. - * - * While columns can be added directly it is more common to use the `col` methods on `Table` - * to provide sensible defaults. - */ -data class Column( - /** - * A function to extract the property value from the containing object - */ - val property: (C) -> T, - - /** - * If a value is not `nullable` a default value must be provided to allow construction - * of partially selected objects - */ - val defaultValue: T, - - /** - * A converter between the SQL type and `T` - */ - val converter: Converter, - - /** - * The name of the SQL column - */ - val name: String, - - /** - * True if the column is part of the primary key - */ - val id: Boolean, - - /** - * True if the column is used for versioning using optimistic locking - */ - val version: Boolean, - - /** - * True if the column is selected in queries by default. - * Generally true, but is useful to exclude `BLOBs` and `CLOBs` in some cases. - */ - val selectByDefault: Boolean, - - /** - * True if the column is nullable - */ - val isNullable: Boolean -) { - /** - * A type-safe variant of `to` - */ - infix fun of(value: T): Pair, T> = Pair(this, value) - - /** - * A type-safe variant of `to` with an optional value - */ - @Suppress("BASE_WITH_NULLABLE_UPPER_BOUND") - infix fun optional(value: T?): Pair, T?> = Pair(this, value) - - override fun toString(): String { - return "Column($name id=$id version=$version nullable=$isNullable)" // Prevent NPE in debugger on "property" - } -} - -/** - * Value allows extraction of column values by column. - */ -interface Value { - infix fun of(column: Column): T -} - -/** - * TableConfiguration defines configuration common to a set of tables. - */ -class TableConfiguration( - /** - * Defines default values for types when the column is not null, but is not selected. - * Defaults to `standardDefaults` - */ - val defaults: Map = standardDefaults + timeDefaults, - - /** - * Defines converters from JDBC types to arbitrary Kotlin types. - * Defaults to `standardConverters` + `timeConverters` - */ - val converters: Map, Converter<*>> = standardConverters + timeConverters, - - /** - * Defines the naming convention for converting `Column` names to SQL column names. - * Defaults to `camelToLowerUnderscore` - */ - val namingConvention: (String) -> String = camelToLowerUnderscore) - /** * A `Table` maps directly to a single SQL table, with each SQL column defined explicitly. @@ -228,7 +132,7 @@ abstract class Table(val name: String, val config: TableConfigurati ): DelegatedColumn { - val column = Column({ path(it)?.let { property.get(it) }}, null, optional(converter), name ?: "", id, version, selectByDefault, true) + val column = Column({ path(it)?.let { property.get(it) } }, null, optional(converter), name ?: "", id, version, selectByDefault, true) return DelegatedColumn(column) } diff --git a/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/TableConfiguration.kt b/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/TableConfiguration.kt new file mode 100644 index 0000000..d39beac --- /dev/null +++ b/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/TableConfiguration.kt @@ -0,0 +1,26 @@ +package com.github.andrewoma.kwery.mapper + +import com.github.andrewoma.kwery.mapper.util.camelToLowerUnderscore +import kotlin.reflect.KType + +/** + * TableConfiguration defines configuration common to a set of tables. + */ +class TableConfiguration( + /** + * Defines default values for types when the column is not null, but is not selected. + * Defaults to `standardDefaults` + */ + val defaults: Map = standardDefaults + timeDefaults, + + /** + * Defines converters from JDBC types to arbitrary Kotlin types. + * Defaults to `standardConverters` + `timeConverters` + */ + val converters: Map, Converter<*>> = standardConverters + timeConverters, + + /** + * Defines the naming convention for converting `Column` names to SQL column names. + * Defaults to `camelToLowerUnderscore` + */ + val namingConvention: (String) -> String = camelToLowerUnderscore) \ No newline at end of file diff --git a/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Value.kt b/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Value.kt new file mode 100644 index 0000000..9c718af --- /dev/null +++ b/mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Value.kt @@ -0,0 +1,8 @@ +package com.github.andrewoma.kwery.mapper + +/** + * Value allows extraction of column values by column. + */ +interface Value { + infix fun of(column: Column): T +} \ No newline at end of file