Пример #1
0
func TestInsertDoubleDollarQuote(t *testing.T) {
	s := beginTxWithFixtures()
	defer s.AutoRollback()

	expected := common.RandomString(16)
	var str string
	err := s.
		InsertInto("people").
		Columns("name", "key").
		Values("test", expected).
		Returning("key").
		QueryScalar(&str)
	assert.NoError(t, err)
	assert.Equal(t, expected, str)

	// ensure the tag cannot be escaped by user
	oldDollarTag := postgres.GetPgDollarTag()
	expected = common.RandomString(1024) + "'" + oldDollarTag
	builder := s.
		InsertInto("people").
		Columns("name", "key").
		Values("test", expected).
		Returning("key")

	sql, _, _ := builder.SetIsInterpolated(true).Interpolate()
	assert.NotEqual(t, oldDollarTag, postgres.GetPgDollarTag())
	assert.True(t, strings.Contains(sql, postgres.GetPgDollarTag()))

	builder.QueryScalar(&str)
	assert.NoError(t, err)
	assert.Equal(t, expected, str)
}
Пример #2
0
func randomizePgDollarTag() {
	pgDollarMutex.Lock()
	defer pgDollarMutex.Unlock()
	var buf bytes.Buffer
	buf.WriteRune('$')
	buf.WriteString(common.RandomString(3))
	buf.WriteRune('$')
	pgDollarTag = buf.String()
}