| 300 | } |
| 301 | |
| 302 | func (con *converter) callDuration(target *exprpb.Expr, args []*exprpb.Expr) error { |
| 303 | if len(args) != 1 { |
| 304 | return fmt.Errorf("arguments must be single") |
| 305 | } |
| 306 | arg := args[0] |
| 307 | var durationString string |
| 308 | switch arg.ExprKind.(type) { |
| 309 | case *exprpb.Expr_ConstExpr: |
| 310 | switch arg.GetConstExpr().ConstantKind.(type) { |
| 311 | case *exprpb.Constant_StringValue: |
| 312 | durationString = arg.GetConstExpr().GetStringValue() |
| 313 | default: |
| 314 | return fmt.Errorf("unsupported constant kind %t", arg.GetConstExpr().ConstantKind) |
| 315 | } |
| 316 | default: |
| 317 | return fmt.Errorf("unsupported kind %t", arg.ExprKind) |
| 318 | } |
| 319 | d, err := time.ParseDuration(durationString) |
| 320 | if err != nil { |
| 321 | return err |
| 322 | } |
| 323 | con.str.WriteString("INTERVAL ") |
| 324 | switch d { |
| 325 | case d.Round(time.Hour): |
| 326 | con.str.WriteString(strconv.FormatFloat(d.Hours(), 'f', 0, 64)) |
| 327 | con.str.WriteString(" HOUR") |
| 328 | case d.Round(time.Minute): |
| 329 | con.str.WriteString(strconv.FormatFloat(d.Minutes(), 'f', 0, 64)) |
| 330 | con.str.WriteString(" MINUTE") |
| 331 | case d.Round(time.Second): |
| 332 | con.str.WriteString(strconv.FormatFloat(d.Seconds(), 'f', 0, 64)) |
| 333 | con.str.WriteString(" SECOND") |
| 334 | case d.Round(time.Millisecond): |
| 335 | con.str.WriteString(strconv.FormatInt(d.Milliseconds(), 10)) |
| 336 | con.str.WriteString(" MILLISECOND") |
| 337 | default: |
| 338 | con.str.WriteString(strconv.FormatInt(d.Truncate(time.Microsecond).Microseconds(), 10)) |
| 339 | con.str.WriteString(" MICROSECOND") |
| 340 | } |
| 341 | return nil |
| 342 | } |
| 343 | |
| 344 | func (con *converter) callInterval(target *exprpb.Expr, args []*exprpb.Expr) error { |
| 345 | con.str.WriteString("INTERVAL ") |