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

Use zero based index in PreparedStatement #3269

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -222,24 +222,24 @@ private class AndroidPreparedStatement(
private val statement: SupportSQLiteStatement
) : AndroidStatement {
override fun bindBytes(index: Int, bytes: ByteArray?) {
if (bytes == null) statement.bindNull(index) else statement.bindBlob(index, bytes)
if (bytes == null) statement.bindNull(index + 1) else statement.bindBlob(index + 1, bytes)
}

override fun bindLong(index: Int, long: Long?) {
if (long == null) statement.bindNull(index) else statement.bindLong(index, long)
if (long == null) statement.bindNull(index + 1) else statement.bindLong(index + 1, long)
}

override fun bindDouble(index: Int, double: Double?) {
if (double == null) statement.bindNull(index) else statement.bindDouble(index, double)
if (double == null) statement.bindNull(index + 1) else statement.bindDouble(index + 1, double)
}

override fun bindString(index: Int, string: String?) {
if (string == null) statement.bindNull(index) else statement.bindString(index, string)
if (string == null) statement.bindNull(index + 1) else statement.bindString(index + 1, string)
}

override fun bindBoolean(index: Int, boolean: Boolean?) {
if (boolean == null) statement.bindNull(index)
else statement.bindLong(index, if (boolean) 1L else 0L)
if (boolean == null) statement.bindNull(index + 1)
else statement.bindLong(index + 1, if (boolean) 1L else 0L)
}

override fun <R> executeQuery(mapper: (SqlCursor) -> R): R = throw UnsupportedOperationException()
Expand All @@ -258,28 +258,28 @@ private class AndroidQuery(
private val database: SupportSQLiteDatabase,
private val argCount: Int
) : SupportSQLiteQuery, AndroidStatement {
private val binds: MutableMap<Int, (SupportSQLiteProgram) -> Unit> = LinkedHashMap()
private val binds = MutableList<((SupportSQLiteProgram) -> Unit)?>(argCount) { null }

override fun bindBytes(index: Int, bytes: ByteArray?) {
binds[index] = { if (bytes == null) it.bindNull(index) else it.bindBlob(index, bytes) }
binds[index] = { if (bytes == null) it.bindNull(index + 1) else it.bindBlob(index + 1, bytes) }
}

override fun bindLong(index: Int, long: Long?) {
binds[index] = { if (long == null) it.bindNull(index) else it.bindLong(index, long) }
binds[index] = { if (long == null) it.bindNull(index + 1) else it.bindLong(index + 1, long) }
}

override fun bindDouble(index: Int, double: Double?) {
binds[index] = { if (double == null) it.bindNull(index) else it.bindDouble(index, double) }
binds[index] = { if (double == null) it.bindNull(index + 1) else it.bindDouble(index + 1, double) }
}

override fun bindString(index: Int, string: String?) {
binds[index] = { if (string == null) it.bindNull(index) else it.bindString(index, string) }
binds[index] = { if (string == null) it.bindNull(index + 1) else it.bindString(index + 1, string) }
}

override fun bindBoolean(index: Int, boolean: Boolean?) {
binds[index] = {
if (boolean == null) it.bindNull(index)
else it.bindLong(index, if (boolean) 1L else 0L)
if (boolean == null) it.bindNull(index + 1)
else it.bindLong(index + 1, if (boolean) 1L else 0L)
}
}

Expand All @@ -291,8 +291,8 @@ private class AndroidQuery(
}

override fun bindTo(statement: SupportSQLiteProgram) {
for (action in binds.values) {
action(statement)
for (action in binds) {
action!!(statement)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ abstract class DriverTest {
}

insert {
bindLong(1, 1)
bindString(2, "Alec")
bindLong(0, 1)
bindString(1, "Alec")
}

query {
Expand All @@ -112,8 +112,8 @@ abstract class DriverTest {
}

insert {
bindLong(1, 2)
bindString(2, "Jake")
bindLong(0, 2)
bindString(1, "Jake")
}
assertEquals(1, changes())

Expand All @@ -140,13 +140,13 @@ abstract class DriverTest {
}

insert {
bindLong(1, 1)
bindString(2, "Alec")
bindLong(0, 1)
bindString(1, "Alec")
}
assertEquals(1, changes())
insert {
bindLong(1, 2)
bindString(2, "Jake")
bindLong(0, 2)
bindString(1, "Jake")
}
assertEquals(1, changes())

Expand All @@ -156,7 +156,7 @@ abstract class DriverTest {

query(
binders = {
bindString(1, "Jake")
bindString(0, "Jake")
},
mapper = {
assertTrue(it.next())
Expand All @@ -168,7 +168,7 @@ abstract class DriverTest {
// Second time running the query is fine
query(
binders = {
bindString(1, "Jake")
bindString(0, "Jake")
},
mapper = {
assertTrue(it.next())
Expand All @@ -183,11 +183,11 @@ abstract class DriverTest {
driver.execute(7, "INSERT INTO nullability_test VALUES (?, ?, ?, ?, ?);", 5, binders)
}
insert {
bindLong(1, 1)
bindLong(2, null)
bindString(3, null)
bindBytes(4, null)
bindDouble(5, null)
bindLong(0, 1)
bindLong(1, null)
bindString(2, null)
bindBytes(3, null)
bindDouble(4, null)
}
assertEquals(1, changes())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ abstract class QueryTest {

private fun insertTestData(testData: TestData) {
driver.execute(1, "INSERT INTO test VALUES (?, ?)", 2) {
bindLong(1, testData.id)
bindString(2, testData.value)
bindLong(0, testData.id)
bindString(1, testData.value)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,89 +177,89 @@ open class JdbcPreparedStatement(
) : SqlPreparedStatement {
override fun bindBytes(index: Int, bytes: ByteArray?) {
if (bytes == null) {
preparedStatement.setNull(index, Types.BLOB)
preparedStatement.setNull(index + 1, Types.BLOB)
} else {
preparedStatement.setBytes(index, bytes)
preparedStatement.setBytes(index + 1, bytes)
}
}

override fun bindBoolean(index: Int, boolean: Boolean?) {
if (boolean == null) {
preparedStatement.setNull(index, Types.BOOLEAN)
preparedStatement.setNull(index + 1, Types.BOOLEAN)
} else {
preparedStatement.setBoolean(index, boolean)
preparedStatement.setBoolean(index + 1, boolean)
}
}

fun bindByte(index: Int, byte: Byte?) {
if (byte == null) {
preparedStatement.setNull(index, Types.TINYINT)
preparedStatement.setNull(index + 1, Types.TINYINT)
} else {
preparedStatement.setByte(index, byte)
preparedStatement.setByte(index + 1, byte)
}
}

fun bindShort(index: Int, short: Short?) {
if (short == null) {
preparedStatement.setNull(index, Types.SMALLINT)
preparedStatement.setNull(index + 1, Types.SMALLINT)
} else {
preparedStatement.setShort(index, short)
preparedStatement.setShort(index + 1, short)
}
}

fun bindInt(index: Int, int: Int?) {
if (int == null) {
preparedStatement.setNull(index, Types.INTEGER)
preparedStatement.setNull(index + 1, Types.INTEGER)
} else {
preparedStatement.setInt(index, int)
preparedStatement.setInt(index + 1, int)
}
}

override fun bindLong(index: Int, long: Long?) {
if (long == null) {
preparedStatement.setNull(index, Types.BIGINT)
preparedStatement.setNull(index + 1, Types.BIGINT)
} else {
preparedStatement.setLong(index, long)
preparedStatement.setLong(index + 1, long)
}
}

fun bindFloat(index: Int, float: Float?) {
if (float == null) {
preparedStatement.setNull(index, Types.REAL)
preparedStatement.setNull(index + 1, Types.REAL)
} else {
preparedStatement.setFloat(index, float)
preparedStatement.setFloat(index + 1, float)
}
}

override fun bindDouble(index: Int, double: Double?) {
if (double == null) {
preparedStatement.setNull(index, Types.DOUBLE)
preparedStatement.setNull(index + 1, Types.DOUBLE)
} else {
preparedStatement.setDouble(index, double)
preparedStatement.setDouble(index + 1, double)
}
}

fun bindBigDecimal(index: Int, decimal: BigDecimal?) {
if (decimal == null) {
preparedStatement.setNull(index, Types.NUMERIC)
preparedStatement.setNull(index + 1, Types.NUMERIC)
} else {
preparedStatement.setBigDecimal(index, decimal)
preparedStatement.setBigDecimal(index + 1, decimal)
}
}

fun bindObject(index: Int, obj: Any?) {
if (obj == null) {
preparedStatement.setNull(index, Types.OTHER)
preparedStatement.setNull(index + 1, Types.OTHER)
} else {
preparedStatement.setObject(index, obj)
preparedStatement.setObject(index + 1, obj)
}
}

override fun bindString(index: Int, string: String?) {
if (string == null) {
preparedStatement.setNull(index, Types.VARCHAR)
preparedStatement.setNull(index + 1, Types.VARCHAR)
} else {
preparedStatement.setString(index, string)
preparedStatement.setString(index + 1, string)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ internal class SqliterStatement(
private val statement: Statement
) : SqlPreparedStatement {
override fun bindBytes(index: Int, bytes: ByteArray?) {
statement.bindBlob(index, bytes)
statement.bindBlob(index + 1, bytes)
}

override fun bindLong(index: Int, long: Long?) {
statement.bindLong(index, long)
statement.bindLong(index + 1, long)
}

override fun bindDouble(index: Int, double: Double?) {
statement.bindDouble(index, double)
statement.bindDouble(index + 1, double)
}

override fun bindString(index: Int, string: String?) {
statement.bindString(index, string)
statement.bindString(index + 1, string)
}

override fun bindBoolean(index: Int, boolean: Boolean?) {
statement.bindLong(
index,
index + 1,
when (boolean) {
null -> null
true -> 1L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ abstract class BaseConcurrencyTest {

internal fun insertTestData(testData: TestData, driver: SqlDriver = this.driver) {
driver.execute(1, "INSERT INTO test VALUES (?, ?)", 2) {
bindLong(1, testData.id)
bindString(2, testData.value)
bindLong(0, testData.id)
bindString(1, testData.value)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,49 +86,49 @@ class R2dbcDriver(private val connection: Connection) : SqlDriver {
open class R2dbcPreparedStatement(private val statement: Statement) : SqlPreparedStatement {
override fun bindBytes(index: Int, bytes: ByteArray?) {
if (bytes == null) {
statement.bindNull(index - 1, ByteArray::class.java)
statement.bindNull(index, ByteArray::class.java)
} else {
statement.bind(index - 1, bytes)
statement.bind(index, bytes)
}
}

override fun bindLong(index: Int, long: Long?) {
if (long == null) {
statement.bindNull(index - 1, Long::class.java)
statement.bindNull(index, Long::class.java)
} else {
statement.bind(index - 1, long)
statement.bind(index, long)
}
}

override fun bindDouble(index: Int, double: Double?) {
if (double == null) {
statement.bindNull(index - 1, Double::class.java)
statement.bindNull(index, Double::class.java)
} else {
statement.bind(index - 1, double)
statement.bind(index, double)
}
}

override fun bindString(index: Int, string: String?) {
if (string == null) {
statement.bindNull(index - 1, String::class.java)
statement.bindNull(index, String::class.java)
} else {
statement.bind(index - 1, string)
statement.bind(index, string)
}
}

override fun bindBoolean(index: Int, boolean: Boolean?) {
if (boolean == null) {
statement.bindNull(index - 1, Boolean::class.java)
statement.bindNull(index, Boolean::class.java)
} else {
statement.bind(index - 1, boolean)
statement.bind(index, boolean)
}
}

fun bindObject(index: Int, any: Any?) {
if (any == null) {
statement.bindNull(index - 1, Any::class.java)
statement.bindNull(index, Any::class.java)
} else {
statement.bind(index - 1, any)
statement.bind(index, any)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private class InMemoryConnectionManager(
properties: Properties
) : JdbcSqliteDriverConnectionManager() {
override var transaction: Transaction? = null
private val connection = DriverManager.getConnection(IN_MEMORY, properties)
private val connection: Connection = DriverManager.getConnection(IN_MEMORY, properties)

override fun getConnection() = connection
override fun closeConnection(connection: Connection) = Unit
Expand Down
Loading