Skip to content

Commit 13fbfc3

Browse files
authored
Merge pull request #30 from qiaoyuang/main
Fix the problem about empty ByteArray
2 parents f11778b + dfc8e57 commit 13fbfc3

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

sqllin-driver/src/commonTest/kotlin/com/ctrip/sqllin/driver/CommonBasicTest.kt

+12-9
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ import kotlin.test.assertEquals
2626

2727
class CommonBasicTest(private val path: DatabasePath) {
2828

29-
private data class Book(
29+
private class Book(
3030
val name: String,
3131
val author: String,
3232
val pages: Int,
3333
val price: Double,
34+
val array: ByteArray,
3435
)
3536

3637
private val bookList = listOf(
37-
Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96),
38-
Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95),
38+
Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96, byteArrayOf()),
39+
Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95, byteArrayOf(1, 2, 3)),
3940
)
4041

4142
fun testCreateAndUpgrade() {
@@ -84,8 +85,8 @@ class CommonBasicTest(private val path: DatabasePath) {
8485
val readWriteConfig = getDefaultDBConfig(false)
8586
openDatabase(readWriteConfig) {
8687
it.withTransaction { connection ->
87-
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Da Vinci Code", "Dan Brown", 454, 16.96))
88-
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Lost Symbol", "Dan Brown", 510, 19.95))
88+
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Da Vinci Code", "Dan Brown", 454, 16.96, byteArrayOf()))
89+
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Lost Symbol", "Dan Brown", 510, 19.95, byteArrayOf(1, 2, 3)))
8990
}
9091
}
9192
val readOnlyConfig = getDefaultDBConfig(true)
@@ -98,6 +99,7 @@ class CommonBasicTest(private val path: DatabasePath) {
9899
assertEquals(book.author, cursor.getString(++columnIndex))
99100
assertEquals(book.pages, cursor.getInt(++columnIndex))
100101
assertEquals(book.price, cursor.getDouble(++columnIndex))
102+
assertEquals(book.array.size, cursor.getByteArray(++columnIndex)?.size)
101103
}
102104
}
103105
}
@@ -161,8 +163,8 @@ class CommonBasicTest(private val path: DatabasePath) {
161163
val readWriteConfig = getDefaultDBConfig(false)
162164
openDatabase(readWriteConfig) {
163165
it.withTransaction { connection ->
164-
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Da Vinci Code", "Dan Brown", 454, 16.96))
165-
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Lost Symbol", "Dan Brown", 510, 19.95))
166+
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Da Vinci Code", "Dan Brown", 454, 16.96, byteArrayOf()))
167+
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Lost Symbol", "Dan Brown", 510, 19.95, byteArrayOf(1, 2, 3)))
166168
}
167169

168170
try {
@@ -202,13 +204,14 @@ class CommonBasicTest(private val path: DatabasePath) {
202204
assertEquals(book.author, cursor.getString(++columnIndex))
203205
assertEquals(book.pages, cursor.getInt(++columnIndex))
204206
assertEquals(book.price, cursor.getDouble(++columnIndex))
207+
assertEquals(book.array.size, cursor.getByteArray(++columnIndex)?.size)
205208
}
206209
}
207210
}
208211
}
209212
it.withTransaction { connection ->
210-
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Da Vinci Code", "Dan Brown", 454, 16.96))
211-
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Lost Symbol", "Dan Brown", 510, 19.95))
213+
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Da Vinci Code", "Dan Brown", 454, 16.96, byteArrayOf()))
214+
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Lost Symbol", "Dan Brown", 510, 19.95, byteArrayOf(1, 2, 3)))
212215
}
213216
}
214217
}

