Skip to content

Commit 5f35a86

Browse files
MatrixDevmatrixdev
and
matrixdev
authored
[#14] Table names with a - leads to invalid code generation (#15)
Co-authored-by: matrixdev <[email protected]>
1 parent 232098f commit 5f35a86

File tree

5 files changed

+184
-14
lines changed

5 files changed

+184
-14
lines changed

RoomigrantCompiler/src/main/java/dev/matrix/roomigrant/compiler/Database.kt

+18-10
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,29 @@ class Database(val environment: ProcessingEnvironment, element: TypeElement) {
104104

105105
code.addStatement("")
106106

107-
for (table in scheme.tables) {
108-
val tableInfo = "tableInfo_${table.name}"
109-
val indices = "indices_${table.name}"
110107

111-
val indicesType = HashMap::class.parameterizedBy(String::class, IndexInfo::class)
112-
code.addStatement("val %L = %T()", indices, indicesType)
113-
code.addStatement("")
108+
val tableInfo = "tableInfo"
109+
val tableInfoType = TableInfo::class
110+
code.addStatement("var %L: %T", tableInfo, tableInfoType)
114111

115-
code.addStatement("val %L = %T(%L, %S, %S, %L)", tableInfo, TableInfo::class, schemeInfo, table.name, table.createSql(), indices)
116-
code.addStatement("%L.put(%S, %L)", tablesMap, table.name, tableInfo)
117-
code.addStatement("")
112+
val indices = "indices"
113+
val indicesType = HashMap::class.parameterizedBy(String::class, IndexInfo::class)
114+
code.addStatement("var %L: %T", indices, indicesType)
115+
116+
code.addStatement("")
118117

118+
for (table in scheme.tables) {
119+
code.addComment("Table %S", table.name)
120+
121+
code.addStatement("%L = %T()", indices, indicesType)
119122
for (index in table.indices) {
120-
code.addStatement("%L.put(%S, %T(%L, %S, %S))", indices, index.name, IndexInfo::class, tableInfo, index.name, index.createSql(table.name))
123+
code.addStatement("%L[%S] = %T(%L, %S, %S)", indices, index.name, IndexInfo::class, tableInfo, index.name, index.createSql(table.name))
121124
}
125+
126+
code.addStatement("%L = %T(%L, %S, %S, %L)", tableInfo, tableInfoType, schemeInfo, table.name, table.createSql(), indices)
127+
code.addStatement("%L[%S] = %L", tablesMap, table.name, tableInfo)
128+
129+
code.addStatement("")
122130
}
123131

124132
code.addStatement("return %L", schemeInfo)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 9,
5+
"identityHash": "8017954132dd47795507179aea33ae39",
6+
"entities": [
7+
{
8+
"tableName": "Object1Dbo",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `intValRenamed` INTEGER NOT NULL, `stringValRenamed` TEXT NOT NULL, `nullIntVal` INTEGER, `nullStringVal` TEXT NOT NULL, PRIMARY KEY(`id`))",
10+
"fields": [
11+
{
12+
"fieldPath": "id",
13+
"columnName": "id",
14+
"affinity": "TEXT",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "intValRenamed",
19+
"columnName": "intValRenamed",
20+
"affinity": "INTEGER",
21+
"notNull": true
22+
},
23+
{
24+
"fieldPath": "stringValRenamed",
25+
"columnName": "stringValRenamed",
26+
"affinity": "TEXT",
27+
"notNull": true
28+
},
29+
{
30+
"fieldPath": "nullIntVal",
31+
"columnName": "nullIntVal",
32+
"affinity": "INTEGER",
33+
"notNull": false
34+
},
35+
{
36+
"fieldPath": "nullStringVal",
37+
"columnName": "nullStringVal",
38+
"affinity": "TEXT",
39+
"notNull": true
40+
}
41+
],
42+
"primaryKey": {
43+
"columnNames": [
44+
"id"
45+
],
46+
"autoGenerate": false
47+
},
48+
"indices": [],
49+
"foreignKeys": []
50+
},
51+
{
52+
"tableName": "Object2Dbo",
53+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, PRIMARY KEY(`id`))",
54+
"fields": [
55+
{
56+
"fieldPath": "id",
57+
"columnName": "id",
58+
"affinity": "TEXT",
59+
"notNull": true
60+
}
61+
],
62+
"primaryKey": {
63+
"columnNames": [
64+
"id"
65+
],
66+
"autoGenerate": false
67+
},
68+
"indices": [],
69+
"foreignKeys": []
70+
},
71+
{
72+
"tableName": "special-chars",
73+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`primary-id` TEXT NOT NULL, PRIMARY KEY(`primary-id`))",
74+
"fields": [
75+
{
76+
"fieldPath": "id",
77+
"columnName": "primary-id",
78+
"affinity": "TEXT",
79+
"notNull": true
80+
}
81+
],
82+
"primaryKey": {
83+
"columnNames": [
84+
"primary-id"
85+
],
86+
"autoGenerate": false
87+
},
88+
"indices": [],
89+
"foreignKeys": []
90+
},
91+
{
92+
"tableName": "indices",
93+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, PRIMARY KEY(`id`))",
94+
"fields": [
95+
{
96+
"fieldPath": "id",
97+
"columnName": "id",
98+
"affinity": "TEXT",
99+
"notNull": true
100+
}
101+
],
102+
"primaryKey": {
103+
"columnNames": [
104+
"id"
105+
],
106+
"autoGenerate": false
107+
},
108+
"indices": [
109+
{
110+
"name": "id_index",
111+
"unique": false,
112+
"columnNames": [
113+
"id"
114+
],
115+
"createSql": "CREATE INDEX IF NOT EXISTS `id_index` ON `${TABLE_NAME}` (`id`)"
116+
}
117+
],
118+
"foreignKeys": []
119+
}
120+
],
121+
"views": [
122+
{
123+
"viewName": "ObjectDboView",
124+
"createSql": "CREATE VIEW `${VIEW_NAME}` AS SELECT item.id AS oneId, (SELECT id FROM Object2Dbo WHERE id = item.id) AS twoId FROM Object1Dbo item"
125+
}
126+
],
127+
"setupQueries": [
128+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
129+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '8017954132dd47795507179aea33ae39')"
130+
]
131+
}
132+
}

