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) { ...@@ -76,6 +76,8 @@ func (a *App) uploads(w http.ResponseWriter, r *http.Request) {
res := a.DB.ListUploads(q, r.Form["extra_label"], limit) res := a.DB.ListUploads(q, r.Form["extra_label"], limit)
defer res.Close() defer res.Close()
infof(ctx, "query: %s", res.Debug())
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
e := json.NewEncoder(w) e := json.NewEncoder(w)
for res.Next() { for res.Next() {
......
...@@ -554,6 +554,10 @@ func (db *DB) Close() error { ...@@ -554,6 +554,10 @@ func (db *DB) Close() error {
type UploadList struct { type UploadList struct {
rows *sql.Rows rows *sql.Rows
extraLabels []string extraLabels []string
// for Debug
q string
sqlQuery string
sqlArgs []interface{}
// from last call to Next // from last call to Next
count int count int
uploadID string uploadID string
...@@ -561,11 +565,25 @@ type UploadList struct { ...@@ -561,11 +565,25 @@ type UploadList struct {
err error 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. // ListUploads searches for uploads containing results matching the given query string.
// The query may be empty, in which case all uploads will be returned. // 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. // 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. // 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 { func (db *DB) ListUploads(q string, extraLabels []string, limit int) *UploadList {
ret := &UploadList{q: q, extraLabels: extraLabels}
var args []interface{} var args []interface{}
query := "SELECT j.UploadID, rCount" query := "SELECT j.UploadID, rCount"
for i, label := range extraLabels { for i, label := range extraLabels {
...@@ -575,7 +593,8 @@ func (db *DB) ListUploads(q string, extraLabels []string, limit int) *UploadList ...@@ -575,7 +593,8 @@ func (db *DB) ListUploads(q string, extraLabels []string, limit int) *UploadList
query += " FROM (SELECT UploadID, COUNT(*) as rCount FROM " query += " FROM (SELECT UploadID, COUNT(*) as rCount FROM "
sql, qArgs, err := parseQuery(q) sql, qArgs, err := parseQuery(q)
if err != nil { if err != nil {
return &UploadList{err: err} ret.err = err
return ret
} }
args = append(args, qArgs...) args = append(args, qArgs...)
for i, part := range sql { for i, part := range sql {
...@@ -600,11 +619,9 @@ func (db *DB) ListUploads(q string, extraLabels []string, limit int) *UploadList ...@@ -600,11 +619,9 @@ func (db *DB) ListUploads(q string, extraLabels []string, limit int) *UploadList
query += fmt.Sprintf(" LIMIT %d", limit) query += fmt.Sprintf(" LIMIT %d", limit)
} }
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 &UploadList{err: err} return ret
}
return &UploadList{rows: rows, extraLabels: extraLabels}
} }
// Next prepares the next result for reading with the Result // 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