github.com/team-ide/go-dialect@v1.9.20/dialect/statement_parser_if.go (about) 1 package dialect 2 3 import ( 4 "errors" 5 "regexp" 6 "strings" 7 ) 8 9 func getIfCondition(str string, parent Statement) (condition string, expressionStatement *ExpressionStatement, err error) { 10 condition = strings.TrimSpace(str) 11 condition = strings.TrimPrefix(condition, "{") 12 condition = strings.TrimSuffix(condition, "}") 13 condition = strings.TrimSpace(condition) 14 var reg *regexp.Regexp 15 reg, err = regexp.Compile("(else\\s+)?if") 16 if reg == nil || err != nil { 17 return 18 } 19 condition = reg.ReplaceAllString(condition, "") 20 condition = strings.TrimSpace(condition) 21 22 expressionStatement, err = parseExpressionStatement(condition, parent) 23 if err != nil { 24 return 25 } 26 27 return 28 } 29 func (this_ *StatementParser) checkIfStatement() (startIndex int, endIndex int, err error) { 30 var reg *regexp.Regexp 31 var finds [][]int 32 var curStr = this_.curStr 33 // 匹配格式如: 34 // { if xxx } 35 // { if (xxx) } 36 reg, err = regexp.Compile("\\{\\s*if[\\s\\(]+.*\\}$") 37 if reg == nil || err != nil { 38 return 39 } 40 finds = reg.FindAllStringIndex(curStr, -1) 41 if len(finds) > 0 { 42 43 startIndex = finds[0][0] 44 endIndex = finds[0][1] 45 text := curStr[0:startIndex] 46 if text != "" { 47 var statements []Statement 48 statements, err = parseTextStatement(text, this_.curParent) 49 if err != nil { 50 return 51 } 52 *this_.curParent.GetChildren() = append(*this_.curParent.GetChildren(), statements...) 53 } 54 statement := &IfStatement{ 55 AbstractStatement: &AbstractStatement{ 56 Content: curStr[startIndex:endIndex], 57 Parent: this_.curParent, 58 }, 59 } 60 statement.Condition, statement.ConditionExpression, err = getIfCondition(statement.Content, statement.Parent) 61 if err != nil { 62 return 63 } 64 65 *this_.curParent.GetChildren() = append(*this_.curParent.GetChildren(), statement) 66 this_.curStatement = statement 67 this_.curParent = statement 68 this_.curIf = statement 69 this_.curElseIf = nil 70 this_.curElse = nil 71 return 72 } 73 return 74 } 75 76 func (this_ *StatementParser) checkElseIfStatement() (startIndex int, endIndex int, err error) { 77 var reg *regexp.Regexp 78 var finds [][]int 79 var curStr = this_.curStr 80 81 // 匹配格式如: 82 // { else if xxx } 83 // { else if (xxx) } 84 reg, err = regexp.Compile("\\{\\s*else\\s*if[\\s\\(]+.+\\s*\\}$") 85 if reg == nil || err != nil { 86 return 87 } 88 finds = reg.FindAllStringIndex(curStr, -1) 89 if len(finds) > 0 { 90 if this_.curIf == nil { 91 err = errors.New("sql template [" + curStr + "] parse error, not find ”if“") 92 return 93 } 94 startIndex = finds[0][0] 95 endIndex = finds[0][1] 96 text := curStr[0:startIndex] 97 if text != "" { 98 p := this_.curParent 99 if this_.curElseIf != nil { 100 p = this_.curElseIf 101 } else { 102 p = this_.curIf 103 } 104 var statements []Statement 105 statements, err = parseTextStatement(text, p) 106 if err != nil { 107 return 108 } 109 if this_.curElseIf != nil { 110 this_.curElseIf.Children = append(this_.curElseIf.Children, statements...) 111 } else { 112 this_.curIf.Children = append(this_.curIf.Children, statements...) 113 } 114 } 115 116 statement := &ElseIfStatement{ 117 AbstractStatement: &AbstractStatement{ 118 Content: curStr[startIndex:endIndex], 119 Parent: this_.curIf, 120 }, 121 If: this_.curIf, 122 Index: len(this_.curIf.ElseIfs), 123 } 124 statement.Condition, statement.ConditionExpression, err = getIfCondition(statement.Content, statement.Parent) 125 if err != nil { 126 return 127 } 128 this_.curIf.ElseIfs = append(this_.curIf.ElseIfs, statement) 129 this_.curStatement = statement 130 this_.curParent = statement 131 this_.curElseIf = statement 132 this_.curElse = nil 133 return 134 } 135 return 136 } 137 func (this_ *StatementParser) checkElseStatement() (startIndex int, endIndex int, err error) { 138 var reg *regexp.Regexp 139 var finds [][]int 140 var curStr = this_.curStr 141 // 匹配格式如: 142 // { else } 143 reg, err = regexp.Compile("\\{\\s*else\\s*\\}$") 144 if reg == nil || err != nil { 145 return 146 } 147 finds = reg.FindAllStringIndex(curStr, -1) 148 if len(finds) > 0 { 149 if this_.curIf == nil { 150 err = errors.New("sql template [" + curStr + "] parse error, not find ”if“") 151 return 152 } 153 154 startIndex = finds[0][0] 155 endIndex = finds[0][1] 156 text := curStr[0:startIndex] 157 if text != "" { 158 p := this_.curParent 159 if this_.curElseIf != nil { 160 p = this_.curElseIf 161 } else { 162 p = this_.curIf 163 } 164 var statements []Statement 165 statements, err = parseTextStatement(text, p) 166 if err != nil { 167 return 168 } 169 if this_.curElseIf != nil { 170 this_.curElseIf.Children = append(this_.curElseIf.Children, statements...) 171 } else { 172 this_.curIf.Children = append(this_.curIf.Children, statements...) 173 } 174 } 175 176 statement := &ElseStatement{ 177 AbstractStatement: &AbstractStatement{ 178 Content: curStr[startIndex:endIndex], 179 Parent: this_.curIf, 180 }, 181 If: this_.curIf, 182 } 183 184 this_.curIf.Else = statement 185 this_.curStatement = statement 186 this_.curParent = statement 187 this_.curElse = statement 188 return 189 } 190 return 191 } 192 func (this_ *StatementParser) checkIfEndStatement() (startIndex int, endIndex int, err error) { 193 var reg *regexp.Regexp 194 var finds [][]int 195 var curStr = this_.curStr 196 // 匹配格式如: 197 // { } 198 reg, err = regexp.Compile("\\{\\s*\\}$") 199 if reg == nil || err != nil { 200 return 201 } 202 finds = reg.FindAllStringIndex(curStr, -1) 203 if len(finds) > 0 { 204 if this_.curIf == nil { 205 err = errors.New("sql template [" + curStr + "] parse error, not find ”if“") 206 return 207 } 208 209 startIndex = finds[0][0] 210 endIndex = finds[0][1] 211 text := curStr[0:startIndex] 212 if text != "" { 213 p := this_.curParent 214 if this_.curElse != nil { 215 p = this_.curElse 216 } else if this_.curElseIf != nil { 217 p = this_.curElseIf 218 } else { 219 p = this_.curIf 220 } 221 var statements []Statement 222 statements, err = parseTextStatement(text, p) 223 if err != nil { 224 return 225 } 226 if this_.curElse != nil { 227 this_.curElse.Children = append(this_.curElse.Children, statements...) 228 } else if this_.curElseIf != nil { 229 this_.curElseIf.Children = append(this_.curElseIf.Children, statements...) 230 } else { 231 this_.curIf.Children = append(this_.curIf.Children, statements...) 232 } 233 } 234 235 this_.curStatement = nil 236 this_.curParent = this_.curIf.Parent 237 this_.curIf = nil 238 this_.curElseIf = nil 239 this_.curElse = nil 240 return 241 } 242 243 return 244 }