Skip to content

Commit 4489a10

Browse files
committed
orm provider
1 parent 30575a1 commit 4489a10

File tree

3 files changed

+127
-135
lines changed

3 files changed

+127
-135
lines changed

src/main/kotlin/com/github/oldmegit/goframeidea/goFrame/OrmCallables.kt

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,101 @@ package com.github.oldmegit.goframeidea.goFrame
33
import com.goide.inspections.core.GoCallableDescriptorSet
44
import com.goide.inspections.core.GoMethodDescriptor
55

6-
val OrmCallables = GoCallableDescriptorSet(
7-
setOf(
8-
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Where"),
9-
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Order")
6+
object OrmCallables {
7+
val conditionList = GoCallableDescriptorSet(
8+
setOf(
9+
// https://github.com/gogf/gf/blob/master/database/gdb/gdb_model_order_group.go
10+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Order"),
11+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).OrderAsc"),
12+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).OrderDesc"),
13+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).OrderRandom"),
14+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Group"),
15+
16+
// https://github.com/gogf/gf/blob/master/database/gdb/gdb_model_whereor.go
17+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOr"),
18+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrf"),
19+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrLT"),
20+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrLTE"),
21+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrGT"),
22+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrGTE"),
23+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrBetween"),
24+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrLike"),
25+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrIn"),
26+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrNull"),
27+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrNotBetween"),
28+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrNotLike"),
29+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrNot"),
30+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrNotIn"),
31+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereOrNotNull"),
32+
33+
// https://github.com/gogf/gf/blob/master/database/gdb/gdb_model_join.go
34+
// GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).LeftJoin"),
35+
// GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).RightJoin"),
36+
// GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).InnerJoin"),
37+
38+
// https://github.com/gogf/gf/blob/master/database/gdb/gdb_model_update.go
39+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Update"),
40+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).UpdateAndGetAffected"),
41+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Increment"),
42+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Decrement"),
43+
44+
// https://github.com/gogf/gf/blob/master/database/gdb/gdb_model_where.go
45+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Where"),
46+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Wheref"),
47+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WherePri"),
48+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereLT"),
49+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereLTE"),
50+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereGT"),
51+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereGTE"),
52+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereBetween"),
53+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereLike"),
54+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereIn"),
55+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereNull"),
56+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereNotBetween"),
57+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereNotLike"),
58+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereNot"),
59+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereNotIn"),
60+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).WhereNotNull"),
61+
62+
// https://github.com/gogf/gf/blob/master/database/gdb/gdb_model_select.go
63+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).All"),
64+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).One"),
65+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Array"),
66+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Value"),
67+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Count"),
68+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).CountColumn"),
69+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Min"),
70+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Max"),
71+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Avg"),
72+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Sum"),
73+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Having"),
74+
75+
// https://github.com/gogf/gf/blob/master/database/gdb/gdb_model_insert.go
76+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Data"),
77+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).OnDuplicate"),
78+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).OnDuplicateEx"),
79+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Insert"),
80+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).InsertAndGetId"),
81+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).InsertIgnore"),
82+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Replace"),
83+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Save"),
84+
85+
// https://github.com/gogf/gf/blob/master/database/gdb/gdb_model_fields.go
86+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Fields"),
87+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).FieldsPrefix"),
88+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).FieldsEx"),
89+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).FieldsExPrefix"),
90+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).FieldCount"),
91+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).FieldSum"),
92+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).FieldMin"),
93+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).FieldMax"),
94+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).FieldAvg"),
95+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).GetFieldsStr"),
96+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).GetFieldsExStr"),
97+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).HasField"),
98+
99+
// https://github.com/gogf/gf/blob/master/database/gdb/gdb_model_delete.go
100+
GoMethodDescriptor.of("(*github.com/gogf/gf/database/gdb.Model).Delete"),
101+
)
10102
)
11-
)
103+
}

src/main/kotlin/com/github/oldmegit/goframeidea/goFrame/OrmUtil.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@ import com.intellij.psi.util.PsiTreeUtil
99
import com.intellij.util.ObjectUtils
1010

