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

Update version to 1.3.1 #85

Merged
merged 5 commits into from
Apr 24, 2024
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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

- Date format: YYYY-MM-dd

## v1.3.1 / 2024-04-24

### sqllin-dsl

* Fix a crash when a data class doesn't contain any `String` element.
* Fix the [issue#81](https://github.com/ctripcorp/SQLlin/issues/81) about insert and query null values
* Fix some wrongs about generation of SQL syntax

### sqllin-driver

* **Breaking change**: Remove the deprecated API `CommonCursor#forEachRows`
* **Breaking change**: the `getInt`, `getLong`, `getFloat` and `getDouble` will throw an exception when the value is NULl in SQLite
* Add a new public API: `CommonCursor#isNull`, for check if the value is NULL in SQLite

## v1.3.0 / 2024-04-21

### All
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=1.3.0
VERSION=1.3.1
GROUP=com.ctrip.kotlin

kotlinVersion=1.9.23
Expand Down
2 changes: 1 addition & 1 deletion sqllin-driver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ databaseConnection.executeUpdateDelete(SQL.UPDATE, arrayOf(20, "Tom"))

// SELECT
val cursor: CommonCursor = databaseConnection.query(SQL.QUERY, arrayOf(20, "Tom"))
cursor.forEachRows { index -> // Index of rows
cursor.forEachRow { index -> // Index of rows
val age: Int = cursor.getInt("age")
val name: String = cursor.getString("name")
}
Expand Down
2 changes: 1 addition & 1 deletion sqllin-driver/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ databaseConnection.executeUpdateDelete(SQL.UPDATE, arrayOf(20, "Tom"))

// SELECT
val cursor: CommonCursor = databaseConnection.query(SQL.QUERY, arrayOf(20, "Tom"))
cursor.forEachRows { index -> // Index of rows
cursor.forEachRow { index -> // Index of rows
val age: Int = cursor.getInt("age")
val name: String = cursor.getString("name")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,33 @@ import android.database.Cursor

internal class AndroidCursor(private val cursor: Cursor) : CommonCursor {

override fun getInt(columnIndex: Int): Int = cursor.getInt(columnIndex)
override fun getLong(columnIndex: Int): Long = cursor.getLong(columnIndex)
override fun getFloat(columnIndex: Int): Float = cursor.getFloat(columnIndex)
override fun getDouble(columnIndex: Int): Double = cursor.getDouble(columnIndex)
override fun getInt(columnIndex: Int): Int = try {
cursor.getInt(columnIndex)
} catch (e: Exception) {
e.printStackTrace()
throw SQLiteException("The value of column $columnIndex is NULL")
}

override fun getLong(columnIndex: Int): Long = try {
cursor.getLong(columnIndex)
} catch (e: Exception) {
e.printStackTrace()
throw SQLiteException("The value of column $columnIndex is NULL")
}

override fun getFloat(columnIndex: Int): Float = try {
cursor.getFloat(columnIndex)
} catch (e: Exception) {
e.printStackTrace()
throw SQLiteException("The value of column $columnIndex is NULL")
}

override fun getDouble(columnIndex: Int): Double = try {
cursor.getDouble(columnIndex)
} catch (e: Exception) {
e.printStackTrace()
throw SQLiteException("The value of column $columnIndex is NULL")
}

override fun getString(columnIndex: Int): String? = try {
cursor.getString(columnIndex)
Expand All @@ -46,12 +69,6 @@ internal class AndroidCursor(private val cursor: Cursor) : CommonCursor {

override fun getColumnIndex(columnName: String): Int = cursor.getColumnIndexOrThrow(columnName)

@Deprecated(
message = "Please use the new API: forEachRow",
replaceWith = ReplaceWith(expression = "forEachRow"),
)
override fun forEachRows(block: (Int) -> Unit) = forEachRow(block)

override fun forEachRow(block: (Int) -> Unit) {
if (!cursor.moveToFirst()) return
var index = 0
Expand All @@ -61,5 +78,7 @@ internal class AndroidCursor(private val cursor: Cursor) : CommonCursor {

override fun next(): Boolean = cursor.moveToNext()

override fun isNull(columnIndex: Int): Boolean = cursor.isNull(columnIndex)

override fun close() = cursor.close()
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ public interface CommonCursor : AutoCloseable {

public fun getColumnIndex(columnName: String): Int

@Deprecated(
message = "Please use the new API: forEachRow",
replaceWith = ReplaceWith(expression = "forEachRow"),
)
public fun forEachRows(block: (Int) -> Unit)
public fun forEachRow(block: (Int) -> Unit)

public fun next(): Boolean

public fun isNull(columnIndex: Int): Boolean

public override fun close()
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal fun DatabaseConnection.migrateIfNeeded(
) = withTransaction {
val initialVersion = withQuery("PRAGMA user_version;") {
it.next()
it.getInt(0)
it.getInt(0) ?: 0
}
if (initialVersion == 0) {
create(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2024 Trip.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.ctrip.sqllin.driver

/**
* The exceptions about SQLite, they include the native SQLite result codes and error message
* @author Yuang Qiao
*/

public open class SQLiteException(message: String) : Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ class CommonBasicTest(private val path: DatabasePath) {

private class Book(
val name: String,
val author: String,
val author: String?,
val pages: Int,
val price: Double,
val array: ByteArray,
val array: ByteArray?,
)

private val bookList = listOf(
Book(name = "The Da Vinci Code", author = "Dan Brown", pages = 454, price = 16.96, byteArrayOf()),
Book(name = "The Lost Symbol", author = "Dan Brown", pages = 510, price = 19.95, byteArrayOf(1, 2, 3)),
Book(name = "", author = "Dan Brown", pages = 454, price = 16.96, byteArrayOf()),
Book(name = "The Lost Symbol", author = null, pages = 510, price = 19.95, null),
)

fun testCreateAndUpgrade() {
Expand Down Expand Up @@ -87,6 +89,8 @@ class CommonBasicTest(private val path: DatabasePath) {
it.withTransaction { connection ->
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Da Vinci Code", "Dan Brown", 454, 16.96, byteArrayOf()))
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Lost Symbol", "Dan Brown", 510, 19.95, byteArrayOf(1, 2, 3)))
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("", "Dan Brown", 454, 16.96, byteArrayOf()))
connection.executeInsert(SQL.INSERT_BOOK, arrayOf("The Lost Symbol", null, 510, 19.95, null))
}
}
val readOnlyConfig = getDefaultDBConfig(true)
Expand All @@ -99,7 +103,7 @@ class CommonBasicTest(private val path: DatabasePath) {
assertEquals(book.author, cursor.getString(++columnIndex))
assertEquals(book.pages, cursor.getInt(++columnIndex))
assertEquals(book.price, cursor.getDouble(++columnIndex))
assertEquals(book.array.size, cursor.getByteArray(++columnIndex)?.size)
assertEquals(book.array?.size, cursor.getByteArray(++columnIndex)?.size)
}
}
}
Expand Down Expand Up @@ -204,7 +208,7 @@ class CommonBasicTest(private val path: DatabasePath) {
assertEquals(book.author, cursor.getString(++columnIndex))
assertEquals(book.pages, cursor.getInt(++columnIndex))
assertEquals(book.price, cursor.getDouble(++columnIndex))
assertEquals(book.array.size, cursor.getByteArray(++columnIndex)?.size)
assertEquals(book.array?.size, cursor.getByteArray(++columnIndex)?.size)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,40 @@ import java.sql.ResultSet
*/
internal class JdbcCursor(private val resultSet: ResultSet) : CommonCursor {

override fun getInt(columnIndex: Int): Int = resultSet.getInt(columnIndex + 1)
override fun getInt(columnIndex: Int): Int {
val result = resultSet.getInt(columnIndex + 1)
if (resultSet.wasNull())
throw SQLiteException("The value of column $columnIndex is NULL")
return result
}

override fun getLong(columnIndex: Int): Long = resultSet.getLong(columnIndex + 1)
override fun getLong(columnIndex: Int): Long {
val result = resultSet.getLong(columnIndex + 1)
if (resultSet.wasNull())
throw SQLiteException("The value of column $columnIndex is NULL")
return result
}

override fun getFloat(columnIndex: Int): Float = resultSet.getFloat(columnIndex + 1)
override fun getFloat(columnIndex: Int): Float {
val result = resultSet.getFloat(columnIndex + 1)
if (resultSet.wasNull())
throw SQLiteException("The value of column $columnIndex is NULL")
return result
}

override fun getDouble(columnIndex: Int): Double = resultSet.getDouble(columnIndex + 1)
override fun getDouble(columnIndex: Int): Double {
val result = resultSet.getDouble(columnIndex + 1)
if (resultSet.wasNull())
throw SQLiteException("The value of column $columnIndex is NULL")
return result
}

override fun getString(columnIndex: Int): String? = resultSet.getString(columnIndex + 1)

override fun getByteArray(columnIndex: Int): ByteArray? = resultSet.getBytes(columnIndex + 1)

override fun getColumnIndex(columnName: String): Int = resultSet.findColumn(columnName) - 1

@Deprecated(
message = "Please use the new API: forEachRow",
replaceWith = ReplaceWith(expression = "forEachRow"),
)
override fun forEachRows(block: (Int) -> Unit) = forEachRow(block)

override fun forEachRow(block: (Int) -> Unit) {
var index = 0
while (next())
Expand All @@ -52,6 +66,11 @@ internal class JdbcCursor(private val resultSet: ResultSet) : CommonCursor {

override fun next(): Boolean = resultSet.next()

override fun isNull(columnIndex: Int): Boolean {
resultSet.getObject(columnIndex + 1)
return resultSet.wasNull()
}

override fun close() {
resultSet.close()
resultSet.statement.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ internal class ConcurrentStatement(
private val accessLock: Lock,
) : SQLiteStatement {

override fun isNull(columnIndex: Int): Boolean = accessLock.withLock {
delegateStatement.isNull(columnIndex)
}

override fun columnGetLong(columnIndex: Int): Long = accessLock.withLock {
delegateStatement.columnGetLong(columnIndex)
}
Expand All @@ -38,11 +42,11 @@ internal class ConcurrentStatement(
delegateStatement.columnGetDouble(columnIndex)
}

override fun columnGetString(columnIndex: Int): String = accessLock.withLock {
override fun columnGetString(columnIndex: Int): String? = accessLock.withLock {
delegateStatement.columnGetString(columnIndex)
}

override fun columnGetBlob(columnIndex: Int): ByteArray = accessLock.withLock {
override fun columnGetBlob(columnIndex: Int): ByteArray? = accessLock.withLock {
delegateStatement.columnGetBlob(columnIndex)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@ internal class NativeCursor(

override fun getInt(columnIndex: Int): Int = getLong(columnIndex).toInt()

override fun getLong(columnIndex: Int): Long = statement.columnGetLong(columnIndex)
override fun getLong(columnIndex: Int): Long {
if (isNull(columnIndex))
throw SQLiteException("The value of column $columnIndex is NULL")
return statement.columnGetLong(columnIndex)
}

override fun getFloat(columnIndex: Int): Float = getDouble(columnIndex).toFloat()

override fun getDouble(columnIndex: Int): Double = statement.columnGetDouble(columnIndex)
override fun getDouble(columnIndex: Int): Double {
if (isNull(columnIndex))
throw SQLiteException("The value of column $columnIndex is NULL")
return statement.columnGetDouble(columnIndex)
}

override fun getString(columnIndex: Int): String = statement.columnGetString(columnIndex)
override fun getString(columnIndex: Int): String? = statement.columnGetString(columnIndex)

override fun getByteArray(columnIndex: Int): ByteArray = statement.columnGetBlob(columnIndex)
override fun getByteArray(columnIndex: Int): ByteArray? = statement.columnGetBlob(columnIndex)

override fun getColumnIndex(columnName: String): Int = columnNames[columnName] ?: throw IllegalArgumentException("Col for $columnName not found")

override fun next(): Boolean = statement.step()

@Deprecated(
message = "Please use the new API: forEachRow",
replaceWith = ReplaceWith(expression = "forEachRow"),
)
override fun forEachRows(block: (Int) -> Unit) = forEachRow(block)

override fun forEachRow(block: (Int) -> Unit) {
var index = 0
while (next())
Expand All @@ -55,6 +57,8 @@ internal class NativeCursor(

override fun close() = statement.finalizeStatement()

override fun isNull(columnIndex: Int): Boolean = statement.isNull(columnIndex)

private val columnNames: Map<String, Int> by lazy {
val count = statement.columnCount()
val map = HashMap<String, Int>(count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ package com.ctrip.sqllin.driver
import com.ctrip.sqllin.driver.SQLiteResultCode.Companion.INVALID_CODE
import com.ctrip.sqllin.driver.cinterop.SQLiteErrorType

/**
* The exceptions about SQLite, they include the native SQLite result codes and error message
* @author yaqiao
*/

public open class SQLiteException(message: String) : Exception(message)

/**
* The result codes in SQLite
* @author Yuang Qiao
*/
public class SQLiteResultCode(message: String, resultCode: Int) : SQLiteException(
"$message | error code ${
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ package com.ctrip.sqllin.driver

internal interface SQLiteStatement {

fun isNull(columnIndex: Int): Boolean

fun columnGetLong(columnIndex: Int): Long

fun columnGetDouble(columnIndex: Int): Double

fun columnGetString(columnIndex: Int): String
fun columnGetString(columnIndex: Int): String?

fun columnGetBlob(columnIndex: Int): ByteArray
fun columnGetBlob(columnIndex: Int): ByteArray?

fun columnCount(): Int

Expand Down
Loading
Loading