Skip to content

Commit 67a3dcf

Browse files
committed
Get SQL prefix pass leading comments
Use the same scanner strategy as platform to find the SQL prefix for identification that is after leading comments. Bug: 413061402 Test: SupportSQLiteStatementTest Change-Id: Icbcc3ea09296ee3f10bd38d6cc16ddbb3dc7c821
1 parent 4720f2f commit 67a3dcf

4 files changed

Lines changed: 258 additions & 17 deletions

File tree

‎room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/dao/BooksDao.kt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ interface BooksDao {
225225
)
226226
fun getBooksMultiLineQuery(bookIds: List<String>): List<Book>
227227

228+
@Query(
229+
"""
230+
--- this is a comment
231+
SELECT * FROM book WHERE
232+
bookId IN(:bookIds)
233+
order by bookId DESC
234+
"""
235+
)
236+
fun getBooksMultiLineQueryWithComment(bookIds: List<String>): List<Book>
237+
228238
@Query("SELECT * FROM book WHERE bookId = :bookId")
229239
fun getBookLiveData(bookId: String): LiveData<Book>
230240

‎room/integration-tests/kotlintestapp/src/androidTest/java/androidx/room/integration/kotlintestapp/test/BooksDaoTest.kt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,21 @@ class BooksDaoTest(useDriver: UseDriver) : TestDatabaseTest(useDriver) {
293293
assertThat(books, `is`(listOf(TestUtil.BOOK_2, TestUtil.BOOK_1)))
294294
}
295295

