Commit 6d7edb4e authored by Geofrey Ernest's avatar Geofrey Ernest Committed by GitHub

Merge pull request #175 from gernest/sleep

Add sleep built in function
parents eb22df22 47c3f086
......@@ -3682,3 +3682,43 @@ left join condition on condition.key == product_condkey;
}
}
}
func TestSleep(t *testing.T) {
db, err := OpenMem()
if err != nil {
t.Fatal(err)
}
defer db.Close()
// sleep with duration
rst, _, err := db.run(nil, "select sleep($1);", time.Second)
if err != nil {
t.Fatal(err)
}
for _, rs := range rst {
_, err = rs.FirstRow()
if err != nil {
t.Fatal(err)
}
}
// sleep with an int
rst, _, err = db.run(nil, "select sleep(5);")
if err != nil {
t.Fatal(err)
}
for _, rs := range rst {
start := time.Now()
_, err := rs.FirstRow()
if err != nil {
t.Fatal(err)
}
end := time.Now().Sub(start)
// The duration should be 5 seconds
e := end.String()
if !strings.HasPrefix(e, "5.") {
t.Errorf("expected 5s got %s", e)
}
}
}
......@@ -53,6 +53,7 @@ var builtin = map[string]struct {
"second": {builtinSecond, 1, 1, true, false},
"seconds": {builtinSeconds, 1, 1, true, false},
"since": {builtinSince, 1, 1, false, false},
"sleep": {builtinSleep, 1, 1, false, false},
"sum": {builtinSum, 1, 1, false, true},
"timeIn": {builtinTimeIn, 2, 2, true, false},
"weekday": {builtinWeekday, 1, 1, true, false},
......@@ -873,6 +874,26 @@ func builtinSince(arg []interface{}, ctx map[interface{}]interface{}) (v interfa
}
}
func builtinSleep(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
switch x := arg[0].(type) {
case nil:
return nil, nil
case time.Duration:
time.Sleep(x)
return nil, nil
case idealInt:
v := time.Second * time.Duration(int64(x))
time.Sleep(v)
return nil, nil
case int64:
v := time.Second * time.Duration(x)
time.Sleep(v)
return nil, nil
default:
return nil, invArg(x, "sleep")
}
}
func builtinSum(arg []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
if _, ok := ctx["$agg0"]; ok {
return
......
......@@ -268,7 +268,13 @@ func run(cfg *config, o *bufio.Writer, src string, db *ql.DB) (err error) {
return nil
}
src = "BEGIN TRANSACTION; " + src + "; COMMIT;"
src = strings.TrimSpace(src)
commit := "COMMIT;"
if !strings.HasSuffix(src, ";") {
commit = "; " + commit
}
src = "BEGIN TRANSACTION; " + src + commit
l, err := ql.Compile(src)
if err != nil {
log.Println(src)
......@@ -292,9 +298,16 @@ func run(cfg *config, o *bufio.Writer, src string, db *ql.DB) (err error) {
return true, nil
})
default:
return rs[len(rs)-1].Do(cfg.flds, func(data []interface{}) (bool, error) {
fmt.Fprintln(o, str(data))
return true, nil
})
for _, rst := range rs {
err = rst.Do(cfg.flds, func(data []interface{}) (bool, error) {
fmt.Fprintln(o, str(data))
return true, nil
})
o.Flush()
if err != nil {
return
}
}
return
}
}
......@@ -803,7 +803,7 @@ func (s *selectStmt) plan(ctx *execCtx) (plan, error) { //LATER overlapping goro
}
}
if r == nil {
r = &selectDummyPlan{flds: s.flds}
return &selectDummyPlan{flds: s.flds}, nil
}
if w := s.where; w != nil {
if r, err = (&whereRset{expr: w.expr, src: r, sel: w.sel, exists: w.exists}).plan(ctx); err != nil {
......
......@@ -8400,8 +8400,6 @@ SELECT * FROM t WHERE c1 == 1;
SELECT 42;
┌Selects values from dummy table
└Output field names [""]
┌Evaluate 42 as "",
└Output field names [""]
---- 1348
SELECT * FROM t WHERE EXISTS ( SELECT * FROM t WHERE i == 2 ) ORDER BY i;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment