github.com/pavlo67/common@v0.5.3/common/sqllib/sqllib_pg/wildcards.go (about)

     1  package sqllib_pg
     2  
     3  import (
     4  	"regexp"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/pavlo67/common/common/sqllib"
     9  )
    10  
    11  var _ sqllib.CorrectWildcards = CorrectWildcards
    12  
    13  var reQuestionMark = regexp.MustCompile("\\?")
    14  
    15  func CorrectWildcards(query string) string {
    16  	n := 1
    17  
    18  LOOP:
    19  	loc := reQuestionMark.FindStringIndex(query)
    20  	if len(loc) > 0 {
    21  		query = query[:loc[0]] + "$" + strconv.Itoa(n) + query[loc[1]:]
    22  		n++
    23  
    24  		goto LOOP
    25  	}
    26  
    27  	return query
    28  }
    29  
    30  func WildcardsForUpdate(fields []string) string {
    31  	var wildcards []string
    32  
    33  	for n, f := range fields {
    34  		wildcards = append(wildcards, f+"=$"+strconv.Itoa(n+1))
    35  	}
    36  
    37  	return strings.Join(wildcards, ",")
    38  }
    39  
    40  func WildcardsForInsert(fields []string) string {
    41  	var wildcards []string
    42  	for n := 1; n <= len(fields); n++ {
    43  		wildcards = append(wildcards, "$"+strconv.Itoa(n))
    44  	}
    45  
    46  	return strings.Join(wildcards, ",")
    47  }