Skip to content

Commit

Permalink
Fixing \timing output to work for all queries
Browse files Browse the repository at this point in the history
Fixes xo#444.
  • Loading branch information
kenshaw authored and murfffi committed Mar 19, 2024
1 parent 2d44b74 commit 2de5a14
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,21 @@ func (h *Handler) execSingle(ctx context.Context, w io.Writer, opt metacmd.Optio
f = h.query
}
// exec
return f(ctx, w, opt, prefix, sqlstr)
start := time.Now()
if err := f(ctx, w, opt, prefix, sqlstr); err != nil {
return err
}
if h.timing {
d := time.Since(start)
format := text.TimingDesc
v := []interface{}{float64(d.Microseconds()) / 1000}
if d > 1*time.Second {
format += " (%v)"
v = append(v, d.Round(1*time.Millisecond))
}
h.Print(format, v...)
}
return nil
}

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

// query executes a query against the database.
func (h *Handler) query(ctx context.Context, w io.Writer, opt metacmd.Option, typ, sqlstr string) error {
start := time.Now()
// run query
rows, err := h.DB().QueryContext(ctx, sqlstr)
if err != nil {
Expand Down Expand Up @@ -1147,16 +1160,6 @@ func (h *Handler) query(ctx context.Context, w io.Writer, opt metacmd.Option, ty
case params["format"] == "aligned":
fmt.Fprintln(w)
}
if h.timing {
d := time.Since(start)
format := text.TimingDesc
v := []interface{}{float64(d.Microseconds()) / 1000}
if d > 1*time.Second {
format += " (%v)"
v = append(v, d.Round(1*time.Millisecond))
}
h.Print(format, v...)
}
if pipe != nil {
pipe.Close()
if cmd != nil {
Expand Down

0 comments on commit 2de5a14

Please sign in to comment.