settings section
This commit is contained in:
24
vendor/github.com/jackc/pgx/v4/.gitignore
generated
vendored
Normal file
24
vendor/github.com/jackc/pgx/v4/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
|
||||
.envrc
|
||||
311
vendor/github.com/jackc/pgx/v4/CHANGELOG.md
generated
vendored
Normal file
311
vendor/github.com/jackc/pgx/v4/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,311 @@
|
||||
# 4.18.3 (March 9, 2024)
|
||||
|
||||
Use spaces instead of parentheses for SQL sanitization.
|
||||
|
||||
This still solves the problem of negative numbers creating a line comment, but this avoids breaking edge cases such as
|
||||
`set foo to $1` where the substitution is taking place in a location where an arbitrary expression is not allowed.
|
||||
|
||||
# 4.18.2 (March 4, 2024)
|
||||
|
||||
Fix CVE-2024-27289
|
||||
|
||||
SQL injection can occur when all of the following conditions are met:
|
||||
|
||||
1. The non-default simple protocol is used.
|
||||
2. A placeholder for a numeric value must be immediately preceded by a minus.
|
||||
3. There must be a second placeholder for a string value after the first placeholder; both must be on the same line.
|
||||
4. Both parameter values must be user-controlled.
|
||||
|
||||
Thanks to Paul Gerste for reporting this issue.
|
||||
|
||||
Fix CVE-2024-27304
|
||||
|
||||
SQL injection can occur if an attacker can cause a single query or bind message to exceed 4 GB in size. An integer
|
||||
overflow in the calculated message size can cause the one large message to be sent as multiple messages under the
|
||||
attacker's control.
|
||||
|
||||
Thanks to Paul Gerste for reporting this issue.
|
||||
|
||||
* Fix *dbTx.Exec not checking if it is already closed
|
||||
|
||||
# 4.18.1 (February 27, 2023)
|
||||
|
||||
* Fix: Support pgx v4 and v5 stdlib in same program (Tomáš Procházka)
|
||||
|
||||
# 4.18.0 (February 11, 2023)
|
||||
|
||||
* Upgrade pgconn to v1.14.0
|
||||
* Upgrade pgproto3 to v2.3.2
|
||||
* Upgrade pgtype to v1.14.0
|
||||
* Fix query sanitizer when query text contains Unicode replacement character
|
||||
* Fix context with value in BeforeConnect (David Harju)
|
||||
* Support pgx v4 and v5 stdlib in same program (Vitalii Solodilov)
|
||||
|
||||
# 4.17.2 (September 3, 2022)
|
||||
|
||||
* Fix panic when logging batch error (Tom Möller)
|
||||
|
||||
# 4.17.1 (August 27, 2022)
|
||||
|
||||
* Upgrade puddle to v1.3.0 - fixes context failing to cancel Acquire when acquire is creating resource which was introduced in v4.17.0 (James Hartig)
|
||||
* Fix atomic alignment on 32-bit platforms
|
||||
|
||||
# 4.17.0 (August 6, 2022)
|
||||
|
||||
* Upgrade pgconn to v1.13.0
|
||||
* Upgrade pgproto3 to v2.3.1
|
||||
* Upgrade pgtype to v1.12.0
|
||||
* Allow background pool connections to continue even if cause is canceled (James Hartig)
|
||||
* Add LoggerFunc (Gabor Szabad)
|
||||
* pgxpool: health check should avoid going below minConns (James Hartig)
|
||||
* Add pgxpool.Conn.Hijack()
|
||||
* Logging improvements (Stepan Rabotkin)
|
||||
|
||||
# 4.16.1 (May 7, 2022)
|
||||
|
||||
* Upgrade pgconn to v1.12.1
|
||||
* Fix explicitly prepared statements with describe statement cache mode
|
||||
|
||||
# 4.16.0 (April 21, 2022)
|
||||
|
||||
* Upgrade pgconn to v1.12.0
|
||||
* Upgrade pgproto3 to v2.3.0
|
||||
* Upgrade pgtype to v1.11.0
|
||||
* Fix: Do not panic when context cancelled while getting statement from cache.
|
||||
* Fix: Less memory pinning from old Rows.
|
||||
* Fix: Support '\r' line ending when sanitizing SQL comment.
|
||||
* Add pluggable GSSAPI support (Oliver Tan)
|
||||
|
||||
# 4.15.0 (February 7, 2022)
|
||||
|
||||
* Upgrade to pgconn v1.11.0
|
||||
* Upgrade to pgtype v1.10.0
|
||||
* Upgrade puddle to v1.2.1
|
||||
* Make BatchResults.Close safe to be called multiple times
|
||||
|
||||
# 4.14.1 (November 28, 2021)
|
||||
|
||||
* Upgrade pgtype to v1.9.1 (fixes unintentional change to timestamp binary decoding)
|
||||
* Start pgxpool background health check after initial connections
|
||||
|
||||
# 4.14.0 (November 20, 2021)
|
||||
|
||||
* Upgrade pgconn to v1.10.1
|
||||
* Upgrade pgproto3 to v2.2.0
|
||||
* Upgrade pgtype to v1.9.0
|
||||
* Upgrade puddle to v1.2.0
|
||||
* Add QueryFunc to BatchResults
|
||||
* Add context options to zerologadapter (Thomas Frössman)
|
||||
* Add zerologadapter.NewContextLogger (urso)
|
||||
* Eager initialize minpoolsize on connect (Daniel)
|
||||
* Unpin memory used by large queries immediately after use
|
||||
|
||||
# 4.13.0 (July 24, 2021)
|
||||
|
||||
* Trimmed pseudo-dependencies in Go modules from other packages tests
|
||||
* Upgrade pgconn -- context cancellation no longer will return a net.Error
|
||||
* Support time durations for simple protocol (Michael Darr)
|
||||
|
||||
# 4.12.0 (July 10, 2021)
|
||||
|
||||
* ResetSession hook is called before a connection is reused from pool for another query (Dmytro Haranzha)
|
||||
* stdlib: Add RandomizeHostOrderFunc (dkinder)
|
||||
* stdlib: add OptionBeforeConnect (dkinder)
|
||||
* stdlib: Do not reuse ConnConfig strings (Andrew Kimball)
|
||||
* stdlib: implement Conn.ResetSession (Jonathan Amsterdam)
|
||||
* Upgrade pgconn to v1.9.0
|
||||
* Upgrade pgtype to v1.8.0
|
||||
|
||||
# 4.11.0 (March 25, 2021)
|
||||
|
||||
* Add BeforeConnect callback to pgxpool.Config (Robert Froehlich)
|
||||
* Add Ping method to pgxpool.Conn (davidsbond)
|
||||
* Added a kitlog level log adapter (Fabrice Aneche)
|
||||
* Make ScanArgError public to allow identification of offending column (Pau Sanchez)
|
||||
* Add *pgxpool.AcquireFunc
|
||||
* Add BeginFunc and BeginTxFunc
|
||||
* Add prefer_simple_protocol to connection string
|
||||
* Add logging on CopyFrom (Patrick Hemmer)
|
||||
* Add comment support when sanitizing SQL queries (Rusakow Andrew)
|
||||
* Do not panic on double close of pgxpool.Pool (Matt Schultz)
|
||||
* Avoid panic on SendBatch on closed Tx (Matt Schultz)
|
||||
* Update pgconn to v1.8.1
|
||||
* Update pgtype to v1.7.0
|
||||
|
||||
# 4.10.1 (December 19, 2020)
|
||||
|
||||
* Fix panic on Query error with nil stmtcache.
|
||||
|
||||
# 4.10.0 (December 3, 2020)
|
||||
|
||||
* Add CopyFromSlice to simplify CopyFrom usage (Egon Elbre)
|
||||
* Remove broken prepared statements from stmtcache (Ethan Pailes)
|
||||
* stdlib: consider any Ping error as fatal
|
||||
* Update puddle to v1.1.3 - this fixes an issue where concurrent Acquires can hang when a connection cannot be established
|
||||
* Update pgtype to v1.6.2
|
||||
|
||||
# 4.9.2 (November 3, 2020)
|
||||
|
||||
The underlying library updates fix an issue where appending to a scanned slice could corrupt other data.
|
||||
|
||||
* Update pgconn to v1.7.2
|
||||
* Update pgproto3 to v2.0.6
|
||||
|
||||
# 4.9.1 (October 31, 2020)
|
||||
|
||||
* Update pgconn to v1.7.1
|
||||
* Update pgtype to v1.6.1
|
||||
* Fix SendBatch of all prepared statements with statement cache disabled
|
||||
|
||||
# 4.9.0 (September 26, 2020)
|
||||
|
||||
* pgxpool now waits for connection cleanup to finish before making room in pool for another connection. This prevents temporarily exceeding max pool size.
|
||||
* Fix when scanning a column to nil to skip it on the first row but scanning it to a real value on a subsequent row.
|
||||
* Fix prefer simple protocol with prepared statements. (Jinzhu)
|
||||
* Fix FieldDescriptions not being available on Rows before calling Next the first time.
|
||||
* Various minor fixes in updated versions of pgconn, pgtype, and puddle.
|
||||
|
||||
# 4.8.1 (July 29, 2020)
|
||||
|
||||
* Update pgconn to v1.6.4
|
||||
* Fix deadlock on error after CommandComplete but before ReadyForQuery
|
||||
* Fix panic on parsing DSN with trailing '='
|
||||
|
||||
# 4.8.0 (July 22, 2020)
|
||||
|
||||
* All argument types supported by native pgx should now also work through database/sql
|
||||
* Update pgconn to v1.6.3
|
||||
* Update pgtype to v1.4.2
|
||||
|
||||
# 4.7.2 (July 14, 2020)
|
||||
|
||||
* Improve performance of Columns() (zikaeroh)
|
||||
* Fix fatal Commit() failure not being considered fatal
|
||||
* Update pgconn to v1.6.2
|
||||
* Update pgtype to v1.4.1
|
||||
|
||||
# 4.7.1 (June 29, 2020)
|
||||
|
||||
* Fix stdlib decoding error with certain order and combination of fields
|
||||
|
||||
# 4.7.0 (June 27, 2020)
|
||||
|
||||
* Update pgtype to v1.4.0
|
||||
* Update pgconn to v1.6.1
|
||||
* Update puddle to v1.1.1
|
||||
* Fix context propagation with Tx commit and Rollback (georgysavva)
|
||||
* Add lazy connect option to pgxpool (georgysavva)
|
||||
* Fix connection leak if pgxpool.BeginTx() fail (Jean-Baptiste Bronisz)
|
||||
* Add native Go slice support for strings and numbers to simple protocol
|
||||
* stdlib add default timeouts for Conn.Close() and Stmt.Close() (georgysavva)
|
||||
* Assorted performance improvements especially with large result sets
|
||||
* Fix close pool on not lazy connect failure (Yegor Myskin)
|
||||
* Add Config copy (georgysavva)
|
||||
* Support SendBatch with Simple Protocol (Jordan Lewis)
|
||||
* Better error logging on rows close (Igor V. Kozinov)
|
||||
* Expose stdlib.Conn.Conn() to enable database/sql.Conn.Raw()
|
||||
* Improve unknown type support for database/sql
|
||||
* Fix transaction commit failure closing connection
|
||||
|
||||
# 4.6.0 (March 30, 2020)
|
||||
|
||||
* stdlib: Bail early if preloading rows.Next() results in rows.Err() (Bas van Beek)
|
||||
* Sanitize time to microsecond accuracy (Andrew Nicoll)
|
||||
* Update pgtype to v1.3.0
|
||||
* Update pgconn to v1.5.0
|
||||
* Update golang.org/x/crypto for security fix
|
||||
* Implement "verify-ca" SSL mode
|
||||
|
||||
# 4.5.0 (March 7, 2020)
|
||||
|
||||
* Update to pgconn v1.4.0
|
||||
* Fixes QueryRow with empty SQL
|
||||
* Adds PostgreSQL service file support
|
||||
* Add Len() to *pgx.Batch (WGH)
|
||||
* Better logging for individual batch items (Ben Bader)
|
||||
|
||||
# 4.4.1 (February 14, 2020)
|
||||
|
||||
* Update pgconn to v1.3.2 - better default read buffer size
|
||||
* Fix race in CopyFrom
|
||||
|
||||
# 4.4.0 (February 5, 2020)
|
||||
|
||||
* Update puddle to v1.1.0 - fixes possible deadlock when acquire is cancelled
|
||||
* Update pgconn to v1.3.1 - fixes CopyFrom deadlock when multiple NoticeResponse received during copy
|
||||
* Update pgtype to v1.2.0
|
||||
* Add MaxConnIdleTime to pgxpool (Patrick Ellul)
|
||||
* Add MinConns to pgxpool (Patrick Ellul)
|
||||
* Fix: stdlib.ReleaseConn closes connections left in invalid state
|
||||
|
||||
# 4.3.0 (January 23, 2020)
|
||||
|
||||
* Fix Rows.Values panic when unable to decode
|
||||
* Add Rows.Values support for unknown types
|
||||
* Add DriverContext support for stdlib (Alex Gaynor)
|
||||
* Update pgproto3 to v2.0.1 to never return an io.EOF as it would be misinterpreted by database/sql. Instead return io.UnexpectedEOF.
|
||||
|
||||
# 4.2.1 (January 13, 2020)
|
||||
|
||||
* Update pgconn to v1.2.1 (fixes context cancellation data race introduced in v1.2.0))
|
||||
|
||||
# 4.2.0 (January 11, 2020)
|
||||
|
||||
* Update pgconn to v1.2.0.
|
||||
* Update pgtype to v1.1.0.
|
||||
* Return error instead of panic when wrong number of arguments passed to Exec. (malstoun)
|
||||
* Fix large objects functionality when PreferSimpleProtocol = true.
|
||||
* Restore GetDefaultDriver which existed in v3. (Johan Brandhorst)
|
||||
* Add RegisterConnConfig to stdlib which replaces the removed RegisterDriverConfig from v3.
|
||||
|
||||
# 4.1.2 (October 22, 2019)
|
||||
|
||||
* Fix dbSavepoint.Begin recursive self call
|
||||
* Upgrade pgtype to v1.0.2 - fix scan pointer to pointer
|
||||
|
||||
# 4.1.1 (October 21, 2019)
|
||||
|
||||
* Fix pgxpool Rows.CommandTag() infinite loop / typo
|
||||
|
||||
# 4.1.0 (October 12, 2019)
|
||||
|
||||
## Potentially Breaking Changes
|
||||
|
||||
Technically, two changes are breaking changes, but in practice these are extremely unlikely to break existing code.
|
||||
|
||||
* Conn.Begin and Conn.BeginTx return a Tx interface instead of the internal dbTx struct. This is necessary for the Conn.Begin method to signature as other methods that begin a transaction.
|
||||
* Add Conn() to Tx interface. This is necessary to allow code using a Tx to access the *Conn (and pgconn.PgConn) on which the Tx is executing.
|
||||
|
||||
## Fixes
|
||||
|
||||
* Releasing a busy connection closes the connection instead of returning an unusable connection to the pool
|
||||
* Do not mutate config.Config.OnNotification in connect
|
||||
|
||||
# 4.0.1 (September 19, 2019)
|
||||
|
||||
* Fix statement cache cleanup.
|
||||
* Corrected daterange OID.
|
||||
* Fix Tx when committing or rolling back multiple times in certain cases.
|
||||
* Improve documentation.
|
||||
|
||||
# 4.0.0 (September 14, 2019)
|
||||
|
||||
v4 is a major release with many significant changes some of which are breaking changes. The most significant are
|
||||
included below.
|
||||
|
||||
* Simplified establishing a connection with a connection string.
|
||||
* All potentially blocking operations now require a context.Context. The non-context aware functions have been removed.
|
||||
* OIDs are hard-coded for known types. This saves the query on connection.
|
||||
* Context cancellations while network activity is in progress is now always fatal. Previously, it was sometimes recoverable. This led to increased complexity in pgx itself and in application code.
|
||||
* Go modules are required.
|
||||
* Errors are now implemented in the Go 1.13 style.
|
||||
* `Rows` and `Tx` are now interfaces.
|
||||
* The connection pool as been decoupled from pgx and is now a separate, included package (github.com/jackc/pgx/v4/pgxpool).
|
||||
* pgtype has been spun off to a separate package (github.com/jackc/pgtype).
|
||||
* pgproto3 has been spun off to a separate package (github.com/jackc/pgproto3/v2).
|
||||
* Logical replication support has been spun off to a separate package (github.com/jackc/pglogrepl).
|
||||
* Lower level PostgreSQL functionality is now implemented in a separate package (github.com/jackc/pgconn).
|
||||
* Tests are now configured with environment variables.
|
||||
* Conn has an automatic statement cache by default.
|
||||
* Batch interface has been simplified.
|
||||
* QueryArgs has been removed.
|
||||
22
vendor/github.com/jackc/pgx/v4/LICENSE
generated
vendored
Normal file
22
vendor/github.com/jackc/pgx/v4/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
Copyright (c) 2013-2021 Jack Christensen
|
||||
|
||||
MIT License
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
196
vendor/github.com/jackc/pgx/v4/README.md
generated
vendored
Normal file
196
vendor/github.com/jackc/pgx/v4/README.md
generated
vendored
Normal file
@@ -0,0 +1,196 @@
|
||||
[](https://pkg.go.dev/github.com/jackc/pgx/v4)
|
||||
[](https://travis-ci.org/jackc/pgx)
|
||||
|
||||
---
|
||||
|
||||
This is the previous stable `v4` release. `v5` been released.
|
||||
|
||||
---
|
||||
# pgx - PostgreSQL Driver and Toolkit
|
||||
|
||||
pgx is a pure Go driver and toolkit for PostgreSQL.
|
||||
|
||||
pgx aims to be low-level, fast, and performant, while also enabling PostgreSQL-specific features that the standard `database/sql` package does not allow for.
|
||||
|
||||
The driver component of pgx can be used alongside the standard `database/sql` package.
|
||||
|
||||
The toolkit component is a related set of packages that implement PostgreSQL functionality such as parsing the wire protocol
|
||||
and type mapping between PostgreSQL and Go. These underlying packages can be used to implement alternative drivers,
|
||||
proxies, load balancers, logical replication clients, etc.
|
||||
|
||||
The current release of `pgx v4` requires Go modules. To use the previous version, checkout and vendor the `v3` branch.
|
||||
|
||||
## Example Usage
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// urlExample := "postgres://username:password@localhost:5432/database_name"
|
||||
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer conn.Close(context.Background())
|
||||
|
||||
var name string
|
||||
var weight int64
|
||||
err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(name, weight)
|
||||
}
|
||||
```
|
||||
|
||||
See the [getting started guide](https://github.com/jackc/pgx/wiki/Getting-started-with-pgx) for more information.
|
||||
|
||||
## Choosing Between the pgx and database/sql Interfaces
|
||||
|
||||
It is recommended to use the pgx interface if:
|
||||
1. The application only targets PostgreSQL.
|
||||
2. No other libraries that require `database/sql` are in use.
|
||||
|
||||
The pgx interface is faster and exposes more features.
|
||||
|
||||
The `database/sql` interface only allows the underlying driver to return or receive the following types: `int64`,
|
||||
`float64`, `bool`, `[]byte`, `string`, `time.Time`, or `nil`. Handling other types requires implementing the
|
||||
`database/sql.Scanner` and the `database/sql/driver/driver.Valuer` interfaces which require transmission of values in text format. The binary format can be substantially faster, which is what the pgx interface uses.
|
||||
|
||||
## Features
|
||||
|
||||
pgx supports many features beyond what is available through `database/sql`:
|
||||
|
||||
* Support for approximately 70 different PostgreSQL types
|
||||
* Automatic statement preparation and caching
|
||||
* Batch queries
|
||||
* Single-round trip query mode
|
||||
* Full TLS connection control
|
||||
* Binary format support for custom types (allows for much quicker encoding/decoding)
|
||||
* COPY protocol support for faster bulk data loads
|
||||
* Extendable logging support including built-in support for `log15adapter`, [`logrus`](https://github.com/sirupsen/logrus), [`zap`](https://github.com/uber-go/zap), and [`zerolog`](https://github.com/rs/zerolog)
|
||||
* Connection pool with after-connect hook for arbitrary connection setup
|
||||
* Listen / notify
|
||||
* Conversion of PostgreSQL arrays to Go slice mappings for integers, floats, and strings
|
||||
* Hstore support
|
||||
* JSON and JSONB support
|
||||
* Maps `inet` and `cidr` PostgreSQL types to `net.IPNet` and `net.IP`
|
||||
* Large object support
|
||||
* NULL mapping to Null* struct or pointer to pointer
|
||||
* Supports `database/sql.Scanner` and `database/sql/driver.Valuer` interfaces for custom types
|
||||
* Notice response handling
|
||||
* Simulated nested transactions with savepoints
|
||||
|
||||
## Performance
|
||||
|
||||
There are three areas in particular where pgx can provide a significant performance advantage over the standard
|
||||
`database/sql` interface and other drivers:
|
||||
|
||||
1. PostgreSQL specific types - Types such as arrays can be parsed much quicker because pgx uses the binary format.
|
||||
2. Automatic statement preparation and caching - pgx will prepare and cache statements by default. This can provide an
|
||||
significant free improvement to code that does not explicitly use prepared statements. Under certain workloads, it can
|
||||
perform nearly 3x the number of queries per second.
|
||||
3. Batched queries - Multiple queries can be batched together to minimize network round trips.
|
||||
|
||||
## Testing
|
||||
|
||||
pgx tests naturally require a PostgreSQL database. It will connect to the database specified in the `PGX_TEST_DATABASE` environment
|
||||
variable. The `PGX_TEST_DATABASE` environment variable can either be a URL or DSN. In addition, the standard `PG*` environment
|
||||
variables will be respected. Consider using [direnv](https://github.com/direnv/direnv) to simplify environment variable
|
||||
handling.
|
||||
|
||||
### Example Test Environment
|
||||
|
||||
Connect to your PostgreSQL server and run:
|
||||
|
||||
```
|
||||
create database pgx_test;
|
||||
```
|
||||
|
||||
Connect to the newly-created database and run:
|
||||
|
||||
```
|
||||
create domain uint64 as numeric(20,0);
|
||||
```
|
||||
|
||||
Now, you can run the tests:
|
||||
|
||||
```
|
||||
PGX_TEST_DATABASE="host=/var/run/postgresql database=pgx_test" go test ./...
|
||||
```
|
||||
|
||||
In addition, there are tests specific for PgBouncer that will be executed if `PGX_TEST_PGBOUNCER_CONN_STRING` is set.
|
||||
|
||||
## Supported Go and PostgreSQL Versions
|
||||
|
||||
pgx supports the same versions of Go and PostgreSQL that are supported by their respective teams. For [Go](https://golang.org/doc/devel/release.html#policy) that is the two most recent major releases and for [PostgreSQL](https://www.postgresql.org/support/versioning/) the major releases in the last 5 years. This means pgx supports Go 1.17 and higher and PostgreSQL 10 and higher. pgx also is tested against the latest version of [CockroachDB](https://www.cockroachlabs.com/product/).
|
||||
|
||||
## Version Policy
|
||||
|
||||
pgx follows semantic versioning for the documented public API on stable releases. `v4` is the latest stable major version.
|
||||
|
||||
## PGX Family Libraries
|
||||
|
||||
pgx is the head of a family of PostgreSQL libraries. Many of these can be used independently. Many can also be accessed
|
||||
from pgx for lower-level control.
|
||||
|
||||
### [github.com/jackc/pgconn](https://github.com/jackc/pgconn)
|
||||
|
||||
`pgconn` is a lower-level PostgreSQL database driver that operates at nearly the same level as the C library `libpq`.
|
||||
|
||||
### [github.com/jackc/pgx/v4/pgxpool](https://github.com/jackc/pgx/tree/master/pgxpool)
|
||||
|
||||
`pgxpool` is a connection pool for pgx. pgx is entirely decoupled from its default pool implementation. This means that pgx can be used with a different pool or without any pool at all.
|
||||
|
||||
### [github.com/jackc/pgx/v4/stdlib](https://github.com/jackc/pgx/tree/master/stdlib)
|
||||
|
||||
This is a `database/sql` compatibility layer for pgx. pgx can be used as a normal `database/sql` driver, but at any time, the native interface can be acquired for more performance or PostgreSQL specific functionality.
|
||||
|
||||
### [github.com/jackc/pgtype](https://github.com/jackc/pgtype)
|
||||
|
||||
Over 70 PostgreSQL types are supported including `uuid`, `hstore`, `json`, `bytea`, `numeric`, `interval`, `inet`, and arrays. These types support `database/sql` interfaces and are usable outside of pgx. They are fully tested in pgx and pq. They also support a higher performance interface when used with the pgx driver.
|
||||
|
||||
### [github.com/jackc/pgproto3](https://github.com/jackc/pgproto3)
|
||||
|
||||
pgproto3 provides standalone encoding and decoding of the PostgreSQL v3 wire protocol. This is useful for implementing very low level PostgreSQL tooling.
|
||||
|
||||
### [github.com/jackc/pglogrepl](https://github.com/jackc/pglogrepl)
|
||||
|
||||
pglogrepl provides functionality to act as a client for PostgreSQL logical replication.
|
||||
|
||||
### [github.com/jackc/pgmock](https://github.com/jackc/pgmock)
|
||||
|
||||
pgmock offers the ability to create a server that mocks the PostgreSQL wire protocol. This is used internally to test pgx by purposely inducing unusual errors. pgproto3 and pgmock together provide most of the foundational tooling required to implement a PostgreSQL proxy or MitM (such as for a custom connection pooler).
|
||||
|
||||
### [github.com/jackc/tern](https://github.com/jackc/tern)
|
||||
|
||||
tern is a stand-alone SQL migration system.
|
||||
|
||||
### [github.com/jackc/pgerrcode](https://github.com/jackc/pgerrcode)
|
||||
|
||||
pgerrcode contains constants for the PostgreSQL error codes.
|
||||
|
||||
## 3rd Party Libraries with PGX Support
|
||||
|
||||
### [github.com/georgysavva/scany](https://github.com/georgysavva/scany)
|
||||
|
||||
Library for scanning data from a database into Go structs and more.
|
||||
|
||||
### [https://github.com/otan/gopgkrb5](https://github.com/otan/gopgkrb5)
|
||||
|
||||
Adds GSSAPI / Kerberos authentication support.
|
||||
|
||||
### [https://github.com/vgarvardt/pgx-google-uuid](https://github.com/vgarvardt/pgx-google-uuid)
|
||||
|
||||
Adds support for [`github.com/google/uuid`](https://github.com/google/uuid).
|
||||
228
vendor/github.com/jackc/pgx/v4/batch.go
generated
vendored
Normal file
228
vendor/github.com/jackc/pgx/v4/batch.go
generated
vendored
Normal file
@@ -0,0 +1,228 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
)
|
||||
|
||||
type batchItem struct {
|
||||
query string
|
||||
arguments []interface{}
|
||||
}
|
||||
|
||||
// Batch queries are a way of bundling multiple queries together to avoid
|
||||
// unnecessary network round trips.
|
||||
type Batch struct {
|
||||
items []*batchItem
|
||||
}
|
||||
|
||||
// Queue queues a query to batch b. query can be an SQL query or the name of a prepared statement.
|
||||
func (b *Batch) Queue(query string, arguments ...interface{}) {
|
||||
b.items = append(b.items, &batchItem{
|
||||
query: query,
|
||||
arguments: arguments,
|
||||
})
|
||||
}
|
||||
|
||||
// Len returns number of queries that have been queued so far.
|
||||
func (b *Batch) Len() int {
|
||||
return len(b.items)
|
||||
}
|
||||
|
||||
type BatchResults interface {
|
||||
// Exec reads the results from the next query in the batch as if the query has been sent with Conn.Exec.
|
||||
Exec() (pgconn.CommandTag, error)
|
||||
|
||||
// Query reads the results from the next query in the batch as if the query has been sent with Conn.Query.
|
||||
Query() (Rows, error)
|
||||
|
||||
// QueryRow reads the results from the next query in the batch as if the query has been sent with Conn.QueryRow.
|
||||
QueryRow() Row
|
||||
|
||||
// QueryFunc reads the results from the next query in the batch as if the query has been sent with Conn.QueryFunc.
|
||||
QueryFunc(scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error)
|
||||
|
||||
// Close closes the batch operation. This must be called before the underlying connection can be used again. Any error
|
||||
// that occurred during a batch operation may have made it impossible to resyncronize the connection with the server.
|
||||
// In this case the underlying connection will have been closed. Close is safe to call multiple times.
|
||||
Close() error
|
||||
}
|
||||
|
||||
type batchResults struct {
|
||||
ctx context.Context
|
||||
conn *Conn
|
||||
mrr *pgconn.MultiResultReader
|
||||
err error
|
||||
b *Batch
|
||||
ix int
|
||||
closed bool
|
||||
}
|
||||
|
||||
// Exec reads the results from the next query in the batch as if the query has been sent with Exec.
|
||||
func (br *batchResults) Exec() (pgconn.CommandTag, error) {
|
||||
if br.err != nil {
|
||||
return nil, br.err
|
||||
}
|
||||
if br.closed {
|
||||
return nil, fmt.Errorf("batch already closed")
|
||||
}
|
||||
|
||||
query, arguments, _ := br.nextQueryAndArgs()
|
||||
|
||||
if !br.mrr.NextResult() {
|
||||
err := br.mrr.Close()
|
||||
if err == nil {
|
||||
err = errors.New("no result")
|
||||
}
|
||||
if br.conn.shouldLog(LogLevelError) {
|
||||
br.conn.log(br.ctx, LogLevelError, "BatchResult.Exec", map[string]interface{}{
|
||||
"sql": query,
|
||||
"args": logQueryArgs(arguments),
|
||||
"err": err,
|
||||
})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
commandTag, err := br.mrr.ResultReader().Close()
|
||||
|
||||
if err != nil {
|
||||
if br.conn.shouldLog(LogLevelError) {
|
||||
br.conn.log(br.ctx, LogLevelError, "BatchResult.Exec", map[string]interface{}{
|
||||
"sql": query,
|
||||
"args": logQueryArgs(arguments),
|
||||
"err": err,
|
||||
})
|
||||
}
|
||||
} else if br.conn.shouldLog(LogLevelInfo) {
|
||||
br.conn.log(br.ctx, LogLevelInfo, "BatchResult.Exec", map[string]interface{}{
|
||||
"sql": query,
|
||||
"args": logQueryArgs(arguments),
|
||||
"commandTag": commandTag,
|
||||
})
|
||||
}
|
||||
|
||||
return commandTag, err
|
||||
}
|
||||
|
||||
// Query reads the results from the next query in the batch as if the query has been sent with Query.
|
||||
func (br *batchResults) Query() (Rows, error) {
|
||||
query, arguments, ok := br.nextQueryAndArgs()
|
||||
if !ok {
|
||||
query = "batch query"
|
||||
}
|
||||
|
||||
if br.err != nil {
|
||||
return &connRows{err: br.err, closed: true}, br.err
|
||||
}
|
||||
|
||||
if br.closed {
|
||||
alreadyClosedErr := fmt.Errorf("batch already closed")
|
||||
return &connRows{err: alreadyClosedErr, closed: true}, alreadyClosedErr
|
||||
}
|
||||
|
||||
rows := br.conn.getRows(br.ctx, query, arguments)
|
||||
|
||||
if !br.mrr.NextResult() {
|
||||
rows.err = br.mrr.Close()
|
||||
if rows.err == nil {
|
||||
rows.err = errors.New("no result")
|
||||
}
|
||||
rows.closed = true
|
||||
|
||||
if br.conn.shouldLog(LogLevelError) {
|
||||
br.conn.log(br.ctx, LogLevelError, "BatchResult.Query", map[string]interface{}{
|
||||
"sql": query,
|
||||
"args": logQueryArgs(arguments),
|
||||
"err": rows.err,
|
||||
})
|
||||
}
|
||||
|
||||
return rows, rows.err
|
||||
}
|
||||
|
||||
rows.resultReader = br.mrr.ResultReader()
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// QueryFunc reads the results from the next query in the batch as if the query has been sent with Conn.QueryFunc.
|
||||
func (br *batchResults) QueryFunc(scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error) {
|
||||
if br.closed {
|
||||
return nil, fmt.Errorf("batch already closed")
|
||||
}
|
||||
|
||||
rows, err := br.Query()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
err = rows.Scan(scans...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = f(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rows.CommandTag(), nil
|
||||
}
|
||||
|
||||
// QueryRow reads the results from the next query in the batch as if the query has been sent with QueryRow.
|
||||
func (br *batchResults) QueryRow() Row {
|
||||
rows, _ := br.Query()
|
||||
return (*connRow)(rows.(*connRows))
|
||||
|
||||
}
|
||||
|
||||
// Close closes the batch operation. Any error that occurred during a batch operation may have made it impossible to
|
||||
// resyncronize the connection with the server. In this case the underlying connection will have been closed.
|
||||
func (br *batchResults) Close() error {
|
||||
if br.err != nil {
|
||||
return br.err
|
||||
}
|
||||
|
||||
if br.closed {
|
||||
return nil
|
||||
}
|
||||
br.closed = true
|
||||
|
||||
// log any queries that haven't yet been logged by Exec or Query
|
||||
for {
|
||||
query, args, ok := br.nextQueryAndArgs()
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
if br.conn.shouldLog(LogLevelInfo) {
|
||||
br.conn.log(br.ctx, LogLevelInfo, "BatchResult.Close", map[string]interface{}{
|
||||
"sql": query,
|
||||
"args": logQueryArgs(args),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return br.mrr.Close()
|
||||
}
|
||||
|
||||
func (br *batchResults) nextQueryAndArgs() (query string, args []interface{}, ok bool) {
|
||||
if br.b != nil && br.ix < len(br.b.items) {
|
||||
bi := br.b.items[br.ix]
|
||||
query = bi.query
|
||||
args = bi.arguments
|
||||
ok = true
|
||||
br.ix++
|
||||
}
|
||||
return
|
||||
}
|
||||
857
vendor/github.com/jackc/pgx/v4/conn.go
generated
vendored
Normal file
857
vendor/github.com/jackc/pgx/v4/conn.go
generated
vendored
Normal file
@@ -0,0 +1,857 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgconn/stmtcache"
|
||||
"github.com/jackc/pgproto3/v2"
|
||||
"github.com/jackc/pgtype"
|
||||
"github.com/jackc/pgx/v4/internal/sanitize"
|
||||
)
|
||||
|
||||
// ConnConfig contains all the options used to establish a connection. It must be created by ParseConfig and
|
||||
// then it can be modified. A manually initialized ConnConfig will cause ConnectConfig to panic.
|
||||
type ConnConfig struct {
|
||||
pgconn.Config
|
||||
Logger Logger
|
||||
LogLevel LogLevel
|
||||
|
||||
// Original connection string that was parsed into config.
|
||||
connString string
|
||||
|
||||
// BuildStatementCache creates the stmtcache.Cache implementation for connections created with this config. Set
|
||||
// to nil to disable automatic prepared statements.
|
||||
BuildStatementCache BuildStatementCacheFunc
|
||||
|
||||
// PreferSimpleProtocol disables implicit prepared statement usage. By default pgx automatically uses the extended
|
||||
// protocol. This can improve performance due to being able to use the binary format. It also does not rely on client
|
||||
// side parameter sanitization. However, it does incur two round-trips per query (unless using a prepared statement)
|
||||
// and may be incompatible proxies such as PGBouncer. Setting PreferSimpleProtocol causes the simple protocol to be
|
||||
// used by default. The same functionality can be controlled on a per query basis by setting
|
||||
// QueryExOptions.SimpleProtocol.
|
||||
PreferSimpleProtocol bool
|
||||
|
||||
createdByParseConfig bool // Used to enforce created by ParseConfig rule.
|
||||
}
|
||||
|
||||
// Copy returns a deep copy of the config that is safe to use and modify.
|
||||
// The only exception is the tls.Config:
|
||||
// according to the tls.Config docs it must not be modified after creation.
|
||||
func (cc *ConnConfig) Copy() *ConnConfig {
|
||||
newConfig := new(ConnConfig)
|
||||
*newConfig = *cc
|
||||
newConfig.Config = *newConfig.Config.Copy()
|
||||
return newConfig
|
||||
}
|
||||
|
||||
// ConnString returns the connection string as parsed by pgx.ParseConfig into pgx.ConnConfig.
|
||||
func (cc *ConnConfig) ConnString() string { return cc.connString }
|
||||
|
||||
// BuildStatementCacheFunc is a function that can be used to create a stmtcache.Cache implementation for connection.
|
||||
type BuildStatementCacheFunc func(conn *pgconn.PgConn) stmtcache.Cache
|
||||
|
||||
// Conn is a PostgreSQL connection handle. It is not safe for concurrent usage. Use a connection pool to manage access
|
||||
// to multiple database connections from multiple goroutines.
|
||||
type Conn struct {
|
||||
pgConn *pgconn.PgConn
|
||||
config *ConnConfig // config used when establishing this connection
|
||||
preparedStatements map[string]*pgconn.StatementDescription
|
||||
stmtcache stmtcache.Cache
|
||||
logger Logger
|
||||
logLevel LogLevel
|
||||
|
||||
notifications []*pgconn.Notification
|
||||
|
||||
doneChan chan struct{}
|
||||
closedChan chan error
|
||||
|
||||
connInfo *pgtype.ConnInfo
|
||||
|
||||
wbuf []byte
|
||||
eqb extendedQueryBuilder
|
||||
}
|
||||
|
||||
// Identifier a PostgreSQL identifier or name. Identifiers can be composed of
|
||||
// multiple parts such as ["schema", "table"] or ["table", "column"].
|
||||
type Identifier []string
|
||||
|
||||
// Sanitize returns a sanitized string safe for SQL interpolation.
|
||||
func (ident Identifier) Sanitize() string {
|
||||
parts := make([]string, len(ident))
|
||||
for i := range ident {
|
||||
s := strings.ReplaceAll(ident[i], string([]byte{0}), "")
|
||||
parts[i] = `"` + strings.ReplaceAll(s, `"`, `""`) + `"`
|
||||
}
|
||||
return strings.Join(parts, ".")
|
||||
}
|
||||
|
||||
// ErrNoRows occurs when rows are expected but none are returned.
|
||||
var ErrNoRows = errors.New("no rows in result set")
|
||||
|
||||
// ErrInvalidLogLevel occurs on attempt to set an invalid log level.
|
||||
var ErrInvalidLogLevel = errors.New("invalid log level")
|
||||
|
||||
// Connect establishes a connection with a PostgreSQL server with a connection string. See
|
||||
// pgconn.Connect for details.
|
||||
func Connect(ctx context.Context, connString string) (*Conn, error) {
|
||||
connConfig, err := ParseConfig(connString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return connect(ctx, connConfig)
|
||||
}
|
||||
|
||||
// ConnectConfig establishes a connection with a PostgreSQL server with a configuration struct.
|
||||
// connConfig must have been created by ParseConfig.
|
||||
func ConnectConfig(ctx context.Context, connConfig *ConnConfig) (*Conn, error) {
|
||||
return connect(ctx, connConfig)
|
||||
}
|
||||
|
||||
// ParseConfig creates a ConnConfig from a connection string. ParseConfig handles all options that pgconn.ParseConfig
|
||||
// does. In addition, it accepts the following options:
|
||||
//
|
||||
// statement_cache_capacity
|
||||
// The maximum size of the automatic statement cache. Set to 0 to disable automatic statement caching. Default: 512.
|
||||
//
|
||||
// statement_cache_mode
|
||||
// Possible values: "prepare" and "describe". "prepare" will create prepared statements on the PostgreSQL server.
|
||||
// "describe" will use the anonymous prepared statement to describe a statement without creating a statement on the
|
||||
// server. "describe" is primarily useful when the environment does not allow prepared statements such as when
|
||||
// running a connection pooler like PgBouncer. Default: "prepare"
|
||||
//
|
||||
// prefer_simple_protocol
|
||||
// Possible values: "true" and "false". Use the simple protocol instead of extended protocol. Default: false
|
||||
func ParseConfig(connString string) (*ConnConfig, error) {
|
||||
config, err := pgconn.ParseConfig(connString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var buildStatementCache BuildStatementCacheFunc
|
||||
statementCacheCapacity := 512
|
||||
statementCacheMode := stmtcache.ModePrepare
|
||||
if s, ok := config.RuntimeParams["statement_cache_capacity"]; ok {
|
||||
delete(config.RuntimeParams, "statement_cache_capacity")
|
||||
n, err := strconv.ParseInt(s, 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse statement_cache_capacity: %w", err)
|
||||
}
|
||||
statementCacheCapacity = int(n)
|
||||
}
|
||||
|
||||
if s, ok := config.RuntimeParams["statement_cache_mode"]; ok {
|
||||
delete(config.RuntimeParams, "statement_cache_mode")
|
||||
switch s {
|
||||
case "prepare":
|
||||
statementCacheMode = stmtcache.ModePrepare
|
||||
case "describe":
|
||||
statementCacheMode = stmtcache.ModeDescribe
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid statement_cache_mod: %s", s)
|
||||
}
|
||||
}
|
||||
|
||||
if statementCacheCapacity > 0 {
|
||||
buildStatementCache = func(conn *pgconn.PgConn) stmtcache.Cache {
|
||||
return stmtcache.New(conn, statementCacheMode, statementCacheCapacity)
|
||||
}
|
||||
}
|
||||
|
||||
preferSimpleProtocol := false
|
||||
if s, ok := config.RuntimeParams["prefer_simple_protocol"]; ok {
|
||||
delete(config.RuntimeParams, "prefer_simple_protocol")
|
||||
if b, err := strconv.ParseBool(s); err == nil {
|
||||
preferSimpleProtocol = b
|
||||
} else {
|
||||
return nil, fmt.Errorf("invalid prefer_simple_protocol: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
connConfig := &ConnConfig{
|
||||
Config: *config,
|
||||
createdByParseConfig: true,
|
||||
LogLevel: LogLevelInfo,
|
||||
BuildStatementCache: buildStatementCache,
|
||||
PreferSimpleProtocol: preferSimpleProtocol,
|
||||
connString: connString,
|
||||
}
|
||||
|
||||
return connConfig, nil
|
||||
}
|
||||
|
||||
func connect(ctx context.Context, config *ConnConfig) (c *Conn, err error) {
|
||||
// Default values are set in ParseConfig. Enforce initial creation by ParseConfig rather than setting defaults from
|
||||
// zero values.
|
||||
if !config.createdByParseConfig {
|
||||
panic("config must be created by ParseConfig")
|
||||
}
|
||||
originalConfig := config
|
||||
|
||||
// This isn't really a deep copy. But it is enough to avoid the config.Config.OnNotification mutation from affecting
|
||||
// other connections with the same config. See https://github.com/jackc/pgx/issues/618.
|
||||
{
|
||||
configCopy := *config
|
||||
config = &configCopy
|
||||
}
|
||||
|
||||
c = &Conn{
|
||||
config: originalConfig,
|
||||
connInfo: pgtype.NewConnInfo(),
|
||||
logLevel: config.LogLevel,
|
||||
logger: config.Logger,
|
||||
}
|
||||
|
||||
// Only install pgx notification system if no other callback handler is present.
|
||||
if config.Config.OnNotification == nil {
|
||||
config.Config.OnNotification = c.bufferNotifications
|
||||
} else {
|
||||
if c.shouldLog(LogLevelDebug) {
|
||||
c.log(ctx, LogLevelDebug, "pgx notification handler disabled by application supplied OnNotification", map[string]interface{}{"host": config.Config.Host})
|
||||
}
|
||||
}
|
||||
|
||||
if c.shouldLog(LogLevelInfo) {
|
||||
c.log(ctx, LogLevelInfo, "Dialing PostgreSQL server", map[string]interface{}{"host": config.Config.Host})
|
||||
}
|
||||
c.pgConn, err = pgconn.ConnectConfig(ctx, &config.Config)
|
||||
if err != nil {
|
||||
if c.shouldLog(LogLevelError) {
|
||||
c.log(ctx, LogLevelError, "connect failed", map[string]interface{}{"err": err})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.preparedStatements = make(map[string]*pgconn.StatementDescription)
|
||||
c.doneChan = make(chan struct{})
|
||||
c.closedChan = make(chan error)
|
||||
c.wbuf = make([]byte, 0, 1024)
|
||||
|
||||
if c.config.BuildStatementCache != nil {
|
||||
c.stmtcache = c.config.BuildStatementCache(c.pgConn)
|
||||
}
|
||||
|
||||
// Replication connections can't execute the queries to
|
||||
// populate the c.PgTypes and c.pgsqlAfInet
|
||||
if _, ok := config.Config.RuntimeParams["replication"]; ok {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Close closes a connection. It is safe to call Close on a already closed
|
||||
// connection.
|
||||
func (c *Conn) Close(ctx context.Context) error {
|
||||
if c.IsClosed() {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := c.pgConn.Close(ctx)
|
||||
if c.shouldLog(LogLevelInfo) {
|
||||
c.log(ctx, LogLevelInfo, "closed connection", nil)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Prepare creates a prepared statement with name and sql. sql can contain placeholders
|
||||
// for bound parameters. These placeholders are referenced positional as $1, $2, etc.
|
||||
//
|
||||
// Prepare is idempotent; i.e. it is safe to call Prepare multiple times with the same
|
||||
// name and sql arguments. This allows a code path to Prepare and Query/Exec without
|
||||
// concern for if the statement has already been prepared.
|
||||
func (c *Conn) Prepare(ctx context.Context, name, sql string) (sd *pgconn.StatementDescription, err error) {
|
||||
if name != "" {
|
||||
var ok bool
|
||||
if sd, ok = c.preparedStatements[name]; ok && sd.SQL == sql {
|
||||
return sd, nil
|
||||
}
|
||||
}
|
||||
|
||||
if c.shouldLog(LogLevelError) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
c.log(ctx, LogLevelError, "Prepare failed", map[string]interface{}{"err": err, "name": name, "sql": sql})
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
sd, err = c.pgConn.Prepare(ctx, name, sql, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if name != "" {
|
||||
c.preparedStatements[name] = sd
|
||||
}
|
||||
|
||||
return sd, nil
|
||||
}
|
||||
|
||||
// Deallocate released a prepared statement
|
||||
func (c *Conn) Deallocate(ctx context.Context, name string) error {
|
||||
delete(c.preparedStatements, name)
|
||||
_, err := c.pgConn.Exec(ctx, "deallocate "+quoteIdentifier(name)).ReadAll()
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Conn) bufferNotifications(_ *pgconn.PgConn, n *pgconn.Notification) {
|
||||
c.notifications = append(c.notifications, n)
|
||||
}
|
||||
|
||||
// WaitForNotification waits for a PostgreSQL notification. It wraps the underlying pgconn notification system in a
|
||||
// slightly more convenient form.
|
||||
func (c *Conn) WaitForNotification(ctx context.Context) (*pgconn.Notification, error) {
|
||||
var n *pgconn.Notification
|
||||
|
||||
// Return already received notification immediately
|
||||
if len(c.notifications) > 0 {
|
||||
n = c.notifications[0]
|
||||
c.notifications = c.notifications[1:]
|
||||
return n, nil
|
||||
}
|
||||
|
||||
err := c.pgConn.WaitForNotification(ctx)
|
||||
if len(c.notifications) > 0 {
|
||||
n = c.notifications[0]
|
||||
c.notifications = c.notifications[1:]
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
// IsClosed reports if the connection has been closed.
|
||||
func (c *Conn) IsClosed() bool {
|
||||
return c.pgConn.IsClosed()
|
||||
}
|
||||
|
||||
func (c *Conn) die(err error) {
|
||||
if c.IsClosed() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // force immediate hard cancel
|
||||
c.pgConn.Close(ctx)
|
||||
}
|
||||
|
||||
func (c *Conn) shouldLog(lvl LogLevel) bool {
|
||||
return c.logger != nil && c.logLevel >= lvl
|
||||
}
|
||||
|
||||
func (c *Conn) log(ctx context.Context, lvl LogLevel, msg string, data map[string]interface{}) {
|
||||
if data == nil {
|
||||
data = map[string]interface{}{}
|
||||
}
|
||||
if c.pgConn != nil && c.pgConn.PID() != 0 {
|
||||
data["pid"] = c.pgConn.PID()
|
||||
}
|
||||
|
||||
c.logger.Log(ctx, lvl, msg, data)
|
||||
}
|
||||
|
||||
func quoteIdentifier(s string) string {
|
||||
return `"` + strings.ReplaceAll(s, `"`, `""`) + `"`
|
||||
}
|
||||
|
||||
// Ping executes an empty sql statement against the *Conn
|
||||
// If the sql returns without error, the database Ping is considered successful, otherwise, the error is returned.
|
||||
func (c *Conn) Ping(ctx context.Context) error {
|
||||
_, err := c.Exec(ctx, ";")
|
||||
return err
|
||||
}
|
||||
|
||||
// PgConn returns the underlying *pgconn.PgConn. This is an escape hatch method that allows lower level access to the
|
||||
// PostgreSQL connection than pgx exposes.
|
||||
//
|
||||
// It is strongly recommended that the connection be idle (no in-progress queries) before the underlying *pgconn.PgConn
|
||||
// is used and the connection must be returned to the same state before any *pgx.Conn methods are again used.
|
||||
func (c *Conn) PgConn() *pgconn.PgConn { return c.pgConn }
|
||||
|
||||
// StatementCache returns the statement cache used for this connection.
|
||||
func (c *Conn) StatementCache() stmtcache.Cache { return c.stmtcache }
|
||||
|
||||
// ConnInfo returns the connection info used for this connection.
|
||||
func (c *Conn) ConnInfo() *pgtype.ConnInfo { return c.connInfo }
|
||||
|
||||
// Config returns a copy of config that was used to establish this connection.
|
||||
func (c *Conn) Config() *ConnConfig { return c.config.Copy() }
|
||||
|
||||
// Exec executes sql. sql can be either a prepared statement name or an SQL string. arguments should be referenced
|
||||
// positionally from the sql string as $1, $2, etc.
|
||||
func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
|
||||
startTime := time.Now()
|
||||
|
||||
commandTag, err := c.exec(ctx, sql, arguments...)
|
||||
if err != nil {
|
||||
if c.shouldLog(LogLevelError) {
|
||||
endTime := time.Now()
|
||||
c.log(ctx, LogLevelError, "Exec", map[string]interface{}{"sql": sql, "args": logQueryArgs(arguments), "err": err, "time": endTime.Sub(startTime)})
|
||||
}
|
||||
return commandTag, err
|
||||
}
|
||||
|
||||
if c.shouldLog(LogLevelInfo) {
|
||||
endTime := time.Now()
|
||||
c.log(ctx, LogLevelInfo, "Exec", map[string]interface{}{"sql": sql, "args": logQueryArgs(arguments), "time": endTime.Sub(startTime), "commandTag": commandTag})
|
||||
}
|
||||
|
||||
return commandTag, err
|
||||
}
|
||||
|
||||
func (c *Conn) exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error) {
|
||||
simpleProtocol := c.config.PreferSimpleProtocol
|
||||
|
||||
optionLoop:
|
||||
for len(arguments) > 0 {
|
||||
switch arg := arguments[0].(type) {
|
||||
case QuerySimpleProtocol:
|
||||
simpleProtocol = bool(arg)
|
||||
arguments = arguments[1:]
|
||||
default:
|
||||
break optionLoop
|
||||
}
|
||||
}
|
||||
|
||||
if sd, ok := c.preparedStatements[sql]; ok {
|
||||
return c.execPrepared(ctx, sd, arguments)
|
||||
}
|
||||
|
||||
if simpleProtocol {
|
||||
return c.execSimpleProtocol(ctx, sql, arguments)
|
||||
}
|
||||
|
||||
if len(arguments) == 0 {
|
||||
return c.execSimpleProtocol(ctx, sql, arguments)
|
||||
}
|
||||
|
||||
if c.stmtcache != nil {
|
||||
sd, err := c.stmtcache.Get(ctx, sql)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if c.stmtcache.Mode() == stmtcache.ModeDescribe {
|
||||
return c.execParams(ctx, sd, arguments)
|
||||
}
|
||||
return c.execPrepared(ctx, sd, arguments)
|
||||
}
|
||||
|
||||
sd, err := c.Prepare(ctx, "", sql)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.execPrepared(ctx, sd, arguments)
|
||||
}
|
||||
|
||||
func (c *Conn) execSimpleProtocol(ctx context.Context, sql string, arguments []interface{}) (commandTag pgconn.CommandTag, err error) {
|
||||
if len(arguments) > 0 {
|
||||
sql, err = c.sanitizeForSimpleQuery(sql, arguments...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
mrr := c.pgConn.Exec(ctx, sql)
|
||||
for mrr.NextResult() {
|
||||
commandTag, err = mrr.ResultReader().Close()
|
||||
}
|
||||
err = mrr.Close()
|
||||
return commandTag, err
|
||||
}
|
||||
|
||||
func (c *Conn) execParamsAndPreparedPrefix(sd *pgconn.StatementDescription, arguments []interface{}) error {
|
||||
if len(sd.ParamOIDs) != len(arguments) {
|
||||
return fmt.Errorf("expected %d arguments, got %d", len(sd.ParamOIDs), len(arguments))
|
||||
}
|
||||
|
||||
c.eqb.Reset()
|
||||
|
||||
args, err := convertDriverValuers(arguments)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range args {
|
||||
err = c.eqb.AppendParam(c.connInfo, sd.ParamOIDs[i], args[i])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for i := range sd.Fields {
|
||||
c.eqb.AppendResultFormat(c.ConnInfo().ResultFormatCodeForOID(sd.Fields[i].DataTypeOID))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) execParams(ctx context.Context, sd *pgconn.StatementDescription, arguments []interface{}) (pgconn.CommandTag, error) {
|
||||
err := c.execParamsAndPreparedPrefix(sd, arguments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := c.pgConn.ExecParams(ctx, sd.SQL, c.eqb.paramValues, sd.ParamOIDs, c.eqb.paramFormats, c.eqb.resultFormats).Read()
|
||||
c.eqb.Reset() // Allow c.eqb internal memory to be GC'ed as soon as possible.
|
||||
return result.CommandTag, result.Err
|
||||
}
|
||||
|
||||
func (c *Conn) execPrepared(ctx context.Context, sd *pgconn.StatementDescription, arguments []interface{}) (pgconn.CommandTag, error) {
|
||||
err := c.execParamsAndPreparedPrefix(sd, arguments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := c.pgConn.ExecPrepared(ctx, sd.Name, c.eqb.paramValues, c.eqb.paramFormats, c.eqb.resultFormats).Read()
|
||||
c.eqb.Reset() // Allow c.eqb internal memory to be GC'ed as soon as possible.
|
||||
return result.CommandTag, result.Err
|
||||
}
|
||||
|
||||
func (c *Conn) getRows(ctx context.Context, sql string, args []interface{}) *connRows {
|
||||
r := &connRows{}
|
||||
|
||||
r.ctx = ctx
|
||||
r.logger = c
|
||||
r.connInfo = c.connInfo
|
||||
r.startTime = time.Now()
|
||||
r.sql = sql
|
||||
r.args = args
|
||||
r.conn = c
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// QuerySimpleProtocol controls whether the simple or extended protocol is used to send the query.
|
||||
type QuerySimpleProtocol bool
|
||||
|
||||
// QueryResultFormats controls the result format (text=0, binary=1) of a query by result column position.
|
||||
type QueryResultFormats []int16
|
||||
|
||||
// QueryResultFormatsByOID controls the result format (text=0, binary=1) of a query by the result column OID.
|
||||
type QueryResultFormatsByOID map[uint32]int16
|
||||
|
||||
// Query sends a query to the server and returns a Rows to read the results. Only errors encountered sending the query
|
||||
// and initializing Rows will be returned. Err() on the returned Rows must be checked after the Rows is closed to
|
||||
// determine if the query executed successfully.
|
||||
//
|
||||
// The returned Rows must be closed before the connection can be used again. It is safe to attempt to read from the
|
||||
// returned Rows even if an error is returned. The error will be the available in rows.Err() after rows are closed. It
|
||||
// is allowed to ignore the error returned from Query and handle it in Rows.
|
||||
//
|
||||
// Err() on the returned Rows must be checked after the Rows is closed to determine if the query executed successfully
|
||||
// as some errors can only be detected by reading the entire response. e.g. A divide by zero error on the last row.
|
||||
//
|
||||
// For extra control over how the query is executed, the types QuerySimpleProtocol, QueryResultFormats, and
|
||||
// QueryResultFormatsByOID may be used as the first args to control exactly how the query is executed. This is rarely
|
||||
// needed. See the documentation for those types for details.
|
||||
func (c *Conn) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error) {
|
||||
var resultFormats QueryResultFormats
|
||||
var resultFormatsByOID QueryResultFormatsByOID
|
||||
simpleProtocol := c.config.PreferSimpleProtocol
|
||||
|
||||
optionLoop:
|
||||
for len(args) > 0 {
|
||||
switch arg := args[0].(type) {
|
||||
case QueryResultFormats:
|
||||
resultFormats = arg
|
||||
args = args[1:]
|
||||
case QueryResultFormatsByOID:
|
||||
resultFormatsByOID = arg
|
||||
args = args[1:]
|
||||
case QuerySimpleProtocol:
|
||||
simpleProtocol = bool(arg)
|
||||
args = args[1:]
|
||||
default:
|
||||
break optionLoop
|
||||
}
|
||||
}
|
||||
|
||||
rows := c.getRows(ctx, sql, args)
|
||||
|
||||
var err error
|
||||
sd, ok := c.preparedStatements[sql]
|
||||
|
||||
if simpleProtocol && !ok {
|
||||
sql, err = c.sanitizeForSimpleQuery(sql, args...)
|
||||
if err != nil {
|
||||
rows.fatal(err)
|
||||
return rows, err
|
||||
}
|
||||
|
||||
mrr := c.pgConn.Exec(ctx, sql)
|
||||
if mrr.NextResult() {
|
||||
rows.resultReader = mrr.ResultReader()
|
||||
rows.multiResultReader = mrr
|
||||
} else {
|
||||
err = mrr.Close()
|
||||
rows.fatal(err)
|
||||
return rows, err
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
c.eqb.Reset()
|
||||
|
||||
if !ok {
|
||||
if c.stmtcache != nil {
|
||||
sd, err = c.stmtcache.Get(ctx, sql)
|
||||
if err != nil {
|
||||
rows.fatal(err)
|
||||
return rows, rows.err
|
||||
}
|
||||
} else {
|
||||
sd, err = c.pgConn.Prepare(ctx, "", sql, nil)
|
||||
if err != nil {
|
||||
rows.fatal(err)
|
||||
return rows, rows.err
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(sd.ParamOIDs) != len(args) {
|
||||
rows.fatal(fmt.Errorf("expected %d arguments, got %d", len(sd.ParamOIDs), len(args)))
|
||||
return rows, rows.err
|
||||
}
|
||||
|
||||
rows.sql = sd.SQL
|
||||
|
||||
args, err = convertDriverValuers(args)
|
||||
if err != nil {
|
||||
rows.fatal(err)
|
||||
return rows, rows.err
|
||||
}
|
||||
|
||||
for i := range args {
|
||||
err = c.eqb.AppendParam(c.connInfo, sd.ParamOIDs[i], args[i])
|
||||
if err != nil {
|
||||
rows.fatal(err)
|
||||
return rows, rows.err
|
||||
}
|
||||
}
|
||||
|
||||
if resultFormatsByOID != nil {
|
||||
resultFormats = make([]int16, len(sd.Fields))
|
||||
for i := range resultFormats {
|
||||
resultFormats[i] = resultFormatsByOID[uint32(sd.Fields[i].DataTypeOID)]
|
||||
}
|
||||
}
|
||||
|
||||
if resultFormats == nil {
|
||||
for i := range sd.Fields {
|
||||
c.eqb.AppendResultFormat(c.ConnInfo().ResultFormatCodeForOID(sd.Fields[i].DataTypeOID))
|
||||
}
|
||||
|
||||
resultFormats = c.eqb.resultFormats
|
||||
}
|
||||
|
||||
if c.stmtcache != nil && c.stmtcache.Mode() == stmtcache.ModeDescribe && !ok {
|
||||
rows.resultReader = c.pgConn.ExecParams(ctx, sql, c.eqb.paramValues, sd.ParamOIDs, c.eqb.paramFormats, resultFormats)
|
||||
} else {
|
||||
rows.resultReader = c.pgConn.ExecPrepared(ctx, sd.Name, c.eqb.paramValues, c.eqb.paramFormats, resultFormats)
|
||||
}
|
||||
|
||||
c.eqb.Reset() // Allow c.eqb internal memory to be GC'ed as soon as possible.
|
||||
|
||||
return rows, rows.err
|
||||
}
|
||||
|
||||
// QueryRow is a convenience wrapper over Query. Any error that occurs while
|
||||
// querying is deferred until calling Scan on the returned Row. That Row will
|
||||
// error with ErrNoRows if no rows are returned.
|
||||
func (c *Conn) QueryRow(ctx context.Context, sql string, args ...interface{}) Row {
|
||||
rows, _ := c.Query(ctx, sql, args...)
|
||||
return (*connRow)(rows.(*connRows))
|
||||
}
|
||||
|
||||
// QueryFuncRow is the argument to the QueryFunc callback function.
|
||||
//
|
||||
// QueryFuncRow is an interface instead of a struct to allow tests to mock QueryFunc. However, adding a method to an
|
||||
// interface is technically a breaking change. Because of this the QueryFuncRow interface is partially excluded from
|
||||
// semantic version requirements. Methods will not be removed or changed, but new methods may be added.
|
||||
type QueryFuncRow interface {
|
||||
FieldDescriptions() []pgproto3.FieldDescription
|
||||
|
||||
// RawValues returns the unparsed bytes of the row values. The returned [][]byte is only valid during the current
|
||||
// function call. However, the underlying byte data is safe to retain a reference to and mutate.
|
||||
RawValues() [][]byte
|
||||
}
|
||||
|
||||
// QueryFunc executes sql with args. For each row returned by the query the values will scanned into the elements of
|
||||
// scans and f will be called. If any row fails to scan or f returns an error the query will be aborted and the error
|
||||
// will be returned.
|
||||
func (c *Conn) QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error) {
|
||||
rows, err := c.Query(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
err = rows.Scan(scans...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = f(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rows.CommandTag(), nil
|
||||
}
|
||||
|
||||
// SendBatch sends all queued queries to the server at once. All queries are run in an implicit transaction unless
|
||||
// explicit transaction control statements are executed. The returned BatchResults must be closed before the connection
|
||||
// is used again.
|
||||
func (c *Conn) SendBatch(ctx context.Context, b *Batch) BatchResults {
|
||||
startTime := time.Now()
|
||||
|
||||
simpleProtocol := c.config.PreferSimpleProtocol
|
||||
var sb strings.Builder
|
||||
if simpleProtocol {
|
||||
for i, bi := range b.items {
|
||||
if i > 0 {
|
||||
sb.WriteByte(';')
|
||||
}
|
||||
sql, err := c.sanitizeForSimpleQuery(bi.query, bi.arguments...)
|
||||
if err != nil {
|
||||
return &batchResults{ctx: ctx, conn: c, err: err}
|
||||
}
|
||||
sb.WriteString(sql)
|
||||
}
|
||||
mrr := c.pgConn.Exec(ctx, sb.String())
|
||||
return &batchResults{
|
||||
ctx: ctx,
|
||||
conn: c,
|
||||
mrr: mrr,
|
||||
b: b,
|
||||
ix: 0,
|
||||
}
|
||||
}
|
||||
|
||||
distinctUnpreparedQueries := map[string]struct{}{}
|
||||
|
||||
for _, bi := range b.items {
|
||||
if _, ok := c.preparedStatements[bi.query]; ok {
|
||||
continue
|
||||
}
|
||||
distinctUnpreparedQueries[bi.query] = struct{}{}
|
||||
}
|
||||
|
||||
var stmtCache stmtcache.Cache
|
||||
if len(distinctUnpreparedQueries) > 0 {
|
||||
if c.stmtcache != nil && c.stmtcache.Cap() >= len(distinctUnpreparedQueries) {
|
||||
stmtCache = c.stmtcache
|
||||
} else {
|
||||
stmtCache = stmtcache.New(c.pgConn, stmtcache.ModeDescribe, len(distinctUnpreparedQueries))
|
||||
}
|
||||
|
||||
for sql, _ := range distinctUnpreparedQueries {
|
||||
_, err := stmtCache.Get(ctx, sql)
|
||||
if err != nil {
|
||||
return &batchResults{ctx: ctx, conn: c, err: err}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
batch := &pgconn.Batch{}
|
||||
|
||||
for _, bi := range b.items {
|
||||
c.eqb.Reset()
|
||||
|
||||
sd := c.preparedStatements[bi.query]
|
||||
if sd == nil {
|
||||
var err error
|
||||
sd, err = stmtCache.Get(ctx, bi.query)
|
||||
if err != nil {
|
||||
return c.logBatchResults(ctx, startTime, &batchResults{ctx: ctx, conn: c, err: err})
|
||||
}
|
||||
}
|
||||
|
||||
if len(sd.ParamOIDs) != len(bi.arguments) {
|
||||
return c.logBatchResults(ctx, startTime, &batchResults{ctx: ctx, conn: c, err: fmt.Errorf("mismatched param and argument count")})
|
||||
}
|
||||
|
||||
args, err := convertDriverValuers(bi.arguments)
|
||||
if err != nil {
|
||||
return c.logBatchResults(ctx, startTime, &batchResults{ctx: ctx, conn: c, err: err})
|
||||
}
|
||||
|
||||
for i := range args {
|
||||
err = c.eqb.AppendParam(c.connInfo, sd.ParamOIDs[i], args[i])
|
||||
if err != nil {
|
||||
return c.logBatchResults(ctx, startTime, &batchResults{ctx: ctx, conn: c, err: err})
|
||||
}
|
||||
}
|
||||
|
||||
for i := range sd.Fields {
|
||||
c.eqb.AppendResultFormat(c.ConnInfo().ResultFormatCodeForOID(sd.Fields[i].DataTypeOID))
|
||||
}
|
||||
|
||||
if sd.Name == "" {
|
||||
batch.ExecParams(bi.query, c.eqb.paramValues, sd.ParamOIDs, c.eqb.paramFormats, c.eqb.resultFormats)
|
||||
} else {
|
||||
batch.ExecPrepared(sd.Name, c.eqb.paramValues, c.eqb.paramFormats, c.eqb.resultFormats)
|
||||
}
|
||||
}
|
||||
|
||||
c.eqb.Reset() // Allow c.eqb internal memory to be GC'ed as soon as possible.
|
||||
|
||||
mrr := c.pgConn.ExecBatch(ctx, batch)
|
||||
|
||||
return c.logBatchResults(ctx, startTime, &batchResults{
|
||||
ctx: ctx,
|
||||
conn: c,
|
||||
mrr: mrr,
|
||||
b: b,
|
||||
ix: 0,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Conn) logBatchResults(ctx context.Context, startTime time.Time, results *batchResults) BatchResults {
|
||||
if results.err != nil {
|
||||
if c.shouldLog(LogLevelError) {
|
||||
endTime := time.Now()
|
||||
c.log(ctx, LogLevelError, "SendBatch", map[string]interface{}{"err": results.err, "time": endTime.Sub(startTime)})
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
if c.shouldLog(LogLevelInfo) {
|
||||
endTime := time.Now()
|
||||
c.log(ctx, LogLevelInfo, "SendBatch", map[string]interface{}{"batchLen": results.b.Len(), "time": endTime.Sub(startTime)})
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
func (c *Conn) sanitizeForSimpleQuery(sql string, args ...interface{}) (string, error) {
|
||||
if c.pgConn.ParameterStatus("standard_conforming_strings") != "on" {
|
||||
return "", errors.New("simple protocol queries must be run with standard_conforming_strings=on")
|
||||
}
|
||||
|
||||
if c.pgConn.ParameterStatus("client_encoding") != "UTF8" {
|
||||
return "", errors.New("simple protocol queries must be run with client_encoding=UTF8")
|
||||
}
|
||||
|
||||
var err error
|
||||
valueArgs := make([]interface{}, len(args))
|
||||
for i, a := range args {
|
||||
valueArgs[i], err = convertSimpleArgument(c.connInfo, a)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
return sanitize.SanitizeSQL(sql, valueArgs...)
|
||||
}
|
||||
211
vendor/github.com/jackc/pgx/v4/copy_from.go
generated
vendored
Normal file
211
vendor/github.com/jackc/pgx/v4/copy_from.go
generated
vendored
Normal file
@@ -0,0 +1,211 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgio"
|
||||
)
|
||||
|
||||
// CopyFromRows returns a CopyFromSource interface over the provided rows slice
|
||||
// making it usable by *Conn.CopyFrom.
|
||||
func CopyFromRows(rows [][]interface{}) CopyFromSource {
|
||||
return ©FromRows{rows: rows, idx: -1}
|
||||
}
|
||||
|
||||
type copyFromRows struct {
|
||||
rows [][]interface{}
|
||||
idx int
|
||||
}
|
||||
|
||||
func (ctr *copyFromRows) Next() bool {
|
||||
ctr.idx++
|
||||
return ctr.idx < len(ctr.rows)
|
||||
}
|
||||
|
||||
func (ctr *copyFromRows) Values() ([]interface{}, error) {
|
||||
return ctr.rows[ctr.idx], nil
|
||||
}
|
||||
|
||||
func (ctr *copyFromRows) Err() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CopyFromSlice returns a CopyFromSource interface over a dynamic func
|
||||
// making it usable by *Conn.CopyFrom.
|
||||
func CopyFromSlice(length int, next func(int) ([]interface{}, error)) CopyFromSource {
|
||||
return ©FromSlice{next: next, idx: -1, len: length}
|
||||
}
|
||||
|
||||
type copyFromSlice struct {
|
||||
next func(int) ([]interface{}, error)
|
||||
idx int
|
||||
len int
|
||||
err error
|
||||
}
|
||||
|
||||
func (cts *copyFromSlice) Next() bool {
|
||||
cts.idx++
|
||||
return cts.idx < cts.len
|
||||
}
|
||||
|
||||
func (cts *copyFromSlice) Values() ([]interface{}, error) {
|
||||
values, err := cts.next(cts.idx)
|
||||
if err != nil {
|
||||
cts.err = err
|
||||
}
|
||||
return values, err
|
||||
}
|
||||
|
||||
func (cts *copyFromSlice) Err() error {
|
||||
return cts.err
|
||||
}
|
||||
|
||||
// CopyFromSource is the interface used by *Conn.CopyFrom as the source for copy data.
|
||||
type CopyFromSource interface {
|
||||
// Next returns true if there is another row and makes the next row data
|
||||
// available to Values(). When there are no more rows available or an error
|
||||
// has occurred it returns false.
|
||||
Next() bool
|
||||
|
||||
// Values returns the values for the current row.
|
||||
Values() ([]interface{}, error)
|
||||
|
||||
// Err returns any error that has been encountered by the CopyFromSource. If
|
||||
// this is not nil *Conn.CopyFrom will abort the copy.
|
||||
Err() error
|
||||
}
|
||||
|
||||
type copyFrom struct {
|
||||
conn *Conn
|
||||
tableName Identifier
|
||||
columnNames []string
|
||||
rowSrc CopyFromSource
|
||||
readerErrChan chan error
|
||||
}
|
||||
|
||||
func (ct *copyFrom) run(ctx context.Context) (int64, error) {
|
||||
quotedTableName := ct.tableName.Sanitize()
|
||||
cbuf := &bytes.Buffer{}
|
||||
for i, cn := range ct.columnNames {
|
||||
if i != 0 {
|
||||
cbuf.WriteString(", ")
|
||||
}
|
||||
cbuf.WriteString(quoteIdentifier(cn))
|
||||
}
|
||||
quotedColumnNames := cbuf.String()
|
||||
|
||||
sd, err := ct.conn.Prepare(ctx, "", fmt.Sprintf("select %s from %s", quotedColumnNames, quotedTableName))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
r, w := io.Pipe()
|
||||
doneChan := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
defer close(doneChan)
|
||||
|
||||
// Purposely NOT using defer w.Close(). See https://github.com/golang/go/issues/24283.
|
||||
buf := ct.conn.wbuf
|
||||
|
||||
buf = append(buf, "PGCOPY\n\377\r\n\000"...)
|
||||
buf = pgio.AppendInt32(buf, 0)
|
||||
buf = pgio.AppendInt32(buf, 0)
|
||||
|
||||
moreRows := true
|
||||
for moreRows {
|
||||
var err error
|
||||
moreRows, buf, err = ct.buildCopyBuf(buf, sd)
|
||||
if err != nil {
|
||||
w.CloseWithError(err)
|
||||
return
|
||||
}
|
||||
|
||||
if ct.rowSrc.Err() != nil {
|
||||
w.CloseWithError(ct.rowSrc.Err())
|
||||
return
|
||||
}
|
||||
|
||||
if len(buf) > 0 {
|
||||
_, err = w.Write(buf)
|
||||
if err != nil {
|
||||
w.Close()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
buf = buf[:0]
|
||||
}
|
||||
|
||||
w.Close()
|
||||
}()
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
commandTag, err := ct.conn.pgConn.CopyFrom(ctx, r, fmt.Sprintf("copy %s ( %s ) from stdin binary;", quotedTableName, quotedColumnNames))
|
||||
|
||||
r.Close()
|
||||
<-doneChan
|
||||
|
||||
rowsAffected := commandTag.RowsAffected()
|
||||
endTime := time.Now()
|
||||
if err == nil {
|
||||
if ct.conn.shouldLog(LogLevelInfo) {
|
||||
ct.conn.log(ctx, LogLevelInfo, "CopyFrom", map[string]interface{}{"tableName": ct.tableName, "columnNames": ct.columnNames, "time": endTime.Sub(startTime), "rowCount": rowsAffected})
|
||||
}
|
||||
} else if ct.conn.shouldLog(LogLevelError) {
|
||||
ct.conn.log(ctx, LogLevelError, "CopyFrom", map[string]interface{}{"err": err, "tableName": ct.tableName, "columnNames": ct.columnNames, "time": endTime.Sub(startTime)})
|
||||
}
|
||||
|
||||
return rowsAffected, err
|
||||
}
|
||||
|
||||
func (ct *copyFrom) buildCopyBuf(buf []byte, sd *pgconn.StatementDescription) (bool, []byte, error) {
|
||||
|
||||
for ct.rowSrc.Next() {
|
||||
values, err := ct.rowSrc.Values()
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
if len(values) != len(ct.columnNames) {
|
||||
return false, nil, fmt.Errorf("expected %d values, got %d values", len(ct.columnNames), len(values))
|
||||
}
|
||||
|
||||
buf = pgio.AppendInt16(buf, int16(len(ct.columnNames)))
|
||||
for i, val := range values {
|
||||
buf, err = encodePreparedStatementArgument(ct.conn.connInfo, buf, sd.Fields[i].DataTypeOID, val)
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if len(buf) > 65536 {
|
||||
return true, buf, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, buf, nil
|
||||
}
|
||||
|
||||
// CopyFrom uses the PostgreSQL copy protocol to perform bulk data insertion.
|
||||
// It returns the number of rows copied and an error.
|
||||
//
|
||||
// CopyFrom requires all values use the binary format. Almost all types
|
||||
// implemented by pgx use the binary format by default. Types implementing
|
||||
// Encoder can only be used if they encode to the binary format.
|
||||
func (c *Conn) CopyFrom(ctx context.Context, tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int64, error) {
|
||||
ct := ©From{
|
||||
conn: c,
|
||||
tableName: tableName,
|
||||
columnNames: columnNames,
|
||||
rowSrc: rowSrc,
|
||||
readerErrChan: make(chan error),
|
||||
}
|
||||
|
||||
return ct.run(ctx)
|
||||
}
|
||||
340
vendor/github.com/jackc/pgx/v4/doc.go
generated
vendored
Normal file
340
vendor/github.com/jackc/pgx/v4/doc.go
generated
vendored
Normal file
@@ -0,0 +1,340 @@
|
||||
// Package pgx is a PostgreSQL database driver.
|
||||
/*
|
||||
pgx provides lower level access to PostgreSQL than the standard database/sql. It remains as similar to the database/sql
|
||||
interface as possible while providing better speed and access to PostgreSQL specific features. Import
|
||||
github.com/jackc/pgx/v4/stdlib to use pgx as a database/sql compatible driver.
|
||||
|
||||
Establishing a Connection
|
||||
|
||||
The primary way of establishing a connection is with `pgx.Connect`.
|
||||
|
||||
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
|
||||
|
||||
The database connection string can be in URL or DSN format. Both PostgreSQL settings and pgx settings can be specified
|
||||
here. In addition, a config struct can be created by `ParseConfig` and modified before establishing the connection with
|
||||
`ConnectConfig`.
|
||||
|
||||
config, err := pgx.ParseConfig(os.Getenv("DATABASE_URL"))
|
||||
if err != nil {
|
||||
// ...
|
||||
}
|
||||
config.Logger = log15adapter.NewLogger(log.New("module", "pgx"))
|
||||
|
||||
conn, err := pgx.ConnectConfig(context.Background(), config)
|
||||
|
||||
Connection Pool
|
||||
|
||||
`*pgx.Conn` represents a single connection to the database and is not concurrency safe. Use sub-package pgxpool for a
|
||||
concurrency safe connection pool.
|
||||
|
||||
Query Interface
|
||||
|
||||
pgx implements Query and Scan in the familiar database/sql style.
|
||||
|
||||
var sum int32
|
||||
|
||||
// Send the query to the server. The returned rows MUST be closed
|
||||
// before conn can be used again.
|
||||
rows, err := conn.Query(context.Background(), "select generate_series(1,$1)", 10)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// rows.Close is called by rows.Next when all rows are read
|
||||
// or an error occurs in Next or Scan. So it may optionally be
|
||||
// omitted if nothing in the rows.Next loop can panic. It is
|
||||
// safe to close rows multiple times.
|
||||
defer rows.Close()
|
||||
|
||||
// Iterate through the result set
|
||||
for rows.Next() {
|
||||
var n int32
|
||||
err = rows.Scan(&n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sum += n
|
||||
}
|
||||
|
||||
// Any errors encountered by rows.Next or rows.Scan will be returned here
|
||||
if rows.Err() != nil {
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
// No errors found - do something with sum
|
||||
|
||||
pgx also implements QueryRow in the same style as database/sql.
|
||||
|
||||
var name string
|
||||
var weight int64
|
||||
err := conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Use Exec to execute a query that does not return a result set.
|
||||
|
||||
commandTag, err := conn.Exec(context.Background(), "delete from widgets where id=$1", 42)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if commandTag.RowsAffected() != 1 {
|
||||
return errors.New("No row found to delete")
|
||||
}
|
||||
|
||||
QueryFunc can be used to execute a callback function for every row. This is often easier to use than Query.
|
||||
|
||||
var sum, n int32
|
||||
_, err = conn.QueryFunc(
|
||||
context.Background(),
|
||||
"select generate_series(1,$1)",
|
||||
[]interface{}{10},
|
||||
[]interface{}{&n},
|
||||
func(pgx.QueryFuncRow) error {
|
||||
sum += n
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Base Type Mapping
|
||||
|
||||
pgx maps between all common base types directly between Go and PostgreSQL. In particular:
|
||||
|
||||
Go PostgreSQL
|
||||
-----------------------
|
||||
string varchar
|
||||
text
|
||||
|
||||
// Integers are automatically be converted to any other integer type if
|
||||
// it can be done without overflow or underflow.
|
||||
int8
|
||||
int16 smallint
|
||||
int32 int
|
||||
int64 bigint
|
||||
int
|
||||
uint8
|
||||
uint16
|
||||
uint32
|
||||
uint64
|
||||
uint
|
||||
|
||||
// Floats are strict and do not automatically convert like integers.
|
||||
float32 float4
|
||||
float64 float8
|
||||
|
||||
time.Time date
|
||||
timestamp
|
||||
timestamptz
|
||||
|
||||
[]byte bytea
|
||||
|
||||
|
||||
Null Mapping
|
||||
|
||||
pgx can map nulls in two ways. The first is package pgtype provides types that have a data field and a status field.
|
||||
They work in a similar fashion to database/sql. The second is to use a pointer to a pointer.
|
||||
|
||||
var foo pgtype.Varchar
|
||||
var bar *string
|
||||
err := conn.QueryRow("select foo, bar from widgets where id=$1", 42).Scan(&foo, &bar)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Array Mapping
|
||||
|
||||
pgx maps between int16, int32, int64, float32, float64, and string Go slices and the equivalent PostgreSQL array type.
|
||||
Go slices of native types do not support nulls, so if a PostgreSQL array that contains a null is read into a native Go
|
||||
slice an error will occur. The pgtype package includes many more array types for PostgreSQL types that do not directly
|
||||
map to native Go types.
|
||||
|
||||
JSON and JSONB Mapping
|
||||
|
||||
pgx includes built-in support to marshal and unmarshal between Go types and the PostgreSQL JSON and JSONB.
|
||||
|
||||
Inet and CIDR Mapping
|
||||
|
||||
pgx encodes from net.IPNet to and from inet and cidr PostgreSQL types. In addition, as a convenience pgx will encode
|
||||
from a net.IP; it will assume a /32 netmask for IPv4 and a /128 for IPv6.
|
||||
|
||||
Custom Type Support
|
||||
|
||||
pgx includes support for the common data types like integers, floats, strings, dates, and times that have direct
|
||||
mappings between Go and SQL. In addition, pgx uses the github.com/jackc/pgtype library to support more types. See
|
||||
documention for that library for instructions on how to implement custom types.
|
||||
|
||||
See example_custom_type_test.go for an example of a custom type for the PostgreSQL point type.
|
||||
|
||||
pgx also includes support for custom types implementing the database/sql.Scanner and database/sql/driver.Valuer
|
||||
interfaces.
|
||||
|
||||
If pgx does cannot natively encode a type and that type is a renamed type (e.g. type MyTime time.Time) pgx will attempt
|
||||
to encode the underlying type. While this is usually desired behavior it can produce surprising behavior if one the
|
||||
underlying type and the renamed type each implement database/sql interfaces and the other implements pgx interfaces. It
|
||||
is recommended that this situation be avoided by implementing pgx interfaces on the renamed type.
|
||||
|
||||
Composite types and row values
|
||||
|
||||
Row values and composite types are represented as pgtype.Record (https://pkg.go.dev/github.com/jackc/pgtype?tab=doc#Record).
|
||||
It is possible to get values of your custom type by implementing DecodeBinary interface. Decoding into
|
||||
pgtype.Record first can simplify process by avoiding dealing with raw protocol directly.
|
||||
|
||||
For example:
|
||||
|
||||
type MyType struct {
|
||||
a int // NULL will cause decoding error
|
||||
b *string // there can be NULL in this position in SQL
|
||||
}
|
||||
|
||||
func (t *MyType) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
||||
r := pgtype.Record{
|
||||
Fields: []pgtype.Value{&pgtype.Int4{}, &pgtype.Text{}},
|
||||
}
|
||||
|
||||
if err := r.DecodeBinary(ci, src); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if r.Status != pgtype.Present {
|
||||
return errors.New("BUG: decoding should not be called on NULL value")
|
||||
}
|
||||
|
||||
a := r.Fields[0].(*pgtype.Int4)
|
||||
b := r.Fields[1].(*pgtype.Text)
|
||||
|
||||
// type compatibility is checked by AssignTo
|
||||
// only lossless assignments will succeed
|
||||
if err := a.AssignTo(&t.a); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// AssignTo also deals with null value handling
|
||||
if err := b.AssignTo(&t.b); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
result := MyType{}
|
||||
err := conn.QueryRow(context.Background(), "select row(1, 'foo'::text)", pgx.QueryResultFormats{pgx.BinaryFormatCode}).Scan(&r)
|
||||
|
||||
Raw Bytes Mapping
|
||||
|
||||
[]byte passed as arguments to Query, QueryRow, and Exec are passed unmodified to PostgreSQL.
|
||||
|
||||
Transactions
|
||||
|
||||
Transactions are started by calling Begin.
|
||||
|
||||
tx, err := conn.Begin(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Rollback is safe to call even if the tx is already closed, so if
|
||||
// the tx commits successfully, this is a no-op
|
||||
defer tx.Rollback(context.Background())
|
||||
|
||||
_, err = tx.Exec(context.Background(), "insert into foo(id) values (1)")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Commit(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
The Tx returned from Begin also implements the Begin method. This can be used to implement pseudo nested transactions.
|
||||
These are internally implemented with savepoints.
|
||||
|
||||
Use BeginTx to control the transaction mode.
|
||||
|
||||
BeginFunc and BeginTxFunc are variants that begin a transaction, execute a function, and commit or rollback the
|
||||
transaction depending on the return value of the function. These can be simpler and less error prone to use.
|
||||
|
||||
err = conn.BeginFunc(context.Background(), func(tx pgx.Tx) error {
|
||||
_, err := tx.Exec(context.Background(), "insert into foo(id) values (1)")
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Prepared Statements
|
||||
|
||||
Prepared statements can be manually created with the Prepare method. However, this is rarely necessary because pgx
|
||||
includes an automatic statement cache by default. Queries run through the normal Query, QueryRow, and Exec functions are
|
||||
automatically prepared on first execution and the prepared statement is reused on subsequent executions. See ParseConfig
|
||||
for information on how to customize or disable the statement cache.
|
||||
|
||||
Copy Protocol
|
||||
|
||||
Use CopyFrom to efficiently insert multiple rows at a time using the PostgreSQL copy protocol. CopyFrom accepts a
|
||||
CopyFromSource interface. If the data is already in a [][]interface{} use CopyFromRows to wrap it in a CopyFromSource
|
||||
interface. Or implement CopyFromSource to avoid buffering the entire data set in memory.
|
||||
|
||||
rows := [][]interface{}{
|
||||
{"John", "Smith", int32(36)},
|
||||
{"Jane", "Doe", int32(29)},
|
||||
}
|
||||
|
||||
copyCount, err := conn.CopyFrom(
|
||||
context.Background(),
|
||||
pgx.Identifier{"people"},
|
||||
[]string{"first_name", "last_name", "age"},
|
||||
pgx.CopyFromRows(rows),
|
||||
)
|
||||
|
||||
When you already have a typed array using CopyFromSlice can be more convenient.
|
||||
|
||||
rows := []User{
|
||||
{"John", "Smith", 36},
|
||||
{"Jane", "Doe", 29},
|
||||
}
|
||||
|
||||
copyCount, err := conn.CopyFrom(
|
||||
context.Background(),
|
||||
pgx.Identifier{"people"},
|
||||
[]string{"first_name", "last_name", "age"},
|
||||
pgx.CopyFromSlice(len(rows), func(i int) ([]interface{}, error) {
|
||||
return []interface{}{rows[i].FirstName, rows[i].LastName, rows[i].Age}, nil
|
||||
}),
|
||||
)
|
||||
|
||||
CopyFrom can be faster than an insert with as few as 5 rows.
|
||||
|
||||
Listen and Notify
|
||||
|
||||
pgx can listen to the PostgreSQL notification system with the `Conn.WaitForNotification` method. It blocks until a
|
||||
notification is received or the context is canceled.
|
||||
|
||||
_, err := conn.Exec(context.Background(), "listen channelname")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if notification, err := conn.WaitForNotification(context.Background()); err != nil {
|
||||
// do something with notification
|
||||
}
|
||||
|
||||
|
||||
Logging
|
||||
|
||||
pgx defines a simple logger interface. Connections optionally accept a logger that satisfies this interface. Set
|
||||
LogLevel to control logging verbosity. Adapters for github.com/inconshreveable/log15, github.com/sirupsen/logrus,
|
||||
go.uber.org/zap, github.com/rs/zerolog, and the testing log are provided in the log directory.
|
||||
|
||||
Lower Level PostgreSQL Functionality
|
||||
|
||||
pgx is implemented on top of github.com/jackc/pgconn a lower level PostgreSQL driver. The Conn.PgConn() method can be
|
||||
used to access this lower layer.
|
||||
|
||||
PgBouncer
|
||||
|
||||
pgx is compatible with PgBouncer in two modes. One is when the connection has a statement cache in "describe" mode. The
|
||||
other is when the connection is using the simple protocol. This can be set with the PreferSimpleProtocol config option.
|
||||
*/
|
||||
package pgx
|
||||
161
vendor/github.com/jackc/pgx/v4/extended_query_builder.go
generated
vendored
Normal file
161
vendor/github.com/jackc/pgx/v4/extended_query_builder.go
generated
vendored
Normal file
@@ -0,0 +1,161 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/jackc/pgtype"
|
||||
)
|
||||
|
||||
type extendedQueryBuilder struct {
|
||||
paramValues [][]byte
|
||||
paramValueBytes []byte
|
||||
paramFormats []int16
|
||||
resultFormats []int16
|
||||
}
|
||||
|
||||
func (eqb *extendedQueryBuilder) AppendParam(ci *pgtype.ConnInfo, oid uint32, arg interface{}) error {
|
||||
f := chooseParameterFormatCode(ci, oid, arg)
|
||||
eqb.paramFormats = append(eqb.paramFormats, f)
|
||||
|
||||
v, err := eqb.encodeExtendedParamValue(ci, oid, f, arg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
eqb.paramValues = append(eqb.paramValues, v)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (eqb *extendedQueryBuilder) AppendResultFormat(f int16) {
|
||||
eqb.resultFormats = append(eqb.resultFormats, f)
|
||||
}
|
||||
|
||||
// Reset readies eqb to build another query.
|
||||
func (eqb *extendedQueryBuilder) Reset() {
|
||||
eqb.paramValues = eqb.paramValues[0:0]
|
||||
eqb.paramValueBytes = eqb.paramValueBytes[0:0]
|
||||
eqb.paramFormats = eqb.paramFormats[0:0]
|
||||
eqb.resultFormats = eqb.resultFormats[0:0]
|
||||
|
||||
if cap(eqb.paramValues) > 64 {
|
||||
eqb.paramValues = make([][]byte, 0, 64)
|
||||
}
|
||||
|
||||
if cap(eqb.paramValueBytes) > 256 {
|
||||
eqb.paramValueBytes = make([]byte, 0, 256)
|
||||
}
|
||||
|
||||
if cap(eqb.paramFormats) > 64 {
|
||||
eqb.paramFormats = make([]int16, 0, 64)
|
||||
}
|
||||
if cap(eqb.resultFormats) > 64 {
|
||||
eqb.resultFormats = make([]int16, 0, 64)
|
||||
}
|
||||
}
|
||||
|
||||
func (eqb *extendedQueryBuilder) encodeExtendedParamValue(ci *pgtype.ConnInfo, oid uint32, formatCode int16, arg interface{}) ([]byte, error) {
|
||||
if arg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
refVal := reflect.ValueOf(arg)
|
||||
argIsPtr := refVal.Kind() == reflect.Ptr
|
||||
|
||||
if argIsPtr && refVal.IsNil() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if eqb.paramValueBytes == nil {
|
||||
eqb.paramValueBytes = make([]byte, 0, 128)
|
||||
}
|
||||
|
||||
var err error
|
||||
var buf []byte
|
||||
pos := len(eqb.paramValueBytes)
|
||||
|
||||
if arg, ok := arg.(string); ok {
|
||||
return []byte(arg), nil
|
||||
}
|
||||
|
||||
if formatCode == TextFormatCode {
|
||||
if arg, ok := arg.(pgtype.TextEncoder); ok {
|
||||
buf, err = arg.EncodeText(ci, eqb.paramValueBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if buf == nil {
|
||||
return nil, nil
|
||||
}
|
||||
eqb.paramValueBytes = buf
|
||||
return eqb.paramValueBytes[pos:], nil
|
||||
}
|
||||
} else if formatCode == BinaryFormatCode {
|
||||
if arg, ok := arg.(pgtype.BinaryEncoder); ok {
|
||||
buf, err = arg.EncodeBinary(ci, eqb.paramValueBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if buf == nil {
|
||||
return nil, nil
|
||||
}
|
||||
eqb.paramValueBytes = buf
|
||||
return eqb.paramValueBytes[pos:], nil
|
||||
}
|
||||
}
|
||||
|
||||
if argIsPtr {
|
||||
// We have already checked that arg is not pointing to nil,
|
||||
// so it is safe to dereference here.
|
||||
arg = refVal.Elem().Interface()
|
||||
return eqb.encodeExtendedParamValue(ci, oid, formatCode, arg)
|
||||
}
|
||||
|
||||
if dt, ok := ci.DataTypeForOID(oid); ok {
|
||||
value := dt.Value
|
||||
err := value.Set(arg)
|
||||
if err != nil {
|
||||
{
|
||||
if arg, ok := arg.(driver.Valuer); ok {
|
||||
v, err := callValuerValue(arg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return eqb.encodeExtendedParamValue(ci, oid, formatCode, v)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return eqb.encodeExtendedParamValue(ci, oid, formatCode, value)
|
||||
}
|
||||
|
||||
// There is no data type registered for the destination OID, but maybe there is data type registered for the arg
|
||||
// type. If so use it's text encoder (if available).
|
||||
if dt, ok := ci.DataTypeForValue(arg); ok {
|
||||
value := dt.Value
|
||||
if textEncoder, ok := value.(pgtype.TextEncoder); ok {
|
||||
err := value.Set(arg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf, err = textEncoder.EncodeText(ci, eqb.paramValueBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if buf == nil {
|
||||
return nil, nil
|
||||
}
|
||||
eqb.paramValueBytes = buf
|
||||
return eqb.paramValueBytes[pos:], nil
|
||||
}
|
||||
}
|
||||
|
||||
if strippedArg, ok := stripNamedType(&refVal); ok {
|
||||
return eqb.encodeExtendedParamValue(ci, oid, formatCode, strippedArg)
|
||||
}
|
||||
return nil, SerializationError(fmt.Sprintf("Cannot encode %T into oid %v - %T must implement Encoder or be converted to a string", arg, oid, arg))
|
||||
}
|
||||
61
vendor/github.com/jackc/pgx/v4/go_stdlib.go
generated
vendored
Normal file
61
vendor/github.com/jackc/pgx/v4/go_stdlib.go
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// This file contains code copied from the Go standard library due to the
|
||||
// required function not being public.
|
||||
|
||||
// Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// From database/sql/convert.go
|
||||
|
||||
var valuerReflectType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
|
||||
|
||||
// callValuerValue returns vr.Value(), with one exception:
|
||||
// If vr.Value is an auto-generated method on a pointer type and the
|
||||
// pointer is nil, it would panic at runtime in the panicwrap
|
||||
// method. Treat it like nil instead.
|
||||
// Issue 8415.
|
||||
//
|
||||
// This is so people can implement driver.Value on value types and
|
||||
// still use nil pointers to those types to mean nil/NULL, just like
|
||||
// string/*string.
|
||||
//
|
||||
// This function is mirrored in the database/sql/driver package.
|
||||
func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
|
||||
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Ptr &&
|
||||
rv.IsNil() &&
|
||||
rv.Type().Elem().Implements(valuerReflectType) {
|
||||
return nil, nil
|
||||
}
|
||||
return vr.Value()
|
||||
}
|
||||
326
vendor/github.com/jackc/pgx/v4/internal/sanitize/sanitize.go
generated
vendored
Normal file
326
vendor/github.com/jackc/pgx/v4/internal/sanitize/sanitize.go
generated
vendored
Normal file
@@ -0,0 +1,326 @@
|
||||
package sanitize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Part is either a string or an int. A string is raw SQL. An int is a
|
||||
// argument placeholder.
|
||||
type Part interface{}
|
||||
|
||||
type Query struct {
|
||||
Parts []Part
|
||||
}
|
||||
|
||||
// utf.DecodeRune returns the utf8.RuneError for errors. But that is actually rune U+FFFD -- the unicode replacement
|
||||
// character. utf8.RuneError is not an error if it is also width 3.
|
||||
//
|
||||
// https://github.com/jackc/pgx/issues/1380
|
||||
const replacementcharacterwidth = 3
|
||||
|
||||
func (q *Query) Sanitize(args ...interface{}) (string, error) {
|
||||
argUse := make([]bool, len(args))
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
for _, part := range q.Parts {
|
||||
var str string
|
||||
switch part := part.(type) {
|
||||
case string:
|
||||
str = part
|
||||
case int:
|
||||
argIdx := part - 1
|
||||
if argIdx >= len(args) {
|
||||
return "", fmt.Errorf("insufficient arguments")
|
||||
}
|
||||
arg := args[argIdx]
|
||||
switch arg := arg.(type) {
|
||||
case nil:
|
||||
str = "null"
|
||||
case int64:
|
||||
str = strconv.FormatInt(arg, 10)
|
||||
case float64:
|
||||
str = strconv.FormatFloat(arg, 'f', -1, 64)
|
||||
case bool:
|
||||
str = strconv.FormatBool(arg)
|
||||
case []byte:
|
||||
str = QuoteBytes(arg)
|
||||
case string:
|
||||
str = QuoteString(arg)
|
||||
case time.Time:
|
||||
str = arg.Truncate(time.Microsecond).Format("'2006-01-02 15:04:05.999999999Z07:00:00'")
|
||||
default:
|
||||
return "", fmt.Errorf("invalid arg type: %T", arg)
|
||||
}
|
||||
argUse[argIdx] = true
|
||||
|
||||
// Prevent SQL injection via Line Comment Creation
|
||||
// https://github.com/jackc/pgx/security/advisories/GHSA-m7wr-2xf7-cm9p
|
||||
str = " " + str + " "
|
||||
default:
|
||||
return "", fmt.Errorf("invalid Part type: %T", part)
|
||||
}
|
||||
buf.WriteString(str)
|
||||
}
|
||||
|
||||
for i, used := range argUse {
|
||||
if !used {
|
||||
return "", fmt.Errorf("unused argument: %d", i)
|
||||
}
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func NewQuery(sql string) (*Query, error) {
|
||||
l := &sqlLexer{
|
||||
src: sql,
|
||||
stateFn: rawState,
|
||||
}
|
||||
|
||||
for l.stateFn != nil {
|
||||
l.stateFn = l.stateFn(l)
|
||||
}
|
||||
|
||||
query := &Query{Parts: l.parts}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
func QuoteString(str string) string {
|
||||
return "'" + strings.ReplaceAll(str, "'", "''") + "'"
|
||||
}
|
||||
|
||||
func QuoteBytes(buf []byte) string {
|
||||
return `'\x` + hex.EncodeToString(buf) + "'"
|
||||
}
|
||||
|
||||
type sqlLexer struct {
|
||||
src string
|
||||
start int
|
||||
pos int
|
||||
nested int // multiline comment nesting level.
|
||||
stateFn stateFn
|
||||
parts []Part
|
||||
}
|
||||
|
||||
type stateFn func(*sqlLexer) stateFn
|
||||
|
||||
func rawState(l *sqlLexer) stateFn {
|
||||
for {
|
||||
r, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
l.pos += width
|
||||
|
||||
switch r {
|
||||
case 'e', 'E':
|
||||
nextRune, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
if nextRune == '\'' {
|
||||
l.pos += width
|
||||
return escapeStringState
|
||||
}
|
||||
case '\'':
|
||||
return singleQuoteState
|
||||
case '"':
|
||||
return doubleQuoteState
|
||||
case '$':
|
||||
nextRune, _ := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
if '0' <= nextRune && nextRune <= '9' {
|
||||
if l.pos-l.start > 0 {
|
||||
l.parts = append(l.parts, l.src[l.start:l.pos-width])
|
||||
}
|
||||
l.start = l.pos
|
||||
return placeholderState
|
||||
}
|
||||
case '-':
|
||||
nextRune, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
if nextRune == '-' {
|
||||
l.pos += width
|
||||
return oneLineCommentState
|
||||
}
|
||||
case '/':
|
||||
nextRune, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
if nextRune == '*' {
|
||||
l.pos += width
|
||||
return multilineCommentState
|
||||
}
|
||||
case utf8.RuneError:
|
||||
if width != replacementcharacterwidth {
|
||||
if l.pos-l.start > 0 {
|
||||
l.parts = append(l.parts, l.src[l.start:l.pos])
|
||||
l.start = l.pos
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func singleQuoteState(l *sqlLexer) stateFn {
|
||||
for {
|
||||
r, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
l.pos += width
|
||||
|
||||
switch r {
|
||||
case '\'':
|
||||
nextRune, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
if nextRune != '\'' {
|
||||
return rawState
|
||||
}
|
||||
l.pos += width
|
||||
case utf8.RuneError:
|
||||
if width != replacementcharacterwidth {
|
||||
if l.pos-l.start > 0 {
|
||||
l.parts = append(l.parts, l.src[l.start:l.pos])
|
||||
l.start = l.pos
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func doubleQuoteState(l *sqlLexer) stateFn {
|
||||
for {
|
||||
r, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
l.pos += width
|
||||
|
||||
switch r {
|
||||
case '"':
|
||||
nextRune, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
if nextRune != '"' {
|
||||
return rawState
|
||||
}
|
||||
l.pos += width
|
||||
case utf8.RuneError:
|
||||
if width != replacementcharacterwidth {
|
||||
if l.pos-l.start > 0 {
|
||||
l.parts = append(l.parts, l.src[l.start:l.pos])
|
||||
l.start = l.pos
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// placeholderState consumes a placeholder value. The $ must have already has
|
||||
// already been consumed. The first rune must be a digit.
|
||||
func placeholderState(l *sqlLexer) stateFn {
|
||||
num := 0
|
||||
|
||||
for {
|
||||
r, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
l.pos += width
|
||||
|
||||
if '0' <= r && r <= '9' {
|
||||
num *= 10
|
||||
num += int(r - '0')
|
||||
} else {
|
||||
l.parts = append(l.parts, num)
|
||||
l.pos -= width
|
||||
l.start = l.pos
|
||||
return rawState
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func escapeStringState(l *sqlLexer) stateFn {
|
||||
for {
|
||||
r, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
l.pos += width
|
||||
|
||||
switch r {
|
||||
case '\\':
|
||||
_, width = utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
l.pos += width
|
||||
case '\'':
|
||||
nextRune, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
if nextRune != '\'' {
|
||||
return rawState
|
||||
}
|
||||
l.pos += width
|
||||
case utf8.RuneError:
|
||||
if width != replacementcharacterwidth {
|
||||
if l.pos-l.start > 0 {
|
||||
l.parts = append(l.parts, l.src[l.start:l.pos])
|
||||
l.start = l.pos
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func oneLineCommentState(l *sqlLexer) stateFn {
|
||||
for {
|
||||
r, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
l.pos += width
|
||||
|
||||
switch r {
|
||||
case '\\':
|
||||
_, width = utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
l.pos += width
|
||||
case '\n', '\r':
|
||||
return rawState
|
||||
case utf8.RuneError:
|
||||
if width != replacementcharacterwidth {
|
||||
if l.pos-l.start > 0 {
|
||||
l.parts = append(l.parts, l.src[l.start:l.pos])
|
||||
l.start = l.pos
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func multilineCommentState(l *sqlLexer) stateFn {
|
||||
for {
|
||||
r, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
l.pos += width
|
||||
|
||||
switch r {
|
||||
case '/':
|
||||
nextRune, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
if nextRune == '*' {
|
||||
l.pos += width
|
||||
l.nested++
|
||||
}
|
||||
case '*':
|
||||
nextRune, width := utf8.DecodeRuneInString(l.src[l.pos:])
|
||||
if nextRune != '/' {
|
||||
continue
|
||||
}
|
||||
|
||||
l.pos += width
|
||||
if l.nested == 0 {
|
||||
return rawState
|
||||
}
|
||||
l.nested--
|
||||
|
||||
case utf8.RuneError:
|
||||
if width != replacementcharacterwidth {
|
||||
if l.pos-l.start > 0 {
|
||||
l.parts = append(l.parts, l.src[l.start:l.pos])
|
||||
l.start = l.pos
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SanitizeSQL replaces placeholder values with args. It quotes and escapes args
|
||||
// as necessary. This function is only safe when standard_conforming_strings is
|
||||
// on.
|
||||
func SanitizeSQL(sql string, args ...interface{}) (string, error) {
|
||||
query, err := NewQuery(sql)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return query.Sanitize(args...)
|
||||
}
|
||||
121
vendor/github.com/jackc/pgx/v4/large_objects.go
generated
vendored
Normal file
121
vendor/github.com/jackc/pgx/v4/large_objects.go
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
// LargeObjects is a structure used to access the large objects API. It is only valid within the transaction where it
|
||||
// was created.
|
||||
//
|
||||
// For more details see: http://www.postgresql.org/docs/current/static/largeobjects.html
|
||||
type LargeObjects struct {
|
||||
tx Tx
|
||||
}
|
||||
|
||||
type LargeObjectMode int32
|
||||
|
||||
const (
|
||||
LargeObjectModeWrite LargeObjectMode = 0x20000
|
||||
LargeObjectModeRead LargeObjectMode = 0x40000
|
||||
)
|
||||
|
||||
// Create creates a new large object. If oid is zero, the server assigns an unused OID.
|
||||
func (o *LargeObjects) Create(ctx context.Context, oid uint32) (uint32, error) {
|
||||
err := o.tx.QueryRow(ctx, "select lo_create($1)", oid).Scan(&oid)
|
||||
return oid, err
|
||||
}
|
||||
|
||||
// Open opens an existing large object with the given mode. ctx will also be used for all operations on the opened large
|
||||
// object.
|
||||
func (o *LargeObjects) Open(ctx context.Context, oid uint32, mode LargeObjectMode) (*LargeObject, error) {
|
||||
var fd int32
|
||||
err := o.tx.QueryRow(ctx, "select lo_open($1, $2)", oid, mode).Scan(&fd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &LargeObject{fd: fd, tx: o.tx, ctx: ctx}, nil
|
||||
}
|
||||
|
||||
// Unlink removes a large object from the database.
|
||||
func (o *LargeObjects) Unlink(ctx context.Context, oid uint32) error {
|
||||
var result int32
|
||||
err := o.tx.QueryRow(ctx, "select lo_unlink($1)", oid).Scan(&result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result != 1 {
|
||||
return errors.New("failed to remove large object")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// A LargeObject is a large object stored on the server. It is only valid within the transaction that it was initialized
|
||||
// in. It uses the context it was initialized with for all operations. It implements these interfaces:
|
||||
//
|
||||
// io.Writer
|
||||
// io.Reader
|
||||
// io.Seeker
|
||||
// io.Closer
|
||||
type LargeObject struct {
|
||||
ctx context.Context
|
||||
tx Tx
|
||||
fd int32
|
||||
}
|
||||
|
||||
// Write writes p to the large object and returns the number of bytes written and an error if not all of p was written.
|
||||
func (o *LargeObject) Write(p []byte) (int, error) {
|
||||
var n int
|
||||
err := o.tx.QueryRow(o.ctx, "select lowrite($1, $2)", o.fd, p).Scan(&n)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
||||
if n < 0 {
|
||||
return 0, errors.New("failed to write to large object")
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Read reads up to len(p) bytes into p returning the number of bytes read.
|
||||
func (o *LargeObject) Read(p []byte) (int, error) {
|
||||
var res []byte
|
||||
err := o.tx.QueryRow(o.ctx, "select loread($1, $2)", o.fd, len(p)).Scan(&res)
|
||||
copy(p, res)
|
||||
if err != nil {
|
||||
return len(res), err
|
||||
}
|
||||
|
||||
if len(res) < len(p) {
|
||||
err = io.EOF
|
||||
}
|
||||
return len(res), err
|
||||
}
|
||||
|
||||
// Seek moves the current location pointer to the new location specified by offset.
|
||||
func (o *LargeObject) Seek(offset int64, whence int) (n int64, err error) {
|
||||
err = o.tx.QueryRow(o.ctx, "select lo_lseek64($1, $2, $3)", o.fd, offset, whence).Scan(&n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Tell returns the current read or write location of the large object descriptor.
|
||||
func (o *LargeObject) Tell() (n int64, err error) {
|
||||
err = o.tx.QueryRow(o.ctx, "select lo_tell64($1)", o.fd).Scan(&n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Truncate the large object to size.
|
||||
func (o *LargeObject) Truncate(size int64) (err error) {
|
||||
_, err = o.tx.Exec(o.ctx, "select lo_truncate64($1, $2)", o.fd, size)
|
||||
return err
|
||||
}
|
||||
|
||||
// Close the large object descriptor.
|
||||
func (o *LargeObject) Close() error {
|
||||
_, err := o.tx.Exec(o.ctx, "select lo_close($1)", o.fd)
|
||||
return err
|
||||
}
|
||||
107
vendor/github.com/jackc/pgx/v4/logger.go
generated
vendored
Normal file
107
vendor/github.com/jackc/pgx/v4/logger.go
generated
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// The values for log levels are chosen such that the zero value means that no
|
||||
// log level was specified.
|
||||
const (
|
||||
LogLevelTrace = 6
|
||||
LogLevelDebug = 5
|
||||
LogLevelInfo = 4
|
||||
LogLevelWarn = 3
|
||||
LogLevelError = 2
|
||||
LogLevelNone = 1
|
||||
)
|
||||
|
||||
// LogLevel represents the pgx logging level. See LogLevel* constants for
|
||||
// possible values.
|
||||
type LogLevel int
|
||||
|
||||
func (ll LogLevel) String() string {
|
||||
switch ll {
|
||||
case LogLevelTrace:
|
||||
return "trace"
|
||||
case LogLevelDebug:
|
||||
return "debug"
|
||||
case LogLevelInfo:
|
||||
return "info"
|
||||
case LogLevelWarn:
|
||||
return "warn"
|
||||
case LogLevelError:
|
||||
return "error"
|
||||
case LogLevelNone:
|
||||
return "none"
|
||||
default:
|
||||
return fmt.Sprintf("invalid level %d", ll)
|
||||
}
|
||||
}
|
||||
|
||||
// Logger is the interface used to get logging from pgx internals.
|
||||
type Logger interface {
|
||||
// Log a message at the given level with data key/value pairs. data may be nil.
|
||||
Log(ctx context.Context, level LogLevel, msg string, data map[string]interface{})
|
||||
}
|
||||
|
||||
// LoggerFunc is a wrapper around a function to satisfy the pgx.Logger interface
|
||||
type LoggerFunc func(ctx context.Context, level LogLevel, msg string, data map[string]interface{})
|
||||
|
||||
// Log delegates the logging request to the wrapped function
|
||||
func (f LoggerFunc) Log(ctx context.Context, level LogLevel, msg string, data map[string]interface{}) {
|
||||
f(ctx, level, msg, data)
|
||||
}
|
||||
|
||||
// LogLevelFromString converts log level string to constant
|
||||
//
|
||||
// Valid levels:
|
||||
//
|
||||
// trace
|
||||
// debug
|
||||
// info
|
||||
// warn
|
||||
// error
|
||||
// none
|
||||
func LogLevelFromString(s string) (LogLevel, error) {
|
||||
switch s {
|
||||
case "trace":
|
||||
return LogLevelTrace, nil
|
||||
case "debug":
|
||||
return LogLevelDebug, nil
|
||||
case "info":
|
||||
return LogLevelInfo, nil
|
||||
case "warn":
|
||||
return LogLevelWarn, nil
|
||||
case "error":
|
||||
return LogLevelError, nil
|
||||
case "none":
|
||||
return LogLevelNone, nil
|
||||
default:
|
||||
return 0, errors.New("invalid log level")
|
||||
}
|
||||
}
|
||||
|
||||
func logQueryArgs(args []interface{}) []interface{} {
|
||||
logArgs := make([]interface{}, 0, len(args))
|
||||
|
||||
for _, a := range args {
|
||||
switch v := a.(type) {
|
||||
case []byte:
|
||||
if len(v) < 64 {
|
||||
a = hex.EncodeToString(v)
|
||||
} else {
|
||||
a = fmt.Sprintf("%x (truncated %d bytes)", v[:64], len(v)-64)
|
||||
}
|
||||
case string:
|
||||
if len(v) > 64 {
|
||||
a = fmt.Sprintf("%s (truncated %d bytes)", v[:64], len(v)-64)
|
||||
}
|
||||
}
|
||||
logArgs = append(logArgs, a)
|
||||
}
|
||||
|
||||
return logArgs
|
||||
}
|
||||
23
vendor/github.com/jackc/pgx/v4/messages.go
generated
vendored
Normal file
23
vendor/github.com/jackc/pgx/v4/messages.go
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
|
||||
"github.com/jackc/pgtype"
|
||||
)
|
||||
|
||||
func convertDriverValuers(args []interface{}) ([]interface{}, error) {
|
||||
for i, arg := range args {
|
||||
switch arg := arg.(type) {
|
||||
case pgtype.BinaryEncoder:
|
||||
case pgtype.TextEncoder:
|
||||
case driver.Valuer:
|
||||
v, err := callValuerValue(arg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args[i] = v
|
||||
}
|
||||
}
|
||||
return args, nil
|
||||
}
|
||||
60
vendor/github.com/jackc/pgx/v4/pgxpool/batch_results.go
generated
vendored
Normal file
60
vendor/github.com/jackc/pgx/v4/pgxpool/batch_results.go
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
package pgxpool
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type errBatchResults struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (br errBatchResults) Exec() (pgconn.CommandTag, error) {
|
||||
return nil, br.err
|
||||
}
|
||||
|
||||
func (br errBatchResults) Query() (pgx.Rows, error) {
|
||||
return errRows{err: br.err}, br.err
|
||||
}
|
||||
|
||||
func (br errBatchResults) QueryFunc(scans []interface{}, f func(pgx.QueryFuncRow) error) (pgconn.CommandTag, error) {
|
||||
return nil, br.err
|
||||
}
|
||||
|
||||
func (br errBatchResults) QueryRow() pgx.Row {
|
||||
return errRow{err: br.err}
|
||||
}
|
||||
|
||||
func (br errBatchResults) Close() error {
|
||||
return br.err
|
||||
}
|
||||
|
||||
type poolBatchResults struct {
|
||||
br pgx.BatchResults
|
||||
c *Conn
|
||||
}
|
||||
|
||||
func (br *poolBatchResults) Exec() (pgconn.CommandTag, error) {
|
||||
return br.br.Exec()
|
||||
}
|
||||
|
||||
func (br *poolBatchResults) Query() (pgx.Rows, error) {
|
||||
return br.br.Query()
|
||||
}
|
||||
|
||||
func (br *poolBatchResults) QueryFunc(scans []interface{}, f func(pgx.QueryFuncRow) error) (pgconn.CommandTag, error) {
|
||||
return br.br.QueryFunc(scans, f)
|
||||
}
|
||||
|
||||
func (br *poolBatchResults) QueryRow() pgx.Row {
|
||||
return br.br.QueryRow()
|
||||
}
|
||||
|
||||
func (br *poolBatchResults) Close() error {
|
||||
err := br.br.Close()
|
||||
if br.c != nil {
|
||||
br.c.Release()
|
||||
br.c = nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
142
vendor/github.com/jackc/pgx/v4/pgxpool/conn.go
generated
vendored
Normal file
142
vendor/github.com/jackc/pgx/v4/pgxpool/conn.go
generated
vendored
Normal file
@@ -0,0 +1,142 @@
|
||||
package pgxpool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/jackc/puddle"
|
||||
)
|
||||
|
||||
// Conn is an acquired *pgx.Conn from a Pool.
|
||||
type Conn struct {
|
||||
res *puddle.Resource
|
||||
p *Pool
|
||||
}
|
||||
|
||||
// Release returns c to the pool it was acquired from. Once Release has been called, other methods must not be called.
|
||||
// However, it is safe to call Release multiple times. Subsequent calls after the first will be ignored.
|
||||
func (c *Conn) Release() {
|
||||
if c.res == nil {
|
||||
return
|
||||
}
|
||||
|
||||
conn := c.Conn()
|
||||
res := c.res
|
||||
c.res = nil
|
||||
|
||||
if conn.IsClosed() || conn.PgConn().IsBusy() || conn.PgConn().TxStatus() != 'I' {
|
||||
res.Destroy()
|
||||
// Signal to the health check to run since we just destroyed a connections
|
||||
// and we might be below minConns now
|
||||
c.p.triggerHealthCheck()
|
||||
return
|
||||
}
|
||||
|
||||
// If the pool is consistently being used, we might never get to check the
|
||||
// lifetime of a connection since we only check idle connections in checkConnsHealth
|
||||
// so we also check the lifetime here and force a health check
|
||||
if c.p.isExpired(res) {
|
||||
atomic.AddInt64(&c.p.lifetimeDestroyCount, 1)
|
||||
res.Destroy()
|
||||
// Signal to the health check to run since we just destroyed a connections
|
||||
// and we might be below minConns now
|
||||
c.p.triggerHealthCheck()
|
||||
return
|
||||
}
|
||||
|
||||
if c.p.afterRelease == nil {
|
||||
res.Release()
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
if c.p.afterRelease(conn) {
|
||||
res.Release()
|
||||
} else {
|
||||
res.Destroy()
|
||||
// Signal to the health check to run since we just destroyed a connections
|
||||
// and we might be below minConns now
|
||||
c.p.triggerHealthCheck()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Hijack assumes ownership of the connection from the pool. Caller is responsible for closing the connection. Hijack
|
||||
// will panic if called on an already released or hijacked connection.
|
||||
func (c *Conn) Hijack() *pgx.Conn {
|
||||
if c.res == nil {
|
||||
panic("cannot hijack already released or hijacked connection")
|
||||
}
|
||||
|
||||
conn := c.Conn()
|
||||
res := c.res
|
||||
c.res = nil
|
||||
|
||||
res.Hijack()
|
||||
|
||||
return conn
|
||||
}
|
||||
|
||||
func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
|
||||
return c.Conn().Exec(ctx, sql, arguments...)
|
||||
}
|
||||
|
||||
func (c *Conn) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) {
|
||||
return c.Conn().Query(ctx, sql, args...)
|
||||
}
|
||||
|
||||
func (c *Conn) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
|
||||
return c.Conn().QueryRow(ctx, sql, args...)
|
||||
}
|
||||
|
||||
func (c *Conn) QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(pgx.QueryFuncRow) error) (pgconn.CommandTag, error) {
|
||||
return c.Conn().QueryFunc(ctx, sql, args, scans, f)
|
||||
}
|
||||
|
||||
func (c *Conn) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
|
||||
return c.Conn().SendBatch(ctx, b)
|
||||
}
|
||||
|
||||
func (c *Conn) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) {
|
||||
return c.Conn().CopyFrom(ctx, tableName, columnNames, rowSrc)
|
||||
}
|
||||
|
||||
// Begin starts a transaction block from the *Conn without explicitly setting a transaction mode (see BeginTx with TxOptions if transaction mode is required).
|
||||
func (c *Conn) Begin(ctx context.Context) (pgx.Tx, error) {
|
||||
return c.Conn().Begin(ctx)
|
||||
}
|
||||
|
||||
// BeginTx starts a transaction block from the *Conn with txOptions determining the transaction mode.
|
||||
func (c *Conn) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) {
|
||||
return c.Conn().BeginTx(ctx, txOptions)
|
||||
}
|
||||
|
||||
func (c *Conn) BeginFunc(ctx context.Context, f func(pgx.Tx) error) error {
|
||||
return c.Conn().BeginFunc(ctx, f)
|
||||
}
|
||||
|
||||
func (c *Conn) BeginTxFunc(ctx context.Context, txOptions pgx.TxOptions, f func(pgx.Tx) error) error {
|
||||
return c.Conn().BeginTxFunc(ctx, txOptions, f)
|
||||
}
|
||||
|
||||
func (c *Conn) Ping(ctx context.Context) error {
|
||||
return c.Conn().Ping(ctx)
|
||||
}
|
||||
|
||||
func (c *Conn) Conn() *pgx.Conn {
|
||||
return c.connResource().conn
|
||||
}
|
||||
|
||||
func (c *Conn) connResource() *connResource {
|
||||
return c.res.Value().(*connResource)
|
||||
}
|
||||
|
||||
func (c *Conn) getPoolRow(r pgx.Row) *poolRow {
|
||||
return c.connResource().getPoolRow(c, r)
|
||||
}
|
||||
|
||||
func (c *Conn) getPoolRows(r pgx.Rows) *poolRows {
|
||||
return c.connResource().getPoolRows(c, r)
|
||||
}
|
||||
25
vendor/github.com/jackc/pgx/v4/pgxpool/doc.go
generated
vendored
Normal file
25
vendor/github.com/jackc/pgx/v4/pgxpool/doc.go
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
// Package pgxpool is a concurrency-safe connection pool for pgx.
|
||||
/*
|
||||
pgxpool implements a nearly identical interface to pgx connections.
|
||||
|
||||
Establishing a Connection
|
||||
|
||||
The primary way of establishing a connection is with `pgxpool.Connect`.
|
||||
|
||||
pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
|
||||
|
||||
The database connection string can be in URL or DSN format. PostgreSQL settings, pgx settings, and pool settings can be
|
||||
specified here. In addition, a config struct can be created by `ParseConfig` and modified before establishing the
|
||||
connection with `ConnectConfig`.
|
||||
|
||||
config, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
|
||||
if err != nil {
|
||||
// ...
|
||||
}
|
||||
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
|
||||
// do something with every new connection
|
||||
}
|
||||
|
||||
pool, err := pgxpool.ConnectConfig(context.Background(), config)
|
||||
*/
|
||||
package pgxpool
|
||||
730
vendor/github.com/jackc/pgx/v4/pgxpool/pool.go
generated
vendored
Normal file
730
vendor/github.com/jackc/pgx/v4/pgxpool/pool.go
generated
vendored
Normal file
@@ -0,0 +1,730 @@
|
||||
package pgxpool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/jackc/puddle"
|
||||
)
|
||||
|
||||
var defaultMaxConns = int32(4)
|
||||
var defaultMinConns = int32(0)
|
||||
var defaultMaxConnLifetime = time.Hour
|
||||
var defaultMaxConnIdleTime = time.Minute * 30
|
||||
var defaultHealthCheckPeriod = time.Minute
|
||||
|
||||
type connResource struct {
|
||||
conn *pgx.Conn
|
||||
conns []Conn
|
||||
poolRows []poolRow
|
||||
poolRowss []poolRows
|
||||
}
|
||||
|
||||
func (cr *connResource) getConn(p *Pool, res *puddle.Resource) *Conn {
|
||||
if len(cr.conns) == 0 {
|
||||
cr.conns = make([]Conn, 128)
|
||||
}
|
||||
|
||||
c := &cr.conns[len(cr.conns)-1]
|
||||
cr.conns = cr.conns[0 : len(cr.conns)-1]
|
||||
|
||||
c.res = res
|
||||
c.p = p
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (cr *connResource) getPoolRow(c *Conn, r pgx.Row) *poolRow {
|
||||
if len(cr.poolRows) == 0 {
|
||||
cr.poolRows = make([]poolRow, 128)
|
||||
}
|
||||
|
||||
pr := &cr.poolRows[len(cr.poolRows)-1]
|
||||
cr.poolRows = cr.poolRows[0 : len(cr.poolRows)-1]
|
||||
|
||||
pr.c = c
|
||||
pr.r = r
|
||||
|
||||
return pr
|
||||
}
|
||||
|
||||
func (cr *connResource) getPoolRows(c *Conn, r pgx.Rows) *poolRows {
|
||||
if len(cr.poolRowss) == 0 {
|
||||
cr.poolRowss = make([]poolRows, 128)
|
||||
}
|
||||
|
||||
pr := &cr.poolRowss[len(cr.poolRowss)-1]
|
||||
cr.poolRowss = cr.poolRowss[0 : len(cr.poolRowss)-1]
|
||||
|
||||
pr.c = c
|
||||
pr.r = r
|
||||
|
||||
return pr
|
||||
}
|
||||
|
||||
// detachedCtx wraps a context and will never be canceled, regardless of if
|
||||
// the wrapped one is cancelled. The Err() method will never return any errors.
|
||||
type detachedCtx struct {
|
||||
context.Context
|
||||
}
|
||||
|
||||
func (detachedCtx) Done() <-chan struct{} { return nil }
|
||||
func (detachedCtx) Deadline() (time.Time, bool) { return time.Time{}, false }
|
||||
func (detachedCtx) Err() error { return nil }
|
||||
|
||||
// Pool allows for connection reuse.
|
||||
type Pool struct {
|
||||
// 64 bit fields accessed with atomics must be at beginning of struct to guarantee alignment for certain 32-bit
|
||||
// architectures. See BUGS section of https://pkg.go.dev/sync/atomic and https://github.com/jackc/pgx/issues/1288.
|
||||
newConnsCount int64
|
||||
lifetimeDestroyCount int64
|
||||
idleDestroyCount int64
|
||||
|
||||
p *puddle.Pool
|
||||
config *Config
|
||||
beforeConnect func(context.Context, *pgx.ConnConfig) error
|
||||
afterConnect func(context.Context, *pgx.Conn) error
|
||||
beforeAcquire func(context.Context, *pgx.Conn) bool
|
||||
afterRelease func(*pgx.Conn) bool
|
||||
minConns int32
|
||||
maxConns int32
|
||||
maxConnLifetime time.Duration
|
||||
maxConnLifetimeJitter time.Duration
|
||||
maxConnIdleTime time.Duration
|
||||
healthCheckPeriod time.Duration
|
||||
healthCheckChan chan struct{}
|
||||
|
||||
closeOnce sync.Once
|
||||
closeChan chan struct{}
|
||||
}
|
||||
|
||||
// Config is the configuration struct for creating a pool. It must be created by ParseConfig and then it can be
|
||||
// modified. A manually initialized ConnConfig will cause ConnectConfig to panic.
|
||||
type Config struct {
|
||||
ConnConfig *pgx.ConnConfig
|
||||
|
||||
// BeforeConnect is called before a new connection is made. It is passed a copy of the underlying pgx.ConnConfig and
|
||||
// will not impact any existing open connections.
|
||||
BeforeConnect func(context.Context, *pgx.ConnConfig) error
|
||||
|
||||
// AfterConnect is called after a connection is established, but before it is added to the pool.
|
||||
AfterConnect func(context.Context, *pgx.Conn) error
|
||||
|
||||
// BeforeAcquire is called before a connection is acquired from the pool. It must return true to allow the
|
||||
// acquision or false to indicate that the connection should be destroyed and a different connection should be
|
||||
// acquired.
|
||||
BeforeAcquire func(context.Context, *pgx.Conn) bool
|
||||
|
||||
// AfterRelease is called after a connection is released, but before it is returned to the pool. It must return true to
|
||||
// return the connection to the pool or false to destroy the connection.
|
||||
AfterRelease func(*pgx.Conn) bool
|
||||
|
||||
// MaxConnLifetime is the duration since creation after which a connection will be automatically closed.
|
||||
MaxConnLifetime time.Duration
|
||||
|
||||
// MaxConnLifetimeJitter is the duration after MaxConnLifetime to randomly decide to close a connection.
|
||||
// This helps prevent all connections from being closed at the exact same time, starving the pool.
|
||||
MaxConnLifetimeJitter time.Duration
|
||||
|
||||
// MaxConnIdleTime is the duration after which an idle connection will be automatically closed by the health check.
|
||||
MaxConnIdleTime time.Duration
|
||||
|
||||
// MaxConns is the maximum size of the pool. The default is the greater of 4 or runtime.NumCPU().
|
||||
MaxConns int32
|
||||
|
||||
// MinConns is the minimum size of the pool. After connection closes, the pool might dip below MinConns. A low
|
||||
// number of MinConns might mean the pool is empty after MaxConnLifetime until the health check has a chance
|
||||
// to create new connections.
|
||||
MinConns int32
|
||||
|
||||
// HealthCheckPeriod is the duration between checks of the health of idle connections.
|
||||
HealthCheckPeriod time.Duration
|
||||
|
||||
// If set to true, pool doesn't do any I/O operation on initialization.
|
||||
// And connects to the server only when the pool starts to be used.
|
||||
// The default is false.
|
||||
LazyConnect bool
|
||||
|
||||
createdByParseConfig bool // Used to enforce created by ParseConfig rule.
|
||||
}
|
||||
|
||||
// Copy returns a deep copy of the config that is safe to use and modify.
|
||||
// The only exception is the tls.Config:
|
||||
// according to the tls.Config docs it must not be modified after creation.
|
||||
func (c *Config) Copy() *Config {
|
||||
newConfig := new(Config)
|
||||
*newConfig = *c
|
||||
newConfig.ConnConfig = c.ConnConfig.Copy()
|
||||
return newConfig
|
||||
}
|
||||
|
||||
// ConnString returns the connection string as parsed by pgxpool.ParseConfig into pgxpool.Config.
|
||||
func (c *Config) ConnString() string { return c.ConnConfig.ConnString() }
|
||||
|
||||
// Connect creates a new Pool and immediately establishes one connection. ctx can be used to cancel this initial
|
||||
// connection. See ParseConfig for information on connString format.
|
||||
func Connect(ctx context.Context, connString string) (*Pool, error) {
|
||||
config, err := ParseConfig(connString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ConnectConfig(ctx, config)
|
||||
}
|
||||
|
||||
// ConnectConfig creates a new Pool and immediately establishes one connection. ctx can be used to cancel this initial
|
||||
// connection. config must have been created by ParseConfig.
|
||||
func ConnectConfig(ctx context.Context, config *Config) (*Pool, error) {
|
||||
// Default values are set in ParseConfig. Enforce initial creation by ParseConfig rather than setting defaults from
|
||||
// zero values.
|
||||
if !config.createdByParseConfig {
|
||||
panic("config must be created by ParseConfig")
|
||||
}
|
||||
|
||||
p := &Pool{
|
||||
config: config,
|
||||
beforeConnect: config.BeforeConnect,
|
||||
afterConnect: config.AfterConnect,
|
||||
beforeAcquire: config.BeforeAcquire,
|
||||
afterRelease: config.AfterRelease,
|
||||
minConns: config.MinConns,
|
||||
maxConns: config.MaxConns,
|
||||
maxConnLifetime: config.MaxConnLifetime,
|
||||
maxConnLifetimeJitter: config.MaxConnLifetimeJitter,
|
||||
maxConnIdleTime: config.MaxConnIdleTime,
|
||||
healthCheckPeriod: config.HealthCheckPeriod,
|
||||
healthCheckChan: make(chan struct{}, 1),
|
||||
closeChan: make(chan struct{}),
|
||||
}
|
||||
|
||||
p.p = puddle.NewPool(
|
||||
func(ctx context.Context) (interface{}, error) {
|
||||
// we ignore cancellation on the original context because its either from
|
||||
// the health check or its from a query and we don't want to cancel creating
|
||||
// a connection just because the original query was cancelled since that
|
||||
// could end up stampeding the server
|
||||
// this will keep any Values in the original context and will just ignore
|
||||
// cancellation
|
||||
// see https://github.com/jackc/pgx/issues/1259
|
||||
ctx = detachedCtx{ctx}
|
||||
|
||||
connConfig := p.config.ConnConfig.Copy()
|
||||
|
||||
// But we do want to ensure that a connect won't hang forever.
|
||||
if connConfig.ConnectTimeout <= 0 {
|
||||
connConfig.ConnectTimeout = 2 * time.Minute
|
||||
}
|
||||
|
||||
if p.beforeConnect != nil {
|
||||
if err := p.beforeConnect(ctx, connConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
conn, err := pgx.ConnectConfig(ctx, connConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if p.afterConnect != nil {
|
||||
err = p.afterConnect(ctx, conn)
|
||||
if err != nil {
|
||||
conn.Close(ctx)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
cr := &connResource{
|
||||
conn: conn,
|
||||
conns: make([]Conn, 64),
|
||||
poolRows: make([]poolRow, 64),
|
||||
poolRowss: make([]poolRows, 64),
|
||||
}
|
||||
|
||||
return cr, nil
|
||||
},
|
||||
func(value interface{}) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
conn := value.(*connResource).conn
|
||||
conn.Close(ctx)
|
||||
select {
|
||||
case <-conn.PgConn().CleanupDone():
|
||||
case <-ctx.Done():
|
||||
}
|
||||
cancel()
|
||||
},
|
||||
config.MaxConns,
|
||||
)
|
||||
|
||||
if !config.LazyConnect {
|
||||
if err := p.checkMinConnsWithContext(ctx); err != nil {
|
||||
// Couldn't create resources for minpool size. Close unhealthy pool.
|
||||
p.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Initially establish one connection
|
||||
res, err := p.p.Acquire(ctx)
|
||||
if err != nil {
|
||||
p.Close()
|
||||
return nil, err
|
||||
}
|
||||
res.Release()
|
||||
}
|
||||
|
||||
go p.backgroundHealthCheck()
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// ParseConfig builds a Config from connString. It parses connString with the same behavior as pgx.ParseConfig with the
|
||||
// addition of the following variables:
|
||||
//
|
||||
// pool_max_conns: integer greater than 0
|
||||
// pool_min_conns: integer 0 or greater
|
||||
// pool_max_conn_lifetime: duration string
|
||||
// pool_max_conn_idle_time: duration string
|
||||
// pool_health_check_period: duration string
|
||||
// pool_max_conn_lifetime_jitter: duration string
|
||||
//
|
||||
// See Config for definitions of these arguments.
|
||||
//
|
||||
// # Example DSN
|
||||
// user=jack password=secret host=pg.example.com port=5432 dbname=mydb sslmode=verify-ca pool_max_conns=10
|
||||
//
|
||||
// # Example URL
|
||||
// postgres://jack:secret@pg.example.com:5432/mydb?sslmode=verify-ca&pool_max_conns=10
|
||||
func ParseConfig(connString string) (*Config, error) {
|
||||
connConfig, err := pgx.ParseConfig(connString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config := &Config{
|
||||
ConnConfig: connConfig,
|
||||
createdByParseConfig: true,
|
||||
}
|
||||
|
||||
if s, ok := config.ConnConfig.Config.RuntimeParams["pool_max_conns"]; ok {
|
||||
delete(connConfig.Config.RuntimeParams, "pool_max_conns")
|
||||
n, err := strconv.ParseInt(s, 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse pool_max_conns: %w", err)
|
||||
}
|
||||
if n < 1 {
|
||||
return nil, fmt.Errorf("pool_max_conns too small: %d", n)
|
||||
}
|
||||
config.MaxConns = int32(n)
|
||||
} else {
|
||||
config.MaxConns = defaultMaxConns
|
||||
if numCPU := int32(runtime.NumCPU()); numCPU > config.MaxConns {
|
||||
config.MaxConns = numCPU
|
||||
}
|
||||
}
|
||||
|
||||
if s, ok := config.ConnConfig.Config.RuntimeParams["pool_min_conns"]; ok {
|
||||
delete(connConfig.Config.RuntimeParams, "pool_min_conns")
|
||||
n, err := strconv.ParseInt(s, 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse pool_min_conns: %w", err)
|
||||
}
|
||||
config.MinConns = int32(n)
|
||||
} else {
|
||||
config.MinConns = defaultMinConns
|
||||
}
|
||||
|
||||
if s, ok := config.ConnConfig.Config.RuntimeParams["pool_max_conn_lifetime"]; ok {
|
||||
delete(connConfig.Config.RuntimeParams, "pool_max_conn_lifetime")
|
||||
d, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid pool_max_conn_lifetime: %w", err)
|
||||
}
|
||||
config.MaxConnLifetime = d
|
||||
} else {
|
||||
config.MaxConnLifetime = defaultMaxConnLifetime
|
||||
}
|
||||
|
||||
if s, ok := config.ConnConfig.Config.RuntimeParams["pool_max_conn_idle_time"]; ok {
|
||||
delete(connConfig.Config.RuntimeParams, "pool_max_conn_idle_time")
|
||||
d, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid pool_max_conn_idle_time: %w", err)
|
||||
}
|
||||
config.MaxConnIdleTime = d
|
||||
} else {
|
||||
config.MaxConnIdleTime = defaultMaxConnIdleTime
|
||||
}
|
||||
|
||||
if s, ok := config.ConnConfig.Config.RuntimeParams["pool_health_check_period"]; ok {
|
||||
delete(connConfig.Config.RuntimeParams, "pool_health_check_period")
|
||||
d, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid pool_health_check_period: %w", err)
|
||||
}
|
||||
config.HealthCheckPeriod = d
|
||||
} else {
|
||||
config.HealthCheckPeriod = defaultHealthCheckPeriod
|
||||
}
|
||||
|
||||
if s, ok := config.ConnConfig.Config.RuntimeParams["pool_max_conn_lifetime_jitter"]; ok {
|
||||
delete(connConfig.Config.RuntimeParams, "pool_max_conn_lifetime_jitter")
|
||||
d, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid pool_max_conn_lifetime_jitter: %w", err)
|
||||
}
|
||||
config.MaxConnLifetimeJitter = d
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// Close closes all connections in the pool and rejects future Acquire calls. Blocks until all connections are returned
|
||||
// to pool and closed.
|
||||
func (p *Pool) Close() {
|
||||
p.closeOnce.Do(func() {
|
||||
close(p.closeChan)
|
||||
p.p.Close()
|
||||
})
|
||||
}
|
||||
|
||||
func (p *Pool) isExpired(res *puddle.Resource) bool {
|
||||
now := time.Now()
|
||||
// Small optimization to avoid rand. If it's over lifetime AND jitter, immediately
|
||||
// return true.
|
||||
if now.Sub(res.CreationTime()) > p.maxConnLifetime+p.maxConnLifetimeJitter {
|
||||
return true
|
||||
}
|
||||
if p.maxConnLifetimeJitter == 0 {
|
||||
return false
|
||||
}
|
||||
jitterSecs := rand.Float64() * p.maxConnLifetimeJitter.Seconds()
|
||||
return now.Sub(res.CreationTime()) > p.maxConnLifetime+(time.Duration(jitterSecs)*time.Second)
|
||||
}
|
||||
|
||||
func (p *Pool) triggerHealthCheck() {
|
||||
go func() {
|
||||
// Destroy is asynchronous so we give it time to actually remove itself from
|
||||
// the pool otherwise we might try to check the pool size too soon
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
select {
|
||||
case p.healthCheckChan <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (p *Pool) backgroundHealthCheck() {
|
||||
ticker := time.NewTicker(p.healthCheckPeriod)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-p.closeChan:
|
||||
return
|
||||
case <-p.healthCheckChan:
|
||||
p.checkHealth()
|
||||
case <-ticker.C:
|
||||
p.checkHealth()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Pool) checkHealth() {
|
||||
for {
|
||||
// If checkMinConns failed we don't destroy any connections since we couldn't
|
||||
// even get to minConns
|
||||
if err := p.checkMinConns(); err != nil {
|
||||
// Should we log this error somewhere?
|
||||
break
|
||||
}
|
||||
if !p.checkConnsHealth() {
|
||||
// Since we didn't destroy any connections we can stop looping
|
||||
break
|
||||
}
|
||||
// Technically Destroy is asynchronous but 500ms should be enough for it to
|
||||
// remove it from the underlying pool
|
||||
select {
|
||||
case <-p.closeChan:
|
||||
return
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// checkConnsHealth will check all idle connections, destroy a connection if
|
||||
// it's idle or too old, and returns true if any were destroyed
|
||||
func (p *Pool) checkConnsHealth() bool {
|
||||
var destroyed bool
|
||||
totalConns := p.Stat().TotalConns()
|
||||
resources := p.p.AcquireAllIdle()
|
||||
for _, res := range resources {
|
||||
// We're okay going under minConns if the lifetime is up
|
||||
if p.isExpired(res) && totalConns >= p.minConns {
|
||||
atomic.AddInt64(&p.lifetimeDestroyCount, 1)
|
||||
res.Destroy()
|
||||
destroyed = true
|
||||
// Since Destroy is async we manually decrement totalConns.
|
||||
totalConns--
|
||||
} else if res.IdleDuration() > p.maxConnIdleTime && totalConns > p.minConns {
|
||||
atomic.AddInt64(&p.idleDestroyCount, 1)
|
||||
res.Destroy()
|
||||
destroyed = true
|
||||
// Since Destroy is async we manually decrement totalConns.
|
||||
totalConns--
|
||||
} else {
|
||||
res.ReleaseUnused()
|
||||
}
|
||||
}
|
||||
return destroyed
|
||||
}
|
||||
|
||||
func (p *Pool) checkMinConnsWithContext(ctx context.Context) error {
|
||||
// TotalConns can include ones that are being destroyed but we should have
|
||||
// sleep(500ms) around all of the destroys to help prevent that from throwing
|
||||
// off this check
|
||||
toCreate := p.minConns - p.Stat().TotalConns()
|
||||
if toCreate > 0 {
|
||||
return p.createIdleResources(ctx, int(toCreate))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Pool) checkMinConns() error {
|
||||
return p.checkMinConnsWithContext(context.Background())
|
||||
}
|
||||
|
||||
func (p *Pool) createIdleResources(parentCtx context.Context, targetResources int) error {
|
||||
ctx, cancel := context.WithCancel(parentCtx)
|
||||
defer cancel()
|
||||
|
||||
errs := make(chan error, targetResources)
|
||||
|
||||
for i := 0; i < targetResources; i++ {
|
||||
go func() {
|
||||
atomic.AddInt64(&p.newConnsCount, 1)
|
||||
err := p.p.CreateResource(ctx)
|
||||
errs <- err
|
||||
}()
|
||||
}
|
||||
|
||||
var firstError error
|
||||
for i := 0; i < targetResources; i++ {
|
||||
err := <-errs
|
||||
if err != nil && firstError == nil {
|
||||
cancel()
|
||||
firstError = err
|
||||
}
|
||||
}
|
||||
|
||||
return firstError
|
||||
}
|
||||
|
||||
// Acquire returns a connection (*Conn) from the Pool
|
||||
func (p *Pool) Acquire(ctx context.Context) (*Conn, error) {
|
||||
for {
|
||||
res, err := p.p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cr := res.Value().(*connResource)
|
||||
if p.beforeAcquire == nil || p.beforeAcquire(ctx, cr.conn) {
|
||||
return cr.getConn(p, res), nil
|
||||
}
|
||||
|
||||
res.Destroy()
|
||||
}
|
||||
}
|
||||
|
||||
// AcquireFunc acquires a *Conn and calls f with that *Conn. ctx will only affect the Acquire. It has no effect on the
|
||||
// call of f. The return value is either an error acquiring the *Conn or the return value of f. The *Conn is
|
||||
// automatically released after the call of f.
|
||||
func (p *Pool) AcquireFunc(ctx context.Context, f func(*Conn) error) error {
|
||||
conn, err := p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
return f(conn)
|
||||
}
|
||||
|
||||
// AcquireAllIdle atomically acquires all currently idle connections. Its intended use is for health check and
|
||||
// keep-alive functionality. It does not update pool statistics.
|
||||
func (p *Pool) AcquireAllIdle(ctx context.Context) []*Conn {
|
||||
resources := p.p.AcquireAllIdle()
|
||||
conns := make([]*Conn, 0, len(resources))
|
||||
for _, res := range resources {
|
||||
cr := res.Value().(*connResource)
|
||||
if p.beforeAcquire == nil || p.beforeAcquire(ctx, cr.conn) {
|
||||
conns = append(conns, cr.getConn(p, res))
|
||||
} else {
|
||||
res.Destroy()
|
||||
}
|
||||
}
|
||||
|
||||
return conns
|
||||
}
|
||||
|
||||
// Config returns a copy of config that was used to initialize this pool.
|
||||
func (p *Pool) Config() *Config { return p.config.Copy() }
|
||||
|
||||
// Stat returns a pgxpool.Stat struct with a snapshot of Pool statistics.
|
||||
func (p *Pool) Stat() *Stat {
|
||||
return &Stat{
|
||||
s: p.p.Stat(),
|
||||
newConnsCount: atomic.LoadInt64(&p.newConnsCount),
|
||||
lifetimeDestroyCount: atomic.LoadInt64(&p.lifetimeDestroyCount),
|
||||
idleDestroyCount: atomic.LoadInt64(&p.idleDestroyCount),
|
||||
}
|
||||
}
|
||||
|
||||
// Exec acquires a connection from the Pool and executes the given SQL.
|
||||
// SQL can be either a prepared statement name or an SQL string.
|
||||
// Arguments should be referenced positionally from the SQL string as $1, $2, etc.
|
||||
// The acquired connection is returned to the pool when the Exec function returns.
|
||||
func (p *Pool) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
|
||||
c, err := p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer c.Release()
|
||||
|
||||
return c.Exec(ctx, sql, arguments...)
|
||||
}
|
||||
|
||||
// Query acquires a connection and executes a query that returns pgx.Rows.
|
||||
// Arguments should be referenced positionally from the SQL string as $1, $2, etc.
|
||||
// See pgx.Rows documentation to close the returned Rows and return the acquired connection to the Pool.
|
||||
//
|
||||
// If there is an error, the returned pgx.Rows will be returned in an error state.
|
||||
// If preferred, ignore the error returned from Query and handle errors using the returned pgx.Rows.
|
||||
//
|
||||
// For extra control over how the query is executed, the types QuerySimpleProtocol, QueryResultFormats, and
|
||||
// QueryResultFormatsByOID may be used as the first args to control exactly how the query is executed. This is rarely
|
||||
// needed. See the documentation for those types for details.
|
||||
func (p *Pool) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) {
|
||||
c, err := p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return errRows{err: err}, err
|
||||
}
|
||||
|
||||
rows, err := c.Query(ctx, sql, args...)
|
||||
if err != nil {
|
||||
c.Release()
|
||||
return errRows{err: err}, err
|
||||
}
|
||||
|
||||
return c.getPoolRows(rows), nil
|
||||
}
|
||||
|
||||
// QueryRow acquires a connection and executes a query that is expected
|
||||
// to return at most one row (pgx.Row). Errors are deferred until pgx.Row's
|
||||
// Scan method is called. If the query selects no rows, pgx.Row's Scan will
|
||||
// return ErrNoRows. Otherwise, pgx.Row's Scan scans the first selected row
|
||||
// and discards the rest. The acquired connection is returned to the Pool when
|
||||
// pgx.Row's Scan method is called.
|
||||
//
|
||||
// Arguments should be referenced positionally from the SQL string as $1, $2, etc.
|
||||
//
|
||||
// For extra control over how the query is executed, the types QuerySimpleProtocol, QueryResultFormats, and
|
||||
// QueryResultFormatsByOID may be used as the first args to control exactly how the query is executed. This is rarely
|
||||
// needed. See the documentation for those types for details.
|
||||
func (p *Pool) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
|
||||
c, err := p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return errRow{err: err}
|
||||
}
|
||||
|
||||
row := c.QueryRow(ctx, sql, args...)
|
||||
return c.getPoolRow(row)
|
||||
}
|
||||
|
||||
func (p *Pool) QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(pgx.QueryFuncRow) error) (pgconn.CommandTag, error) {
|
||||
c, err := p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer c.Release()
|
||||
|
||||
return c.QueryFunc(ctx, sql, args, scans, f)
|
||||
}
|
||||
|
||||
func (p *Pool) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
|
||||
c, err := p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return errBatchResults{err: err}
|
||||
}
|
||||
|
||||
br := c.SendBatch(ctx, b)
|
||||
return &poolBatchResults{br: br, c: c}
|
||||
}
|
||||
|
||||
// Begin acquires a connection from the Pool and starts a transaction. Unlike database/sql, the context only affects the begin command. i.e. there is no
|
||||
// auto-rollback on context cancellation. Begin initiates a transaction block without explicitly setting a transaction mode for the block (see BeginTx with TxOptions if transaction mode is required).
|
||||
// *pgxpool.Tx is returned, which implements the pgx.Tx interface.
|
||||
// Commit or Rollback must be called on the returned transaction to finalize the transaction block.
|
||||
func (p *Pool) Begin(ctx context.Context) (pgx.Tx, error) {
|
||||
return p.BeginTx(ctx, pgx.TxOptions{})
|
||||
}
|
||||
|
||||
// BeginTx acquires a connection from the Pool and starts a transaction with pgx.TxOptions determining the transaction mode.
|
||||
// Unlike database/sql, the context only affects the begin command. i.e. there is no auto-rollback on context cancellation.
|
||||
// *pgxpool.Tx is returned, which implements the pgx.Tx interface.
|
||||
// Commit or Rollback must be called on the returned transaction to finalize the transaction block.
|
||||
func (p *Pool) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) {
|
||||
c, err := p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
t, err := c.BeginTx(ctx, txOptions)
|
||||
if err != nil {
|
||||
c.Release()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Tx{t: t, c: c}, nil
|
||||
}
|
||||
|
||||
func (p *Pool) BeginFunc(ctx context.Context, f func(pgx.Tx) error) error {
|
||||
return p.BeginTxFunc(ctx, pgx.TxOptions{}, f)
|
||||
}
|
||||
|
||||
func (p *Pool) BeginTxFunc(ctx context.Context, txOptions pgx.TxOptions, f func(pgx.Tx) error) error {
|
||||
c, err := p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer c.Release()
|
||||
|
||||
return c.BeginTxFunc(ctx, txOptions, f)
|
||||
}
|
||||
|
||||
func (p *Pool) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) {
|
||||
c, err := p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer c.Release()
|
||||
|
||||
return c.Conn().CopyFrom(ctx, tableName, columnNames, rowSrc)
|
||||
}
|
||||
|
||||
// Ping acquires a connection from the Pool and executes an empty sql statement against it.
|
||||
// If the sql returns without error, the database Ping is considered successful, otherwise, the error is returned.
|
||||
func (p *Pool) Ping(ctx context.Context) error {
|
||||
c, err := p.Acquire(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer c.Release()
|
||||
return c.Ping(ctx)
|
||||
}
|
||||
105
vendor/github.com/jackc/pgx/v4/pgxpool/rows.go
generated
vendored
Normal file
105
vendor/github.com/jackc/pgx/v4/pgxpool/rows.go
generated
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
package pgxpool
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgproto3/v2"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type errRows struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (errRows) Close() {}
|
||||
func (e errRows) Err() error { return e.err }
|
||||
func (errRows) CommandTag() pgconn.CommandTag { return nil }
|
||||
func (errRows) FieldDescriptions() []pgproto3.FieldDescription { return nil }
|
||||
func (errRows) Next() bool { return false }
|
||||
func (e errRows) Scan(dest ...interface{}) error { return e.err }
|
||||
func (e errRows) Values() ([]interface{}, error) { return nil, e.err }
|
||||
func (e errRows) RawValues() [][]byte { return nil }
|
||||
|
||||
type errRow struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (e errRow) Scan(dest ...interface{}) error { return e.err }
|
||||
|
||||
type poolRows struct {
|
||||
r pgx.Rows
|
||||
c *Conn
|
||||
err error
|
||||
}
|
||||
|
||||
func (rows *poolRows) Close() {
|
||||
rows.r.Close()
|
||||
if rows.c != nil {
|
||||
rows.c.Release()
|
||||
rows.c = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (rows *poolRows) Err() error {
|
||||
if rows.err != nil {
|
||||
return rows.err
|
||||
}
|
||||
return rows.r.Err()
|
||||
}
|
||||
|
||||
func (rows *poolRows) CommandTag() pgconn.CommandTag {
|
||||
return rows.r.CommandTag()
|
||||
}
|
||||
|
||||
func (rows *poolRows) FieldDescriptions() []pgproto3.FieldDescription {
|
||||
return rows.r.FieldDescriptions()
|
||||
}
|
||||
|
||||
func (rows *poolRows) Next() bool {
|
||||
if rows.err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
n := rows.r.Next()
|
||||
if !n {
|
||||
rows.Close()
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (rows *poolRows) Scan(dest ...interface{}) error {
|
||||
err := rows.r.Scan(dest...)
|
||||
if err != nil {
|
||||
rows.Close()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (rows *poolRows) Values() ([]interface{}, error) {
|
||||
values, err := rows.r.Values()
|
||||
if err != nil {
|
||||
rows.Close()
|
||||
}
|
||||
return values, err
|
||||
}
|
||||
|
||||
func (rows *poolRows) RawValues() [][]byte {
|
||||
return rows.r.RawValues()
|
||||
}
|
||||
|
||||
type poolRow struct {
|
||||
r pgx.Row
|
||||
c *Conn
|
||||
err error
|
||||
}
|
||||
|
||||
func (row *poolRow) Scan(dest ...interface{}) error {
|
||||
if row.err != nil {
|
||||
return row.err
|
||||
}
|
||||
|
||||
err := row.r.Scan(dest...)
|
||||
if row.c != nil {
|
||||
row.c.Release()
|
||||
}
|
||||
return err
|
||||
}
|
||||
84
vendor/github.com/jackc/pgx/v4/pgxpool/stat.go
generated
vendored
Normal file
84
vendor/github.com/jackc/pgx/v4/pgxpool/stat.go
generated
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
package pgxpool
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jackc/puddle"
|
||||
)
|
||||
|
||||
// Stat is a snapshot of Pool statistics.
|
||||
type Stat struct {
|
||||
s *puddle.Stat
|
||||
newConnsCount int64
|
||||
lifetimeDestroyCount int64
|
||||
idleDestroyCount int64
|
||||
}
|
||||
|
||||
// AcquireCount returns the cumulative count of successful acquires from the pool.
|
||||
func (s *Stat) AcquireCount() int64 {
|
||||
return s.s.AcquireCount()
|
||||
}
|
||||
|
||||
// AcquireDuration returns the total duration of all successful acquires from
|
||||
// the pool.
|
||||
func (s *Stat) AcquireDuration() time.Duration {
|
||||
return s.s.AcquireDuration()
|
||||
}
|
||||
|
||||
// AcquiredConns returns the number of currently acquired connections in the pool.
|
||||
func (s *Stat) AcquiredConns() int32 {
|
||||
return s.s.AcquiredResources()
|
||||
}
|
||||
|
||||
// CanceledAcquireCount returns the cumulative count of acquires from the pool
|
||||
// that were canceled by a context.
|
||||
func (s *Stat) CanceledAcquireCount() int64 {
|
||||
return s.s.CanceledAcquireCount()
|
||||
}
|
||||
|
||||
// ConstructingConns returns the number of conns with construction in progress in
|
||||
// the pool.
|
||||
func (s *Stat) ConstructingConns() int32 {
|
||||
return s.s.ConstructingResources()
|
||||
}
|
||||
|
||||
// EmptyAcquireCount returns the cumulative count of successful acquires from the pool
|
||||
// that waited for a resource to be released or constructed because the pool was
|
||||
// empty.
|
||||
func (s *Stat) EmptyAcquireCount() int64 {
|
||||
return s.s.EmptyAcquireCount()
|
||||
}
|
||||
|
||||
// IdleConns returns the number of currently idle conns in the pool.
|
||||
func (s *Stat) IdleConns() int32 {
|
||||
return s.s.IdleResources()
|
||||
}
|
||||
|
||||
// MaxConns returns the maximum size of the pool.
|
||||
func (s *Stat) MaxConns() int32 {
|
||||
return s.s.MaxResources()
|
||||
}
|
||||
|
||||
// TotalConns returns the total number of resources currently in the pool.
|
||||
// The value is the sum of ConstructingConns, AcquiredConns, and
|
||||
// IdleConns.
|
||||
func (s *Stat) TotalConns() int32 {
|
||||
return s.s.TotalResources()
|
||||
}
|
||||
|
||||
// NewConnsCount returns the cumulative count of new connections opened.
|
||||
func (s *Stat) NewConnsCount() int64 {
|
||||
return s.newConnsCount
|
||||
}
|
||||
|
||||
// MaxLifetimeDestroyCount returns the cumulative count of connections destroyed
|
||||
// because they exceeded MaxConnLifetime.
|
||||
func (s *Stat) MaxLifetimeDestroyCount() int64 {
|
||||
return s.lifetimeDestroyCount
|
||||
}
|
||||
|
||||
// MaxIdleDestroyCount returns the cumulative count of connections destroyed because
|
||||
// they exceeded MaxConnIdleTime.
|
||||
func (s *Stat) MaxIdleDestroyCount() int64 {
|
||||
return s.idleDestroyCount
|
||||
}
|
||||
90
vendor/github.com/jackc/pgx/v4/pgxpool/tx.go
generated
vendored
Normal file
90
vendor/github.com/jackc/pgx/v4/pgxpool/tx.go
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
package pgxpool
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
// Tx represents a database transaction acquired from a Pool.
|
||||
type Tx struct {
|
||||
t pgx.Tx
|
||||
c *Conn
|
||||
}
|
||||
|
||||
// Begin starts a pseudo nested transaction implemented with a savepoint.
|
||||
func (tx *Tx) Begin(ctx context.Context) (pgx.Tx, error) {
|
||||
return tx.t.Begin(ctx)
|
||||
}
|
||||
|
||||
func (tx *Tx) BeginFunc(ctx context.Context, f func(pgx.Tx) error) error {
|
||||
return tx.t.BeginFunc(ctx, f)
|
||||
}
|
||||
|
||||
// Commit commits the transaction and returns the associated connection back to the Pool. Commit will return ErrTxClosed
|
||||
// if the Tx is already closed, but is otherwise safe to call multiple times. If the commit fails with a rollback status
|
||||
// (e.g. the transaction was already in a broken state) then ErrTxCommitRollback will be returned.
|
||||
func (tx *Tx) Commit(ctx context.Context) error {
|
||||
err := tx.t.Commit(ctx)
|
||||
if tx.c != nil {
|
||||
tx.c.Release()
|
||||
tx.c = nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Rollback rolls back the transaction and returns the associated connection back to the Pool. Rollback will return ErrTxClosed
|
||||
// if the Tx is already closed, but is otherwise safe to call multiple times. Hence, defer tx.Rollback() is safe even if
|
||||
// tx.Commit() will be called first in a non-error condition.
|
||||
func (tx *Tx) Rollback(ctx context.Context) error {
|
||||
err := tx.t.Rollback(ctx)
|
||||
if tx.c != nil {
|
||||
tx.c.Release()
|
||||
tx.c = nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (tx *Tx) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) {
|
||||
return tx.t.CopyFrom(ctx, tableName, columnNames, rowSrc)
|
||||
}
|
||||
|
||||
func (tx *Tx) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
|
||||
return tx.t.SendBatch(ctx, b)
|
||||
}
|
||||
|
||||
func (tx *Tx) LargeObjects() pgx.LargeObjects {
|
||||
return tx.t.LargeObjects()
|
||||
}
|
||||
|
||||
// Prepare creates a prepared statement with name and sql. If the name is empty,
|
||||
// an anonymous prepared statement will be used. sql can contain placeholders
|
||||
// for bound parameters. These placeholders are referenced positionally as $1, $2, etc.
|
||||
//
|
||||
// Prepare is idempotent; i.e. it is safe to call Prepare multiple times with the same
|
||||
// name and sql arguments. This allows a code path to Prepare and Query/Exec without
|
||||
// needing to first check whether the statement has already been prepared.
|
||||
func (tx *Tx) Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error) {
|
||||
return tx.t.Prepare(ctx, name, sql)
|
||||
}
|
||||
|
||||
func (tx *Tx) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
|
||||
return tx.t.Exec(ctx, sql, arguments...)
|
||||
}
|
||||
|
||||
func (tx *Tx) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) {
|
||||
return tx.t.Query(ctx, sql, args...)
|
||||
}
|
||||
|
||||
func (tx *Tx) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
|
||||
return tx.t.QueryRow(ctx, sql, args...)
|
||||
}
|
||||
|
||||
func (tx *Tx) QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(pgx.QueryFuncRow) error) (pgconn.CommandTag, error) {
|
||||
return tx.t.QueryFunc(ctx, sql, args, scans, f)
|
||||
}
|
||||
|
||||
func (tx *Tx) Conn() *pgx.Conn {
|
||||
return tx.t.Conn()
|
||||
}
|
||||
351
vendor/github.com/jackc/pgx/v4/rows.go
generated
vendored
Normal file
351
vendor/github.com/jackc/pgx/v4/rows.go
generated
vendored
Normal file
@@ -0,0 +1,351 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgproto3/v2"
|
||||
"github.com/jackc/pgtype"
|
||||
)
|
||||
|
||||
// Rows is the result set returned from *Conn.Query. Rows must be closed before
|
||||
// the *Conn can be used again. Rows are closed by explicitly calling Close(),
|
||||
// calling Next() until it returns false, or when a fatal error occurs.
|
||||
//
|
||||
// Once a Rows is closed the only methods that may be called are Close(), Err(), and CommandTag().
|
||||
//
|
||||
// Rows is an interface instead of a struct to allow tests to mock Query. However,
|
||||
// adding a method to an interface is technically a breaking change. Because of this
|
||||
// the Rows interface is partially excluded from semantic version requirements.
|
||||
// Methods will not be removed or changed, but new methods may be added.
|
||||
type Rows interface {
|
||||
// Close closes the rows, making the connection ready for use again. It is safe
|
||||
// to call Close after rows is already closed.
|
||||
Close()
|
||||
|
||||
// Err returns any error that occurred while reading.
|
||||
Err() error
|
||||
|
||||
// CommandTag returns the command tag from this query. It is only available after Rows is closed.
|
||||
CommandTag() pgconn.CommandTag
|
||||
|
||||
FieldDescriptions() []pgproto3.FieldDescription
|
||||
|
||||
// Next prepares the next row for reading. It returns true if there is another
|
||||
// row and false if no more rows are available. It automatically closes rows
|
||||
// when all rows are read.
|
||||
Next() bool
|
||||
|
||||
// Scan reads the values from the current row into dest values positionally.
|
||||
// dest can include pointers to core types, values implementing the Scanner
|
||||
// interface, and nil. nil will skip the value entirely. It is an error to
|
||||
// call Scan without first calling Next() and checking that it returned true.
|
||||
Scan(dest ...interface{}) error
|
||||
|
||||
// Values returns the decoded row values. As with Scan(), it is an error to
|
||||
// call Values without first calling Next() and checking that it returned
|
||||
// true.
|
||||
Values() ([]interface{}, error)
|
||||
|
||||
// RawValues returns the unparsed bytes of the row values. The returned [][]byte is only valid until the next Next
|
||||
// call or the Rows is closed. However, the underlying byte data is safe to retain a reference to and mutate.
|
||||
RawValues() [][]byte
|
||||
}
|
||||
|
||||
// Row is a convenience wrapper over Rows that is returned by QueryRow.
|
||||
//
|
||||
// Row is an interface instead of a struct to allow tests to mock QueryRow. However,
|
||||
// adding a method to an interface is technically a breaking change. Because of this
|
||||
// the Row interface is partially excluded from semantic version requirements.
|
||||
// Methods will not be removed or changed, but new methods may be added.
|
||||
type Row interface {
|
||||
// Scan works the same as Rows. with the following exceptions. If no
|
||||
// rows were found it returns ErrNoRows. If multiple rows are returned it
|
||||
// ignores all but the first.
|
||||
Scan(dest ...interface{}) error
|
||||
}
|
||||
|
||||
// connRow implements the Row interface for Conn.QueryRow.
|
||||
type connRow connRows
|
||||
|
||||
func (r *connRow) Scan(dest ...interface{}) (err error) {
|
||||
rows := (*connRows)(r)
|
||||
|
||||
if rows.Err() != nil {
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
if !rows.Next() {
|
||||
if rows.Err() == nil {
|
||||
return ErrNoRows
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
rows.Scan(dest...)
|
||||
rows.Close()
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
type rowLog interface {
|
||||
shouldLog(lvl LogLevel) bool
|
||||
log(ctx context.Context, lvl LogLevel, msg string, data map[string]interface{})
|
||||
}
|
||||
|
||||
// connRows implements the Rows interface for Conn.Query.
|
||||
type connRows struct {
|
||||
ctx context.Context
|
||||
logger rowLog
|
||||
connInfo *pgtype.ConnInfo
|
||||
values [][]byte
|
||||
rowCount int
|
||||
err error
|
||||
commandTag pgconn.CommandTag
|
||||
startTime time.Time
|
||||
sql string
|
||||
args []interface{}
|
||||
closed bool
|
||||
conn *Conn
|
||||
|
||||
resultReader *pgconn.ResultReader
|
||||
multiResultReader *pgconn.MultiResultReader
|
||||
|
||||
scanPlans []pgtype.ScanPlan
|
||||
}
|
||||
|
||||
func (rows *connRows) FieldDescriptions() []pgproto3.FieldDescription {
|
||||
return rows.resultReader.FieldDescriptions()
|
||||
}
|
||||
|
||||
func (rows *connRows) Close() {
|
||||
if rows.closed {
|
||||
return
|
||||
}
|
||||
|
||||
rows.closed = true
|
||||
|
||||
if rows.resultReader != nil {
|
||||
var closeErr error
|
||||
rows.commandTag, closeErr = rows.resultReader.Close()
|
||||
if rows.err == nil {
|
||||
rows.err = closeErr
|
||||
}
|
||||
}
|
||||
|
||||
if rows.multiResultReader != nil {
|
||||
closeErr := rows.multiResultReader.Close()
|
||||
if rows.err == nil {
|
||||
rows.err = closeErr
|
||||
}
|
||||
}
|
||||
|
||||
if rows.logger != nil {
|
||||
endTime := time.Now()
|
||||
|
||||
if rows.err == nil {
|
||||
if rows.logger.shouldLog(LogLevelInfo) {
|
||||
rows.logger.log(rows.ctx, LogLevelInfo, "Query", map[string]interface{}{"sql": rows.sql, "args": logQueryArgs(rows.args), "time": endTime.Sub(rows.startTime), "rowCount": rows.rowCount})
|
||||
}
|
||||
} else {
|
||||
if rows.logger.shouldLog(LogLevelError) {
|
||||
rows.logger.log(rows.ctx, LogLevelError, "Query", map[string]interface{}{"err": rows.err, "sql": rows.sql, "time": endTime.Sub(rows.startTime), "args": logQueryArgs(rows.args)})
|
||||
}
|
||||
if rows.err != nil && rows.conn.stmtcache != nil {
|
||||
rows.conn.stmtcache.StatementErrored(rows.sql, rows.err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (rows *connRows) CommandTag() pgconn.CommandTag {
|
||||
return rows.commandTag
|
||||
}
|
||||
|
||||
func (rows *connRows) Err() error {
|
||||
return rows.err
|
||||
}
|
||||
|
||||
// fatal signals an error occurred after the query was sent to the server. It
|
||||
// closes the rows automatically.
|
||||
func (rows *connRows) fatal(err error) {
|
||||
if rows.err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
rows.err = err
|
||||
rows.Close()
|
||||
}
|
||||
|
||||
func (rows *connRows) Next() bool {
|
||||
if rows.closed {
|
||||
return false
|
||||
}
|
||||
|
||||
if rows.resultReader.NextRow() {
|
||||
rows.rowCount++
|
||||
rows.values = rows.resultReader.Values()
|
||||
return true
|
||||
} else {
|
||||
rows.Close()
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (rows *connRows) Scan(dest ...interface{}) error {
|
||||
ci := rows.connInfo
|
||||
fieldDescriptions := rows.FieldDescriptions()
|
||||
values := rows.values
|
||||
|
||||
if len(fieldDescriptions) != len(values) {
|
||||
err := fmt.Errorf("number of field descriptions must equal number of values, got %d and %d", len(fieldDescriptions), len(values))
|
||||
rows.fatal(err)
|
||||
return err
|
||||
}
|
||||
if len(fieldDescriptions) != len(dest) {
|
||||
err := fmt.Errorf("number of field descriptions must equal number of destinations, got %d and %d", len(fieldDescriptions), len(dest))
|
||||
rows.fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if rows.scanPlans == nil {
|
||||
rows.scanPlans = make([]pgtype.ScanPlan, len(values))
|
||||
for i := range dest {
|
||||
rows.scanPlans[i] = ci.PlanScan(fieldDescriptions[i].DataTypeOID, fieldDescriptions[i].Format, dest[i])
|
||||
}
|
||||
}
|
||||
|
||||
for i, dst := range dest {
|
||||
if dst == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
err := rows.scanPlans[i].Scan(ci, fieldDescriptions[i].DataTypeOID, fieldDescriptions[i].Format, values[i], dst)
|
||||
if err != nil {
|
||||
err = ScanArgError{ColumnIndex: i, Err: err}
|
||||
rows.fatal(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rows *connRows) Values() ([]interface{}, error) {
|
||||
if rows.closed {
|
||||
return nil, errors.New("rows is closed")
|
||||
}
|
||||
|
||||
values := make([]interface{}, 0, len(rows.FieldDescriptions()))
|
||||
|
||||
for i := range rows.FieldDescriptions() {
|
||||
buf := rows.values[i]
|
||||
fd := &rows.FieldDescriptions()[i]
|
||||
|
||||
if buf == nil {
|
||||
values = append(values, nil)
|
||||
continue
|
||||
}
|
||||
|
||||
if dt, ok := rows.connInfo.DataTypeForOID(fd.DataTypeOID); ok {
|
||||
value := dt.Value
|
||||
|
||||
switch fd.Format {
|
||||
case TextFormatCode:
|
||||
decoder, ok := value.(pgtype.TextDecoder)
|
||||
if !ok {
|
||||
decoder = &pgtype.GenericText{}
|
||||
}
|
||||
err := decoder.DecodeText(rows.connInfo, buf)
|
||||
if err != nil {
|
||||
rows.fatal(err)
|
||||
}
|
||||
values = append(values, decoder.(pgtype.Value).Get())
|
||||
case BinaryFormatCode:
|
||||
decoder, ok := value.(pgtype.BinaryDecoder)
|
||||
if !ok {
|
||||
decoder = &pgtype.GenericBinary{}
|
||||
}
|
||||
err := decoder.DecodeBinary(rows.connInfo, buf)
|
||||
if err != nil {
|
||||
rows.fatal(err)
|
||||
}
|
||||
values = append(values, value.Get())
|
||||
default:
|
||||
rows.fatal(errors.New("Unknown format code"))
|
||||
}
|
||||
} else {
|
||||
switch fd.Format {
|
||||
case TextFormatCode:
|
||||
decoder := &pgtype.GenericText{}
|
||||
err := decoder.DecodeText(rows.connInfo, buf)
|
||||
if err != nil {
|
||||
rows.fatal(err)
|
||||
}
|
||||
values = append(values, decoder.Get())
|
||||
case BinaryFormatCode:
|
||||
decoder := &pgtype.GenericBinary{}
|
||||
err := decoder.DecodeBinary(rows.connInfo, buf)
|
||||
if err != nil {
|
||||
rows.fatal(err)
|
||||
}
|
||||
values = append(values, decoder.Get())
|
||||
default:
|
||||
rows.fatal(errors.New("Unknown format code"))
|
||||
}
|
||||
}
|
||||
|
||||
if rows.Err() != nil {
|
||||
return nil, rows.Err()
|
||||
}
|
||||
}
|
||||
|
||||
return values, rows.Err()
|
||||
}
|
||||
|
||||
func (rows *connRows) RawValues() [][]byte {
|
||||
return rows.values
|
||||
}
|
||||
|
||||
type ScanArgError struct {
|
||||
ColumnIndex int
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e ScanArgError) Error() string {
|
||||
return fmt.Sprintf("can't scan into dest[%d]: %v", e.ColumnIndex, e.Err)
|
||||
}
|
||||
|
||||
func (e ScanArgError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
// ScanRow decodes raw row data into dest. It can be used to scan rows read from the lower level pgconn interface.
|
||||
//
|
||||
// connInfo - OID to Go type mapping.
|
||||
// fieldDescriptions - OID and format of values
|
||||
// values - the raw data as returned from the PostgreSQL server
|
||||
// dest - the destination that values will be decoded into
|
||||
func ScanRow(connInfo *pgtype.ConnInfo, fieldDescriptions []pgproto3.FieldDescription, values [][]byte, dest ...interface{}) error {
|
||||
if len(fieldDescriptions) != len(values) {
|
||||
return fmt.Errorf("number of field descriptions must equal number of values, got %d and %d", len(fieldDescriptions), len(values))
|
||||
}
|
||||
if len(fieldDescriptions) != len(dest) {
|
||||
return fmt.Errorf("number of field descriptions must equal number of destinations, got %d and %d", len(fieldDescriptions), len(dest))
|
||||
}
|
||||
|
||||
for i, d := range dest {
|
||||
if d == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
err := connInfo.Scan(fieldDescriptions[i].DataTypeOID, fieldDescriptions[i].Format, values[i], d)
|
||||
if err != nil {
|
||||
return ScanArgError{ColumnIndex: i, Err: err}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
452
vendor/github.com/jackc/pgx/v4/tx.go
generated
vendored
Normal file
452
vendor/github.com/jackc/pgx/v4/tx.go
generated
vendored
Normal file
@@ -0,0 +1,452 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
)
|
||||
|
||||
// TxIsoLevel is the transaction isolation level (serializable, repeatable read, read committed or read uncommitted)
|
||||
type TxIsoLevel string
|
||||
|
||||
// Transaction isolation levels
|
||||
const (
|
||||
Serializable TxIsoLevel = "serializable"
|
||||
RepeatableRead TxIsoLevel = "repeatable read"
|
||||
ReadCommitted TxIsoLevel = "read committed"
|
||||
ReadUncommitted TxIsoLevel = "read uncommitted"
|
||||
)
|
||||
|
||||
// TxAccessMode is the transaction access mode (read write or read only)
|
||||
type TxAccessMode string
|
||||
|
||||
// Transaction access modes
|
||||
const (
|
||||
ReadWrite TxAccessMode = "read write"
|
||||
ReadOnly TxAccessMode = "read only"
|
||||
)
|
||||
|
||||
// TxDeferrableMode is the transaction deferrable mode (deferrable or not deferrable)
|
||||
type TxDeferrableMode string
|
||||
|
||||
// Transaction deferrable modes
|
||||
const (
|
||||
Deferrable TxDeferrableMode = "deferrable"
|
||||
NotDeferrable TxDeferrableMode = "not deferrable"
|
||||
)
|
||||
|
||||
// TxOptions are transaction modes within a transaction block
|
||||
type TxOptions struct {
|
||||
IsoLevel TxIsoLevel
|
||||
AccessMode TxAccessMode
|
||||
DeferrableMode TxDeferrableMode
|
||||
}
|
||||
|
||||
var emptyTxOptions TxOptions
|
||||
|
||||
func (txOptions TxOptions) beginSQL() string {
|
||||
if txOptions == emptyTxOptions {
|
||||
return "begin"
|
||||
}
|
||||
buf := &bytes.Buffer{}
|
||||
buf.WriteString("begin")
|
||||
if txOptions.IsoLevel != "" {
|
||||
fmt.Fprintf(buf, " isolation level %s", txOptions.IsoLevel)
|
||||
}
|
||||
if txOptions.AccessMode != "" {
|
||||
fmt.Fprintf(buf, " %s", txOptions.AccessMode)
|
||||
}
|
||||
if txOptions.DeferrableMode != "" {
|
||||
fmt.Fprintf(buf, " %s", txOptions.DeferrableMode)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
var ErrTxClosed = errors.New("tx is closed")
|
||||
|
||||
// ErrTxCommitRollback occurs when an error has occurred in a transaction and
|
||||
// Commit() is called. PostgreSQL accepts COMMIT on aborted transactions, but
|
||||
// it is treated as ROLLBACK.
|
||||
var ErrTxCommitRollback = errors.New("commit unexpectedly resulted in rollback")
|
||||
|
||||
// Begin starts a transaction. Unlike database/sql, the context only affects the begin command. i.e. there is no
|
||||
// auto-rollback on context cancellation.
|
||||
func (c *Conn) Begin(ctx context.Context) (Tx, error) {
|
||||
return c.BeginTx(ctx, TxOptions{})
|
||||
}
|
||||
|
||||
// BeginTx starts a transaction with txOptions determining the transaction mode. Unlike database/sql, the context only
|
||||
// affects the begin command. i.e. there is no auto-rollback on context cancellation.
|
||||
func (c *Conn) BeginTx(ctx context.Context, txOptions TxOptions) (Tx, error) {
|
||||
_, err := c.Exec(ctx, txOptions.beginSQL())
|
||||
if err != nil {
|
||||
// begin should never fail unless there is an underlying connection issue or
|
||||
// a context timeout. In either case, the connection is possibly broken.
|
||||
c.die(errors.New("failed to begin transaction"))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dbTx{conn: c}, nil
|
||||
}
|
||||
|
||||
// BeginFunc starts a transaction and calls f. If f does not return an error the transaction is committed. If f returns
|
||||
// an error the transaction is rolled back. The context will be used when executing the transaction control statements
|
||||
// (BEGIN, ROLLBACK, and COMMIT) but does not otherwise affect the execution of f.
|
||||
func (c *Conn) BeginFunc(ctx context.Context, f func(Tx) error) (err error) {
|
||||
return c.BeginTxFunc(ctx, TxOptions{}, f)
|
||||
}
|
||||
|
||||
// BeginTxFunc starts a transaction with txOptions determining the transaction mode and calls f. If f does not return
|
||||
// an error the transaction is committed. If f returns an error the transaction is rolled back. The context will be
|
||||
// used when executing the transaction control statements (BEGIN, ROLLBACK, and COMMIT) but does not otherwise affect
|
||||
// the execution of f.
|
||||
func (c *Conn) BeginTxFunc(ctx context.Context, txOptions TxOptions, f func(Tx) error) (err error) {
|
||||
var tx Tx
|
||||
tx, err = c.BeginTx(ctx, txOptions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
rollbackErr := tx.Rollback(ctx)
|
||||
if rollbackErr != nil && !errors.Is(rollbackErr, ErrTxClosed) {
|
||||
err = rollbackErr
|
||||
}
|
||||
}()
|
||||
|
||||
fErr := f(tx)
|
||||
if fErr != nil {
|
||||
_ = tx.Rollback(ctx) // ignore rollback error as there is already an error to return
|
||||
return fErr
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// Tx represents a database transaction.
|
||||
//
|
||||
// Tx is an interface instead of a struct to enable connection pools to be implemented without relying on internal pgx
|
||||
// state, to support pseudo-nested transactions with savepoints, and to allow tests to mock transactions. However,
|
||||
// adding a method to an interface is technically a breaking change. If new methods are added to Conn it may be
|
||||
// desirable to add them to Tx as well. Because of this the Tx interface is partially excluded from semantic version
|
||||
// requirements. Methods will not be removed or changed, but new methods may be added.
|
||||
type Tx interface {
|
||||
// Begin starts a pseudo nested transaction.
|
||||
Begin(ctx context.Context) (Tx, error)
|
||||
|
||||
// BeginFunc starts a pseudo nested transaction and executes f. If f does not return an err the pseudo nested
|
||||
// transaction will be committed. If it does then it will be rolled back.
|
||||
BeginFunc(ctx context.Context, f func(Tx) error) (err error)
|
||||
|
||||
// Commit commits the transaction if this is a real transaction or releases the savepoint if this is a pseudo nested
|
||||
// transaction. Commit will return ErrTxClosed if the Tx is already closed, but is otherwise safe to call multiple
|
||||
// times. If the commit fails with a rollback status (e.g. the transaction was already in a broken state) then
|
||||
// ErrTxCommitRollback will be returned.
|
||||
Commit(ctx context.Context) error
|
||||
|
||||
// Rollback rolls back the transaction if this is a real transaction or rolls back to the savepoint if this is a
|
||||
// pseudo nested transaction. Rollback will return ErrTxClosed if the Tx is already closed, but is otherwise safe to
|
||||
// call multiple times. Hence, a defer tx.Rollback() is safe even if tx.Commit() will be called first in a non-error
|
||||
// condition. Any other failure of a real transaction will result in the connection being closed.
|
||||
Rollback(ctx context.Context) error
|
||||
|
||||
CopyFrom(ctx context.Context, tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int64, error)
|
||||
SendBatch(ctx context.Context, b *Batch) BatchResults
|
||||
LargeObjects() LargeObjects
|
||||
|
||||
Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error)
|
||||
|
||||
Exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error)
|
||||
Query(ctx context.Context, sql string, args ...interface{}) (Rows, error)
|
||||
QueryRow(ctx context.Context, sql string, args ...interface{}) Row
|
||||
QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error)
|
||||
|
||||
// Conn returns the underlying *Conn that on which this transaction is executing.
|
||||
Conn() *Conn
|
||||
}
|
||||
|
||||
// dbTx represents a database transaction.
|
||||
//
|
||||
// All dbTx methods return ErrTxClosed if Commit or Rollback has already been
|
||||
// called on the dbTx.
|
||||
type dbTx struct {
|
||||
conn *Conn
|
||||
err error
|
||||
savepointNum int64
|
||||
closed bool
|
||||
}
|
||||
|
||||
// Begin starts a pseudo nested transaction implemented with a savepoint.
|
||||
func (tx *dbTx) Begin(ctx context.Context) (Tx, error) {
|
||||
if tx.closed {
|
||||
return nil, ErrTxClosed
|
||||
}
|
||||
|
||||
tx.savepointNum++
|
||||
_, err := tx.conn.Exec(ctx, "savepoint sp_"+strconv.FormatInt(tx.savepointNum, 10))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dbSimulatedNestedTx{tx: tx, savepointNum: tx.savepointNum}, nil
|
||||
}
|
||||
|
||||
func (tx *dbTx) BeginFunc(ctx context.Context, f func(Tx) error) (err error) {
|
||||
if tx.closed {
|
||||
return ErrTxClosed
|
||||
}
|
||||
|
||||
var savepoint Tx
|
||||
savepoint, err = tx.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
rollbackErr := savepoint.Rollback(ctx)
|
||||
if rollbackErr != nil && !errors.Is(rollbackErr, ErrTxClosed) {
|
||||
err = rollbackErr
|
||||
}
|
||||
}()
|
||||
|
||||
fErr := f(savepoint)
|
||||
if fErr != nil {
|
||||
_ = savepoint.Rollback(ctx) // ignore rollback error as there is already an error to return
|
||||
return fErr
|
||||
}
|
||||
|
||||
return savepoint.Commit(ctx)
|
||||
}
|
||||
|
||||
// Commit commits the transaction.
|
||||
func (tx *dbTx) Commit(ctx context.Context) error {
|
||||
if tx.closed {
|
||||
return ErrTxClosed
|
||||
}
|
||||
|
||||
commandTag, err := tx.conn.Exec(ctx, "commit")
|
||||
tx.closed = true
|
||||
if err != nil {
|
||||
if tx.conn.PgConn().TxStatus() != 'I' {
|
||||
_ = tx.conn.Close(ctx) // already have error to return
|
||||
}
|
||||
return err
|
||||
}
|
||||
if string(commandTag) == "ROLLBACK" {
|
||||
return ErrTxCommitRollback
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Rollback rolls back the transaction. Rollback will return ErrTxClosed if the
|
||||
// Tx is already closed, but is otherwise safe to call multiple times. Hence, a
|
||||
// defer tx.Rollback() is safe even if tx.Commit() will be called first in a
|
||||
// non-error condition.
|
||||
func (tx *dbTx) Rollback(ctx context.Context) error {
|
||||
if tx.closed {
|
||||
return ErrTxClosed
|
||||
}
|
||||
|
||||
_, err := tx.conn.Exec(ctx, "rollback")
|
||||
tx.closed = true
|
||||
if err != nil {
|
||||
// A rollback failure leaves the connection in an undefined state
|
||||
tx.conn.die(fmt.Errorf("rollback failed: %w", err))
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Exec delegates to the underlying *Conn
|
||||
func (tx *dbTx) Exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error) {
|
||||
if tx.closed {
|
||||
return pgconn.CommandTag{}, ErrTxClosed
|
||||
}
|
||||
|
||||
return tx.conn.Exec(ctx, sql, arguments...)
|
||||
}
|
||||
|
||||
// Prepare delegates to the underlying *Conn
|
||||
func (tx *dbTx) Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error) {
|
||||
if tx.closed {
|
||||
return nil, ErrTxClosed
|
||||
}
|
||||
|
||||
return tx.conn.Prepare(ctx, name, sql)
|
||||
}
|
||||
|
||||
// Query delegates to the underlying *Conn
|
||||
func (tx *dbTx) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error) {
|
||||
if tx.closed {
|
||||
// Because checking for errors can be deferred to the *Rows, build one with the error
|
||||
err := ErrTxClosed
|
||||
return &connRows{closed: true, err: err}, err
|
||||
}
|
||||
|
||||
return tx.conn.Query(ctx, sql, args...)
|
||||
}
|
||||
|
||||
// QueryRow delegates to the underlying *Conn
|
||||
func (tx *dbTx) QueryRow(ctx context.Context, sql string, args ...interface{}) Row {
|
||||
rows, _ := tx.Query(ctx, sql, args...)
|
||||
return (*connRow)(rows.(*connRows))
|
||||
}
|
||||
|
||||
// QueryFunc delegates to the underlying *Conn.
|
||||
func (tx *dbTx) QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error) {
|
||||
if tx.closed {
|
||||
return nil, ErrTxClosed
|
||||
}
|
||||
|
||||
return tx.conn.QueryFunc(ctx, sql, args, scans, f)
|
||||
}
|
||||
|
||||
// CopyFrom delegates to the underlying *Conn
|
||||
func (tx *dbTx) CopyFrom(ctx context.Context, tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int64, error) {
|
||||
if tx.closed {
|
||||
return 0, ErrTxClosed
|
||||
}
|
||||
|
||||
return tx.conn.CopyFrom(ctx, tableName, columnNames, rowSrc)
|
||||
}
|
||||
|
||||
// SendBatch delegates to the underlying *Conn
|
||||
func (tx *dbTx) SendBatch(ctx context.Context, b *Batch) BatchResults {
|
||||
if tx.closed {
|
||||
return &batchResults{err: ErrTxClosed}
|
||||
}
|
||||
|
||||
return tx.conn.SendBatch(ctx, b)
|
||||
}
|
||||
|
||||
// LargeObjects returns a LargeObjects instance for the transaction.
|
||||
func (tx *dbTx) LargeObjects() LargeObjects {
|
||||
return LargeObjects{tx: tx}
|
||||
}
|
||||
|
||||
func (tx *dbTx) Conn() *Conn {
|
||||
return tx.conn
|
||||
}
|
||||
|
||||
// dbSimulatedNestedTx represents a simulated nested transaction implemented by a savepoint.
|
||||
type dbSimulatedNestedTx struct {
|
||||
tx Tx
|
||||
savepointNum int64
|
||||
closed bool
|
||||
}
|
||||
|
||||
// Begin starts a pseudo nested transaction implemented with a savepoint.
|
||||
func (sp *dbSimulatedNestedTx) Begin(ctx context.Context) (Tx, error) {
|
||||
if sp.closed {
|
||||
return nil, ErrTxClosed
|
||||
}
|
||||
|
||||
return sp.tx.Begin(ctx)
|
||||
}
|
||||
|
||||
func (sp *dbSimulatedNestedTx) BeginFunc(ctx context.Context, f func(Tx) error) (err error) {
|
||||
if sp.closed {
|
||||
return ErrTxClosed
|
||||
}
|
||||
|
||||
return sp.tx.BeginFunc(ctx, f)
|
||||
}
|
||||
|
||||
// Commit releases the savepoint essentially committing the pseudo nested transaction.
|
||||
func (sp *dbSimulatedNestedTx) Commit(ctx context.Context) error {
|
||||
if sp.closed {
|
||||
return ErrTxClosed
|
||||
}
|
||||
|
||||
_, err := sp.Exec(ctx, "release savepoint sp_"+strconv.FormatInt(sp.savepointNum, 10))
|
||||
sp.closed = true
|
||||
return err
|
||||
}
|
||||
|
||||
// Rollback rolls back to the savepoint essentially rolling back the pseudo nested transaction. Rollback will return
|
||||
// ErrTxClosed if the dbSavepoint is already closed, but is otherwise safe to call multiple times. Hence, a defer sp.Rollback()
|
||||
// is safe even if sp.Commit() will be called first in a non-error condition.
|
||||
func (sp *dbSimulatedNestedTx) Rollback(ctx context.Context) error {
|
||||
if sp.closed {
|
||||
return ErrTxClosed
|
||||
}
|
||||
|
||||
_, err := sp.Exec(ctx, "rollback to savepoint sp_"+strconv.FormatInt(sp.savepointNum, 10))
|
||||
sp.closed = true
|
||||
return err
|
||||
}
|
||||
|
||||
// Exec delegates to the underlying Tx
|
||||
func (sp *dbSimulatedNestedTx) Exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error) {
|
||||
if sp.closed {
|
||||
return nil, ErrTxClosed
|
||||
}
|
||||
|
||||
return sp.tx.Exec(ctx, sql, arguments...)
|
||||
}
|
||||
|
||||
// Prepare delegates to the underlying Tx
|
||||
func (sp *dbSimulatedNestedTx) Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error) {
|
||||
if sp.closed {
|
||||
return nil, ErrTxClosed
|
||||
}
|
||||
|
||||
return sp.tx.Prepare(ctx, name, sql)
|
||||
}
|
||||
|
||||
// Query delegates to the underlying Tx
|
||||
func (sp *dbSimulatedNestedTx) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error) {
|
||||
if sp.closed {
|
||||
// Because checking for errors can be deferred to the *Rows, build one with the error
|
||||
err := ErrTxClosed
|
||||
return &connRows{closed: true, err: err}, err
|
||||
}
|
||||
|
||||
return sp.tx.Query(ctx, sql, args...)
|
||||
}
|
||||
|
||||
// QueryRow delegates to the underlying Tx
|
||||
func (sp *dbSimulatedNestedTx) QueryRow(ctx context.Context, sql string, args ...interface{}) Row {
|
||||
rows, _ := sp.Query(ctx, sql, args...)
|
||||
return (*connRow)(rows.(*connRows))
|
||||
}
|
||||
|
||||
// QueryFunc delegates to the underlying Tx.
|
||||
func (sp *dbSimulatedNestedTx) QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error) {
|
||||
if sp.closed {
|
||||
return nil, ErrTxClosed
|
||||
}
|
||||
|
||||
return sp.tx.QueryFunc(ctx, sql, args, scans, f)
|
||||
}
|
||||
|
||||
// CopyFrom delegates to the underlying *Conn
|
||||
func (sp *dbSimulatedNestedTx) CopyFrom(ctx context.Context, tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int64, error) {
|
||||
if sp.closed {
|
||||
return 0, ErrTxClosed
|
||||
}
|
||||
|
||||
return sp.tx.CopyFrom(ctx, tableName, columnNames, rowSrc)
|
||||
}
|
||||
|
||||
// SendBatch delegates to the underlying *Conn
|
||||
func (sp *dbSimulatedNestedTx) SendBatch(ctx context.Context, b *Batch) BatchResults {
|
||||
if sp.closed {
|
||||
return &batchResults{err: ErrTxClosed}
|
||||
}
|
||||
|
||||
return sp.tx.SendBatch(ctx, b)
|
||||
}
|
||||
|
||||
func (sp *dbSimulatedNestedTx) LargeObjects() LargeObjects {
|
||||
return LargeObjects{tx: sp}
|
||||
}
|
||||
|
||||
func (sp *dbSimulatedNestedTx) Conn() *Conn {
|
||||
return sp.tx.Conn()
|
||||
}
|
||||
280
vendor/github.com/jackc/pgx/v4/values.go
generated
vendored
Normal file
280
vendor/github.com/jackc/pgx/v4/values.go
generated
vendored
Normal file
@@ -0,0 +1,280 @@
|
||||
package pgx
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgio"
|
||||
"github.com/jackc/pgtype"
|
||||
)
|
||||
|
||||
// PostgreSQL format codes
|
||||
const (
|
||||
TextFormatCode = 0
|
||||
BinaryFormatCode = 1
|
||||
)
|
||||
|
||||
// SerializationError occurs on failure to encode or decode a value
|
||||
type SerializationError string
|
||||
|
||||
func (e SerializationError) Error() string {
|
||||
return string(e)
|
||||
}
|
||||
|
||||
func convertSimpleArgument(ci *pgtype.ConnInfo, arg interface{}) (interface{}, error) {
|
||||
if arg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
refVal := reflect.ValueOf(arg)
|
||||
if refVal.Kind() == reflect.Ptr && refVal.IsNil() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
switch arg := arg.(type) {
|
||||
|
||||
// https://github.com/jackc/pgx/issues/409 Changed JSON and JSONB to surface
|
||||
// []byte to database/sql instead of string. But that caused problems with the
|
||||
// simple protocol because the driver.Valuer case got taken before the
|
||||
// pgtype.TextEncoder case. And driver.Valuer needed to be first in the usual
|
||||
// case because of https://github.com/jackc/pgx/issues/339. So instead we
|
||||
// special case JSON and JSONB.
|
||||
case *pgtype.JSON:
|
||||
buf, err := arg.EncodeText(ci, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if buf == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return string(buf), nil
|
||||
case *pgtype.JSONB:
|
||||
buf, err := arg.EncodeText(ci, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if buf == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return string(buf), nil
|
||||
|
||||
case driver.Valuer:
|
||||
return callValuerValue(arg)
|
||||
case pgtype.TextEncoder:
|
||||
buf, err := arg.EncodeText(ci, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if buf == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return string(buf), nil
|
||||
case float32:
|
||||
return float64(arg), nil
|
||||
case float64:
|
||||
return arg, nil
|
||||
case bool:
|
||||
return arg, nil
|
||||
case time.Duration:
|
||||
return fmt.Sprintf("%d microsecond", int64(arg)/1000), nil
|
||||
case time.Time:
|
||||
return arg, nil
|
||||
case string:
|
||||
return arg, nil
|
||||
case []byte:
|
||||
return arg, nil
|
||||
case int8:
|
||||
return int64(arg), nil
|
||||
case int16:
|
||||
return int64(arg), nil
|
||||
case int32:
|
||||
return int64(arg), nil
|
||||
case int64:
|
||||
return arg, nil
|
||||
case int:
|
||||
return int64(arg), nil
|
||||
case uint8:
|
||||
return int64(arg), nil
|
||||
case uint16:
|
||||
return int64(arg), nil
|
||||
case uint32:
|
||||
return int64(arg), nil
|
||||
case uint64:
|
||||
if arg > math.MaxInt64 {
|
||||
return nil, fmt.Errorf("arg too big for int64: %v", arg)
|
||||
}
|
||||
return int64(arg), nil
|
||||
case uint:
|
||||
if uint64(arg) > math.MaxInt64 {
|
||||
return nil, fmt.Errorf("arg too big for int64: %v", arg)
|
||||
}
|
||||
return int64(arg), nil
|
||||
}
|
||||
|
||||
if dt, found := ci.DataTypeForValue(arg); found {
|
||||
v := dt.Value
|
||||
err := v.Set(arg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf, err := v.(pgtype.TextEncoder).EncodeText(ci, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if buf == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return string(buf), nil
|
||||
}
|
||||
|
||||
if refVal.Kind() == reflect.Ptr {
|
||||
arg = refVal.Elem().Interface()
|
||||
return convertSimpleArgument(ci, arg)
|
||||
}
|
||||
|
||||
if strippedArg, ok := stripNamedType(&refVal); ok {
|
||||
return convertSimpleArgument(ci, strippedArg)
|
||||
}
|
||||
return nil, SerializationError(fmt.Sprintf("Cannot encode %T in simple protocol - %T must implement driver.Valuer, pgtype.TextEncoder, or be a native type", arg, arg))
|
||||
}
|
||||
|
||||
func encodePreparedStatementArgument(ci *pgtype.ConnInfo, buf []byte, oid uint32, arg interface{}) ([]byte, error) {
|
||||
if arg == nil {
|
||||
return pgio.AppendInt32(buf, -1), nil
|
||||
}
|
||||
|
||||
switch arg := arg.(type) {
|
||||
case pgtype.BinaryEncoder:
|
||||
sp := len(buf)
|
||||
buf = pgio.AppendInt32(buf, -1)
|
||||
argBuf, err := arg.EncodeBinary(ci, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if argBuf != nil {
|
||||
buf = argBuf
|
||||
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
|
||||
}
|
||||
return buf, nil
|
||||
case pgtype.TextEncoder:
|
||||
sp := len(buf)
|
||||
buf = pgio.AppendInt32(buf, -1)
|
||||
argBuf, err := arg.EncodeText(ci, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if argBuf != nil {
|
||||
buf = argBuf
|
||||
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
|
||||
}
|
||||
return buf, nil
|
||||
case string:
|
||||
buf = pgio.AppendInt32(buf, int32(len(arg)))
|
||||
buf = append(buf, arg...)
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
refVal := reflect.ValueOf(arg)
|
||||
|
||||
if refVal.Kind() == reflect.Ptr {
|
||||
if refVal.IsNil() {
|
||||
return pgio.AppendInt32(buf, -1), nil
|
||||
}
|
||||
arg = refVal.Elem().Interface()
|
||||
return encodePreparedStatementArgument(ci, buf, oid, arg)
|
||||
}
|
||||
|
||||
if dt, ok := ci.DataTypeForOID(oid); ok {
|
||||
value := dt.Value
|
||||
err := value.Set(arg)
|
||||
if err != nil {
|
||||
{
|
||||
if arg, ok := arg.(driver.Valuer); ok {
|
||||
v, err := callValuerValue(arg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return encodePreparedStatementArgument(ci, buf, oid, v)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sp := len(buf)
|
||||
buf = pgio.AppendInt32(buf, -1)
|
||||
argBuf, err := value.(pgtype.BinaryEncoder).EncodeBinary(ci, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if argBuf != nil {
|
||||
buf = argBuf
|
||||
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
|
||||
}
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
if strippedArg, ok := stripNamedType(&refVal); ok {
|
||||
return encodePreparedStatementArgument(ci, buf, oid, strippedArg)
|
||||
}
|
||||
return nil, SerializationError(fmt.Sprintf("Cannot encode %T into oid %v - %T must implement Encoder or be converted to a string", arg, oid, arg))
|
||||
}
|
||||
|
||||
// chooseParameterFormatCode determines the correct format code for an
|
||||
// argument to a prepared statement. It defaults to TextFormatCode if no
|
||||
// determination can be made.
|
||||
func chooseParameterFormatCode(ci *pgtype.ConnInfo, oid uint32, arg interface{}) int16 {
|
||||
switch arg := arg.(type) {
|
||||
case pgtype.ParamFormatPreferrer:
|
||||
return arg.PreferredParamFormat()
|
||||
case pgtype.BinaryEncoder:
|
||||
return BinaryFormatCode
|
||||
case string, *string, pgtype.TextEncoder:
|
||||
return TextFormatCode
|
||||
}
|
||||
|
||||
return ci.ParamFormatCodeForOID(oid)
|
||||
}
|
||||
|
||||
func stripNamedType(val *reflect.Value) (interface{}, bool) {
|
||||
switch val.Kind() {
|
||||
case reflect.Int:
|
||||
convVal := int(val.Int())
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
case reflect.Int8:
|
||||
convVal := int8(val.Int())
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
case reflect.Int16:
|
||||
convVal := int16(val.Int())
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
case reflect.Int32:
|
||||
convVal := int32(val.Int())
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
case reflect.Int64:
|
||||
convVal := int64(val.Int())
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
case reflect.Uint:
|
||||
convVal := uint(val.Uint())
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
case reflect.Uint8:
|
||||
convVal := uint8(val.Uint())
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
case reflect.Uint16:
|
||||
convVal := uint16(val.Uint())
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
case reflect.Uint32:
|
||||
convVal := uint32(val.Uint())
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
case reflect.Uint64:
|
||||
convVal := uint64(val.Uint())
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
case reflect.String:
|
||||
convVal := val.String()
|
||||
return convVal, reflect.TypeOf(convVal) != val.Type()
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
Reference in New Issue
Block a user