From da9fc85c4404a53f910e2f8210be5add1bc50454 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 24 Aug 2019 20:39:01 -0500 Subject: [PATCH] Rename PreparedStatementDescription to StatementDescription PreparedStatementDescription was too long. It also no longer entirely represents its purpose now that it is also intended for use with described statements. --- pgconn.go | 9 +++++---- stmtcache/lru.go | 8 ++++---- stmtcache/stmtcache.go | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pgconn.go b/pgconn.go index 797080bd..8f3291f1 100644 --- a/pgconn.go +++ b/pgconn.go @@ -517,15 +517,16 @@ func (ct CommandTag) String() string { return string(ct) } -type PreparedStatementDescription struct { +type StatementDescription struct { Name string SQL string ParamOIDs []uint32 Fields []pgproto3.FieldDescription } -// Prepare creates a prepared statement. -func (pgConn *PgConn) Prepare(ctx context.Context, name, sql string, paramOIDs []uint32) (*PreparedStatementDescription, error) { +// Prepare creates a prepared statement. If the name is empty, the anonymous prepared statement will be used. This +// allows Prepare to also to describe statements without creating a server-side prepared statement. +func (pgConn *PgConn) Prepare(ctx context.Context, name, sql string, paramOIDs []uint32) (*StatementDescription, error) { if err := pgConn.lock(); err != nil { return nil, linkErrors(err, ErrNoBytesSent) } @@ -553,7 +554,7 @@ func (pgConn *PgConn) Prepare(ctx context.Context, name, sql string, paramOIDs [ return nil, linkErrors(ctx.Err(), err) } - psd := &PreparedStatementDescription{Name: name, SQL: sql} + psd := &StatementDescription{Name: name, SQL: sql} var parseErr error diff --git a/stmtcache/lru.go b/stmtcache/lru.go index 432a70b4..fff4d0b7 100644 --- a/stmtcache/lru.go +++ b/stmtcache/lru.go @@ -40,10 +40,10 @@ func NewLRU(conn *pgconn.PgConn, mode int, cap int) *LRU { } // Get returns the prepared statement description for sql preparing or describing the sql on the server as needed. -func (c *LRU) Get(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) { +func (c *LRU) Get(ctx context.Context, sql string) (*pgconn.StatementDescription, error) { if el, ok := c.m[sql]; ok { c.l.MoveToFront(el) - return el.Value.(*pgconn.PreparedStatementDescription), nil + return el.Value.(*pgconn.StatementDescription), nil } if c.l.Len() == c.cap { @@ -91,7 +91,7 @@ func (c *LRU) Mode() int { return c.mode } -func (c *LRU) prepare(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) { +func (c *LRU) prepare(ctx context.Context, sql string) (*pgconn.StatementDescription, error) { var name string if c.mode == ModePrepare { name = fmt.Sprintf("%s_%d", c.psNamePrefix, c.prepareCount) @@ -105,7 +105,7 @@ func (c *LRU) removeOldest(ctx context.Context) error { oldest := c.l.Back() c.l.Remove(oldest) if c.mode == ModePrepare { - return c.conn.Exec(ctx, fmt.Sprintf("deallocate %s", oldest.Value.(*pgconn.PreparedStatementDescription).Name)).Close() + return c.conn.Exec(ctx, fmt.Sprintf("deallocate %s", oldest.Value.(*pgconn.StatementDescription).Name)).Close() } return nil } diff --git a/stmtcache/stmtcache.go b/stmtcache/stmtcache.go index 9bedf549..96215799 100644 --- a/stmtcache/stmtcache.go +++ b/stmtcache/stmtcache.go @@ -15,7 +15,7 @@ const ( // Cache prepares and caches prepared statement descriptions. type Cache interface { // Get returns the prepared statement description for sql preparing or describing the sql on the server as needed. - Get(ctx context.Context, sql string) (*pgconn.PreparedStatementDescription, error) + Get(ctx context.Context, sql string) (*pgconn.StatementDescription, error) // Clear removes all entries in the cache. Any prepared statements will be deallocated from the PostgreSQL session. Clear(ctx context.Context) error