go-bouquins/bouquins/dbseries.go
2017-08-05 19:40:58 +02:00

154 lines
3.5 KiB
Go

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) 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(limit, offset int, sort, order string) ([]*SeriesAdv, bool, error) {
series, more, err := app.querySeriesList(limit, offset, sort, order)
if err != nil {
return nil, false, err
}
authors, err := app.querySeriesListAuthors(limit, offset, sort, order)
if err != nil {
return nil, false, err
}
assignAuthorsSeries(series, authors)
return series, more, nil
}