Commit 74d6b5f2 authored by Quentin Smith's avatar Quentin Smith

storage: log /uploads SQL queries

Change-Id: I0aad9567cc8887922541145868fd927d8c4fb10b
Reviewed-on: https://go-review.googlesource.com/37167
Run-TryBot: Quentin Smith <quentin@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 00f4f8ab
......@@ -76,6 +76,8 @@ func (a *App) uploads(w http.ResponseWriter, r *http.Request) {
res := a.DB.ListUploads(q, r.Form["extra_label"], limit)
defer res.Close()
infof(ctx, "query: %s", res.Debug())
w.Header().Set("Content-Type", "application/json")
e := json.NewEncoder(w)
for res.Next() {
......
......@@ -554,6 +554,10 @@ func (db *DB) Close() error {
type UploadList struct {
rows *sql.Rows
extraLabels []string
// for Debug
q string
sqlQuery string
sqlArgs []interface{}
// from last call to Next
count int
uploadID string
......@@ -561,11 +565,25 @@ type UploadList struct {
err error
}
// Debug returns the human-readable state of ul.
func (ul *UploadList) Debug() string {
ret := fmt.Sprintf("q=%q", ul.q)
if ul.sqlQuery != "" || len(ul.sqlArgs) > 0 {
ret += fmt.Sprintf(" sql={%q %#v}", ul.sqlQuery, ul.sqlArgs)
}
if ul.err != nil {
ret += fmt.Sprintf(" err=%v", ul.err)
}
return ret
}
// ListUploads searches for uploads containing results matching the given query string.
// The query may be empty, in which case all uploads will be returned.
// For each label in extraLabels, one unspecified record's value will be obtained for each upload.
// If limit is non-zero, only the limit most recent uploads will be returned.
func (db *DB) ListUploads(q string, extraLabels []string, limit int) *UploadList {
ret := &UploadList{q: q, extraLabels: extraLabels}
var args []interface{}
query := "SELECT j.UploadID, rCount"
for i, label := range extraLabels {
......@@ -575,7 +593,8 @@ func (db *DB) ListUploads(q string, extraLabels []string, limit int) *UploadList
query += " FROM (SELECT UploadID, COUNT(*) as rCount FROM "
sql, qArgs, err := parseQuery(q)
if err != nil {
return &UploadList{err: err}
ret.err = err
return ret
}
args = append(args, qArgs...)
for i, part := range sql {
......@@ -600,11 +619,9 @@ func (db *DB) ListUploads(q string, extraLabels []string, limit int) *UploadList
query += fmt.Sprintf(" LIMIT %d", limit)
}
rows, err := db.sql.Query(query, args...)
if err != nil {
return &UploadList{err: err}
}
return &UploadList{rows: rows, extraLabels: extraLabels}
ret.sqlQuery, ret.sqlArgs = query, args
ret.rows, ret.err = db.sql.Query(query, args...)
return ret
}
// Next prepares the next result for reading with the Result
......
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