Skip to content

Commit d9e1383

Browse files
committed
Add support for binding empty bytes.
1 parent a5a532b commit d9e1383

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/interop/ActualSqliteStatement.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ expect inline fun bytesToString(bv:CPointer<ByteVar>):String
88

99
internal class ActualSqliteStatement(private val db: SqliteDatabase, private val stmtPointer: SqliteStatementPointer) :
1010
SqliteStatement {
11+
private val emptyBytes = ByteArray(0)
1112

1213
//Cursor methods
1314
override fun isNull(index: Int): Boolean =
@@ -27,8 +28,12 @@ internal class ActualSqliteStatement(private val db: SqliteDatabase, private val
2728
val blobSize = sqlite3_column_bytes(stmtPointer, columnIndex)
2829
val blob = sqlite3_column_blob(stmtPointer, columnIndex)
2930

30-
if (blobSize < 0 || blob == null)
31+
if (blobSize < 0 || blob == null) {
32+
if (blobSize == 0 && !isNull(columnIndex)) {
33+
return emptyBytes
34+
}
3135
throw sqlException(db.logger, db.config, "Byte array size/type issue col $columnIndex")
36+
}
3237

3338
return blob.readBytes(blobSize)
3439
}
@@ -123,7 +128,11 @@ internal class ActualSqliteStatement(private val db: SqliteDatabase, private val
123128
}
124129

125130
override fun bindBlob(index: Int, value: ByteArray) = opResult(db) {
126-
sqlite3_bind_blob(stmtPointer, index, value.refTo(0), value.size, SQLITE_TRANSIENT)
131+
if (value.isEmpty()) {
132+
sqlite3_bind_zeroblob(stmtPointer, index, 0)
133+
} else {
134+
sqlite3_bind_blob(stmtPointer, index, value.refTo(0), value.size, SQLITE_TRANSIENT)
135+
}
127136
}
128137

129138
private inline fun opResult(db: SqliteDatabase, block: () -> Int) {

sqliter-driver/src/nativeCommonTest/kotlin/co/touchlab/sqliter/NativeStatementTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ class NativeStatementTest : BaseDatabaseTest(){
221221
val TWO_COL_WITH_BLOB = "CREATE TABLE test (num INTEGER NOT NULL, " +
222222
"blb BLOB NOT NULL)"
223223

224-
// @Test
225-
// Need to review what other drivers do here. It's not acting as expected
226-
// https://github.com/touchlab/SQLiter/issues/62
224+
@Test
227225
fun bindEmptyBlob() {
228226
basicTestDb(TWO_COL_WITH_BLOB) {
229227
it.withConnection {

0 commit comments

Comments
 (0)