2
0

Add automatic statement cache

This commit is contained in:
Jack Christensen
2019-08-24 20:29:54 -05:00
parent 180dfe6954
commit 0c3e59b07a
7 changed files with 349 additions and 22 deletions
+53
View File
@@ -13,11 +13,13 @@ import (
"github.com/cockroachdb/apd"
"github.com/jackc/pgconn"
"github.com/jackc/pgconn/stmtcache"
"github.com/jackc/pgtype"
satori "github.com/jackc/pgtype/ext/satori-uuid"
"github.com/jackc/pgx/v4"
uuid "github.com/satori/go.uuid"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
errors "golang.org/x/xerrors"
)
@@ -1563,3 +1565,54 @@ func TestConnSimpleProtocolRefusesNonStandardConformingStrings(t *testing.T) {
ensureConnValid(t, conn)
}
func TestQueryPreparedStatementCacheModes(t *testing.T) {
t.Parallel()
config := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE"))
tests := []struct {
name string
buildPreparedStatementCache pgx.BuildPreparedStatementCacheFunc
}{
{
name: "disabled",
buildPreparedStatementCache: nil,
},
{
name: "prepare",
buildPreparedStatementCache: func(conn *pgconn.PgConn) stmtcache.Cache {
return stmtcache.New(conn, stmtcache.ModePrepare, 32)
},
},
{
name: "describe",
buildPreparedStatementCache: func(conn *pgconn.PgConn) stmtcache.Cache {
return stmtcache.New(conn, stmtcache.ModeDescribe, 32)
},
},
}
for _, tt := range tests {
func() {
config.BuildPreparedStatementCache = tt.buildPreparedStatementCache
conn := mustConnect(t, config)
defer closeConn(t, conn)
var n int
err := conn.QueryRow(context.Background(), "select 1").Scan(&n)
assert.NoError(t, err, tt.name)
assert.Equal(t, 1, n, tt.name)
err = conn.QueryRow(context.Background(), "select 2").Scan(&n)
assert.NoError(t, err, tt.name)
assert.Equal(t, 2, n, tt.name)
err = conn.QueryRow(context.Background(), "select 1").Scan(&n)
assert.NoError(t, err, tt.name)
assert.Equal(t, 1, n, tt.name)
ensureConnValid(t, conn)
}()
}
}