github.com/team-ide/go-dialect@v1.9.20/dialect/mapping_test.go (about) 1 package dialect 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "reflect" 7 "strings" 8 "testing" 9 ) 10 11 func TestMappingSqlTemplate(t *testing.T) { 12 content := ` 13 { if EqualFold(indexType, 'UNIQUE') {} 14 ALTER TABLE [{ownerName}.]{tableName} ADD UNIQUE {indexName} ({columnNames}) [COMMENT {columnComment}] 15 { } else if EqualFold(indexType, 'FULLTEXT') {} 16 ALTER TABLE [{ownerName}.]{tableName} ADD FULLTEXT {indexName} ({columnNames}) [COMMENT {columnComment}] 17 { } else if indexType == '' {} 18 ALTER TABLE [{ownerName}.]{tableName} ADD INDEX {indexName} ({columnNames}) [COMMENT {columnComment}] 19 { } else {} 20 ALTER TABLE [{ownerName}.]{tableName} ADD {indexType} {indexName} ({columnNames}) [COMMENT {columnComment}] 21 { }} 22 ` 23 24 statement, err := statementParse(content) 25 if err != nil { 26 panic(err) 27 } 28 29 fmt.Println("sql-template:", statement.GetTemplate()) 30 fmt.Println("sql-statement:", statement.GetTemplate()) 31 testOut(statement, 0) 32 } 33 34 func testOut(statement Statement, tab int) { 35 if statement == nil { 36 return 37 } 38 39 cr := *statement.GetChildren() 40 *statement.GetChildren() = []Statement{} 41 bs, _ := json.Marshal(statement) 42 for i := 0; i < tab; i++ { 43 fmt.Print("\t") 44 } 45 fmt.Print(reflect.TypeOf(statement).String() + ":") 46 *statement.GetChildren() = cr 47 fmt.Println("", string(bs)) 48 switch data := statement.(type) { 49 case *IfStatement: 50 for i := 0; i < tab+1; i++ { 51 fmt.Print("\t") 52 } 53 fmt.Println("if condition:") 54 testOut(data.ConditionExpression, tab+2) 55 case *ElseIfStatement: 56 for i := 0; i < tab+1; i++ { 57 fmt.Print("\t") 58 } 59 fmt.Println("else if condition:") 60 testOut(data.ConditionExpression, tab+2) 61 } 62 if statement.GetChildren() != nil { 63 for _, node := range *statement.GetChildren() { 64 testOut(node, tab+1) 65 } 66 } 67 switch stat := statement.(type) { 68 case *IfStatement: 69 for _, one := range stat.ElseIfs { 70 testOut(one, tab) 71 } 72 if stat.Else != nil { 73 testOut(stat.Else, tab) 74 } 75 break 76 } 77 78 } 79 func TestSqlStatementParser(t *testing.T) { 80 content := ` 81 { if EqualFold(indexType, 'UNIQUE') } 82 ALTER TABLE [{ownerName}.]{tableName} ADD UNIQUE {indexName} ({columnNames}) [COMMENT {indexComment}] 83 { else if EqualFold(indexType, 'FULLTEXT') } 84 ALTER TABLE [{ownerName}.]{tableName} ADD FULLTEXT {indexName} ({columnNames}) [COMMENT {indexComment}] 85 { else if indexType == '' } 86 ALTER TABLE [{ownerName}.]{tableName} ADD INDEX {indexName} ({columnNames}) [COMMENT {indexComment}] 87 { else } 88 ALTER TABLE [{ownerName}.]{tableName} ADD {indexType} {indexName} ({columnNames}) [COMMENT {indexComment}] 89 { } 90 ` 91 92 statement, err := statementParse(content) 93 if err != nil { 94 panic(err) 95 } 96 97 fmt.Println("sql-template:", statement.GetTemplate()) 98 bs, _ := json.Marshal(statement) 99 fmt.Println("sql-statement:", string(bs)) 100 testOut(statement, 0) 101 statementContext := NewStatementContext() 102 statementContext.SetData("ownerName", "库名") 103 statementContext.SetData("tableName", "表名") 104 statementContext.SetData("indexName", "索引名称") 105 statementContext.SetData("columnNames", "字段1,字段2") 106 statementContext.SetData("indexComment", "索引注释") 107 statementContext.SetData("indexType", "uniqUe") 108 109 statementContext.AddMethod("EqualFold", StringEqualFold) 110 111 text, err := statement.Format(statementContext) 112 if err != nil { 113 panic(err) 114 } 115 fmt.Println("sql-text:", text) 116 } 117 118 func StringEqualFold(arg1 interface{}, arg2 interface{}) (equal bool, err error) { 119 str1 := GetStringValue(arg1) 120 str2 := GetStringValue(arg2) 121 equal = strings.EqualFold(str1, str2) 122 123 //fmt.Println("StringEqualFold str1:", str1, ",str2:", str2, ",equal:", equal) 124 return 125 } 126 127 func TestSplitOperator(t *testing.T) { 128 res, err := splitOperator("a+a-s/d") 129 if err != nil { 130 panic(err) 131 } 132 for _, one := range res { 133 fmt.Println(one) 134 } 135 136 } 137 138 func TestMappingSql(t *testing.T) { 139 _, err := NewDialect(TypeMysql.Name) 140 if err != nil { 141 panic(err) 142 } 143 } 144 145 func method1() string { 146 return "a" 147 } 148 149 type methodObject struct { 150 } 151 152 func (this_ *methodObject) method1() string { 153 return "a" 154 } 155 func TestMethod(t *testing.T) { 156 methodType := reflect.ValueOf(method1) 157 fmt.Println(methodType.Type().NumOut()) 158 methodValue := reflect.ValueOf(method1) 159 res := methodValue.Call([]reflect.Value{}) 160 println("call method1 result:", res) 161 162 obj := &methodObject{} 163 methodValue = reflect.ValueOf(obj.method1) 164 res = methodValue.Call([]reflect.Value{}) 165 println("call methodObject method1 result:", res) 166 }