RoomigrantTest/src/main/java/dev/matrix/roomigrant/test/Database.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import dev.matrix.roomigrant.GenerateRoomMigrations
77
/**
88
* @author matrixdev
99
*
10-
* 7 - Add ObjectDboView
11-
* 8 - Add twoId to ObjectDboView
10+
* 7 - Add ObjectDboView
11+
* 8 - Add twoId to ObjectDboView
12+
* 9 - Special characters, indices
1213
*/
1314
@Database(
14-
version = 8,
15-
entities = [Object1Dbo::class, Object2Dbo::class],
15+
version = 9,
16+
entities = [Object1Dbo::class, Object2Dbo::class, SpecialCharsDbo::class, IndicesDbo::class],
1617
views = [ObjectDboView::class]
1718
)
1819
@GenerateRoomMigrations(Rules::class)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dev.matrix.roomigrant.test
2+
3+
import androidx.room.Entity
4+
import androidx.room.Index
5+
import androidx.room.PrimaryKey
6+
7+
/**
8+
* @author matrixdev
9+
*/
10+
@Entity(tableName = "indices", indices = [Index(name = "id_index", value = ["id"])])
11+
class IndicesDbo {
12+
@PrimaryKey
13+
var id = ""
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package dev.matrix.roomigrant.test
2+
3+
import androidx.room.ColumnInfo
4+
import androidx.room.Entity
5+
import androidx.room.PrimaryKey
6+
7+
/**
8+
* @author matrixdev
9+
*/
10+
@Entity(tableName = "special-chars")
11+
class SpecialCharsDbo {
12+
@PrimaryKey
13+
@ColumnInfo(name = "primary-id")
14+
var id = ""
15+
}

0 commit comments

Comments
 (0)