-
Notifications
You must be signed in to change notification settings - Fork 513
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
Expose more JDBC/R2DBC statement methods for dialect authors #5098
Changes from all commits
508e287
9fd033c
52111b7
ce9fa31
da8a257
aed0efe
be9203c
e0c54a2
e775f83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,11 +182,7 @@ class JdbcPreparedStatement( | |
private val preparedStatement: PreparedStatement, | ||
) : SqlPreparedStatement { | ||
override fun bindBytes(index: Int, bytes: ByteArray?) { | ||
if (bytes == null) { | ||
preparedStatement.setNull(index + 1, Types.BLOB) | ||
} else { | ||
preparedStatement.setBytes(index + 1, bytes) | ||
} | ||
preparedStatement.setBytes(index + 1, bytes) | ||
} | ||
|
||
override fun bindBoolean(index: Int, boolean: Boolean?) { | ||
|
@@ -246,11 +242,7 @@ class JdbcPreparedStatement( | |
} | ||
|
||
fun bindBigDecimal(index: Int, decimal: BigDecimal?) { | ||
if (decimal == null) { | ||
preparedStatement.setNull(index + 1, Types.NUMERIC) | ||
} else { | ||
preparedStatement.setBigDecimal(index + 1, decimal) | ||
} | ||
preparedStatement.setBigDecimal(index + 1, decimal) | ||
} | ||
|
||
fun bindObject(index: Int, obj: Any?) { | ||
|
@@ -261,20 +253,28 @@ class JdbcPreparedStatement( | |
} | ||
} | ||
|
||
fun bindObjectOther(index: Int, obj: Any?) { | ||
fun bindObject(index: Int, obj: Any?, type: Int) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this makes this a binary-incompatible change, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we can also keep it but it's only used by the Postgres driver. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that was pre-2.0. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Binary compatibility matters less for a driver like R2DBC which is still more "experimental", but the JDBC driver is widely used (and the SQLite one is an implementation of it) so we should definitely maintain compatibility for it. We should enable the API dumper plugin! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💁 I can refactor uses of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is already part of this PR, but thanks.
In this case I agree, we can change it.
Yes, will do it in another PR. |
||
if (obj == null) { | ||
preparedStatement.setNull(index + 1, Types.OTHER) | ||
preparedStatement.setNull(index + 1, type) | ||
} else { | ||
preparedStatement.setObject(index + 1, obj, Types.OTHER) | ||
preparedStatement.setObject(index + 1, obj, type) | ||
} | ||
} | ||
|
||
override fun bindString(index: Int, string: String?) { | ||
if (string == null) { | ||
preparedStatement.setNull(index + 1, Types.VARCHAR) | ||
} else { | ||
preparedStatement.setString(index + 1, string) | ||
} | ||
preparedStatement.setString(index + 1, string) | ||
} | ||
|
||
fun bindDate(index: Int, date: java.sql.Date?) { | ||
preparedStatement.setDate(index, date) | ||
} | ||
|
||
fun bindTime(index: Int, date: java.sql.Time?) { | ||
preparedStatement.setTime(index, date) | ||
} | ||
|
||
fun bindTimestamp(index: Int, timestamp: java.sql.Timestamp?) { | ||
preparedStatement.setTimestamp(index, timestamp) | ||
} | ||
|
||
fun <R> executeQuery(mapper: (SqlCursor) -> R): R { | ||
|
@@ -312,6 +312,9 @@ class JdbcCursor(val resultSet: ResultSet) : SqlCursor { | |
override fun getDouble(index: Int): Double? = getAtIndex(index, resultSet::getDouble) | ||
fun getBigDecimal(index: Int): BigDecimal? = resultSet.getBigDecimal(index + 1) | ||
inline fun <reified T : Any> getObject(index: Int): T? = resultSet.getObject(index + 1, T::class.java) | ||
fun getDate(index: Int): java.sql.Date? = resultSet.getDate(index) | ||
fun getTime(index: Int): java.sql.Time? = resultSet.getTime(index) | ||
fun getTimestamp(index: Int): java.sql.Timestamp? = resultSet.getTimestamp(index) | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T> getArray(index: Int) = getAtIndex(index, resultSet::getArray)?.array as Array<T>? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import app.cash.sqldelight.db.SqlDriver | |
import app.cash.sqldelight.db.SqlPreparedStatement | ||
import io.r2dbc.spi.Connection | ||
import io.r2dbc.spi.Statement | ||
import java.math.BigDecimal | ||
import kotlinx.coroutines.CancellationException | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.CoroutineScope | ||
|
@@ -155,7 +156,7 @@ fun CoroutineScope.R2dbcDriver( | |
} | ||
|
||
// R2DBC uses boxed Java classes instead primitives: https://r2dbc.io/spec/1.0.0.RELEASE/spec/html/#datatypes | ||
class R2dbcPreparedStatement(private val statement: Statement) : SqlPreparedStatement { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exposed to allow casting to a driver specific Statement to access driver methods. |
||
class R2dbcPreparedStatement(val statement: Statement) : SqlPreparedStatement { | ||
override fun bindBytes(index: Int, bytes: ByteArray?) { | ||
if (bytes == null) { | ||
statement.bindNull(index, ByteArray::class.java) | ||
|
@@ -164,6 +165,22 @@ class R2dbcPreparedStatement(private val statement: Statement) : SqlPreparedStat | |
} | ||
} | ||
|
||
override fun bindBoolean(index: Int, boolean: Boolean?) { | ||
if (boolean == null) { | ||
statement.bindNull(index, Boolean::class.javaObjectType) | ||
} else { | ||
statement.bind(index, boolean) | ||
} | ||
} | ||
|
||
fun bindByte(index: Int, byte: Byte?) { | ||
if (byte == null) { | ||
statement.bindNull(index, Byte::class.javaObjectType) | ||
} else { | ||
statement.bind(index, byte) | ||
} | ||
} | ||
|
||
fun bindShort(index: Int, short: Short?) { | ||
if (short == null) { | ||
statement.bindNull(index, Short::class.javaObjectType) | ||
|
@@ -188,6 +205,14 @@ class R2dbcPreparedStatement(private val statement: Statement) : SqlPreparedStat | |
} | ||
} | ||
|
||
fun bindFloat(index: Int, float: Float?) { | ||
if (float == null) { | ||
statement.bindNull(index, Float::class.javaObjectType) | ||
} else { | ||
statement.bind(index, float) | ||
} | ||
} | ||
|
||
override fun bindDouble(index: Int, double: Double?) { | ||
if (double == null) { | ||
statement.bindNull(index, Double::class.javaObjectType) | ||
|
@@ -196,29 +221,38 @@ class R2dbcPreparedStatement(private val statement: Statement) : SqlPreparedStat | |
} | ||
} | ||
|
||
override fun bindString(index: Int, string: String?) { | ||
if (string == null) { | ||
statement.bindNull(index, String::class.java) | ||
fun bindBigDecimal(index: Int, decimal: BigDecimal?) { | ||
if (decimal == null) { | ||
statement.bindNull(index, BigDecimal::class.java) | ||
} else { | ||
statement.bind(index, string) | ||
statement.bind(index, decimal) | ||
} | ||
} | ||
|
||
override fun bindBoolean(index: Int, boolean: Boolean?) { | ||
if (boolean == null) { | ||
statement.bindNull(index, Boolean::class.javaObjectType) | ||
fun bindObject(index: Int, any: Any?, ignoredSqlType: Int = 0) { | ||
if (any == null) { | ||
statement.bindNull(index, Any::class.java) | ||
} else { | ||
statement.bind(index, boolean) | ||
statement.bind(index, any) | ||
} | ||
} | ||
|
||
fun bindObject(index: Int, any: Any?) { | ||
@JvmName("bindTypedObject") | ||
inline fun <reified T : Any> bindObject(index: Int, any: T?) { | ||
if (any == null) { | ||
statement.bindNull(index, Any::class.java) | ||
statement.bindNull(index, T::class.java) | ||
} else { | ||
statement.bind(index, any) | ||
} | ||
} | ||
|
||
override fun bindString(index: Int, string: String?) { | ||
if (string == null) { | ||
statement.bindNull(index, String::class.java) | ||
} else { | ||
statement.bind(index, string) | ||
} | ||
} | ||
} | ||
|
||
internal fun <T : Any> Publisher<T>.asIterator(): AsyncPublisherIterator<T> = | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed, the JDBC driver calls
setNull
for non primitive types itself.