package bouquins // MERGE SUB QUERIES // func assignAuthorsSeries(series []*SeriesAdv, authors map[int64][]*Author) { for _, s := range series { s.Authors = authors[s.Id] } } // SUB QUERIES // func (app *Bouquins) searchSeries(limit int, terms []string, all bool) ([]*SeriesAdv, int, error) { rows, err := app.searchHelper(all, terms, STMT_SERIES_SEARCH, STMT_SEARCH_TERM_SERIES, STMT_SEARCH_ORDER_SERIES) if err != nil { return nil, 0, err } defer rows.Close() series := make([]*SeriesAdv, 0, limit) count := 0 for rows.Next() { if len(series) <= limit { serie := new(SeriesAdv) if err := rows.Scan(&serie.Id, &serie.Name); err != nil { return nil, 0, err } series = append(series, serie) } count++ } if err := rows.Err(); err != nil { return nil, 0, err } return series, count, nil } func (app *Bouquins) querySeriesList(limit, offset int, sort, order string) ([]*SeriesAdv, bool, error) { series := make([]*SeriesAdv, 0, limit) stmt, err := app.psSortSeries(SERIES, 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(series) == limit { more = true } else { serie := new(SeriesAdv) if err := rows.Scan(&serie.Id, &serie.Name, &serie.Count); err != nil { return nil, false, err } series = append(series, serie) } } if err := rows.Err(); err != nil { return nil, false, err } return series, more, nil } func (app *Bouquins) querySeriesListAuthors(limit, offset int, sort, order string) (map[int64][]*Author, error) { authors := make(map[int64][]*Author) stmt, err := app.psSortBooks(SERIES_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 serId int64 if err := rows.Scan(&author.Id, &author.Name, &serId); err != nil { return nil, err } if authors[serId] == nil { authors[serId] = append(make([]*Author, 0), author) } else { authors[serId] = append(authors[serId], author) } } if err := rows.Err(); err != nil { return nil, err } return authors, nil } func (app *Bouquins) querySeries(id int64) (*SeriesFull, error) { stmt, err := app.ps(SERIE) if err != nil { return nil, err } series := new(SeriesFull) err = stmt.QueryRow(id).Scan(&series.Id, &series.Name) return series, nil } func (app *Bouquins) querySeriesAuthors(series *SeriesFull) error { stmt, err := app.ps(SERIE_AUTHORS) if err != nil { return err } rows, err := stmt.Query(series.Id) if err != nil { return err } defer rows.Close() for rows.Next() { author := new(Author) if err = rows.Scan(&author.Id, &author.Name); err != nil { return err } series.Authors = append(series.Authors, author) } if err := rows.Err(); err != nil { return err } return nil } func (app *Bouquins) querySeriesBooks(series *SeriesFull) error { stmt, err := app.ps(SERIE_BOOKS) if err != nil { return err } rows, err := stmt.Query(series.Id) if err != nil { return err } defer rows.Close() for rows.Next() { book := new(Book) if err = rows.Scan(&book.Id, &book.Title, &book.SeriesIndex); err != nil { return err } series.Books = append(series.Books, book) } if err := rows.Err(); err != nil { return err } return nil } // DB LOADS // func (app *Bouquins) SeriesFull(id int64) (*SeriesFull, error) { series, err := app.querySeries(id) if err != nil { return nil, err } err = app.querySeriesBooks(series) if err != nil { return nil, err } err = app.querySeriesAuthors(series) if err != nil { return nil, err } return series, nil } func (app *Bouquins) SeriesAdv(params *ReqParams) ([]*SeriesAdv, int, bool, error) { limit, offset, sort, order := params.Limit, params.Offset, params.Sort, params.Order if len(params.Terms) > 0 { series, count, err := app.searchSeries(limit, params.Terms, params.AllWords) return series, count, count > limit, err } series, more, err := app.querySeriesList(limit, offset, sort, order) if err != nil { return nil, 0, false, err } authors, err := app.querySeriesListAuthors(limit, offset, sort, order) if err != nil { return nil, 0, false, err } assignAuthorsSeries(series, authors) return series, 0, more, nil }