Fix randquote for terms with _ or %

main
Nick Thomas 2021-02-25 14:23:34 +00:00
parent 45369b890e
commit 8b2338188b
2 changed files with 11 additions and 2 deletions

View File

@ -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 = `

View File

@ -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)