Commit f5907089 authored by Quentin Smith's avatar Quentin Smith

storage: log issued SQL query

Change-Id: I89a2ad20d0378077fb90cae45fcc8cab33c147ec
Reviewed-on: https://go-review.googlesource.com/36952
Run-TryBot: Quentin Smith <quentin@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 4defa8fc
...@@ -13,6 +13,8 @@ import ( ...@@ -13,6 +13,8 @@ import (
) )
func (a *App) search(w http.ResponseWriter, r *http.Request) { func (a *App) search(w http.ResponseWriter, r *http.Request) {
ctx := requestContext(r)
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
...@@ -27,6 +29,8 @@ func (a *App) search(w http.ResponseWriter, r *http.Request) { ...@@ -27,6 +29,8 @@ func (a *App) search(w http.ResponseWriter, r *http.Request) {
query := a.DB.Query(q) query := a.DB.Query(q)
defer query.Close() defer query.Close()
infof(ctx, "query: %s", query.Debug())
w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.Header().Set("Content-Type", "text/plain; charset=utf-8")
bw := benchfmt.NewPrinter(w) bw := benchfmt.NewPrinter(w)
for query.Next() { for query.Next() {
...@@ -36,6 +40,7 @@ func (a *App) search(w http.ResponseWriter, r *http.Request) { ...@@ -36,6 +40,7 @@ func (a *App) search(w http.ResponseWriter, r *http.Request) {
} }
} }
if err := query.Err(); err != nil { if err := query.Err(); err != nil {
errorf(ctx, "query returned error: %v", err)
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
......
...@@ -396,6 +396,8 @@ func parseQueryPart(part string) (sql string, args []interface{}, err error) { ...@@ -396,6 +396,8 @@ func parseQueryPart(part string) (sql string, args []interface{}, err error) {
func (db *DB) Query(q string) *Query { func (db *DB) Query(q string) *Query {
qparts := query.SplitWords(q) qparts := query.SplitWords(q)
ret := &Query{q: q}
var args []interface{} var args []interface{}
query := "SELECT r.Content FROM " query := "SELECT r.Content FROM "
for i, part := range qparts { for i, part := range qparts {
...@@ -403,8 +405,9 @@ func (db *DB) Query(q string) *Query { ...@@ -403,8 +405,9 @@ func (db *DB) Query(q string) *Query {
query += " INNER JOIN " query += " INNER JOIN "
} }
partSql, partArgs, err := parseQueryPart(part) partSql, partArgs, err := parseQueryPart(part)
ret.err = err
if err != nil { if err != nil {
return &Query{err: err} return ret
} }
query += fmt.Sprintf("(%s) t%d", partSql, i) query += fmt.Sprintf("(%s) t%d", partSql, i)
args = append(args, partArgs...) args = append(args, partArgs...)
...@@ -421,11 +424,9 @@ func (db *DB) Query(q string) *Query { ...@@ -421,11 +424,9 @@ func (db *DB) Query(q string) *Query {
query += " USING (UploadID, RecordID)" query += " USING (UploadID, RecordID)"
} }
rows, err := db.sql.Query(query, args...) ret.sqlQuery, ret.sqlArgs = query, args
if err != nil { ret.rows, ret.err = db.sql.Query(query, args...)
return &Query{err: err} return ret
}
return &Query{rows: rows}
} }
// Query is the result of a query. // Query is the result of a query.
...@@ -441,11 +442,27 @@ func (db *DB) Query(q string) *Query { ...@@ -441,11 +442,27 @@ func (db *DB) Query(q string) *Query {
// ... // ...
type Query struct { type Query struct {
rows *sql.Rows rows *sql.Rows
// for Debug
q string
sqlQuery string
sqlArgs []interface{}
// from last call to Next // from last call to Next
br *benchfmt.Reader br *benchfmt.Reader
err error err error
} }
// Debug returns the human-readable state of the query.
func (q *Query) Debug() string {
ret := fmt.Sprintf("q=%q", q.q)
if q.sqlQuery != "" || len(q.sqlArgs) > 0 {
ret += fmt.Sprintf(" sql={%q %#v}", q.sqlQuery, q.sqlArgs)
}
if q.err != nil {
ret += fmt.Sprintf(" err=%v", q.err)
}
return ret
}
// Next prepares the next result for reading with the Result // Next prepares the next result for reading with the Result
// method. It returns false when there are no more results, either by // method. It returns false when there are no more results, either by
// reaching the end of the input or an error. // reaching the end of the input or an error.
......
...@@ -6,7 +6,6 @@ package db_test ...@@ -6,7 +6,6 @@ package db_test
import ( import (
"bytes" "bytes"
"context"
"fmt" "fmt"
"reflect" "reflect"
"sort" "sort"
...@@ -15,6 +14,7 @@ import ( ...@@ -15,6 +14,7 @@ import (
"testing" "testing"
"time" "time"
"golang.org/x/net/context"
"golang.org/x/perf/internal/diff" "golang.org/x/perf/internal/diff"
"golang.org/x/perf/storage/benchfmt" "golang.org/x/perf/storage/benchfmt"
. "golang.org/x/perf/storage/db" . "golang.org/x/perf/storage/db"
......
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