This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathsleep.go
73 lines (59 loc) · 1.56 KB
/
sleep.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package function
import (
"context"
"fmt"
"time"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
)
// Sleep is a function that just waits for the specified number of seconds
// and returns 0.
// It can be useful to test timeouts or long queries.
type Sleep struct {
expression.UnaryExpression
}
// NewSleep creates a new Sleep expression.
func NewSleep(e sql.Expression) sql.Expression {
return &Sleep{expression.UnaryExpression{Child: e}}
}
// Eval implements the Expression interface.
func (s *Sleep) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
child, err := s.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if child == nil {
return nil, nil
}
child, err = sql.Float64.Convert(child)
if err != nil {
return nil, err
}
t := time.NewTimer(time.Duration(child.(float64)*1000) * time.Millisecond)
defer t.Stop()
select {
case <-ctx.Done():
return 0, context.Canceled
case <-t.C:
return 0, nil
}
}
// String implements the Stringer interface.
func (s *Sleep) String() string {
return fmt.Sprintf("SLEEP(%s)", s.Child)
}
// IsNullable implements the Expression interface.
func (s *Sleep) IsNullable() bool {
return false
}
// WithChildren implements the Expression interface.
func (s *Sleep) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 1)
}
return NewSleep(children[0]), nil
}
// Type implements the Expression interface.
func (s *Sleep) Type() sql.Type {
return sql.Int32
}