Skip to content

Commit 9b9cda3

Browse files
committed
Fixing \timing output to work for all queries
Fixes #444.
1 parent 55b2580 commit 9b9cda3

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

handler/handler.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,21 @@ func (h *Handler) execSingle(ctx context.Context, w io.Writer, opt metacmd.Optio
10141014
f = h.query
10151015
}
10161016
// exec
1017-
return f(ctx, w, opt, prefix, sqlstr)
1017+
start := time.Now()
1018+
if err := f(ctx, w, opt, prefix, sqlstr); err != nil {
1019+
return err
1020+
}
1021+
if h.timing {
1022+
d := time.Since(start)
1023+
format := text.TimingDesc
1024+
v := []interface{}{float64(d.Microseconds()) / 1000}
1025+
if d > 1*time.Second {
1026+
format += " (%v)"
1027+
v = append(v, d.Round(1*time.Millisecond))
1028+
}
1029+
h.Print(format, v...)
1030+
}
1031+
return nil
10181032
}
10191033

10201034
// execSet executes a SQL query, setting all returned columns as variables.
@@ -1079,7 +1093,6 @@ func (h *Handler) execExec(ctx context.Context, w io.Writer, _ metacmd.Option, p
10791093

10801094
// query executes a query against the database.
10811095
func (h *Handler) query(ctx context.Context, w io.Writer, opt metacmd.Option, typ, sqlstr string) error {
1082-
start := time.Now()
10831096
// run query
10841097
rows, err := h.DB().QueryContext(ctx, sqlstr)
10851098
if err != nil {
@@ -1147,16 +1160,6 @@ func (h *Handler) query(ctx context.Context, w io.Writer, opt metacmd.Option, ty
11471160
case params["format"] == "aligned":
11481161
fmt.Fprintln(w)
11491162
}
1150-
if h.timing {
1151-
d := time.Since(start)
1152-
format := text.TimingDesc
1153-
v := []interface{}{float64(d.Microseconds()) / 1000}
1154-
if d > 1*time.Second {
1155-
format += " (%v)"
1156-
v = append(v, d.Round(1*time.Millisecond))
1157-
}
1158-
h.Print(format, v...)
1159-
}
11601163
if pipe != nil {
11611164
pipe.Close()
11621165
if cmd != nil {

0 commit comments

Comments
 (0)