Add origin table to ColumnInfo.
The information comes from JDBC's usage of sqlite3_column_origin_name and even though it is not used right now, it can be used to resolve ambiguous column issues.
Test: DatabaseVerifierTest
Change-Id: Id122bd619b897df69bfaea8dc0a9bad8f894e9be
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/verifier/ColumnInfo.kt b/room/room-compiler/src/main/kotlin/androidx/room/verifier/ColumnInfo.kt
index 788beaf..b7f937b 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/verifier/ColumnInfo.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/verifier/ColumnInfo.kt
@@ -21,4 +21,8 @@
/**
* Represents a column in a query response
*/
-data class ColumnInfo(val name: String, val type: SQLTypeAffinity)
+data class ColumnInfo(
+ val name: String,
+ val type: SQLTypeAffinity,
+ val originTable: String?,
+)
diff --git a/room/room-compiler/src/main/kotlin/androidx/room/verifier/jdbc_ext.kt b/room/room-compiler/src/main/kotlin/androidx/room/verifier/jdbc_ext.kt
index a5da941..bd3b351 100644
--- a/room/room-compiler/src/main/kotlin/androidx/room/verifier/jdbc_ext.kt
+++ b/room/room-compiler/src/main/kotlin/androidx/room/verifier/jdbc_ext.kt
@@ -61,5 +61,11 @@
internal fun PreparedStatement.columnInfo(): List<ColumnInfo> {
// see: http://sqlite.1065341.n5.nabble.com/Column-order-in-resultset-td23127.html
- return map { index, data -> ColumnInfo(data.getColumnName(index), tryGetAffinity(index)) }
+ return map { index, data ->
+ ColumnInfo(
+ name = data.getColumnName(index),
+ type = tryGetAffinity(index),
+ originTable = data.getTableName(index)?.ifEmpty { null }
+ )
+ }
}
diff --git a/room/room-compiler/src/test/kotlin/androidx/room/processor/DatabaseViewProcessorTest.kt b/room/room-compiler/src/test/kotlin/androidx/room/processor/DatabaseViewProcessorTest.kt
index 8e5d448..93870ff 100644
--- a/room/room-compiler/src/test/kotlin/androidx/room/processor/DatabaseViewProcessorTest.kt
+++ b/room/room-compiler/src/test/kotlin/androidx/room/processor/DatabaseViewProcessorTest.kt
@@ -90,8 +90,8 @@
val resultInfo = view.query.resultInfo!!
assertThat(resultInfo.columns).hasSize(2)
assertThat(resultInfo.columns).containsAtLeast(
- ColumnInfo("id", SQLTypeAffinity.INTEGER),
- ColumnInfo("name", SQLTypeAffinity.TEXT)
+ ColumnInfo("id", SQLTypeAffinity.INTEGER, "Team"),
+ ColumnInfo("name", SQLTypeAffinity.TEXT, "Team")
)
assertThat(view.viewName).isEqualTo("MyView")
}
diff --git a/room/room-compiler/src/test/kotlin/androidx/room/verifier/DatabaseVerifierTest.kt b/room/room-compiler/src/test/kotlin/androidx/room/verifier/DatabaseVerifierTest.kt
index 51c6f6a..0b66837 100644
--- a/room/room-compiler/src/test/kotlin/androidx/room/verifier/DatabaseVerifierTest.kt
+++ b/room/room-compiler/src/test/kotlin/androidx/room/verifier/DatabaseVerifierTest.kt
@@ -85,10 +85,10 @@
`is`(
QueryResultInfo(
listOf(
- ColumnInfo("id", SQLTypeAffinity.INTEGER),
- ColumnInfo("name", SQLTypeAffinity.TEXT),
- ColumnInfo("lastName", SQLTypeAffinity.TEXT),
- ColumnInfo("ratio", SQLTypeAffinity.REAL)
+ ColumnInfo("id", SQLTypeAffinity.INTEGER, "User"),
+ ColumnInfo("name", SQLTypeAffinity.TEXT, "User"),
+ ColumnInfo("lastName", SQLTypeAffinity.TEXT, "User"),
+ ColumnInfo("ratio", SQLTypeAffinity.REAL, "User")
)
)
)
@@ -104,8 +104,48 @@
`is`(
QueryResultInfo(
listOf(
- ColumnInfo("id", SQLTypeAffinity.INTEGER),
- ColumnInfo("lastName", SQLTypeAffinity.TEXT)
+ ColumnInfo("id", SQLTypeAffinity.INTEGER, "User"),
+ ColumnInfo("lastName", SQLTypeAffinity.TEXT, "User")
+ )
+ )
+ )
+ )
+ }
+ }
+
+ @Test
+ fun testFlattenQuery() {
+ validQueryTest("SELECT id, lastName FROM (select * from User)") {
+ assertThat(
+ it,
+ `is`(
+ QueryResultInfo(
+ listOf(
+ ColumnInfo("id", SQLTypeAffinity.INTEGER, "User"),
+ ColumnInfo("lastName", SQLTypeAffinity.TEXT, "User"),
+ )
+ )
+ )
+ )
+ }
+ }
+
+ @Test
+ fun testColumnSubquery() {
+ validQueryTest("""
+ SELECT
+ lastName,
+ (SELECT COUNT(*) FROM user AS iu WHERE iu.lastName = u.lastName) = 1 AS isUnique
+ FROM user AS u
+ GROUP BY lastName
+ """.trimIndent()) {
+ assertThat(
+ it,
+ `is`(
+ QueryResultInfo(
+ listOf(
+ ColumnInfo("lastName", SQLTypeAffinity.TEXT, "User"),
+ ColumnInfo("isUnique", SQLTypeAffinity.NULL, null),
)
)
)
@@ -121,8 +161,8 @@
`is`(
QueryResultInfo(
listOf(
- ColumnInfo("myId", SQLTypeAffinity.INTEGER),
- ColumnInfo("lastName", SQLTypeAffinity.TEXT)
+ ColumnInfo("myId", SQLTypeAffinity.INTEGER, "User"),
+ ColumnInfo("lastName", SQLTypeAffinity.TEXT, "User")
)
)
)
@@ -139,7 +179,7 @@
QueryResultInfo(
listOf(
// unfortunately, we don't get this information
- ColumnInfo("MAX(ratio)", SQLTypeAffinity.NULL)
+ ColumnInfo("MAX(ratio)", SQLTypeAffinity.NULL, null)
)
)
)
@@ -156,7 +196,7 @@
QueryResultInfo(
listOf(
// unfortunately, we don't get this information
- ColumnInfo("mergedName", SQLTypeAffinity.NULL)
+ ColumnInfo("mergedName", SQLTypeAffinity.NULL, null)
)
)
)
@@ -172,9 +212,9 @@
`is`(
QueryResultInfo(
listOf(
+ ColumnInfo("id", SQLTypeAffinity.INTEGER, "User"),
// unfortunately, we don't get this information
- ColumnInfo("id", SQLTypeAffinity.INTEGER),
- ColumnInfo("mergedName", SQLTypeAffinity.NULL)
+ ColumnInfo("mergedName", SQLTypeAffinity.NULL, null)
)
)
)
@@ -213,9 +253,8 @@
`is`(
QueryResultInfo(
listOf(
- // unfortunately, we don't get this information
- ColumnInfo("id", SQLTypeAffinity.INTEGER),
- ColumnInfo("name", SQLTypeAffinity.TEXT)
+ ColumnInfo("id", SQLTypeAffinity.INTEGER, "User"),
+ ColumnInfo("name", SQLTypeAffinity.TEXT, "User")
)
)
)
@@ -242,8 +281,8 @@
`is`(
QueryResultInfo(
listOf(
- ColumnInfo("id", SQLTypeAffinity.INTEGER),
- ColumnInfo("name", SQLTypeAffinity.TEXT)
+ ColumnInfo("id", SQLTypeAffinity.INTEGER, "User"),
+ ColumnInfo("name", SQLTypeAffinity.TEXT, "User")
)
)
)