Skip to content

Commit

Permalink
Fixes sequences for SQLServer #492, #1164, #1209
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Apr 20, 2021
1 parent 9f34c5c commit 69ef407
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
}

override fun createStatement(): List<String> {
val createSequence = autoIncColumn?.autoIncColumnType?.autoincSeq?.let { Sequence(it).createStatement() }.orEmpty()
val createSequence = autoIncColumn?.autoIncColumnType?.autoincSeq?.let { Sequence(it, startWith = 0, minValue = 0, maxValue = Long.MAX_VALUE).createStatement() }.orEmpty()

val addForeignKeysInAlterPart = SchemaUtils.checkCycle(this) && currentDialect !is SQLiteDialect

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ open class SQLServerBatchInsertStatement(table: Table, ignore: Boolean = false,
val values = arguments!!
val sql = if (values.isEmpty()) ""
else {
val output = table.autoIncColumn?.let { " OUTPUT inserted.${transaction.identity(it)} AS GENERATED_KEYS" }?.takeIf { shouldReturnGeneratedValues }.orEmpty()
val output = table.autoIncColumn?.takeIf { shouldReturnGeneratedValues && it.autoIncColumnType?.nextValExpression == null }?.let {
" OUTPUT inserted.${transaction.identity(it)} AS GENERATED_KEYS"
}.orEmpty()

QueryBuilder(true).apply {
values.appendTo(prefix = "$output VALUES") {
it.appendTo(prefix = "(", postfix = ")") { (col, value) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ open class InsertStatement<Key : Any>(val table: Table, val isIgnore: Boolean =
val nextValExpressionColumns = values.filterValues { it is NextVal<*> }.keys
return targets.flatMap { it.columns }.filter { column ->
when {
column.autoIncColumnType?.nextValExpression != null -> currentDialect.supportsSequenceAsGeneratedKeys
column.columnType.isAutoInc -> true
column in nextValExpressionColumns -> currentDialect.supportsSequenceAsGeneratedKeys
column.columnType is EntityIDColumnType<*> -> !currentDialect.supportsOnlyIdentifiersInGeneratedKeys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ abstract class FunctionProvider {
val autoIncColumn = table.autoIncColumn

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

val (columnsToInsert, valuesExpr) = when {
isInsertFromSelect -> columns to expr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class DDLTests : DatabaseTestsBase() {

@Test fun testBlob() {
val t = object : Table("t1") {
val id = integer("id").autoIncrement("t1_seq")
val id = integer("id").autoIncrement()
val b = blob("blob")

override val primaryKey = PrimaryKey(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.util.*

object DMLTestsData {
object Cities : Table() {
val id: Column<Int> = integer("cityId").autoIncrement("cities_seq")
val id: Column<Int> = integer("cityId").autoIncrement()
val name: Column<String> = varchar("name", 50)
override val primaryKey = PrimaryKey(id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class InsertTests : DatabaseTestsBase() {
}

object LongIdTable : Table() {
val id = long("id").autoIncrement("long_id_seq")
val id = long("id").autoIncrement()
val name = text("name")

override val primaryKey = PrimaryKey(id)
Expand Down

0 comments on commit 69ef407

Please sign in to comment.