X-Git-Url: http://jaekl.net/gitweb/?p=quanweb.git;a=blobdiff_plain;f=main%2Fdb.go;h=53c0a2c37b0959cf9ca522067e1bc463ea6a1671;hp=fa34828d5504562bea5837ff03917b05f8fe49a0;hb=52b98df2f362f0a231cd54bf1c43f4b5aa4b6ff1;hpb=ca96e0b6276f6efe56b102ad8286a2534d6e264b diff --git a/main/db.go b/main/db.go index fa34828..53c0a2c 100644 --- a/main/db.go +++ b/main/db.go @@ -4,6 +4,7 @@ import ( "database/sql" "fmt" _ "github.com/lib/pq" + "strings" "strconv" "sync" ) @@ -161,12 +162,31 @@ func queryBooksByIds(ids []int) []Book { return res } -func queryIds(criteria []SearchTerm) []int { - fmt.Println("queryIds():", criteria) +func queryBookPathById(id int) (string) { + query := "SELECT b.path FROM Books b WHERE b.id=$1" + + ps, err := getDb().Prepare(query) + if nil != err { + report("Failed to Prepare query: " + query, err) + return "" + } + defer ps.Close() + + row := ps.QueryRow(id) + var path sql.NullString + err = row.Scan(&path) + if nil != err { + report(fmt.Sprintf("Failed to retrieve path for book id %v: ", id), err) + return "" + } + + return nsVal(path) +} +func queryIds(criteria []SearchTerm) []int { query := "SELECT b.id FROM Books b" + " INNER JOIN Authors a ON a.id=b.author" + - " LEFT OUTER JOIN Series s ON s.id=b.series" + " LEFT OUTER JOIN Series s ON s.id=b.series" args := make([]interface{}, len(criteria)) @@ -176,20 +196,26 @@ func queryIds(criteria []SearchTerm) []int { } else { query += " AND " } + + text := criterion.Text + switch criterion.Attribute { case Author: - query += " a.grouping LIKE $" + strconv.Itoa(i + 1) + query += " UPPER(a.grouping) LIKE UPPER($" + strconv.Itoa(i + 1) + ")" + text = strings.Replace(text, " ", "", -1) // Remove spaces case Series: - query += " s.descr LIKE $" + strconv.Itoa(i + 1) + query += " UPPER(s.descr) LIKE UPPER($" + strconv.Itoa(i + 1) + ")" case Title: - query += " b.title LIKE $" + strconv.Itoa(i + 1) + query += " UPPER(b.title) LIKE UPPER($" + strconv.Itoa(i + 1) + ")" default: report("Error: unrecognized search field in queryIds(): " + criterion.Attribute.String(), nil) return nil } - args[i] = criterion.Text + args[i] = text } + query += " ORDER BY b.path" + res := []int{} ps, err := getDb().Prepare(query) @@ -207,7 +233,7 @@ func queryIds(criteria []SearchTerm) []int { } defer rows.Close() - for rows.Next(); rows.Next(); { + for rows.Next() { var id int rows.Scan(&id) res = append(res, id)