Browse Source

Fix randquote for terms with _ or %

main
Nick Thomas 1 year ago
parent
commit
8b2338188b
  1. 2
      handlers/quotedb/dao.go
  2. 11
      handlers/quotedb/quotedb.go

2
handlers/quotedb/dao.go

@ -41,7 +41,7 @@ var (
findQuoteById = `SELECT ` + quoteCols + ` FROM quotes WHERE id = ? LIMIT 1`
findQuoteByQuoteId = `SELECT ` + quoteCols + ` FROM quotes WHERE quote_id = ? AND channel = ? LIMIT 1`
findLastQuote = `SELECT ` + quoteCols + ` FROM quotes WHERE channel = ? ORDER BY quote_id DESC LIMIT 1`
findQuotesByTerm = `SELECT ` + quoteCols + ` FROM quotes WHERE channel = ? AND data LIKE ? ORDER BY quote_id DESC`
findQuotesByTerm = `SELECT ` + quoteCols + ` FROM quotes WHERE channel = ? AND data LIKE ? ESCAPE '\' ORDER BY quote_id DESC`
findRandomQuote = `SELECT ` + quoteCols + ` FROM quotes WHERE deleted_at IS NULL AND channel = ? ORDER BY RANDOM() LIMIT 1`
insertQuote = `

11
handlers/quotedb/quotedb.go

@ -5,6 +5,7 @@ import (
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"strings"
"time"
)
@ -85,8 +86,16 @@ func (q *QuoteDB) FindLastQuote(channel string) (*Quote, error) {
return quote, err
}
func escapeLike(text string) string {
text = strings.ReplaceAll(text, `\`, ``)
text = strings.ReplaceAll(text, `%`, `\%`)
text = strings.ReplaceAll(text, `_`, `\_`)
return text
}
func (q QuoteDB) FindQuotesByString(search string, channel string) ([]*Quote, error) {
term := fmt.Sprintf("%%%s%%", search)
term := fmt.Sprintf("%%%s%%", escapeLike(search))
rows, err := q.DB.Query(findQuotesByTerm, channel, term)

Loading…
Cancel
Save