github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/db/db_common/postgres.go (about)

     1  package db_common
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // PgEscapeName escapes strings which will be usaed for Podsdtgres object identifiers
     9  // (table names, column names, schema names)
    10  func PgEscapeName(name string) string {
    11  	// first escape all quotes by prefixing an addition quote
    12  	name = strings.Replace(name, `"`, `""`, -1)
    13  	// now wrap the whole string in quotes
    14  	return fmt.Sprintf(`"%s"`, name)
    15  }
    16  
    17  // PgEscapeString escapes strings which are to be inserted
    18  // use a custom escape tag to avoid chance of clash with the escaped text
    19  // https://medium.com/@lnishada/postgres-dollar-quoting-6d23e4f186ec
    20  func PgEscapeString(str string) string {
    21  	return fmt.Sprintf(`$steampipe_escape$%s$steampipe_escape$`, str)
    22  }
    23  
    24  // PgEscapeSearchPath applies postgres escaping to search path and remove whitespace
    25  func PgEscapeSearchPath(searchPath []string) []string {
    26  	res := make([]string, len(searchPath))
    27  	for idx, path := range searchPath {
    28  		res[idx] = PgEscapeName(strings.TrimSpace(path))
    29  	}
    30  	return res
    31  }