-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split table classes into separate files
- Loading branch information
Showing
5 changed files
with
104 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Column.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<C, T>( | ||
/** | ||
* 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<T>, | ||
|
||
/** | ||
* 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<Column<C, T>, 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<Column<C, T>, T?> = Pair(this, value) | ||
|
||
override fun toString(): String { | ||
return "Column($name id=$id version=$version nullable=$isNullable)" // Prevent NPE in debugger on "property" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/TableConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<KType, *> = standardDefaults + timeDefaults, | ||
|
||
/** | ||
* Defines converters from JDBC types to arbitrary Kotlin types. | ||
* Defaults to `standardConverters` + `timeConverters` | ||
*/ | ||
val converters: Map<Class<*>, Converter<*>> = standardConverters + timeConverters, | ||
|
||
/** | ||
* Defines the naming convention for converting `Column` names to SQL column names. | ||
* Defaults to `camelToLowerUnderscore` | ||
*/ | ||
val namingConvention: (String) -> String = camelToLowerUnderscore) |
8 changes: 8 additions & 0 deletions
8
mapper/src/main/kotlin/com/github/andrewoma/kwery/mapper/Value.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.github.andrewoma.kwery.mapper | ||
|
||
/** | ||
* Value allows extraction of column values by column. | ||
*/ | ||
interface Value<C> { | ||
infix fun <T> of(column: Column<C, T>): T | ||
} |