296+
@Test
297+
fun findBooksInMultiLineQueryWithComment() {
298+
booksDao.addPublishers(TestUtil.PUBLISHER)
299+
booksDao.addBooks(TestUtil.BOOK_1)
300+
booksDao.addBooks(TestUtil.BOOK_2)
301+
302+
val books =
303+
database
304+
.booksDao()
305+
.getBooksMultiLineQueryWithComment(
306+
arrayListOf(TestUtil.BOOK_1.bookId, TestUtil.BOOK_2.bookId)
307+
)
308+
assertThat(books, `is`(listOf(TestUtil.BOOK_2, TestUtil.BOOK_1)))
309+
}
310+
296311
@Test
297312
fun findBooksByLanguage() {
298313
booksDao.addPublishers(TestUtil.PUBLISHER)
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.sqlite.driver
18+
19+
import androidx.kruth.assertThat
20+
import androidx.sqlite.driver.SupportSQLiteStatement.Companion.getStatementPrefix
21+
import androidx.test.filters.SmallTest
22+
import kotlin.test.Test
23+
24+
@SmallTest
25+
class SupportSQLiteStatementTest {
26+
27+
@Test
28+
fun statementPrefixScanner() {
29+
// Validate trimming
30+
assertThat(getStatementPrefix("SELECT")).isEqualTo("SEL")
31+
assertThat(getStatementPrefix(" SELECT")).isEqualTo("SEL")
32+
assertThat(getStatementPrefix(" \nSELECT")).isEqualTo("SEL")
33+
assertThat(
34+
getStatementPrefix(
35+
"""
36+
SELECT
37+
"""
38+
.trimIndent()
39+
)
40+
)
41+
.isEqualTo("SEL")
42+
assertThat(getStatementPrefix(" \tSELECT")).isEqualTo("SEL")
43+
44+
// Validate short leading comments
45+
assertThat(getStatementPrefix("SE")).isNull()
46+
assertThat(getStatementPrefix("SE LECT")).isEqualTo("SE ")
47+
assertThat(getStatementPrefix("-- cmt\n SE")).isNull()
48+
assertThat(getStatementPrefix("-- cmt\n SELECT")).isEqualTo("SEL")
49+
assertThat(getStatementPrefix("-")).isNull()
50+
assertThat(getStatementPrefix("--")).isNull()
51+
assertThat(getStatementPrefix("/***")).isNull()
52+
assertThat(getStatementPrefix("/* foo */ SELECT")).isEqualTo("SEL")
53+
54+
// Validate leading line comments
55+
assertThat(
56+
getStatementPrefix(
57+
"""
58+
-- cmt
59+
SELECT
60+
"""
61+
.trimIndent()
62+
)
63+
)
64+
.isEqualTo("SEL")
65+
assertThat(
66+
getStatementPrefix(
67+
"""
68+
-- line 1
69+
-- line 2
70+
SELECT
71+
"""
72+
.trimIndent()
73+
)
74+
)
75+
.isEqualTo("SEL")
76+
assertThat(
77+
getStatementPrefix(
78+
"""
79+
-- line 1
80+
SELECT
81+
-- line 2
82+
"""
83+
.trimIndent()
84+
)
85+
)
86+
.isEqualTo("SEL")
87+
88+
// Validate leading multiline comments
89+
assertThat(
90+
getStatementPrefix(
91+
"""
92+
/* cmt */
93+
SELECT
94+
"""
95+
.trimIndent()
96+
)
97+
)
98+
.isEqualTo("SEL")
99+
assertThat(
100+
getStatementPrefix(
101+
"""
102+
/* line 1
103+
line 2 */
104+
SELECT
105+
"""
106+
.trimIndent()
107+
)
108+
)
109+
.isEqualTo("SEL")
110+
assertThat(
111+
getStatementPrefix(
112+
"""
113+
/*
114+
* line 1
115+
* line 2
116+
*/
117+
SELECT
118+
"""
119+
.trimIndent()
120+
)
121+
)
122+
.isEqualTo("SEL")
123+
assertThat(
124+
getStatementPrefix(
125+
"""
126+
/*
127+
* /* foo
128+
* * bar
129+
* // baz
130+
*/
131+
SELECT
132+
"""
133+
.trimIndent()
134+
)
135+
)
136+
.isEqualTo("SEL")
137+
assertThat(
138+
getStatementPrefix(
139+
"""
140+
/********
141+
* line 1
142+
* line 2
143+
********/
144+
SELECT
145+
"""
146+
.trimIndent()
147+
)
148+
)
149+
.isEqualTo("SEL")
150+
151+
// Validate mixed comments
152+
assertThat(
153+
getStatementPrefix(
154+
"""
155+
/* line 1 */
156+
-- line 2
157+
SELECT
158+
"""
159+
.trimIndent()
160+
)
161+
)
162+
.isEqualTo("SEL")
163+
assertThat(
164+
getStatementPrefix(
165+
"""
166+
-- line 1
167+
/* line 2 */
168+
SELECT
169+
"""
170+
.trimIndent()
171+
)
172+
)
173+
.isEqualTo("SEL")
174+
}
175+
}

‎sqlite/sqlite-framework/src/androidMain/kotlin/androidx/sqlite/driver/SupportSQLiteStatement.android.kt‎

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import android.database.Cursor.FIELD_TYPE_FLOAT
2222
import android.database.Cursor.FIELD_TYPE_INTEGER
2323
import android.database.Cursor.FIELD_TYPE_NULL
2424
import android.database.Cursor.FIELD_TYPE_STRING
25+
import androidx.annotation.VisibleForTesting
2526
import androidx.sqlite.SQLITE_DATA_BLOB
2627
import androidx.sqlite.SQLITE_DATA_FLOAT
2728
import androidx.sqlite.SQLITE_DATA_INTEGER
@@ -50,14 +51,16 @@ internal sealed class SupportSQLiteStatement(
5051

5152
public companion object {
5253
public fun create(db: SupportSQLiteDatabase, sql: String): SupportSQLiteStatement {
53-
// TODO(b/413061402): Improve categorization to handle for comments.
54-
// https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/android/database/DatabaseUtils.java;drc=61197364367c9e404c7da6900658f1b16c42d0da;l=1581
5554
val sqlString = sql.trim().uppercase()
56-
val transactionOp = getTransactionOperation(sqlString)
55+
val sqlPrefix = getStatementPrefix(sqlString)
56+
if (sqlPrefix == null) {
57+
return OtherSQLiteStatement(db, sql)
58+
}
59+
val transactionOp = getTransactionOperation(sqlPrefix, sqlString)
5760
return if (transactionOp != null) {
5861
// Special-case statement for transactions
5962
TransactionSQLiteStatement(db, sql, transactionOp)
60-
} else if (isRowStatement(sqlString)) {
63+
} else if (isRowStatement(sqlPrefix)) {
6164
// Statements that return rows (SQLITE_ROW)
6265
RowSQLiteStatement(db, sql)
6366
} else {
@@ -66,12 +69,8 @@ internal sealed class SupportSQLiteStatement(
6669
}
6770
}
6871

69-
private fun getTransactionOperation(sql: String): TransactionOperation? {
70-
val prefix = sql.trim()
71-
if (prefix.length < 3) {
72-
return null
73-
}
74-
return when (prefix.substring(0, 3)) {
72+
private fun getTransactionOperation(prefix: String, sql: String): TransactionOperation? =
73+
when (prefix) {
7574
"END",
7675
"COM" -> TransactionOperation.END
7776
"ROL" ->
@@ -91,19 +90,61 @@ internal sealed class SupportSQLiteStatement(
9190
}
9291
else -> null
9392
}
94-
}
9593

96-
private fun isRowStatement(sql: String): Boolean {
97-
val prefix = sql.trim()
98-
if (prefix.length < 3) {
99-
return false
100-
}
101-
return when (prefix.substring(0, 3).uppercase()) {
94+
private fun isRowStatement(prefix: String) =
95+
when (prefix) {
10296
"SEL",
10397
"PRA",
10498
"WIT" -> true
10599
else -> false
106100
}
101+
102+
/**
103+
* Returns the 3-character prefix of the SQL statement or null if the statement is
104+
* malformed.
105+
*/
106+
@VisibleForTesting
107+
internal fun getStatementPrefix(sql: String): String? {
108+
val index = getStatementPrefixIndex(sql)
109+
if (index < 0 || index > sql.length) {
110+
// Bad comment syntax or incomplete statement
111+
return null
112+
}
113+
return sql.substring(index, minOf(index + 3, sql.length))
114+
}
115+
116+
/**
117+
* Return the index of the first character past comments and whitespace.
118+
*
119+
* Taken from SQLiteDatabase.getSqlStatementPrefixOffset() implementation.
120+
*/
121+
private fun getStatementPrefixIndex(s: String): Int {
122+
val limit: Int = s.length - 2
123+
if (limit < 0) return -1
124+
var i = 0
125+
while (i < limit) {
126+
val c = s[i]
127+
when {
128+
c <= ' ' -> i++
129+
c == '-' -> {
130+
if (s[i + 1] != '-') return i
131+
i = s.indexOf('\n', i + 2)
132+
if (i < 0) return -1
133+
i++
134+
}
135+
c == '/' -> {
136+
if (s[i + 1] != '*') return i
137+
i++
138+
do {
139+
i = s.indexOf('*', i + 1)
140+
if (i < 0) return -1
141+
} while (i + 1 < limit && s[i + 1] != '/')
142+
i += 2
143+
}
144+
else -> return i
145+
}
146+
}
147+
return -1
107148
}
108149

109150
private enum class TransactionOperation {

0 commit comments

Comments
 (0)