github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/store/sqlstore/utils.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package sqlstore
     5  
     6  import (
     7  	"database/sql"
     8  	"strconv"
     9  	"strings"
    10  	"unicode"
    11  
    12  	"github.com/mattermost/gorp"
    13  
    14  	"github.com/mattermost/mattermost-server/v5/mlog"
    15  )
    16  
    17  var escapeLikeSearchChar = []string{
    18  	"%",
    19  	"_",
    20  }
    21  
    22  func sanitizeSearchTerm(term string, escapeChar string) string {
    23  	term = strings.Replace(term, escapeChar, "", -1)
    24  
    25  	for _, c := range escapeLikeSearchChar {
    26  		term = strings.Replace(term, c, escapeChar+c, -1)
    27  	}
    28  
    29  	return term
    30  }
    31  
    32  // Converts a list of strings into a list of query parameters and a named parameter map that can
    33  // be used as part of a SQL query.
    34  func MapStringsToQueryParams(list []string, paramPrefix string) (string, map[string]interface{}) {
    35  	var keys strings.Builder
    36  	params := make(map[string]interface{}, len(list))
    37  	for i, entry := range list {
    38  		if keys.Len() > 0 {
    39  			keys.WriteString(",")
    40  		}
    41  
    42  		key := paramPrefix + strconv.Itoa(i)
    43  		keys.WriteString(":" + key)
    44  		params[key] = entry
    45  	}
    46  
    47  	return "(" + keys.String() + ")", params
    48  }
    49  
    50  // finalizeTransaction ensures a transaction is closed after use, rolling back if not already committed.
    51  func finalizeTransaction(transaction *gorp.Transaction) {
    52  	// Rollback returns sql.ErrTxDone if the transaction was already closed.
    53  	if err := transaction.Rollback(); err != nil && err != sql.ErrTxDone {
    54  		mlog.Error("Failed to rollback transaction", mlog.Err(err))
    55  	}
    56  }
    57  
    58  // removeNonAlphaNumericUnquotedTerms removes all unquoted words that only contain
    59  // non-alphanumeric chars from given line
    60  func removeNonAlphaNumericUnquotedTerms(line, separator string) string {
    61  	words := strings.Split(line, separator)
    62  	filteredResult := make([]string, 0, len(words))
    63  
    64  	for _, w := range words {
    65  		if isQuotedWord(w) || containsAlphaNumericChar(w) {
    66  			filteredResult = append(filteredResult, strings.TrimSpace(w))
    67  		}
    68  	}
    69  	return strings.Join(filteredResult, separator)
    70  }
    71  
    72  // containsAlphaNumericChar returns true in case any letter or digit is present, false otherwise
    73  func containsAlphaNumericChar(s string) bool {
    74  	for _, r := range s {
    75  		if unicode.IsLetter(r) || unicode.IsDigit(r) {
    76  			return true
    77  		}
    78  	}
    79  	return false
    80  }
    81  
    82  // isQuotedWord return true if the input string is quoted, false otherwise. Ex :-
    83  // 		"quoted string"  -  will return true
    84  // 		unquoted string  -  will return false
    85  func isQuotedWord(s string) bool {
    86  	if len(s) < 2 {
    87  		return false
    88  	}
    89  
    90  	return s[0] == '"' && s[len(s)-1] == '"'
    91  }