go-bouquins/bouquins/dbauthors.go

167 lines
3.8 KiB
Go
Raw Normal View History

2017-08-03 18:36:46 +00:00
package bouquins
2017-08-04 16:57:15 +00:00
import (
"database/sql"
)
2017-08-03 18:36:46 +00:00
// SUB QUERIES //
2017-08-06 10:50:43 +00:00
func (app *Bouquins) searchAuthors(limit int, terms []string, all bool) ([]*AuthorAdv, int, error) {
2017-08-06 18:25:20 +00:00
rows, err := app.searchHelper(all, terms, sqlAuthorsSearch, sqlAuthorsTerm, sqlAuthorsOrder)
2017-08-06 10:50:43 +00:00
if err != nil {
return nil, 0, err
}
2017-08-06 16:49:40 +00:00
authors := make([]*AuthorAdv, 0, limit)
count := 0
2017-08-06 10:50:43 +00:00
defer rows.Close()
for rows.Next() {
if len(authors) <= limit {
author := new(AuthorAdv)
2017-08-06 18:25:20 +00:00
if err := rows.Scan(&author.ID, &author.Name); err != nil {
2017-08-06 10:50:43 +00:00
return nil, 0, err
}
authors = append(authors, author)
}
count++
}
if err := rows.Err(); err != nil {
return nil, 0, err
}
return authors, count, nil
}
2017-08-05 17:40:58 +00:00
func (app *Bouquins) queryAuthors(limit, offset int, sort, order string) ([]*AuthorAdv, bool, error) {
2017-08-05 15:48:06 +00:00
authors := make([]*AuthorAdv, 0, limit)
2017-08-06 18:25:20 +00:00
stmt, err := app.psSortAuthors(qtAuthors, sort, order)
2017-08-05 15:48:06 +00:00
if err != nil {
2017-08-05 17:40:58 +00:00
return nil, false, err
2017-08-05 15:48:06 +00:00
}
2017-08-05 17:40:58 +00:00
rows, err := stmt.Query(limit+1, offset)
2017-08-05 15:48:06 +00:00
if err != nil {
2017-08-05 17:40:58 +00:00
return nil, false, err
2017-08-05 15:48:06 +00:00
}
defer rows.Close()
2017-08-05 17:40:58 +00:00
more := false
2017-08-05 15:48:06 +00:00
for rows.Next() {
2017-08-05 17:40:58 +00:00
if len(authors) == limit {
more = true
} else {
author := new(AuthorAdv)
2017-08-06 18:25:20 +00:00
if err := rows.Scan(&author.ID, &author.Name, &author.Count); err != nil {
2017-08-05 17:40:58 +00:00
return nil, false, err
}
authors = append(authors, author)
2017-08-05 15:48:06 +00:00
}
}
if err := rows.Err(); err != nil {
2017-08-05 17:40:58 +00:00
return nil, false, err
2017-08-05 15:48:06 +00:00
}
2017-08-05 17:40:58 +00:00
return authors, more, nil
2017-08-05 15:48:06 +00:00
}
2017-08-03 18:36:46 +00:00
func (app *Bouquins) queryAuthorBooks(author *AuthorFull) error {
2017-08-06 18:25:20 +00:00
stmt, err := app.ps(qtAuthorBooks)
2017-08-03 18:36:46 +00:00
if err != nil {
return err
}
2017-08-06 18:25:20 +00:00
rows, err := stmt.Query(author.ID)
2017-08-03 18:36:46 +00:00
if err != nil {
return err
}
defer rows.Close()
2017-08-04 16:57:15 +00:00
series := make(map[int64]*Series, 0)
2017-08-03 18:36:46 +00:00
for rows.Next() {
2017-08-04 16:57:15 +00:00
book := new(Book)
2017-08-06 18:25:20 +00:00
var seriesID sql.NullInt64
2017-08-03 18:36:46 +00:00
var seriesName sql.NullString
2017-08-06 18:25:20 +00:00
if err = rows.Scan(&book.ID, &book.Title, &book.SeriesIndex, &seriesName, &seriesID); err != nil {
2017-08-03 18:36:46 +00:00
return err
}
2017-08-06 18:25:20 +00:00
if seriesID.Valid && seriesName.Valid {
series[seriesID.Int64] = &Series{
seriesID.Int64,
2017-08-03 18:36:46 +00:00
seriesName.String,
}
}
author.Books = append(author.Books, book)
}
if err := rows.Err(); err != nil {
return err
}
2017-08-04 16:57:15 +00:00
author.Series = make([]*Series, 0, len(series))
for _, s := range series {
author.Series = append(author.Series, s)
}
2017-08-03 18:36:46 +00:00
return nil
}
func (app *Bouquins) queryAuthorAuthors(author *AuthorFull) error {
2017-08-06 18:25:20 +00:00
stmt, err := app.ps(qtAuthorCoauthors)
2017-08-04 16:57:15 +00:00
if err != nil {
return err
}
2017-08-06 18:25:20 +00:00
rows, err := stmt.Query(author.ID, author.ID)
2017-08-04 16:57:15 +00:00
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
coauthor := new(Author)
2017-08-06 18:25:20 +00:00
if err = rows.Scan(&coauthor.ID, &coauthor.Name); err != nil {
2017-08-04 16:57:15 +00:00
return err
}
author.CoAuthors = append(author.CoAuthors, coauthor)
}
if err := rows.Err(); err != nil {
return err
}
2017-08-03 18:36:46 +00:00
return nil
}
func (app *Bouquins) queryAuthor(id int64) (*AuthorFull, error) {
2017-08-06 18:25:20 +00:00
stmt, err := app.ps(qtAuthor)
2017-08-03 18:36:46 +00:00
if err != nil {
return nil, err
}
author := new(AuthorFull)
2017-08-06 18:25:20 +00:00
author.ID = id
2017-08-03 18:36:46 +00:00
err = stmt.QueryRow(id).Scan(&author.Name)
if err != nil {
return nil, err
}
return author, nil
}
// DB LOADS //
2017-08-06 18:25:20 +00:00
// AuthorsAdv loads a list of authors
2017-08-06 10:50:43 +00:00
func (app *Bouquins) AuthorsAdv(params *ReqParams) ([]*AuthorAdv, int, bool, error) {
limit, offset, sort, order := params.Limit, params.Offset, params.Sort, params.Order
if len(params.Terms) > 0 {
authors, count, err := app.searchAuthors(limit, params.Terms, params.AllWords)
return authors, count, count > limit, err
}
2017-08-05 17:40:58 +00:00
authors, more, err := app.queryAuthors(limit, offset, sort, order)
2017-08-05 15:48:06 +00:00
if err != nil {
2017-08-06 10:50:43 +00:00
return nil, 0, false, err
2017-08-05 15:48:06 +00:00
}
2017-08-06 10:50:43 +00:00
return authors, 0, more, nil
2017-08-05 15:48:06 +00:00
}
2017-08-06 18:25:20 +00:00
// AuthorFull loads an author
2017-08-03 18:36:46 +00:00
func (app *Bouquins) AuthorFull(id int64) (*AuthorFull, error) {
author, err := app.queryAuthor(id)
if err != nil {
return nil, err
}
err = app.queryAuthorBooks(author)
if err != nil {
return nil, err
}
err = app.queryAuthorAuthors(author)
if err != nil {
return nil, err
}
return author, nil
}