|
16 | 16 |
|
17 | 17 | package app.softnetwork.elastic.sql.parser |
18 | 18 |
|
19 | | -import app.softnetwork.elastic.sql.PainlessContextType.Processor |
20 | 19 | import app.softnetwork.elastic.sql._ |
21 | 20 | import app.softnetwork.elastic.sql.function.time.DateTimeFunction |
22 | 21 | import app.softnetwork.elastic.sql.function._ |
@@ -230,42 +229,24 @@ object Parser |
230 | 229 | case None => None |
231 | 230 | } |
232 | 231 |
|
233 | | - def script: PackratParser[PainlessScript] = |
234 | | - ("SCRIPT" ~ "AS") ~ start ~ (identifierWithArithmeticExpression | |
| 232 | + def scriptValue: PackratParser[PainlessScript] = identifierWithArithmeticExpression | |
235 | 233 | identifierWithTransformation | |
236 | 234 | identifierWithIntervalFunction | |
237 | | - identifierWithFunction) ~ end ^^ { case _ ~ _ ~ s ~ _ => s } |
| 235 | + identifierWithFunction |
| 236 | + |
| 237 | + def script: PackratParser[PainlessScript] = |
| 238 | + ("SCRIPT" ~ "AS") ~ start ~ scriptValue ~ end ^^ { case _ ~ _ ~ s ~ _ => s } |
238 | 239 |
|
239 | 240 | def column: PackratParser[Column] = |
240 | 241 | ident ~ extension_type ~ (script | multiFields) ~ defaultVal ~ notNull ~ comment ~ (options | success( |
241 | 242 | ListMap.empty[String, Value[_]] |
242 | 243 | )) ^^ { case name ~ dt ~ mfs ~ dv ~ nn ~ ct ~ opts => |
243 | 244 | mfs match { |
244 | 245 | case script: PainlessScript => |
245 | | - val ctx = PainlessContext(Processor) |
246 | | - val scr = script.painless(Some(ctx)) |
247 | | - val temp = s"$ctx$scr" |
248 | | - val ret = |
249 | | - temp.split(";") match { |
250 | | - case Array(single) if single.trim.startsWith("return ") => |
251 | | - val stripReturn = single.trim.stripPrefix("return ").trim |
252 | | - s"ctx.$name = $stripReturn" |
253 | | - case multiple => |
254 | | - val last = multiple.last.trim |
255 | | - val temp = multiple.dropRight(1) :+ s" ctx.$name = $last" |
256 | | - temp.mkString(";") |
257 | | - } |
258 | 246 | Column( |
259 | 247 | name, |
260 | 248 | dt, |
261 | | - Some( |
262 | | - ScriptProcessor( |
263 | | - script = script.sql, |
264 | | - column = name, |
265 | | - dataType = dt, |
266 | | - source = ret |
267 | | - ) |
268 | | - ), |
| 249 | + Some(ScriptProcessor.fromScript(name, script, Some(dt))), |
269 | 250 | Nil, |
270 | 251 | dv, |
271 | 252 | nn, |
@@ -521,27 +502,9 @@ object Parser |
521 | 502 |
|
522 | 503 | def alterColumnScript: PackratParser[AlterColumnScript] = |
523 | 504 | alterColumnIfExists ~ ident ~ "SET" ~ script ^^ { case ie ~ name ~ _ ~ ns => |
524 | | - val ctx = PainlessContext(Processor) |
525 | | - val scr = ns.painless(Some(ctx)) |
526 | | - val temp = s"$ctx$scr" |
527 | | - val ret = |
528 | | - temp.split(";") match { |
529 | | - case Array(single) if single.trim.startsWith("return ") => |
530 | | - val stripReturn = single.trim.stripPrefix("return ").trim |
531 | | - s"ctx.$name = $stripReturn" |
532 | | - case multiple => |
533 | | - val last = multiple.last.trim |
534 | | - val temp = multiple.dropRight(1) :+ s" ctx.$name = $last" |
535 | | - temp.mkString(";") |
536 | | - } |
537 | 505 | AlterColumnScript( |
538 | 506 | name, |
539 | | - ScriptProcessor( |
540 | | - script = ns.sql, |
541 | | - column = name, |
542 | | - dataType = ns.out, |
543 | | - source = ret |
544 | | - ), |
| 507 | + ScriptProcessor.fromScript(name, ns, Some(ns.out)), |
545 | 508 | ifExists = ie |
546 | 509 | ) |
547 | 510 | } |
@@ -1025,10 +988,12 @@ object Parser |
1025 | 988 |
|
1026 | 989 | /** UPDATE table SET col1 = v1, col2 = v2 [WHERE ...] */ |
1027 | 990 | def update: PackratParser[Update] = |
1028 | | - ("UPDATE" ~> ident) ~ ("SET" ~> repsep(ident ~ "=" ~ value, separator)) ~ where.? ^^ { |
1029 | | - case table ~ assigns ~ w => |
1030 | | - val values = ListMap(assigns.map { case col ~ _ ~ v => col -> v }: _*) |
1031 | | - Update(table, values, w) |
| 991 | + ("UPDATE" ~> ident) ~ ("SET" ~> repsep( |
| 992 | + ident ~ "=" ~ (value | scriptValue), |
| 993 | + separator |
| 994 | + )) ~ where.? ^^ { case table ~ assigns ~ w => |
| 995 | + val values = ListMap(assigns.map { case col ~ _ ~ v => col -> v }: _*) |
| 996 | + Update(table, values, w) |
1032 | 997 | } |
1033 | 998 |
|
1034 | 999 | /** DELETE FROM table [WHERE ...] */ |
@@ -1094,7 +1059,7 @@ trait Parser |
1094 | 1059 | val endStruct: Parser[String] = "}" |
1095 | 1060 |
|
1096 | 1061 | def objectValue: PackratParser[ObjectValue] = |
1097 | | - lparen ~> rep1sep(option, comma) <~ rparen ^^ { opts => |
| 1062 | + lparen ~> repsep(option, comma) <~ rparen ^^ { opts => |
1098 | 1063 | ObjectValue(ListMap(opts: _*)) |
1099 | 1064 | } |
1100 | 1065 |
|
|
0 commit comments