go-bouquins/bouquins/db.go

168 lines
7.7 KiB
Go
Raw Normal View History

2017-07-30 18:14:20 +00:00
package bouquins
import (
"database/sql"
2017-08-02 17:54:09 +00:00
"errors"
"fmt"
2017-07-31 17:37:52 +00:00
"log"
2017-07-30 18:14:20 +00:00
)
const (
STMT_PAGE = " LIMIT ? OFFSET ?"
STMT_BOOKS0 = `SELECT books.id AS id,title,series_index,name as series_name,series.id AS series_id
FROM books LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book
LEFT OUTER JOIN series ON series.id = books_series_link.series `
STMT_BOOKS_TAGS0 = `SELECT name, books_tags_link.book as book FROM tags, books_tags_link
WHERE tags.id = books_tags_link.tag AND books_tags_link.book IN ( SELECT id FROM books `
STMT_BOOKS_AUTHORS0 = `SELECT authors.id, authors.name, books_authors_link.book as book
FROM authors, books_authors_link WHERE books_authors_link.author = authors.id
AND books_authors_link.book IN ( SELECT id FROM books `
2017-08-03 17:51:56 +00:00
STMT_SEARCH_TERM_BOOKS = " books.sort like ? "
STMT_SERIES0 = `SELECT series.id, series.name, count(book) FROM series
LEFT OUTER JOIN books_series_link ON books_series_link.series = series.id
GROUP BY series.id `
STMT_SERIES_AUTHORS0 = `SELECT DISTINCT authors.id, authors.name, books_series_link.series
FROM authors, books_authors_link, books_series_link
WHERE books_authors_link.book = books_series_link.book AND books_authors_link.author = authors.id
AND books_series_link.series IN ( SELECT id FROM series `
STMT_SERIES_SEARCH = "SELECT series.id, series.name FROM series WHERE "
STMT_SEARCH_TERM_SERIES = " series.sort like ? "
2017-07-30 18:14:20 +00:00
STMT_WHERE = " WHERE "
STMT_BOOL_AND = " AND "
STMT_BOOL_OR = " OR "
STMT_SEARCH_ORDER = " ORDER BY books.sort"
STMT_BOOKS_COUNT = "SELECT count(id) FROM books"
STMT_BOOK = `SELECT books.id AS id,title, series_index, series.name AS series_name, series.id AS series_id,
strftime('%s', timestamp), strftime('%Y', pubdate), isbn,lccn,path,uuid,has_cover,
languages.lang_code, publishers.name AS pubname FROM books
LEFT OUTER JOIN books_languages_link ON books_languages_link.book = books.id
LEFT OUTER JOIN languages ON languages.id = books_languages_link.lang_code
LEFT OUTER JOIN data ON data.book = books.id
LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book
LEFT OUTER JOIN series ON series.id = books_series_link.series
LEFT OUTER JOIN books_publishers_link ON books.id = books_publishers_link.book
LEFT OUTER JOIN publishers ON publishers.id = books_publishers_link.publisher
WHERE books.id = ?`
STMT_BOOK_TAGS = "SELECT name FROM tags, books_tags_link WHERE tags.id = books_tags_link.tag AND books_tags_link.book = ?"
STMT_BOOK_AUTHORS = `SELECT authors.id, authors.name, books_authors_link.book as book
FROM authors, books_authors_link WHERE books_authors_link.author = authors.id
AND books_authors_link.book = ?`
STMT_BOOK_DATA = "SELECT data.name, data.format, data.uncompressed_size FROM data WHERE data.book = ?"
STMT_BOOKS_ID_ASC = STMT_BOOKS0 + " ORDER BY id" + STMT_PAGE
STMT_BOOKS_ID_DESC = STMT_BOOKS0 + "ORDER BY id DESC" + STMT_PAGE
STMT_BOOKS_TITLE_ASC = STMT_BOOKS0 + "ORDER BY books.sort" + STMT_PAGE
STMT_BOOKS_TITLE_DESC = STMT_BOOKS0 + "ORDER BY books.sort DESC" + STMT_PAGE
STMT_BOOKS_TAGS_ID_ASC = STMT_BOOKS_TAGS0 + "ORDER BY id" + STMT_PAGE + ")"
STMT_BOOKS_TAGS_ID_DESC = STMT_BOOKS_TAGS0 + "ORDER BY id DESC" + STMT_PAGE + ")"
STMT_BOOKS_TAGS_TITLE_ASC = STMT_BOOKS_TAGS0 + "ORDER BY books.sort" + STMT_PAGE + ")"
STMT_BOOKS_TAGS_TITLE_DESC = STMT_BOOKS_TAGS0 + "ORDER BY books.sort DESC" + STMT_PAGE + ")"
STMT_BOOKS_AUTHORS_ID_ASC = STMT_BOOKS_AUTHORS0 + "ORDER BY id" + STMT_PAGE + ")"
STMT_BOOKS_AUTHORS_ID_DESC = STMT_BOOKS_AUTHORS0 + "ORDER BY id DESC" + STMT_PAGE + ")"
STMT_BOOKS_AUTHORS_TITLE_ASC = STMT_BOOKS_AUTHORS0 + "ORDER BY books.sort" + STMT_PAGE + ")"
STMT_BOOKS_AUTHORS_TITLE_DESC = STMT_BOOKS_AUTHORS0 + "ORDER BY books.sort DESC" + STMT_PAGE + ")"
2017-08-03 17:51:56 +00:00
STMT_SERIES_ID_ASC = STMT_SERIES0 + " ORDER BY series.id" + STMT_PAGE
STMT_SERIES_ID_DESC = STMT_SERIES0 + " ORDER BY series.id DESC" + STMT_PAGE
STMT_SERIES_NAME_ASC = STMT_SERIES0 + " ORDER BY series.sort" + STMT_PAGE
STMT_SERIES_NAME_DESC = STMT_SERIES0 + " ORDER BY series.sort DESC" + STMT_PAGE
STMT_SERIES_AUTHORS_ID_ASC = STMT_SERIES_AUTHORS0 + " ORDER BY series.id" + STMT_PAGE + ")"
STMT_SERIES_AUTHORS_ID_DESC = STMT_SERIES_AUTHORS0 + " ORDER BY series.id DESC" + STMT_PAGE + ")"
STMT_SERIES_AUTHORS_NAME_ASC = STMT_SERIES_AUTHORS0 + " ORDER BY series.sort" + STMT_PAGE + ")"
STMT_SERIES_AUTHORS_NAME_DESC = STMT_SERIES_AUTHORS0 + " ORDER BY series.sort DESC" + STMT_PAGE + ")"
STMT_SERIE = "SELECT series.id, series.name FROM series WHERE series.id = ?"
STMT_SERIE_BOOKS = `SELECT books.id, title, series_index FROM books
LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book
WHERE books_series_link.series = ? ORDER BY series_index ASC`
STMT_SERIE_AUTHORS = `SELECT DISTINCT authors.id, authors.name
FROM authors, books_authors_link, books_series_link
WHERE books_authors_link.book = books_series_link.book AND books_authors_link.author = authors.id
AND books_series_link.series = ?`
2017-07-30 18:14:20 +00:00
DEF_LIM = 10
2017-07-31 17:37:52 +00:00
2017-08-03 17:51:56 +00:00
BOOKS QueryType = iota
BOOKS_TAGS QueryType = iota
BOOKS_AUTHORS QueryType = iota
BOOK QueryType = iota
BOOK_TAGS QueryType = iota
BOOK_DATA QueryType = iota
BOOK_AUTHORS QueryType = iota
BOOKS_COUNT QueryType = iota
SERIES QueryType = iota
SERIES_AUTHORS QueryType = iota
SERIES_BOOKS QueryType = iota
2017-07-30 18:14:20 +00:00
)
2017-07-31 17:37:52 +00:00
var QUERIES = map[Query]string{
2017-08-03 17:51:56 +00:00
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,
Query{BOOK, false, false}: STMT_BOOK,
Query{BOOK_TAGS, false, false}: STMT_BOOK_TAGS,
Query{BOOK_DATA, false, false}: STMT_BOOK_DATA,
Query{BOOK_AUTHORS, false, false}: STMT_BOOK_AUTHORS,
Query{BOOKS_COUNT, false, false}: STMT_BOOKS_COUNT,
Query{SERIES, false, false}: STMT_SERIE,
Query{SERIES_AUTHORS, false, false}: STMT_SERIE_AUTHORS,
Query{SERIES_BOOKS, false, false}: STMT_SERIE_BOOKS,
2017-07-31 17:37:52 +00:00
}
2017-08-02 17:54:09 +00:00
var STMTS = make(map[Query]*sql.Stmt)
2017-07-31 17:37:52 +00:00
type QueryType uint
type Query struct {
Type QueryType
Title bool
Desc bool
}
// PREPARED STATEMENTS //
2017-08-02 17:54:09 +00:00
func (app *Bouquins) PrepareAll() error {
errcount := 0
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]
2017-07-31 17:37:52 +00:00
log.Println(query)
2017-08-02 17:54:09 +00:00
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, "", "")
2017-07-31 17:37:52 +00:00
}