go-bouquins/bouquins/dbseries.go

152 lines
3.3 KiB
Go
Raw Normal View History

2017-08-03 17:51:56 +00:00
package bouquins
2017-08-05 16:24:57 +00:00
// MERGE SUB QUERIES //
func assignAuthorsSeries(series []*SeriesAdv, authors map[int64][]*Author) {
for _, s := range series {
s.Authors = authors[s.Id]
}
}
2017-08-03 17:51:56 +00:00
// SUB QUERIES //
2017-08-05 16:24:57 +00:00
func (app *Bouquins) querySeriesList(limit, offset int, sort, order string) ([]*SeriesAdv, error) {
series := make([]*SeriesAdv, 0, limit)
stmt, err := app.psSortSeries(SERIES, 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() {
serie := new(SeriesAdv)
if err := rows.Scan(&serie.Id, &serie.Name, &serie.Count); err != nil {
return nil, err
}
series = append(series, serie)
}
if err := rows.Err(); err != nil {
return nil, err
}
return series, 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
}
2017-08-03 17:51:56 +00:00
func (app *Bouquins) querySeries(id int64) (*SeriesFull, error) {
2017-08-05 16:24:57 +00:00
stmt, err := app.ps(SERIE)
2017-08-03 17:51:56 +00:00
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 {
2017-08-05 16:24:57 +00:00
stmt, err := app.ps(SERIE_AUTHORS)
2017-08-03 17:51:56 +00:00
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 {
2017-08-05 16:24:57 +00:00
stmt, err := app.ps(SERIE_BOOKS)
2017-08-03 17:51:56 +00:00
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
}
2017-08-05 15:48:06 +00:00
func (app *Bouquins) SeriesAdv(limit, offset int, sort, order string) ([]*SeriesAdv, error) {
2017-08-05 16:24:57 +00:00
if limit == 0 {
limit = DEF_LIM
}
series, err := app.querySeriesList(limit, offset, sort, order)
if err != nil {
return nil, err
}
authors, err := app.querySeriesListAuthors(limit, offset, sort, order)
if err != nil {
return nil, err
}
assignAuthorsSeries(series, authors)
return series, nil
2017-08-05 15:48:06 +00:00
}