package bouquins import ( "database/sql" "log" ) // MERGE SUB QUERIES // func assignAuthorsTagsBooks(books []*BookAdv, authors map[int64][]*Author, tags map[int64][]string) { for _, b := range books { b.Authors = authors[b.Id] b.Tags = tags[b.Id] } } // SUB QUERIES // func (app *Bouquins) searchBooks(limit int, terms []string, all bool) ([]*BookAdv, int, error) { books := make([]*BookAdv, 0, limit) // FIXME factorize searchAuthors,searchSeries count := 0 query := STMT_BOOKS0 + STMT_WHERE queryTerms := make([]interface{}, 0, len(terms)) for i, term := range terms { queryTerms = append(queryTerms, "%"+term+"%") query += STMT_SEARCH_TERM_BOOKS if i < len(terms)-1 && all { query += STMT_BOOL_AND } if i < len(terms)-1 && !all { query += STMT_BOOL_OR } } query += STMT_SEARCH_ORDER_BOOKS log.Println("Search:", query) stmt, err := app.DB.Prepare(query) if err != nil { return nil, 0, err } rows, err := stmt.Query(queryTerms...) if err != nil { return nil, 0, err } defer rows.Close() // FIXME factorize queryBooks for rows.Next() { if len(books) <= limit { book := new(BookAdv) var series_name sql.NullString var series_id sql.NullInt64 if err := rows.Scan(&book.Id, &book.Title, &book.SeriesIndex, &series_name, &series_id); err != nil { return nil, 0, err } if series_name.Valid && series_id.Valid { book.Series = &Series{ series_id.Int64, series_name.String, } } books = append(books, book) } count++ } if err := rows.Err(); err != nil { return nil, 0, err } return books, count, nil } func (app *Bouquins) queryBooks(limit, offset int, sort, order string) ([]*BookAdv, bool, error) { books := make([]*BookAdv, 0, limit) stmt, err := app.psSortBooks(BOOKS, sort, order) if err != nil { return nil, false, err } rows, err := stmt.Query(limit+1, offset) if err != nil { return nil, false, err } defer rows.Close() more := false for rows.Next() { if len(books) == limit { more = true } else { book := new(BookAdv) var series_name sql.NullString var series_id sql.NullInt64 if err := rows.Scan(&book.Id, &book.Title, &book.SeriesIndex, &series_name, &series_id); err != nil { return nil, false, err } if series_name.Valid && series_id.Valid { book.Series = &Series{ series_id.Int64, series_name.String, } } books = append(books, book) } } if err := rows.Err(); err != nil { return nil, false, err } return books, more, nil } func (app *Bouquins) queryBooksAuthors(limit, offset int, sort, order string) (map[int64][]*Author, error) { authors := make(map[int64][]*Author) stmt, err := app.psSortBooks(BOOKS_AUTHORS, sort, order) if err != nil { return nil, err } rows, err := stmt.Query(limit, offset) if err != nil { return nil, err } defer rows.Close() for rows.Next() { author := new(Author) var book int64 if err := rows.Scan(&author.Id, &author.Name, &book); err != nil { return nil, err } if authors[book] == nil { authors[book] = append(make([]*Author, 0), author) } else { authors[book] = append(authors[book], author) } } if err := rows.Err(); err != nil { return nil, err } return authors, nil } func (app *Bouquins) queryBooksTags(limit, offset int, sort, order string) (map[int64][]string, error) { stmt, err := app.psSortBooks(BOOKS_TAGS, sort, order) if err != nil { return nil, err } rows, err := stmt.Query(limit, offset) if err != nil { return nil, err } defer rows.Close() tags := make(map[int64][]string) for rows.Next() { var tag string var book int64 if err := rows.Scan(&tag, &book); err != nil { return nil, err } bookTags := tags[book] if bookTags == nil { bookTags = make([]string, 0) tags[book] = bookTags } tags[book] = append(bookTags, tag) } if err := rows.Err(); err != nil { return nil, err } return tags, nil } func (app *Bouquins) queryBook(id int64) (*BookFull, error) { stmt, err := app.ps(BOOK) if err != nil { return nil, err } book := new(BookFull) var seriesIdx sql.NullFloat64 var seriesId, timestamp, pubdate sql.NullInt64 var seriesName, isbn, lccn, uuid, lang, publisher sql.NullString var cover sql.NullBool err = stmt.QueryRow(id).Scan(&book.Id, &book.Title, &seriesIdx, &seriesName, &seriesId, ×tamp, &pubdate, &isbn, &lccn, &book.Path, &uuid, &cover, &lang, &publisher) if err != nil { return nil, err } if seriesId.Valid && seriesName.Valid && seriesIdx.Valid { book.SeriesIndex = seriesIdx.Float64 book.Series = &Series{seriesId.Int64, seriesName.String} } if timestamp.Valid { book.Timestamp = timestamp.Int64 } if pubdate.Valid { book.Pubdate = pubdate.Int64 } if isbn.Valid { book.Isbn = isbn.String } if lccn.Valid { book.Lccn = lccn.String } if uuid.Valid { book.Uuid = uuid.String } if lang.Valid { book.Lang = lang.String } if publisher.Valid { book.Publisher = publisher.String } if cover.Valid { book.Has_cover = cover.Bool } return book, nil } func (app *Bouquins) queryBookTags(book *BookFull) error { stmt, err := app.ps(BOOK_TAGS) if err != nil { return err } rows, err := stmt.Query(book.Id) if err != nil { return err } defer rows.Close() for rows.Next() { var tag string if err = rows.Scan(&tag); err != nil { return err } book.Tags = append(book.Tags, tag) } if err := rows.Err(); err != nil { return err } return nil } func (app *Bouquins) queryBookData(book *BookFull) error { stmt, err := app.ps(BOOK_DATA) if err != nil { return err } rows, err := stmt.Query(book.Id) if err != nil { return err } defer rows.Close() for rows.Next() { data := new(BookData) if err = rows.Scan(&data.Name, &data.Format, &data.Size); err != nil { return err } book.Data = append(book.Data, data) } if err := rows.Err(); err != nil { return err } return nil } func (app *Bouquins) queryBookAuthors(book *BookFull) error { stmt, err := app.ps(BOOK_AUTHORS) if err != nil { return err } rows, err := stmt.Query(book.Id) if err != nil { return err } defer rows.Close() for rows.Next() { author := new(Author) var bookId int64 if err = rows.Scan(&author.Id, &author.Name, &bookId); err != nil { return err } book.Authors = append(book.Authors, author) } if err := rows.Err(); err != nil { return err } return nil } // DB LOADS // func (app *Bouquins) BookCount() (int64, error) { var count int64 stmt, err := app.ps(BOOKS_COUNT) if err != nil { return 0, err } row := stmt.QueryRow() err = row.Scan(&count) return count, err } func (app *Bouquins) BookFull(id int64) (*BookFull, error) { book, err := app.queryBook(id) if err != nil { return nil, err } err = app.queryBookTags(book) if err != nil { return nil, err } err = app.queryBookAuthors(book) if err != nil { return nil, err } err = app.queryBookData(book) if err != nil { return nil, err } return book, nil } func (app *Bouquins) BooksAdv(params *ReqParams) ([]*BookAdv, int, bool, error) { limit, offset, sort, order := params.Limit, params.Offset, params.Sort, params.Order if len(params.Terms) > 0 { books, count, err := app.searchBooks(limit, params.Terms, params.AllWords) return books, count, count > limit, err } books, more, err := app.queryBooks(limit, offset, sort, order) if err != nil { return nil, 0, false, err } authors, err := app.queryBooksAuthors(limit, offset, sort, order) if err != nil { return nil, 0, false, err } tags, err := app.queryBooksTags(limit, offset, sort, order) if err != nil { return nil, 0, false, err } assignAuthorsTagsBooks(books, authors, tags) return books, 0, more, nil }