Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For update #247

Merged
merged 15 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
basic for update options for Oracle and SqlServer; formatting
  • Loading branch information
Eric Fenderbosch committed Feb 5, 2021
commit 5c5aa90ef4be3c20d6c3cefac29d8738c6689443
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,15 @@ public open class MySqlFormatter(
forUpdate == ForUpdate -> writeKeyword("for update ")
forUpdate == ForShare && version == MySql5 -> writeKeyword("lock in share mode ")
forUpdate == ForShare && version == MySql8 -> writeKeyword("for share ")
else -> throw DialectFeatureNotSupportedException("Unsupported ForUpdateOption ${forUpdate::class.java.name}.")
else -> throw DialectFeatureNotSupportedException(
"Unsupported ForUpdateOption ${forUpdate::class.java.name}."
)
}
}
}

/**
* MySql Specific ForUpdateExpressions.
* MySql Specific ForUpdateOptions.
*/
public sealed class MySqlForUpdateOption : ForUpdateOption {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.ktorm.database.DialectFeatureNotSupportedException
import org.ktorm.database.SqlDialect
import org.ktorm.expression.*
import org.ktorm.schema.IntSqlType
import org.ktorm.support.oracle.OracleForUpdateOption.ForUpdate

/**
* [SqlDialect] implementation for Oracle database.
Expand All @@ -32,6 +33,14 @@ public open class OracleDialect : SqlDialect {
}
}

/**
* Oracle Specific ForUpdateOptions.
*/
public sealed class OracleForUpdateOption : ForUpdateOption {
/** The generated SQL would be `select ... for update`. */
public object ForUpdate : OracleForUpdateOption()
}

/**
* [SqlFormatter] implementation for Oracle, formatting SQL expressions as strings with their execution arguments.
*/
Expand All @@ -51,11 +60,20 @@ public open class OracleFormatter(
return identifier.startsWith('_') || super.shouldQuote(identifier)
}

override fun writeForUpdate(forUpdate: ForUpdateOption) {
when (forUpdate) {
ForUpdate -> writeKeyword("for update ")
else -> throw DialectFeatureNotSupportedException(
"Unsupported ForUpdateOption ${forUpdate::class.java.name}."
)
}
}

override fun visitQuery(expr: QueryExpression): QueryExpression {
if (expr.offset == null && expr.limit == null) {
return super.visitQuery(expr)
}
if (expr is SelectExpression && expr.forUpdate) {
if (expr is SelectExpression && expr.forUpdate != null) {
throw DialectFeatureNotSupportedException("SELECT FOR UPDATE not supported when using offset/limit params.")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class OracleTest : BaseTest() {
val employee = database
.sequenceOf(Employees, withReferences = false)
.filter { it.id eq 1 }
.forUpdate()
.forUpdate(OracleForUpdateOption.ForUpdate)
.single()

val future = Executors.newSingleThreadExecutor().submit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public open class PostgreSqlDialect : SqlDialect {
}

/**
* Postgres Specific ForUpdateExpressions.
* Postgres Specific ForUpdateOptions.
*/
public sealed class PostgresForUpdateOption : ForUpdateOption {
/** The generated SQL would be `select ... for update skip locked`. */
Expand All @@ -58,7 +58,9 @@ public open class PostgreSqlFormatter(
SkipLocked -> writeKeyword("for update skip locked ")
NoWait -> writeKeyword("for update nowait ")
is Wait -> writeKeyword("for update wait ${forUpdate.seconds} ")
else -> throw DialectFeatureNotSupportedException("Unsupported ForUpdateOption ${forUpdate::class.java.name}.")
else -> throw DialectFeatureNotSupportedException(
"Unsupported ForUpdateOption ${forUpdate::class.java.name}."
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.ktorm.support.sqlite

import org.ktorm.database.*
import org.ktorm.expression.ArgumentExpression
import org.ktorm.expression.ForUpdateOption
import org.ktorm.expression.QueryExpression
import org.ktorm.expression.SqlFormatter
import org.ktorm.schema.IntSqlType
Expand Down Expand Up @@ -62,6 +63,9 @@ public open class SQLiteDialect : SqlDialect {
public open class SQLiteFormatter(
database: Database, beautifySql: Boolean, indentSize: Int
) : SqlFormatter(database, beautifySql, indentSize) {
override fun writeForUpdate(forUpdate: ForUpdateOption) {
throw DialectFeatureNotSupportedException("SQLite does not support SELECT ... FOR UPDATE.")
}

override fun writePagination(expr: QueryExpression) {
newLine(Indentation.SAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.ktorm.database.Database
import org.ktorm.database.DialectFeatureNotSupportedException
import org.ktorm.database.SqlDialect
import org.ktorm.expression.*
import org.ktorm.support.sqlserver.SqlServerForUpdateOption.ForUpdate

/**
* [SqlDialect] implementation for Microsoft SqlServer database.
Expand All @@ -31,6 +32,14 @@ public open class SqlServerDialect : SqlDialect {
}
}

/**
* SqlServer Specific ForUpdateOptions.
*/
public sealed class SqlServerForUpdateOption : ForUpdateOption {
/** The generated SQL would be `select ... for update`. */
public object ForUpdate : SqlServerForUpdateOption()
}

/**
* [SqlFormatter] implementation for SqlServer, formatting SQL expressions as strings with their execution arguments.
*/
Expand All @@ -45,11 +54,20 @@ public open class SqlServerFormatter(
}
}

override fun writeForUpdate(forUpdate: ForUpdateOption) {
when (forUpdate) {
ForUpdate -> writeKeyword("for update ")
else -> throw DialectFeatureNotSupportedException(
"Unsupported ForUpdateOption ${forUpdate::class.java.name}."
)
}
}

override fun visitQuery(expr: QueryExpression): QueryExpression {
if (expr.offset == null && expr.limit == null) {
return super.visitQuery(expr)
}
if (expr is SelectExpression && expr.forUpdate) {
if (expr is SelectExpression && expr.forUpdate != null) {
throw DialectFeatureNotSupportedException("SELECT FOR UPDATE not supported when using offset/limit params.")
}

Expand Down