Skip to content

Commit

Permalink
Fixes for inserts with select and h2 #492, #1164, #1209
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Apr 20, 2021
1 parent 5b0124b commit 9f34c5c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class AutoIncColumnType(

/** Returns the name of the sequence used to generate new values for this auto-increment column. */
val autoincSeq: String?
get() = _autoincSeq ?: fallbackSeqName.takeIf { currentDialect.needsSequenceToAutoInc }
get() = _autoincSeq.takeIf { currentDialect.supportsCreateSequence } ?: fallbackSeqName.takeIf { currentDialect.needsSequenceToAutoInc }

val nextValExpression: NextVal<*>? get() = nextValValue.takeIf { autoincSeq != null }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,15 @@ fun <T : Table> T.replace(body: T.(UpdateBuilder<*>) -> Unit): ReplaceStatement<
/**
* @sample org.jetbrains.exposed.sql.tests.shared.DMLTests.testInsertSelect01
*/
fun <T : Table> T.insert(selectQuery: AbstractQuery<*>, columns: List<Column<*>> = this.columns.filterNot { it.columnType.isAutoInc }) =
InsertSelectStatement(columns, selectQuery).execute(TransactionManager.current())

fun <T : Table> T.insertIgnore(selectQuery: AbstractQuery<*>, columns: List<Column<*>> = this.columns.filterNot { it.columnType.isAutoInc }) =
InsertSelectStatement(columns, selectQuery, true).execute(TransactionManager.current())
fun <T : Table> T.insert(
selectQuery: AbstractQuery<*>,
columns: List<Column<*>> = this.columns.filter { !it.columnType.isAutoInc || it.autoIncColumnType?.nextValExpression != null }
) = InsertSelectStatement(columns, selectQuery).execute(TransactionManager.current())

fun <T : Table> T.insertIgnore(
selectQuery: AbstractQuery<*>,
columns: List<Column<*>> = this.columns.filter { !it.columnType.isAutoInc || it.autoIncColumnType?.nextValExpression != null }
) = InsertSelectStatement(columns, selectQuery, true).execute(TransactionManager.current())

/**
* @sample org.jetbrains.exposed.sql.tests.shared.DMLTests.testUpdate01
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,10 @@ abstract class FunctionProvider {
val autoIncColumn = table.autoIncColumn

val nextValExpression = autoIncColumn?.autoIncColumnType?.nextValExpression?.takeIf { autoIncColumn !in columns }
val isInsertFromSelect = expr.isNotEmpty() && !expr.startsWith("VALUES")

val (columnsToInsert, valuesExpr) = when {
isInsertFromSelect -> columns to expr
nextValExpression != null && columns.isNotEmpty() -> (columns + autoIncColumn) to expr.dropLast(1) + ", $nextValExpression)"
nextValExpression != null -> listOf(autoIncColumn) to "VALUES ($nextValExpression)"
columns.isNotEmpty() -> columns to expr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class InsertSelectTests : DatabaseTestsBase() {
@Test
fun testInsertSelect01() {
withCitiesAndUsers(exclude = listOf(TestDB.ORACLE)) { cities, users, userData ->
val nextVal = cities.id.autoIncColumnType?.nextValExpression
val substring = users.name.substring(1, 2)
cities.insert(users.slice(substring).selectAll().orderBy(users.id).limit(2))
val slice = listOfNotNull(nextVal, substring)
cities.insert(users.slice(slice).selectAll().orderBy(users.id).limit(2))

val r = cities.slice(cities.name).selectAll().orderBy(cities.id, SortOrder.DESC).limit(2).toList()
assertEquals(2, r.size)
Expand Down

0 comments on commit 9f34c5c

Please sign in to comment.