forked from JetBrains/Exposed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add set operations JetBrains#402 / UNION and UNION ALL
- Loading branch information
1 parent
ad00553
commit 2be392a
Showing
6 changed files
with
409 additions
and
108 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/AbstractQuery.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,90 @@ | ||
package org.jetbrains.exposed.sql | ||
|
||
import org.jetbrains.exposed.sql.statements.Statement | ||
import org.jetbrains.exposed.sql.statements.StatementType | ||
import org.jetbrains.exposed.sql.transactions.TransactionManager | ||
import java.sql.ResultSet | ||
|
||
abstract class AbstractQuery<T : AbstractQuery<T>>(targets: List<Table>) : SizedIterable<ResultRow>, Statement<ResultSet>(StatementType.SELECT, targets) { | ||
protected val transaction get() = TransactionManager.current() | ||
|
||
var orderByExpressions: List<Pair<Expression<*>, SortOrder>> = mutableListOf() | ||
private set | ||
var distinct: Boolean = false | ||
protected set | ||
var limit: Int? = null | ||
protected set | ||
var offset: Long = 0 | ||
private set | ||
var fetchSize: Int? = null | ||
private set | ||
|
||
abstract val set: FieldSet | ||
|
||
protected fun copyTo(other: AbstractQuery<T>) { | ||
other.orderByExpressions = orderByExpressions.toMutableList() | ||
other.distinct = distinct | ||
other.limit = limit | ||
other.offset = offset | ||
other.fetchSize = fetchSize | ||
} | ||
|
||
override fun prepareSQL(transaction: Transaction) = prepareSQL(QueryBuilder(true)) | ||
|
||
internal abstract fun prepareSQL(builder: QueryBuilder): String | ||
|
||
override fun arguments() = QueryBuilder(true).let { | ||
prepareSQL(it) | ||
if (it.args.isNotEmpty()) listOf(it.args) else emptyList() | ||
} | ||
|
||
fun withDistinct(value: Boolean = true): T = apply { | ||
distinct = value | ||
} as T | ||
|
||
override fun limit(n: Int, offset: Long): T = apply { | ||
limit = n | ||
this.offset = offset | ||
} as T | ||
|
||
fun orderBy(column: Expression<*>, order: SortOrder = SortOrder.ASC): T = orderBy(column to order) | ||
|
||
override fun orderBy(vararg order: Pair<Expression<*>, SortOrder>): T = apply { | ||
(orderByExpressions as MutableList).addAll(order) | ||
} as T | ||
|
||
fun fetchSize(n: Int): T = apply { | ||
fetchSize = n | ||
} as T | ||
|
||
protected var count: Boolean = false | ||
|
||
protected abstract val queryToExecute: Statement<ResultSet> | ||
|
||
override fun iterator(): Iterator<ResultRow> { | ||
val resultIterator = ResultIterator(transaction.exec(queryToExecute)!!) | ||
return if (transaction.db.supportsMultipleResultSets) resultIterator | ||
else { | ||
Iterable { resultIterator }.toList().iterator() | ||
} | ||
} | ||
|
||
protected inner class ResultIterator(val rs: ResultSet) : Iterator<ResultRow> { | ||
private var hasNext: Boolean? = null | ||
|
||
private val fields = set.realFields | ||
|
||
override operator fun next(): ResultRow { | ||
if (hasNext == null) hasNext() | ||
if (hasNext == false) throw NoSuchElementException() | ||
hasNext = null | ||
return ResultRow.create(rs, fields) | ||
} | ||
|
||
override fun hasNext(): Boolean { | ||
if (hasNext == null) hasNext = rs.next() | ||
if (hasNext == false) rs.close() | ||
return hasNext!! | ||
} | ||
} | ||
} |
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
Oops, something went wrong.