Fix postgres query for search

This commit is contained in:
Torjus Håkestad 2021-04-14 17:55:44 +02:00
parent a2ffbad4a3
commit 82d07eaaf4

View File

@ -161,6 +161,7 @@ func (s *PostgresStore) statsTotal(limit int) ([]StatsResult, error) {
func (s *PostgresStore) Query(query AttemptQuery) ([]models.LoginAttempt, error) { func (s *PostgresStore) Query(query AttemptQuery) ([]models.LoginAttempt, error) {
var stmt string var stmt string
queryString := query.Query
switch query.QueryType { switch query.QueryType {
case AttemptQueryTypeIP: case AttemptQueryTypeIP:
@ -168,15 +169,17 @@ func (s *PostgresStore) Query(query AttemptQuery) ([]models.LoginAttempt, error)
FROM login_attempts WHERE remote_ip = $1` FROM login_attempts WHERE remote_ip = $1`
case AttemptQueryTypePassword: case AttemptQueryTypePassword:
stmt = `SELECT id, date, remote_ip, username, password, client_version, connection_uuid, country stmt = `SELECT id, date, remote_ip, username, password, client_version, connection_uuid, country
FROM login_attempts WHERE password like '%$1%'` FROM login_attempts WHERE password like $1`
queryString = fmt.Sprintf("%%%s%%", queryString)
case AttemptQueryTypeUsername: case AttemptQueryTypeUsername:
stmt = `SELECT id, date, remote_ip, username, password, client_version, connection_uuid, country stmt = `SELECT id, date, remote_ip, username, password, client_version, connection_uuid, country
FROM login_attempts WHERE username like '%$1%'` FROM login_attempts WHERE username like $1`
queryString = fmt.Sprintf("%%%s%%", queryString)
default: default:
return nil, fmt.Errorf("Invalid query type") return nil, fmt.Errorf("Invalid query type")
} }
rows, err := s.db.Query(stmt, query.Query) rows, err := s.db.Query(stmt, queryString)
if err != nil { if err != nil {
return nil, fmt.Errorf("Unable to query database: %w", err) return nil, fmt.Errorf("Unable to query database: %w", err)
} }