sqllin-driver/src/commonTest/kotlin/com/ctrip/sqllin/driver/SQL.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ object SQL {
2525

2626
const val DATABASE_NAME = "BookStore.db"
2727

28-
const val CREATE_BOOK = "create table book (id integer primary key autoincrement, name text, author text, pages integer, price real)"
28+
const val CREATE_BOOK = "create table book (id integer primary key autoincrement, name text, author text, pages integer, price real, array blob)"
2929

3030
const val CREATE_CATEGORY = "create table Category (id integer primary key autoincrement, category_name text, category_code integer)"
3131

3232
const val ASSOCIATE = "alter table Book add column category_id integer"
3333

34-
const val INSERT_BOOK = "insert into Book (name, author, pages, price) values (?, ?, ?, ?)"
34+
const val INSERT_BOOK = "insert into Book (name, author, pages, price, array) values (?, ?, ?, ?, ?)"
3535

3636
const val QUERY_BOOK = "select * from Book"
3737

sqllin-driver/src/nativeMain/kotlin/com/ctrip/sqllin/driver/cinterop/NativeStatement.kt

+16-18
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import com.ctrip.sqllin.sqlite3.sqlite3_bind_int64
3535
import com.ctrip.sqllin.sqlite3.sqlite3_bind_null
3636
import com.ctrip.sqllin.sqlite3.sqlite3_bind_parameter_index
3737
import com.ctrip.sqllin.sqlite3.sqlite3_bind_text
38+
import com.ctrip.sqllin.sqlite3.sqlite3_bind_zeroblob
3839
import com.ctrip.sqllin.sqlite3.sqlite3_changes
3940
import com.ctrip.sqllin.sqlite3.sqlite3_clear_bindings
4041
import com.ctrip.sqllin.sqlite3.sqlite3_column_blob
@@ -86,12 +87,11 @@ internal class NativeStatement(
8687

8788
override fun columnGetBlob(columnIndex: Int): ByteArray {
8889
val blobSize = sqlite3_column_bytes(cStatementPointer, columnIndex)
89-
val blob = sqlite3_column_blob(cStatementPointer, columnIndex)
90-
91-
if (blobSize < 0 || blob == null)
92-
throw sqliteException("Byte array size/type issue col $columnIndex")
93-
94-
return blob.readBytes(blobSize)
90+
return if (blobSize == 0)
91+
byteArrayOf()
92+
else
93+
sqlite3_column_blob(cStatementPointer, columnIndex)?.readBytes(blobSize)
94+
?: throw sqliteException("Byte array size/type issue col $columnIndex")
9595
}
9696

9797
override fun columnCount(): Int = sqlite3_column_count(cStatementPointer)
@@ -106,9 +106,7 @@ internal class NativeStatement(
106106
when (val err = sqlite3_step(cStatementPointer)) {
107107
SQLITE_ROW -> return true
108108
SQLITE_DONE -> return false
109-
SQLITE_LOCKED, SQLITE_BUSY -> {
110-
usleep(1000u)
111-
}
109+
SQLITE_LOCKED, SQLITE_BUSY -> usleep(1000u)
112110
else -> throw sqliteException("sqlite3_step failed", err)
113111
}
114112
}
@@ -151,12 +149,11 @@ internal class NativeStatement(
151149
}
152150

153151
private fun executeForLastInsertedRowId(): Long {
154-
val err = executeNonQuery();
155-
return if (err == SQLITE_DONE && sqlite3_changes(database.dbPointer) > 0) {
152+
val err = executeNonQuery()
153+
return if (err == SQLITE_DONE && sqlite3_changes(database.dbPointer) > 0)
156154
sqlite3_last_insert_rowid(database.dbPointer)
157-
} else {
155+
else
158156
-1
159-
}
160157
}
161158

162159
override fun executeUpdateDelete(): Int = try {
@@ -168,11 +165,10 @@ internal class NativeStatement(
168165

169166
private fun executeForChangedRowCount(): Int {
170167
val err = executeNonQuery()
171-
return if (err == SQLITE_DONE) {
168+
return if (err == SQLITE_DONE)
172169
sqlite3_changes(database.dbPointer)
173-
} else {
170+
else
174171
-1
175-
}
176172
}
177173

178174
private fun executeNonQuery(): Int {
@@ -199,12 +195,14 @@ internal class NativeStatement(
199195
}
200196

201197
override fun bindString(index: Int, value: String) = opResult(database) {
202-
// TODO: Was using UTF 16 function previously. Do a little research.
203198
sqlite3_bind_text(cStatementPointer, index, value, -1, SQLITE_TRANSIENT)
204199
}
205200

206201
override fun bindBlob(index: Int, value: ByteArray) = opResult(database) {
207-
sqlite3_bind_blob(cStatementPointer, index, value.refTo(0), value.size, SQLITE_TRANSIENT)
202+
if (value.isEmpty())
203+
sqlite3_bind_zeroblob(cStatementPointer, index, 0)
204+
else
205+
sqlite3_bind_blob(cStatementPointer, index, value.refTo(0), value.size, SQLITE_TRANSIENT)
208206
}
209207

210208
private inline fun opResult(database: NativeDatabase, block: () -> Int) {

0 commit comments

Comments
 (0)