12 Age string // recommended age, e.g. "beginner", "junior", "ya" (Young Adult), "adult"
13 AuthorGrouping string // unique rendering of the author's name, used for internal grouping
14 AuthorReading string // reading order of author's name, e.g. "Charles Dickens"
15 AuthorSort string // sort order of author's name, e.g. "Dickens, Charles"
16 DDC string // Dewey Decimal Classification
17 Description string // Back cover / inside flap blurb, describing the book
18 Genre string // e.g. "adventure", "historical", "mystery", "romance", "sf" (Science Fiction)
19 LCC string // Library of Congress Classification
26 // TODO: protect DB with a real password, and read DB config from an external file
27 db := openDb("quanlib", "quanlib", "quanlib")
33 query := "SELECT COUNT(1) FROM Books"
34 ps, err := db.Prepare(query)
36 fmt.Println("Error: failed to prepare statement: " + query)
41 err = ps.QueryRow().Scan(&count)
42 if sql.ErrNoRows == err {
43 fmt.Println("Error: No rows returned by statement: " + query)
45 fmt.Println("Found ", count, " books.")
48 ids := []int {1041, 1951, 2148, 103}
49 books := queryBooksByIds(db, ids)
51 fmt.Println("Found books:", books)
54 func nsVal(ns sql.NullString) string {
61 func openDb(user, pass, dbName string) (*sql.DB) {
62 db, err := sql.Open("postgres","user=" + user + " password=" + pass + " dbname=" + dbName + " sslmode=disable")
64 fmt.Println("Error: DB arguments incorrect?", err)
70 fmt.Println("Error: could not connect to DB.", err)
78 func queryBooksByIds(db *sql.DB, ids []int) []Book {
79 query := `SELECT s.age,a.grouping,a.reading,a.sort,c.ddc,b.description,s.genre,c.lcc,s.descr,b.title,b.volume
81 INNER JOIN Books b ON a.id=b.author
82 LEFT OUTER JOIN Classifications c ON c.id=b.classification
83 LEFT OUTER JOIN Series s ON s.id=b.series
86 ps, err := db.Prepare(query)
88 fmt.Println("Error: failed to prepare statement: " + query)
93 res := make([]Book, len(ids))
95 for n, id := range ids {
96 row := ps.QueryRow(id)
98 var age, grouping, reading, sort, ddc, description, genre, lcc, name, title, volume sql.NullString
100 err = row.Scan(&age, &grouping, &reading, &sort, &ddc, &description, &genre, &lcc, &name, &title, &volume)
102 fmt.Println("Error: Failed to read book:", id, ":", err)
107 b.AuthorGrouping = nsVal(grouping)
108 b.AuthorReading = nsVal(reading)
109 b.AuthorSort = nsVal(sort)
111 b.Description = nsVal(description)
112 b.Genre = nsVal(genre)
114 b.SeriesName = nsVal(name)
115 b.Title = nsVal(title)
116 b.Volume = nsVal(volume)