package bouquins import ( "database/sql" "errors" "fmt" "log" ) const ( 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 ` 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 ? " STMT_AUTHORS0 = `SELECT authors.id, authors.name, count(book) as count FROM authors, books_authors_link WHERE authors.id = books_authors_link.author GROUP BY author ` STMT_AUTHORS_SEARCH = "SELECT id, name FROM authors WHERE " STMT_SEARCH_TERM_AUTHOR = " sort like ? " STMT_PAGE = " LIMIT ? OFFSET ?" STMT_WHERE = " WHERE " STMT_BOOL_AND = " AND " STMT_BOOL_OR = " OR " STMT_SEARCH_ORDER_BOOKS = " ORDER BY books.sort" STMT_SEARCH_ORDER_AUTHORS = " ORDER BY authors.sort" STMT_SEARCH_ORDER_SERIES = " ORDER BY series.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 + ")" 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 = ?` STMT_AUTHORS_ID_ASC = STMT_AUTHORS0 + "ORDER BY authors.id " + STMT_PAGE STMT_AUTHORS_ID_DESC = STMT_AUTHORS0 + "ORDER BY authors.id DESC " + STMT_PAGE STMT_AUTHORS_NAME_ASC = STMT_AUTHORS0 + "ORDER BY authors.sort " + STMT_PAGE STMT_AUTHORS_NAME_DESC = STMT_AUTHORS0 + "ORDER BY authors.sort DESC " + STMT_PAGE STMT_AUTHOR_BOOKS = `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 LEFT OUTER JOIN books_authors_link ON books.id = books_authors_link.book WHERE books_authors_link.author = ? ORDER BY id` STMT_AUTHOR_AUTHORS = `SELECT DISTINCT authors.id, authors.name FROM authors, books_authors_link WHERE books_authors_link.author = authors.id AND books_authors_link.book IN ( SELECT books.id FROM books LEFT OUTER JOIN books_authors_link ON books.id = books_authors_link.book WHERE books_authors_link.author = ? ORDER BY books.id) AND authors.id != ? ORDER BY authors.id` STMT_AUTHOR = "SELECT name FROM authors WHERE id = ?" DEF_LIM = 10 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 SERIE QueryType = iota SERIES QueryType = iota SERIES_AUTHORS QueryType = iota SERIE_AUTHORS QueryType = iota SERIE_BOOKS QueryType = iota AUTHORS QueryType = iota AUTHOR QueryType = iota AUTHOR_BOOKS QueryType = iota AUTHOR_COAUTHORS QueryType = iota ) 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, 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{SERIE, false, false}: STMT_SERIE, Query{SERIES, true, true}: STMT_SERIES_NAME_DESC, Query{SERIES, true, false}: STMT_SERIES_NAME_ASC, Query{SERIES, false, true}: STMT_SERIES_ID_DESC, Query{SERIES, false, false}: STMT_SERIES_ID_ASC, Query{SERIES_AUTHORS, true, true}: STMT_SERIES_AUTHORS_NAME_DESC, Query{SERIES_AUTHORS, true, false}: STMT_SERIES_AUTHORS_NAME_ASC, Query{SERIES_AUTHORS, false, true}: STMT_SERIES_AUTHORS_ID_DESC, Query{SERIES_AUTHORS, false, false}: STMT_SERIES_AUTHORS_ID_ASC, Query{SERIE_AUTHORS, false, false}: STMT_SERIE_AUTHORS, Query{SERIE_BOOKS, false, false}: STMT_SERIE_BOOKS, Query{AUTHORS, true, true}: STMT_AUTHORS_NAME_DESC, Query{AUTHORS, true, false}: STMT_AUTHORS_NAME_ASC, Query{AUTHORS, false, true}: STMT_AUTHORS_ID_DESC, Query{AUTHORS, false, false}: STMT_AUTHORS_ID_ASC, Query{AUTHOR, false, false}: STMT_AUTHOR, Query{AUTHOR_BOOKS, false, false}: STMT_AUTHOR_BOOKS, Query{AUTHOR_COAUTHORS, false, false}: STMT_AUTHOR_AUTHORS, } var STMTS = make(map[Query]*sql.Stmt) type QueryType uint type Query struct { Type QueryType SortField bool // sort by name or title Desc bool } func (app *Bouquins) searchHelper(all bool, terms []string, stub, termExpr, orderExpr string) (*sql.Rows, error) { query := stub queryTerms := make([]interface{}, 0, len(terms)) for i, term := range terms { queryTerms = append(queryTerms, "%"+term+"%") query += termExpr if i < len(terms)-1 && all { query += STMT_BOOL_AND } if i < len(terms)-1 && !all { query += STMT_BOOL_OR } } query += orderExpr log.Println("Search:", query) stmt, err := app.DB.Prepare(query) if err != nil { return nil, err } rows, err := stmt.Query(queryTerms...) if err != nil { return nil, err } return rows, nil } // PREPARED STATEMENTS // 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) { return app.psSort("title", qt, sort, order) } func (app *Bouquins) psSortAuthors(qt QueryType, sort, order string) (*sql.Stmt, error) { return app.psSort("name", qt, sort, order) } func (app *Bouquins) psSortSeries(qt QueryType, sort, order string) (*sql.Stmt, error) { return app.psSort("name", qt, sort, order) } func (app *Bouquins) psSort(sortNameField string, qt QueryType, sort, order string) (*sql.Stmt, error) { q := Query{qt, sort == sortNameField, order == "desc"} query := QUERIES[q] log.Println(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.psSort("any", qt, "", "") }