1111
object OrmUtil {
12+
fun getData(psiElement: PsiElement): Map<String, String> {
13+
return try {
14+
val statement = getStatementContainDao(psiElement)
15+
val dao = getDaoByStatement(statement!!)
16+
val column = getColumnByDao(dao!!)
17+
getTableData(column as GoType)
18+
} catch (e: Exception) {
19+
hashMapOf()
20+
}
21+
}
22+
1223
// get statement contain XXXDao by given PsiElement
13-
fun getStatementContainDao(psiElement: PsiElement): GoStatement? {
24+
private fun getStatementContainDao(psiElement: PsiElement): GoStatement? {
1425
// get direct statement
1526
var statement = PsiTreeUtil.findFirstParent(psiElement) { e: PsiElement? ->
1627
e is GoStatement
@@ -26,7 +37,7 @@ object OrmUtil {
2637
}
2738

2839
// get Xxx dao by statement
29-
fun getDaoByStatement(statement: GoStatement): GoStructType? {
40+
private fun getDaoByStatement(statement: GoStatement): GoStructType? {
3041
for (callExpr in PsiTreeUtil.findChildrenOfType(statement, GoCallExpr::class.java)) {
3142
val reference = GoPsiUtil.getCallReference(callExpr)
3243
// get the declaration of fun
@@ -66,7 +77,7 @@ object OrmUtil {
6677
}
6778

6879
// get column GoType by XxxDao of type GoStructType
69-
fun getColumnByDao(structType: GoStructType): GoType? {
80+
private fun getColumnByDao(structType: GoStructType): GoType? {
7081
for (field in structType.fieldDeclarationList) {
7182
val name = field.fieldDefinitionList[0].name?.lowercase()
7283
if (name?.endsWith("columns")!!) {
@@ -77,7 +88,7 @@ object OrmUtil {
7788
}
7889

7990
// get table data by XXXColumns of type GoTypeSpec
80-
fun getTableData(columnType: GoType): Map<String, String> {
91+
private fun getTableData(columnType: GoType): Map<String, String> {
8192
val typeSpec = columnType.resolve(ResolveState.initial()) as GoTypeSpec
8293
val fields = PsiTreeUtil.findChildrenOfType(typeSpec, GoFieldDeclaration::class.java)
8394
val data: MutableMap<String, String> = hashMapOf()
@@ -125,7 +136,7 @@ object OrmUtil {
125136
return getStatementContainDao(last.resolve() as GoVarDefinition)
126137
}
127138

128-
return null
139+
return simpleStatement
129140
}
130141

131142
private fun extractTextFromComment(comment: String): String {
Lines changed: 14 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,32 @@
11
package com.github.oldmegit.goframeidea.provider
22

3-
import com.goide.psi.*
4-
import com.goide.psi.impl.GoPsiUtil
3+
import com.github.oldmegit.goframeidea.goFrame.Gf
4+
import com.github.oldmegit.goframeidea.goFrame.OrmCallables
5+
import com.github.oldmegit.goframeidea.goFrame.OrmUtil
6+
import com.goide.psi.GoCallExpr
7+
import com.intellij.codeInsight.lookup.LookupElementBuilder
58
import com.intellij.psi.PsiElement
6-
import com.intellij.psi.ResolveState
79
import com.intellij.psi.util.PsiTreeUtil
8-
import com.intellij.util.ObjectUtils
9-
1010

1111
class OrmProvider : GfProvider() {
1212
override fun addCompletionsEvent() {
13-
// println(1)
14-
15-
val call = PsiTreeUtil.findFirstParent(this.position) { e: PsiElement? ->
13+
val call = PsiTreeUtil.findFirstParent(this.position) { e: PsiElement ->
1614
e is GoCallExpr
17-
} as GoCallExpr? ?: return
18-
19-
// val descriptor = OrmCallables.find(call, false)
20-
// println(descriptor == null)
21-
getTable()
22-
}
23-
24-
private fun getBaseState(psiElement: PsiElement): GoStatement? {
25-
// get direct statement
26-
val positionStatement = PsiTreeUtil.findFirstParent(psiElement) { e: PsiElement? ->
27-
e is GoStatement
28-
} as GoStatement?
29-
30-
if (positionStatement is GoAssignmentStatement) {
31-
// if direct statement is kind of assign
32-
// for example:
33-
// db = db.Xxx
34-
for (expression in positionStatement.leftHandExprList.expressionList) {
35-
val psiElementRoot = expression.reference?.resolve()
36-
return getBaseState(psiElementRoot!!)
37-
}
38-
} else if (positionStatement is GoSimpleStatement) {
39-
// if direct statement is kind of simple
40-
// for example:
41-
// db := db.Xxx
42-
// db.Xxx or dao.Xxx
43-
val declaration = positionStatement.shortVarDeclaration
44-
// direct return if db := db.Xxx
45-
if (declaration != null) {
46-
// TODO 待优化
47-
return positionStatement
48-
}
15+
} as GoCallExpr
4916

50-
// db.Xxx
51-
val list = PsiTreeUtil.findChildrenOfType(positionStatement, GoReferenceExpression::class.java)
52-
val last = list.last()
53-
if (last.resolve() is GoVarDefinition) {
54-
return getBaseState(last.resolve() as GoVarDefinition)
55-
}
17+
OrmCallables.conditionList.find(call, false) ?: return
5618

57-
// dao.Xxx
58-
// TODO 待优化
59-
}
60-
61-
return positionStatement
62-
}
63-
64-
private fun getDaoByStatement(statement: GoStatement): GoStructType? {
65-
for (callExpr in PsiTreeUtil.findChildrenOfType(statement, GoCallExpr::class.java)) {
66-
val reference = GoPsiUtil.getCallReference(callExpr)
67-
// get the declaration of fun
68-
val declaration = ObjectUtils.tryCast(
69-
reference!!.resolve(),
70-
GoMethodDeclaration::class.java
19+
val data = OrmUtil.getData(position)
20+
for ((k, v) in data) {
21+
result.addElement(
22+
LookupElementBuilder.create(k)
23+
.withIcon(Gf.icon)
24+
.withTailText(" $v", true)
7125
)
72-
73-
// get the return type of fun's declaration
74-
var resultType = declaration!!.resultType
75-
76-
// get the type if return type is pointer
77-
if (resultType is GoPointerType) {
78-
resultType = resultType.type!!
79-
}
80-
81-
if (resultType.resolve(ResolveState.initial()) is GoTypeSpec) {
82-
if (declaration.receiver!!.type is GoPointerType) {
83-
val pointerType = declaration.receiver!!.type as GoPointerType
84-
val type = pointerType.type ?: return null
85-
if (type.text?.endsWith("Dao")!!) {
86-
val spec = type.resolve(ResolveState.initial())
87-
if (spec is GoTypeSpec) {
88-
return spec.specType.type as GoStructType
89-
}
90-
}
91-
}
92-
}
93-
}
94-
return null
95-
}
96-
97-
private fun getColumn(structType: GoStructType): GoType? {
98-
for (field in structType.fieldDeclarationList) {
99-
val name = field.fieldDefinitionList[0].name?.lowercase()
100-
if (name?.endsWith("columns")!!) {
101-
return field.type
102-
}
10326
}
104-
return null
105-
}
106-
107-
fun getTable() {
108-
val statement = getBaseState(position)
109-
110-
val dao = getDaoByStatement(statement!!)
111-
112-
val column = getColumn(dao!!)
113-
114-
println(column?.text)
115-
116-
// val goType = column as GoType
117-
// val goTypeSpec = goType.resolve(ResolveState.initial()) as GoTypeSpec
118-
// for (search in GoReferencesSearch.search(goTypeSpec)) {
119-
// val goCompositeLit = search.getElement().getParent() as GoCompositeLit
120-
// val goLiteralValue = goCompositeLit.getLiteralValue()
121-
// if (goLiteralValue != null) {
122-
// for (goElement in goLiteralValue.elementList) {
123-
// if (goElement.key != null && goElement.key!!.fieldName != null && goElement.value != null && goElement.value!!
124-
// .expression is GoStringLiteral
125-
// ) {
126-
// println(goElement.key!!.fieldName!!.identifier.text)
127-
// val goStringLiteral = goElement.getValue()?.getExpression() as GoStringLiteral
128-
// println(goStringLiteral.getDecodedText())
129-
// }
130-
// }
131-
// }
132-
// }
13327
}
13428

13529
override fun isValidFolder(): Boolean {
13630
return true
13731
}
138-
139-
fun getCallName(call: GoCallExpr): String {
140-
val callRef = GoPsiUtil.getCallReference(call)
141-
return callRef?.identifier?.text.toString()
142-
}
14332
}

0 commit comments

Comments
 (0)