Skip to content

Commit

Permalink
support for joinTable when columns are from the same source
Browse files Browse the repository at this point in the history
  • Loading branch information
3ricL committed Apr 30, 2017
1 parent 8a26717 commit fff5f6f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main/kotlin/org/jetbrains/exposed/dao/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,16 @@ class View<out Target: Entity<*>> (val op : Op<Boolean>, val factory: EntityClas

@Suppress("UNCHECKED_CAST")
class InnerTableLink<ID:Any, Target: Entity<ID>>(val table: Table,
val target: EntityClass<ID, Target>) {
val target: EntityClass<ID, Target>,
val source: Column<*>? = null) {

private fun getSourceRefColumn(o: Entity<*>): Column<EntityID<*>> {
val sourceRefColumn = table.columns.singleOrNull { it.referee == o.klass.table.id } as? Column<EntityID<*>> ?: error("Table does not reference source")
val sourceRefColumn = table.columns.singleOrNull { it.referee == o.klass.table.id && if(source == null) true else it == source } as? Column<EntityID<*>> ?: error("Table does not reference source")
return sourceRefColumn
}

private fun getTargetRefColumn(): Column<EntityID<*>> {
val sourceRefColumn = table.columns.singleOrNull { it.referee == target.table.id } as? Column<EntityID<*>> ?: error("Table does not reference target")
val sourceRefColumn = table.columns.singleOrNull { it.referee == target.table.id && if(source == null) true else it != source } as? Column<EntityID<*>> ?: error("Table does not reference target")
return sourceRefColumn
}

Expand Down Expand Up @@ -251,6 +253,10 @@ open class Entity<ID:Any>(val id: EntityID<ID>) {
return InnerTableLink(table, this@via)
}

infix fun <ID:Any, Target:Entity<ID>> EntityClass<ID, Target>.via(sourceColumn: Column<EntityID<ID>>): InnerTableLink<ID, Target> {
return InnerTableLink(sourceColumn.table, this@via, sourceColumn)
}

fun <T: Entity<*>> s(c: EntityClass<T, *>): EntityClass<T, *> = c

open fun delete(){
Expand Down

0 comments on commit fff5f6f

Please sign in to comment.