Add support to query books on specified lists (of award winners).
[quanweb.git] / main / db.go
index d903b26209d2caf501b1bdf8767b51f5e6f44fcb..53feb90908e7e6fc3d33bb5d579c602a1a06a238 100644 (file)
@@ -30,7 +30,7 @@ type Book struct {
 // ---------------------------------------------------------------------------
 type Field string
 const (
-  Author, Language, Series, Title Field = "aut", "lan", "ser", "tit"
+  Author, Language, List, Series, Title Field = "aut", "lan", "lst", "ser", "tit"
 )
 
 func (f Field) String() string {
@@ -188,34 +188,53 @@ func queryBookPathById(id int) (string) {
 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" +
+           " LEFT OUTER JOIN Lists_Books lb ON b.id=lb.book" +
+           " LEFT OUTER JOIN Lists l ON l.id=lb.list"
 
-  args := make([]interface{}, len(criteria))
+  args := make([]interface{}, 0)
 
-  for i, criterion := range criteria {
+  i := 0
+
+  for _, criterion := range criteria {
     if 0 == i {
       query += " WHERE "
     } else {
       query += " AND "
     }
 
-    text := criterion.Text
-
     switch criterion.Attribute {
     case Author:
       query += " UPPER(a.grouping) LIKE UPPER($" + strconv.Itoa(i + 1) + ")"
-      text = strings.Replace(text, " ", "", -1)        // Remove spaces
+      args = append(args, strings.Replace(criterion.Text, " ", "", -1))
     case Language:
       query += " UPPER(b.language) LIKE UPPER($" + strconv.Itoa(i + 1) + ")"
+      args = append(args, criterion.Text)
+    case List:
+      codes := strings.Split(criterion.Text, ",")
+      query += " UPPER(l.code) IN ("
+
+      for j, code := range codes {
+        if j > 0 {
+          query += ","
+        }
+        query += "UPPER($" + strconv.Itoa(i + j + 1) + ")"
+        args = append(args, code)
+      }
+      query += ")"
+      i += len(codes) - 1
     case Series:
       query += " UPPER(s.descr) LIKE UPPER($" + strconv.Itoa(i + 1) + ")"
+      args = append(args, criterion.Text)
     case Title:
       query += " UPPER(b.title) LIKE UPPER($" + strconv.Itoa(i + 1) + ")"
+      args = append(args, criterion.Text)
     default:
       report("Error:  unrecognized search field in queryIds():  " + criterion.Attribute.String(), nil)
       return nil
     }
-    args[i] = text
+
+    i++
   }
 
   query += " ORDER BY a.grouping,s.descr,b.volume,b.title,b.path"