-
Notifications
You must be signed in to change notification settings - Fork 497
/
TreeUtil.kt
296 lines (267 loc) · 11 KB
/
TreeUtil.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright (C) 2017 Square, Inc.
*
* 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 app.cash.sqldelight.core.lang.util
import app.cash.sqldelight.core.lang.MigrationFile
import app.cash.sqldelight.core.lang.SqlDelightFile
import app.cash.sqldelight.core.lang.SqlDelightQueriesFile
import app.cash.sqldelight.core.lang.acceptsTableInterface
import app.cash.sqldelight.core.lang.psi.ColumnTypeMixin
import app.cash.sqldelight.core.lang.psi.InsertStmtValuesMixin
import app.cash.sqldelight.dialect.api.ExposableType
import app.cash.sqldelight.dialect.api.IntermediateType
import app.cash.sqldelight.dialect.api.PrimitiveType
import app.cash.sqldelight.dialect.api.PrimitiveType.INTEGER
import app.cash.sqldelight.dialect.api.PrimitiveType.TEXT
import com.alecstrong.sql.psi.core.psi.AliasElement
import com.alecstrong.sql.psi.core.psi.SqlAnnotatedElement
import com.alecstrong.sql.psi.core.psi.SqlColumnName
import com.alecstrong.sql.psi.core.psi.SqlCreateTableStmt
import com.alecstrong.sql.psi.core.psi.SqlCreateViewStmt
import com.alecstrong.sql.psi.core.psi.SqlCreateVirtualTableStmt
import com.alecstrong.sql.psi.core.psi.SqlExpr
import com.alecstrong.sql.psi.core.psi.SqlModuleArgument
import com.alecstrong.sql.psi.core.psi.SqlPragmaName
import com.alecstrong.sql.psi.core.psi.SqlResultColumn
import com.alecstrong.sql.psi.core.psi.SqlTableName
import com.alecstrong.sql.psi.core.psi.SqlTypeName
import com.alecstrong.sql.psi.core.psi.SqlTypes
import com.alecstrong.sql.psi.core.psi.mixins.ColumnDefMixin
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.tree.IElementType
import com.intellij.psi.tree.TokenSet
import com.intellij.psi.util.PsiTreeUtil
import org.jgrapht.Graph
import org.jgrapht.graph.DefaultEdge
import org.jgrapht.graph.DirectedAcyclicGraph
import org.jgrapht.traverse.TopologicalOrderIterator
internal fun PsiElement.type(): IntermediateType = when (this) {
is ExposableType -> type()
is SqlTypeName -> sqFile().typeResolver.definitionType(this)
is AliasElement -> source().type().copy(name = name)
is ColumnDefMixin -> (columnType as ColumnTypeMixin).type()
is SqlPragmaName -> IntermediateType(TEXT)
is SqlColumnName -> {
when (val parentRule = parent) {
is ColumnDefMixin -> parentRule.type()
is SqlCreateVirtualTableStmt -> IntermediateType(TEXT, name = this.name)
else -> {
when (val resolvedReference = reference?.resolve()) {
null -> IntermediateType(PrimitiveType.NULL)
// Synthesized columns refer directly to the table
is SqlCreateTableStmt,
is SqlCreateVirtualTableStmt, -> synthesizedColumnType(this.name)
else -> {
val columnSelected = queryAvailable(this).flatMap { it.columns }
.firstOrNull { it.element == resolvedReference }
columnSelected?.nullable?.let {
resolvedReference.type().nullableIf(it)
} ?: resolvedReference.type()
}
}
}
}
}
is SqlExpr -> sqFile().typeResolver.resolvedType(this)
is SqlResultColumn -> sqFile().typeResolver.resolvedType(expr!!)
else -> throw IllegalStateException("Cannot get function type for psi type ${this.javaClass}")
}
private fun synthesizedColumnType(columnName: String): IntermediateType {
val dialectType = when (columnName) {
"docid", "rowid", "oid", "_rowid_" -> INTEGER
else -> TEXT
}
return IntermediateType(dialectType, name = columnName)
}
fun PsiDirectory.queryFiles(): Sequence<SqlDelightQueriesFile> {
return children.asSequence().flatMap {
when (it) {
is PsiDirectory -> it.queryFiles()
is SqlDelightQueriesFile -> sequenceOf(it)
else -> emptySequence()
}
}
}
fun PsiDirectory.migrationFiles(): Sequence<MigrationFile> {
return children.asSequence().flatMap {
when (it) {
is PsiDirectory -> it.migrationFiles()
is MigrationFile -> sequenceOf(it)
else -> emptySequence()
}
}
}
internal fun PsiElement.sqFile(): SqlDelightFile = containingFile as SqlDelightFile
inline fun <reified T : SqlAnnotatedElement> PsiElement.findChildrenOfType(): Collection<T> {
return PsiTreeUtil.findChildrenOfType(this, T::class.java)
}
inline fun <reified T : SqlAnnotatedElement> PsiElement.findChildOfType(): T? {
return PsiTreeUtil.findChildOfType(this, T::class.java)
}
fun PsiElement.childOfType(type: IElementType): PsiElement? {
return node.findChildByType(type)?.psi
}
fun PsiElement.childOfType(types: TokenSet): PsiElement? {
return node.findChildByType(types)?.psi
}
fun ASTNode.findChildRecursive(type: IElementType): ASTNode? {
getChildren(null).forEach {
if (it.elementType == type) return it
it.findChildByType(type)?.let { return it }
}
return null
}
inline fun <reified T : PsiElement> PsiElement.nextSiblingOfType(): T {
return PsiTreeUtil.getNextSiblingOfType(this, T::class.java)!!
}
private fun PsiElement.rangesToReplace(): List<Pair<IntRange, String>> {
return if (this is ColumnTypeMixin && javaTypeName != null) {
listOf(
Pair(
first = (typeName.node.startOffset + typeName.node.textLength) until
(javaTypeName!!.node.startOffset + javaTypeName!!.node.textLength),
second = "",
),
)
} else if (this is ColumnTypeMixin && node.getChildren(null).any { it.text == "VALUE" || it.text == "LOCK" }) {
listOf(
Pair(
first = (typeName.node.startOffset + typeName.node.textLength) until
(node.getChildren(null).single { it.text == "VALUE" || it.text == "LOCK" }.let { it.startOffset + it.textLength }),
second = "",
),
)
} else if (this is SqlModuleArgument && moduleArgumentDef?.columnDef != null && (parent as SqlCreateVirtualTableStmt).moduleName?.text?.lowercase() == "fts5") {
val columnDef = moduleArgumentDef!!.columnDef!!
// If there is a space at the end of the constraints, preserve it.
val lengthModifier = if (columnDef.columnConstraintList.isNotEmpty() && columnDef.columnConstraintList.last()?.lastChild?.prevSibling is PsiWhiteSpace) 1 else 0
listOf(
Pair(
first = (columnDef.columnName.node.startOffset + columnDef.columnName.node.textLength) until
(columnDef.columnName.node.startOffset + columnDef.node.textLength - lengthModifier),
second = "",
),
)
} else if (this is InsertStmtValuesMixin && parent?.acceptsTableInterface() == true) {
listOf(
Pair(
first = childOfType(SqlTypes.BIND_EXPR)!!.range,
second = parent!!.columns.joinToString(separator = ", ", prefix = "(", postfix = ")") { "?" },
),
)
} else {
children.flatMap { it.rangesToReplace() }
}
}
private operator fun IntRange.minus(amount: Int): IntRange {
return IntRange(start - amount, endInclusive - amount)
}
private val IntRange.length: Int
get() = endInclusive - start + 1
fun PsiElement.rawSqlText(
replacements: List<Pair<IntRange, String>> = emptyList(),
): String {
return (replacements + rangesToReplace())
.sortedBy { it.first.first }
.map { (range, replacement) -> (range - node.startOffset) to replacement }
.fold(
0 to text,
{ (totalRemoved, sqlText), (range, replacement) ->
(totalRemoved + (range.length - replacement.length)) to sqlText.replaceRange(range - totalRemoved, replacement)
},
).second
}
val PsiElement.range: IntRange
get() = node.startOffset until (node.startOffset + node.textLength)
fun Collection<SqlDelightQueriesFile>.forInitializationStatements(
allowReferenceCycles: Boolean,
body: (sqlText: String) -> Unit,
) {
val views = ArrayList<SqlCreateViewStmt>()
val tables = ArrayList<SqlCreateTableStmt>()
val creators = ArrayList<PsiElement>()
val miscellanious = ArrayList<PsiElement>()
forEach { file ->
file.sqlStatements()
.filter { (label, _) -> label.name == null }
.forEach { (_, sqlStatement) ->
when {
sqlStatement.createTableStmt != null -> tables.add(sqlStatement.createTableStmt!!)
sqlStatement.createViewStmt != null -> views.add(sqlStatement.createViewStmt!!)
sqlStatement.createTriggerStmt != null -> creators.add(sqlStatement.createTriggerStmt!!)
sqlStatement.createIndexStmt != null -> creators.add(sqlStatement.createIndexStmt!!)
else -> miscellanious.add(sqlStatement)
}
}
}
when (allowReferenceCycles) {
// If we allow cycles, don't attempt to order the table creation statements. The dialect
// is permissive.
true -> tables.forEach { body(it.rawSqlText()) }
false -> tables.buildGraph().topological().forEach { body(it.rawSqlText()) }
}
views.orderStatements(
{ it.viewName.name },
{ view: SqlCreateViewStmt -> view.compoundSelectStmt!!.findChildrenOfType<SqlTableName>() },
{ it.name },
body,
)
creators.forEach { body(it.rawSqlText()) }
miscellanious.forEach { body(it.rawSqlText()) }
}
private fun ArrayList<SqlCreateTableStmt>.buildGraph(): Graph<SqlCreateTableStmt, DefaultEdge> {
val graph = DirectedAcyclicGraph<SqlCreateTableStmt, DefaultEdge>(DefaultEdge::class.java)
val namedStatements = this.associateBy { it.tableName.name }
this.forEach { table ->
graph.addVertex(table)
table.columnDefList.forEach { column ->
column.columnConstraintList.mapNotNull { it.foreignKeyClause?.foreignTable }.forEach { fk ->
try {
val foreignTable = namedStatements[fk.name]
graph.apply {
addVertex(foreignTable)
addEdge(foreignTable, table)
}
} catch (e: IllegalArgumentException) {
error("Detected cycle between ${table.tableName.name}.${column.columnName.name} and ${fk.name}. Consider lifting the foreign key constraint out of the table definition.")
}
}
}
}
return graph
}
private fun <V, E> Graph<V, E>.topological(): Iterator<V> = TopologicalOrderIterator(this)
private fun <T : PsiElement, E : PsiElement> ArrayList<T>.orderStatements(
nameSelector: (T) -> String,
relationIdentifier: (T) -> Collection<E>,
relatedNameSelector: (E) -> String,
body: (sqlText: String) -> Unit,
) {
val statementsLeft = this.map(nameSelector).toMutableSet()
while (this.isNotEmpty()) {
this.removeAll { statement ->
val relatedStatements = relationIdentifier(statement)
if (relatedStatements.any { relatedNameSelector(it) in statementsLeft }) {
return@removeAll false
}
body(statement.rawSqlText())
statementsLeft.remove(nameSelector(statement))
return@removeAll true
}
}
}