Skip to content

Commit 7bbf0ea

Browse files
authored
Merge pull request #108 from touchlab/robxyy-robx.20230729.emptybytes
Robxyy robx.20230729.emptybytes
2 parents 6656533 + 3b90dc7 commit 7bbf0ea

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
@@ -10,6 +10,7 @@ private const val EMPTY_STRING = ""
1010

1111
internal class ActualSqliteStatement(private val db: SqliteDatabase, private val stmtPointer: SqliteStatementPointer) :
1212
SqliteStatement {
13+
private val emptyBytes = ByteArray(0)
1314

1415
//Cursor methods
1516
override fun isNull(index: Int): Boolean =
@@ -29,8 +30,12 @@ internal class ActualSqliteStatement(private val db: SqliteDatabase, private val
2930
val blobSize = sqlite3_column_bytes(stmtPointer, columnIndex)
3031
val blob = sqlite3_column_blob(stmtPointer, columnIndex)
3132

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

3540
return blob.readBytes(blobSize)
3641
}
@@ -125,7 +130,11 @@ internal class ActualSqliteStatement(private val db: SqliteDatabase, private val
125130
}
126131

127132
override fun bindBlob(index: Int, value: ByteArray) = opResult(db) {
128-
sqlite3_bind_blob(stmtPointer, index, value.refTo(0), value.size, SQLITE_TRANSIENT)
133+
if (value.isEmpty()) {
134+
sqlite3_bind_zeroblob(stmtPointer, index, 0)
135+
} else {
136+
sqlite3_bind_blob(stmtPointer, index, value.refTo(0), value.size, SQLITE_TRANSIENT)
137+
}
129138
}
130139

131140
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)