github.com/mad-app/mattermost-server@v5.11.1+incompatible/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 "bytes" 8 "database/sql" 9 "fmt" 10 "strconv" 11 12 "github.com/mattermost/gorp" 13 "github.com/mattermost/mattermost-server/mlog" 14 ) 15 16 // Converts a list of strings into a list of query parameters and a named parameter map that can 17 // be used as part of a SQL query. 18 func MapStringsToQueryParams(list []string, paramPrefix string) (string, map[string]interface{}) { 19 keys := bytes.Buffer{} 20 params := make(map[string]interface{}, len(list)) 21 for i, entry := range list { 22 if keys.Len() > 0 { 23 keys.WriteString(",") 24 } 25 26 key := paramPrefix + strconv.Itoa(i) 27 keys.WriteString(":" + key) 28 params[key] = entry 29 } 30 31 return fmt.Sprintf("(%v)", keys.String()), params 32 } 33 34 // finalizeTransaction ensures a transaction is closed after use, rolling back if not already committed. 35 func finalizeTransaction(transaction *gorp.Transaction) { 36 // Rollback returns sql.ErrTxDone if the transaction was already closed. 37 if err := transaction.Rollback(); err != nil && err != sql.ErrTxDone { 38 mlog.Error("Failed to rollback transaction", mlog.Err(err)) 39 } 40 }