Reuse prepared statements, close rows
This commit is contained in:
parent
a1df078f90
commit
b82967565b
@ -2,6 +2,8 @@ package bouquins
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -56,6 +58,10 @@ const (
|
|||||||
BOOKS QueryType = iota
|
BOOKS QueryType = iota
|
||||||
BOOKS_TAGS QueryType = iota
|
BOOKS_TAGS QueryType = iota
|
||||||
BOOKS_AUTHORS QueryType = iota
|
BOOKS_AUTHORS QueryType = iota
|
||||||
|
BOOK QueryType = iota
|
||||||
|
BOOK_TAGS QueryType = iota
|
||||||
|
BOOK_DATA QueryType = iota
|
||||||
|
BOOKS_COUNT QueryType = iota
|
||||||
)
|
)
|
||||||
|
|
||||||
var QUERIES = map[Query]string{
|
var QUERIES = map[Query]string{
|
||||||
@ -71,7 +77,12 @@ var QUERIES = map[Query]string{
|
|||||||
Query{BOOKS_AUTHORS, true, false}: STMT_BOOKS_AUTHORS_TITLE_ASC,
|
Query{BOOKS_AUTHORS, true, false}: STMT_BOOKS_AUTHORS_TITLE_ASC,
|
||||||
Query{BOOKS_AUTHORS, false, true}: STMT_BOOKS_AUTHORS_ID_DESC,
|
Query{BOOKS_AUTHORS, false, true}: STMT_BOOKS_AUTHORS_ID_DESC,
|
||||||
Query{BOOKS_AUTHORS, false, false}: STMT_BOOKS_AUTHORS_ID_ASC,
|
Query{BOOKS_AUTHORS, false, false}: STMT_BOOKS_AUTHORS_ID_ASC,
|
||||||
|
Query{BOOK, false, false}: STMT_BOOK,
|
||||||
|
Query{BOOK_TAGS, false, false}: STMT_BOOK_TAGS,
|
||||||
|
Query{BOOK_DATA, false, false}: STMT_BOOK_DATA,
|
||||||
|
Query{BOOKS_COUNT, false, false}: STMT_BOOKS_COUNT,
|
||||||
}
|
}
|
||||||
|
var STMTS = make(map[Query]*sql.Stmt)
|
||||||
|
|
||||||
type QueryType uint
|
type QueryType uint
|
||||||
type Query struct {
|
type Query struct {
|
||||||
@ -81,11 +92,42 @@ type Query struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PREPARED STATEMENTS //
|
// PREPARED STATEMENTS //
|
||||||
func (app *Bouquins) psBooks(qt QueryType, sort, order string) (*sql.Stmt, error) {
|
func (app *Bouquins) PrepareAll() error {
|
||||||
//TODO cache
|
errcount := 0
|
||||||
query := QUERIES[Query{qt, sort == "title", order == "desc"}]
|
for q, v := range QUERIES {
|
||||||
|
stmt, err := app.DB.Prepare(v)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err, v)
|
||||||
|
errcount++
|
||||||
|
}
|
||||||
|
STMTS[q] = stmt
|
||||||
|
}
|
||||||
|
if errcount > 0 {
|
||||||
|
return errors.New(fmt.Sprintf("%d errors on queries, see logs", errcount))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepared statement with sort on books
|
||||||
|
func (app *Bouquins) psSortBooks(qt QueryType, sort, order string) (*sql.Stmt, error) {
|
||||||
|
q := Query{qt, sort == "title", order == "desc"}
|
||||||
|
query := QUERIES[q]
|
||||||
log.Println(query)
|
log.Println(query)
|
||||||
return app.DB.Prepare(query)
|
stmt := STMTS[q]
|
||||||
|
if stmt == nil {
|
||||||
|
log.Println("Missing statement for ", q)
|
||||||
|
var err error
|
||||||
|
stmt, err = app.DB.Prepare(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stmt, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepared statement without sort
|
||||||
|
func (app *Bouquins) ps(qt QueryType) (*sql.Stmt, error) {
|
||||||
|
return app.psSortBooks(qt, "", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// MERGE SUB QUERIES //
|
// MERGE SUB QUERIES //
|
||||||
@ -100,7 +142,7 @@ func assignAuthorsTagsBooks(books []*BookAdv, authors map[int64][]*Author, tags
|
|||||||
|
|
||||||
func (app *Bouquins) queryBooks(limit, offset int, sort, order string) ([]*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.psBooks(BOOKS, sort, order)
|
stmt, err := app.psSortBooks(BOOKS, sort, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -108,6 +150,7 @@ func (app *Bouquins) queryBooks(limit, offset int, sort, order string) ([]*BookA
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
book := new(BookAdv)
|
book := new(BookAdv)
|
||||||
var series_name sql.NullString
|
var series_name sql.NullString
|
||||||
@ -131,7 +174,7 @@ func (app *Bouquins) queryBooks(limit, offset int, sort, order string) ([]*BookA
|
|||||||
|
|
||||||
func (app *Bouquins) queryBooksAuthors(limit, offset int, sort, order string) (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.psBooks(BOOKS_AUTHORS, sort, order)
|
stmt, err := app.psSortBooks(BOOKS_AUTHORS, sort, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -139,6 +182,7 @@ func (app *Bouquins) queryBooksAuthors(limit, offset int, sort, order string) (m
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
author := new(Author)
|
author := new(Author)
|
||||||
var book int64
|
var book int64
|
||||||
@ -158,7 +202,7 @@ func (app *Bouquins) queryBooksAuthors(limit, offset int, sort, order string) (m
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (app *Bouquins) queryBooksTags(limit, offset int, sort, order string) (map[int64][]string, error) {
|
func (app *Bouquins) queryBooksTags(limit, offset int, sort, order string) (map[int64][]string, error) {
|
||||||
stmt, err := app.psBooks(BOOKS_TAGS, sort, order)
|
stmt, err := app.psSortBooks(BOOKS_TAGS, sort, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -166,6 +210,7 @@ func (app *Bouquins) queryBooksTags(limit, offset int, sort, order string) (map[
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
tags := make(map[int64][]string)
|
tags := make(map[int64][]string)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var tag string
|
var tag string
|
||||||
@ -186,7 +231,7 @@ func (app *Bouquins) queryBooksTags(limit, offset int, sort, order string) (map[
|
|||||||
return tags, nil
|
return tags, nil
|
||||||
}
|
}
|
||||||
func (app *Bouquins) queryBook(id int64) (*BookFull, error) {
|
func (app *Bouquins) queryBook(id int64) (*BookFull, error) {
|
||||||
stmt, err := app.DB.Prepare(STMT_BOOK)
|
stmt, err := app.ps(BOOK)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -231,7 +276,7 @@ func (app *Bouquins) queryBook(id int64) (*BookFull, error) {
|
|||||||
return book, nil
|
return book, nil
|
||||||
}
|
}
|
||||||
func (app *Bouquins) queryBookTags(book *BookFull) error {
|
func (app *Bouquins) queryBookTags(book *BookFull) error {
|
||||||
stmt, err := app.DB.Prepare(STMT_BOOK_TAGS)
|
stmt, err := app.ps(BOOK_TAGS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -239,6 +284,7 @@ func (app *Bouquins) queryBookTags(book *BookFull) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var tag string
|
var tag string
|
||||||
if err = rows.Scan(&tag); err != nil {
|
if err = rows.Scan(&tag); err != nil {
|
||||||
@ -251,8 +297,8 @@ func (app *Bouquins) queryBookTags(book *BookFull) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (app *Bouquins) queryBookAuthors(book *BookFull) error {
|
func (app *Bouquins) queryBookData(book *BookFull) error {
|
||||||
stmt, err := app.DB.Prepare(STMT_BOOK_DATA)
|
stmt, err := app.ps(BOOK_DATA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -260,6 +306,7 @@ func (app *Bouquins) queryBookAuthors(book *BookFull) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
data := new(BookData)
|
data := new(BookData)
|
||||||
if err = rows.Scan(&data.Name, &data.Format, &data.Size); err != nil {
|
if err = rows.Scan(&data.Name, &data.Format, &data.Size); err != nil {
|
||||||
@ -272,7 +319,7 @@ func (app *Bouquins) queryBookAuthors(book *BookFull) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (app *Bouquins) queryBookData(book *BookFull) error {
|
func (app *Bouquins) queryBookAuthors(book *BookFull) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,8 +327,12 @@ func (app *Bouquins) queryBookData(book *BookFull) error {
|
|||||||
|
|
||||||
func (app *Bouquins) BookCount() (int64, error) {
|
func (app *Bouquins) BookCount() (int64, error) {
|
||||||
var count int64
|
var count int64
|
||||||
row := app.DB.QueryRow(STMT_BOOKS_COUNT)
|
stmt, err := app.ps(BOOKS_COUNT)
|
||||||
err := row.Scan(&count)
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
row := stmt.QueryRow()
|
||||||
|
err = row.Scan(&count)
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
func (app *Bouquins) SeriesAdv(limit, offset int, sort, order string) ([]*SeriesAdv, error) {
|
func (app *Bouquins) SeriesAdv(limit, offset int, sort, order string) ([]*SeriesAdv, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user