github.com/hashicorp/vault/sdk@v0.13.0/database/helper/dbutil/dbutil.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package dbutil 5 6 import ( 7 "errors" 8 "fmt" 9 "strings" 10 11 "github.com/hashicorp/vault/sdk/database/dbplugin" 12 "google.golang.org/grpc/codes" 13 "google.golang.org/grpc/status" 14 ) 15 16 var ( 17 ErrEmptyCreationStatement = errors.New("empty creation statements") 18 ErrEmptyRotationStatement = errors.New("empty rotation statements") 19 ) 20 21 // Query templates a query for us. 22 func QueryHelper(tpl string, data map[string]string) string { 23 for k, v := range data { 24 tpl = strings.ReplaceAll(tpl, fmt.Sprintf("{{%s}}", k), v) 25 } 26 27 return tpl 28 } 29 30 // StatementCompatibilityHelper will populate the statements fields to support 31 // compatibility 32 func StatementCompatibilityHelper(statements dbplugin.Statements) dbplugin.Statements { 33 switch { 34 case len(statements.Creation) > 0 && len(statements.CreationStatements) == 0: 35 statements.CreationStatements = strings.Join(statements.Creation, ";") 36 case len(statements.CreationStatements) > 0: 37 statements.Creation = []string{statements.CreationStatements} 38 } 39 switch { 40 case len(statements.Revocation) > 0 && len(statements.RevocationStatements) == 0: 41 statements.RevocationStatements = strings.Join(statements.Revocation, ";") 42 case len(statements.RevocationStatements) > 0: 43 statements.Revocation = []string{statements.RevocationStatements} 44 } 45 switch { 46 case len(statements.Renewal) > 0 && len(statements.RenewStatements) == 0: 47 statements.RenewStatements = strings.Join(statements.Renewal, ";") 48 case len(statements.RenewStatements) > 0: 49 statements.Renewal = []string{statements.RenewStatements} 50 } 51 switch { 52 case len(statements.Rollback) > 0 && len(statements.RollbackStatements) == 0: 53 statements.RollbackStatements = strings.Join(statements.Rollback, ";") 54 case len(statements.RollbackStatements) > 0: 55 statements.Rollback = []string{statements.RollbackStatements} 56 } 57 return statements 58 } 59 60 // Unimplemented returns a gRPC error with the Unimplemented code 61 func Unimplemented() error { 62 return status.Error(codes.Unimplemented, "Not yet implemented") 63 }