sort/order in queries
This commit is contained in:
parent
e56b436928
commit
ff0c3eabfe
@ -146,7 +146,7 @@ func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) {
|
|||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
model := NewIndexModel("", count)
|
model := NewIndexModel("", count)
|
||||||
model.Books, err = app.BooksAdv(0, 0)
|
model.Books, err = app.BooksAdv(0, 0, "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package bouquins
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -51,8 +52,44 @@ const (
|
|||||||
STMT_BOOKS_AUTHORS_TITLE_DESC = STMT_BOOKS_AUTHORS0 + "ORDER BY books.sort DESC" + STMT_PAGE + ")"
|
STMT_BOOKS_AUTHORS_TITLE_DESC = STMT_BOOKS_AUTHORS0 + "ORDER BY books.sort DESC" + STMT_PAGE + ")"
|
||||||
|
|
||||||
DEF_LIM = 10
|
DEF_LIM = 10
|
||||||
|
|
||||||
|
BOOKS QueryType = 0
|
||||||
|
BOOKS_TAGS QueryType = 1
|
||||||
|
BOOKS_AUTHORS QueryType = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var QUERIES = map[Query]string{
|
||||||
|
Query{BOOKS, true, true}: STMT_BOOKS_TITLE_DESC,
|
||||||
|
Query{BOOKS, true, false}: STMT_BOOKS_TITLE_ASC,
|
||||||
|
Query{BOOKS, false, true}: STMT_BOOKS_ID_DESC,
|
||||||
|
Query{BOOKS, false, false}: STMT_BOOKS_ID_ASC,
|
||||||
|
|
||||||
|
Query{BOOKS_TAGS, true, true}: STMT_BOOKS_TAGS_TITLE_DESC,
|
||||||
|
Query{BOOKS_TAGS, true, false}: STMT_BOOKS_TAGS_TITLE_ASC,
|
||||||
|
Query{BOOKS_TAGS, false, true}: STMT_BOOKS_TAGS_ID_DESC,
|
||||||
|
Query{BOOKS_TAGS, false, false}: STMT_BOOKS_TAGS_ID_ASC,
|
||||||
|
|
||||||
|
Query{BOOKS_AUTHORS, true, true}: STMT_BOOKS_AUTHORS_TITLE_DESC,
|
||||||
|
Query{BOOKS_AUTHORS, true, false}: STMT_BOOKS_AUTHORS_TITLE_ASC,
|
||||||
|
Query{BOOKS_AUTHORS, false, true}: STMT_BOOKS_AUTHORS_ID_DESC,
|
||||||
|
Query{BOOKS_AUTHORS, false, false}: STMT_BOOKS_AUTHORS_ID_ASC,
|
||||||
|
}
|
||||||
|
|
||||||
|
type QueryType uint
|
||||||
|
type Query struct {
|
||||||
|
Type QueryType
|
||||||
|
Title bool
|
||||||
|
Desc bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// PREPARED STATEMENTS //
|
||||||
|
func (app *Bouquins) ps(qt QueryType, sort, order string) (*sql.Stmt, error) {
|
||||||
|
//TODO cache
|
||||||
|
query := QUERIES[Query{qt, sort == "title", order == "desc"}]
|
||||||
|
log.Println(query)
|
||||||
|
return app.DB.Prepare(query)
|
||||||
|
}
|
||||||
|
|
||||||
// MERGE SUB QUERIES //
|
// MERGE SUB QUERIES //
|
||||||
func assignAuthorsTagsBooks(books []*BookAdv, authors map[int64][]*Author, tags map[int64][]string) {
|
func assignAuthorsTagsBooks(books []*BookAdv, authors map[int64][]*Author, tags map[int64][]string) {
|
||||||
for _, b := range books {
|
for _, b := range books {
|
||||||
@ -63,9 +100,9 @@ func assignAuthorsTagsBooks(books []*BookAdv, authors map[int64][]*Author, tags
|
|||||||
|
|
||||||
// SUB QUERIES //
|
// SUB QUERIES //
|
||||||
|
|
||||||
func (app *Bouquins) queryBooks(limit, offset int) ([]*BookAdv, error) {
|
func (app *Bouquins) queryBooks(limit, offset int, sort, order string) ([]*BookAdv, error) {
|
||||||
books := make([]*BookAdv, 0, limit)
|
books := make([]*BookAdv, 0, limit)
|
||||||
stmt, err := app.DB.Prepare(STMT_BOOKS_ID_ASC)
|
stmt, err := app.ps(BOOKS, sort, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -94,9 +131,9 @@ func (app *Bouquins) queryBooks(limit, offset int) ([]*BookAdv, error) {
|
|||||||
return books, nil
|
return books, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Bouquins) queryBooksAuthors(limit, offset int) (map[int64][]*Author, error) {
|
func (app *Bouquins) queryBooksAuthors(limit, offset int, sort, order string) (map[int64][]*Author, error) {
|
||||||
authors := make(map[int64][]*Author)
|
authors := make(map[int64][]*Author)
|
||||||
stmt, err := app.DB.Prepare(STMT_BOOKS_AUTHORS_ID_ASC)
|
stmt, err := app.ps(BOOKS_AUTHORS, sort, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -122,9 +159,9 @@ func (app *Bouquins) queryBooksAuthors(limit, offset int) (map[int64][]*Author,
|
|||||||
return authors, nil
|
return authors, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Bouquins) queryBooksTags(limit, offset int) (map[int64][]string, error) {
|
func (app *Bouquins) queryBooksTags(limit, offset int, sort, order string) (map[int64][]string, error) {
|
||||||
tags := make(map[int64][]string)
|
tags := make(map[int64][]string)
|
||||||
stmt, err := app.DB.Prepare(STMT_BOOKS_TAGS_ID_ASC)
|
stmt, err := app.ps(BOOKS_TAGS, sort, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -160,19 +197,19 @@ func (app *Bouquins) BookCount() (int64, error) {
|
|||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Bouquins) BooksAdv(limit int, offset int) ([]*BookAdv, error) {
|
func (app *Bouquins) BooksAdv(limit, offset int, sort, order string) ([]*BookAdv, error) {
|
||||||
if limit == 0 {
|
if limit == 0 {
|
||||||
limit = DEF_LIM
|
limit = DEF_LIM
|
||||||
}
|
}
|
||||||
books, err := app.queryBooks(limit, offset)
|
books, err := app.queryBooks(limit, offset, sort, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
authors, err := app.queryBooksAuthors(limit, offset)
|
authors, err := app.queryBooksAuthors(limit, offset, sort, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tags, err := app.queryBooksTags(limit, offset)
|
tags, err := app.queryBooksTags(limit, offset, sort, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
4
main.go
4
main.go
@ -2,11 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
|
||||||
"meutel.net/meutel/go-bouquins/bouquins"
|
"meutel.net/meutel/go-bouquins/bouquins"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,6 +24,7 @@ const (
|
|||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||||
tpl, err := template.ParseGlob("templates/*.html")
|
tpl, err := template.ParseGlob("templates/*.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user