Switch web query to a regex

This commit is contained in:
Chris Sexton 2016-03-29 12:34:04 -04:00
parent 9d9771c097
commit a34afa97ad
2 changed files with 21 additions and 5 deletions

View File

@ -7,13 +7,13 @@ import (
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"regexp"
"strings" "strings"
"time" "time"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/mattn/go-sqlite3"
"github.com/velour/catbase/config" "github.com/velour/catbase/config"
_ "github.com/mattn/go-sqlite3"
) )
// Bot type provides storage for bot-wide information, configs, and database connections // Bot type provides storage for bot-wide information, configs, and database connections
@ -97,9 +97,21 @@ type Variable struct {
Variable, Value string Variable, Value string
} }
func init() {
regex := func(re, s string) (bool, error) {
return regexp.MatchString(re, s)
}
sql.Register("sqlite3_custom",
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
return conn.RegisterFunc("REGEXP", regex, true)
},
})
}
// NewBot creates a Bot for a given connection and set of handlers. // NewBot creates a Bot for a given connection and set of handlers.
func NewBot(config *config.Config, connector Connector) *Bot { func NewBot(config *config.Config, connector Connector) *Bot {
sqlDB, err := sqlx.Open("sqlite3", config.DB.File) sqlDB, err := sqlx.Open("sqlite3_custom", config.DB.File)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -104,8 +104,12 @@ func getFacts(db *sqlx.DB, fact string) ([]*factoid, error) {
accessed, accessed,
count count
from factoid from factoid
where fact like ?;`, where fact regexp ?;`,
fmt.Sprintf("%%%s%%", fact)) fact)
if err != nil {
log.Printf("Error regexping for facts: %s", err)
return nil, err
}
for rows.Next() { for rows.Next() {
var f factoid var f factoid
var tmpCreated int64 var tmpCreated int64