github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/dbvendor/placeholder_test.go (about)

     1  package dbvendor
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestQuestion(t *testing.T) {
    11  	sql := "x = ? AND y = ?"
    12  	s, _ := Question.ReplacePlaceholders(sql)
    13  	assert.Equal(t, sql, s)
    14  }
    15  
    16  func TestDollar(t *testing.T) {
    17  	sql := "x = ? AND y = ?"
    18  	s, _ := Dollar.ReplacePlaceholders(sql)
    19  	assert.Equal(t, "x = $1 AND y = $2", s)
    20  }
    21  
    22  func TestColon(t *testing.T) {
    23  	sql := "x = ? AND y = ?"
    24  	s, _ := Colon.ReplacePlaceholders(sql)
    25  	assert.Equal(t, "x = :1 AND y = :2", s)
    26  }
    27  
    28  func TestAtp(t *testing.T) {
    29  	sql := "x = ? AND y = ?"
    30  	s, _ := AtP.ReplacePlaceholders(sql)
    31  	assert.Equal(t, "x = @p1 AND y = @p2", s)
    32  }
    33  
    34  func TestPlaceholders(t *testing.T) {
    35  	assert.Equal(t, Placeholders(2), "?,?")
    36  }
    37  
    38  func TestEscapeDollar(t *testing.T) {
    39  	sql := "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE  \"data\" -> 'tags' ??| array['?'] AND enabled = ?"
    40  	s, _ := Dollar.ReplacePlaceholders(sql)
    41  	assert.Equal(t, "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE  \"data\" -> 'tags' ?| array['$1'] AND enabled = $2", s)
    42  }
    43  
    44  func TestEscapeColon(t *testing.T) {
    45  	sql := "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE  \"data\" -> 'tags' ??| array['?'] AND enabled = ?"
    46  	s, _ := Colon.ReplacePlaceholders(sql)
    47  	assert.Equal(t, "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE  \"data\" -> 'tags' ?| array[':1'] AND enabled = :2", s)
    48  }
    49  
    50  func TestEscapeAtp(t *testing.T) {
    51  	sql := "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE  \"data\" -> 'tags' ??| array['?'] AND enabled = ?"
    52  	s, _ := AtP.ReplacePlaceholders(sql)
    53  	assert.Equal(t, "SELECT uuid, \"data\" #> '{tags}' AS tags FROM nodes WHERE  \"data\" -> 'tags' ?| array['@p1'] AND enabled = @p2", s)
    54  }
    55  
    56  func BenchmarkPlaceholdersArray(b *testing.B) {
    57  	var count = b.N
    58  	placeholders := make([]string, count)
    59  	for i := 0; i < count; i++ {
    60  		placeholders[i] = "?"
    61  	}
    62  	var _ = strings.Join(placeholders, ",")
    63  }
    64  
    65  func BenchmarkPlaceholdersStrings(b *testing.B) {
    66  	Placeholders(b.N)
    67  }