Using master pg library
This commit is contained in:
parent
d9a95a997c
commit
e0854aa001
8 changed files with 43 additions and 76 deletions
|
@ -18,8 +18,8 @@ type Book struct {
|
|||
Lang string
|
||||
Isbn string
|
||||
FileSize int
|
||||
Cover bool
|
||||
Active bool
|
||||
Cover bool `sql:",notnull"`
|
||||
Active bool `sql:",notnull"`
|
||||
UploadDate time.Time
|
||||
Tsv string
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ func (db *pgDB) AddBook(book Book) error {
|
|||
book.UploadDate = time.Now()
|
||||
}
|
||||
|
||||
return db.sql.Create(&book)
|
||||
return db.sql.Insert(&book)
|
||||
}
|
||||
|
||||
// GetBooks matching query
|
||||
|
@ -39,8 +39,6 @@ func (db *pgDB) GetBooks(query string, length int, start int) (books []Book, num
|
|||
return db.getBooks(true, query, length, start)
|
||||
}
|
||||
|
||||
// TODO: func (db *pgDB) GetBooksIter() Iter {
|
||||
|
||||
// GetNewBooks returns a list of books in the incoming queue and the number of books
|
||||
// in the queue
|
||||
func (db *pgDB) GetNewBooks(query string, length int, start int) (books []Book, num int, err error) {
|
||||
|
@ -48,45 +46,42 @@ func (db *pgDB) GetNewBooks(query string, length int, start int) (books []Book,
|
|||
}
|
||||
|
||||
func (db *pgDB) getBooks(active bool, query string, length int, start int) (books []Book, num int, err error) {
|
||||
column := []string{}
|
||||
columnParams := []interface{}{}
|
||||
searchCondition := "active = "
|
||||
rank := []string{}
|
||||
rankParams := []interface{}{}
|
||||
searchCondition := ""
|
||||
if active {
|
||||
searchCondition = "true"
|
||||
searchCondition += "active is true"
|
||||
} else {
|
||||
searchCondition = "false"
|
||||
searchCondition += "active is not true"
|
||||
}
|
||||
searchParams := []interface{}{}
|
||||
|
||||
textQuery, columnQuerys, trigramQuerys := buildQuery(query)
|
||||
for _, c := range columnQuerys {
|
||||
searchCondition = searchCondition + " AND " + c.column + " = ?"
|
||||
searchCondition += " AND " + c.column + " = ?"
|
||||
searchParams = append(searchParams, c.value)
|
||||
}
|
||||
for _, c := range trigramQuerys {
|
||||
column = append(column, "word_similarity(?, "+c.column+")")
|
||||
columnParams = append(columnParams, c.value)
|
||||
searchCondition = searchCondition + " AND " + c.column + " %> ?"
|
||||
rank = append(rank, "word_similarity(?, "+c.column+")")
|
||||
rankParams = append(rankParams, c.value)
|
||||
searchCondition += " AND " + c.column + " %> ?"
|
||||
searchParams = append(searchParams, c.value)
|
||||
}
|
||||
if textQuery != "" {
|
||||
column = append(column, "ts_rank(tsv, to_tsquery(?))")
|
||||
columnParams = append(columnParams, textQuery)
|
||||
searchCondition = searchCondition + " AND to_tsquery(?) @@ tsv"
|
||||
rank = append(rank, "ts_rank(tsv, to_tsquery(?))")
|
||||
rankParams = append(rankParams, textQuery)
|
||||
searchCondition += " AND to_tsquery(?) @@ tsv"
|
||||
searchParams = append(searchParams, textQuery)
|
||||
}
|
||||
|
||||
columnStr := "*"
|
||||
order := "upload_date DESC"
|
||||
if len(column) > 0 {
|
||||
columnStr = "*, " + strings.Join(column, "+") + " AS rank"
|
||||
order = "rank DESC, upload_date DESC"
|
||||
if len(rank) > 0 {
|
||||
order = strings.Join(rank, "+") + " DESC, upload_date DESC"
|
||||
}
|
||||
|
||||
num, err = db.sql.Model(&books).
|
||||
ColumnExpr(columnStr, columnParams...).
|
||||
Where(searchCondition, searchParams...).
|
||||
Order(order).
|
||||
OrderExpr(order, rankParams...).
|
||||
Offset(start).
|
||||
Limit(length).
|
||||
SelectAndCountEstimate(100)
|
||||
|
@ -169,7 +164,6 @@ type columnq struct {
|
|||
}
|
||||
|
||||
func buildQuery(query string) (textQuery string, columnQuerys []columnq, trigramQuerys []columnq) {
|
||||
// FIXME: does *Querys need initialization??
|
||||
words := strings.Split(query, " ")
|
||||
for _, w := range words {
|
||||
if w == "" {
|
||||
|
|
Reference in a new issue