79 Commits

Author SHA1 Message Date
georgysavva
a6d42976c6 Make it possible to scan destination of *interface{} type. 2020-06-08 13:18:54 +03:00
Jack Christensen
f6355165a9 Remove superfluous argument from ScanPlan 2020-06-06 09:10:11 -05:00
Jack Christensen
91a46ce219 Clarify and normalize Value semantics
Previously, Get implicitly allowed returning a reference to an internal
value (e.g. a []byte) but AssignTo was documented as requiring a deep
copy.

This inconsistency meant that either Get was unsafe or the deep copy in
AssignTo was superfluous. In addition, Scan into a []byte skips going
through Bytea and returns a []byte of the unparsed bytes directly. i.e.
a reference not a copy.

Standardize on allowing Get and AssignTo to return internal references
but require a Value never mutate internal values - only replace them.
2020-06-06 08:34:56 -05:00
Jack Christensen
9cdd928cb8 CompositeType implements TypeValue 2020-05-12 10:40:13 -05:00
Jack Christensen
e5992d0aed TypeValue should include Value 2020-05-12 10:28:13 -05:00
Jack Christensen
682201a4fc Rename CloneTypeValue to NewTypeValue 2020-05-12 10:26:51 -05:00
Jack Christensen
bff2829b0f Move ComposteType.Scan functionality into AssignTo
Also remove adapter functions that are no longer used.
2020-05-12 10:19:41 -05:00
Jack Christensen
036101deb5 Allow scanning to nil as no-op 2020-05-11 17:41:20 -05:00
Jack Christensen
1b3d694469 Add ArrayType 2020-05-10 19:37:25 -05:00
Jack Christensen
8cd94a14c7 Allow types to specify preference format result and param formats
This will be useful for array and composite types that may have to
support elements that may not support binary encoding.

It also is slightly more convenient for text-ish types to have a default
format of text.
2020-05-10 14:05:16 -05:00
Jack Christensen
cc4d1eafe0 Doc tweaks and renames 2020-05-10 12:45:12 -05:00
Jack Christensen
a71c179ce3 Extract nullAssignmentError 2020-05-10 12:28:47 -05:00
Jack Christensen
52729c1b77 Back off some aggressive PlanScan optimizations
PlanScan used to require the exact same value be used every time. While
this was great for performance, on further consideration I think it is
too much of a potential foot-gun.

This moves back in the other direction. A plan tolerates a change in
destination. It even detects a change in destination type and falls
back to a new plan.

Perfectly matched hot scan paths (e.g. PG int4 to Go int32) are still
much faster than they were before this set of optimizations. The first
scan of a destination that uses a decoder is faster due to not
allocating. It's a little bit slower on subsequent runs than before
this set of optimizations. But it is preferable to optimize for the
most common scan targets (e.g. *int32, *int64, *string) over generic
decoder destinations.

In addition this fees pgx.connRows.Scan from having to check that
the destination is unchanged.
2020-05-10 09:37:15 -05:00
Jack Christensen
7e66ab1e14 Add scan plan system
This can improve performance now and will be useful for the future
transcoder system.
2020-05-09 23:52:48 -05:00
Jack Christensen
97bbe6ae20 Add RegisterDefaultPgType
This allows registering a mapping of a Go type to a PostgreSQL type
name. If the OID of a value to be encoded or decoded is unknown, this
additional mapping will be used to determine a suitable data type.
2020-05-08 16:13:15 -05:00
Jack Christensen
4a50a63f12 Refactor Scan optimization
Instead of hardcoding specific types and skipping type assertions based
on that, only check if a destination is a (sql.Scanner) after a failed
AssignTo.

This is slightly slower in the non-decoder case and *very* slightly
faster in the decoder. However, this approach is cleaner and has the
potential for further optimizations.
2020-05-07 19:48:48 -05:00
Jack Christensen
37e976192b ScanRowValue accepts interface{} dst 2020-05-06 14:56:25 -05:00
Jack Christensen
cce17427e1 Merge branch 'record-expect' of git://github.com/redbaron/pgtype into redbaron-record-expect 2020-05-06 14:43:10 -05:00
Jack Christensen
4d2b5a18c4 Clarify Value.Get() documentation
Specifying behavior for Status Null and Undefined is incorrect because
a Value is not required to have a Status. In addition, standard
behavior is to return nil, not pgtype.Null when the Status is
pgtype.Null.
2020-05-06 09:51:41 -05:00
Jack Christensen
3b7c47a2a7 Add EnumType 2020-05-05 13:23:14 -05:00
Jack Christensen
ab5e597826 Avoid type assertion in Scan
Before:
BenchmarkConnInfoScanInt4IntoBinaryDecoder-16           79859755                14.6 ns/op             0 B/op          0 allocs/op
BenchmarkConnInfoScanInt4IntoGoInt32-16                 38969991                30.0 ns/op             0 B/op          0 allocs/op

After:
BenchmarkConnInfoScanInt4IntoBinaryDecoder-16    	458046958	        13.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkConnInfoScanInt4IntoGoInt32-16          	275791776	        20.6 ns/op	       0 B/op	       0 allocs/op
2020-05-02 20:30:58 -05:00
Jack Christensen
18c64dceee ConnInfo Scan optimizes common native types
This comes at a small expense to scanning into a type that implements
TextDecoder or BinaryDecoder but I think it is a good trade.

Before:
BenchmarkConnInfoScanInt4IntoBinaryDecoder-16    	88181061	        12.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkConnInfoScanInt4IntoGoInt32-16          	30402768	        36.8 ns/op	       0 B/op	       0 allocs/op

After:
BenchmarkConnInfoScanInt4IntoBinaryDecoder-16    	79859755	        14.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkConnInfoScanInt4IntoGoInt32-16          	38969991	        30.0 ns/op	       0 B/op	       0 allocs/op
2020-05-02 20:18:51 -05:00
Jack Christensen
6357d3b3f3 Avoid extra type assertion on native type Scan path
Before:
BenchmarkConnInfoScanInt4IntoBinaryDecoder-16    	89744814	        12.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkConnInfoScanInt4IntoGoInt32-16          	27688370	        41.1 ns/op	       0 B/op	       0 allocs/op

After:
BenchmarkConnInfoScanInt4IntoBinaryDecoder-16    	88181061	        12.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkConnInfoScanInt4IntoGoInt32-16          	30402768	        36.8 ns/op	       0 B/op	       0 allocs/op
2020-05-02 17:31:53 -05:00
Maxim Ivanov
3ce29f9e05 Add Composite type for inplace row() values handling
Composite() function returns a private type, which should
be registered with ConnInfo.RegisterDataType for the composite
type's OID.

All subsequent interaction with Composite types is to be done
via Row(...) function. Function return value can be either
passed as a query argument to build SQL composite value out of
individual fields or passed to Scan to read SQL composite value
back.

When passed to Scan, Row() should have first argument of type
*bool to flag NULL values returned from query.
2020-04-13 17:41:44 +01:00
Maxim Ivanov
368295d3ee Create ROW helper for adhoc decoding of records 2020-04-12 18:40:52 +01:00
Jack Christensen
65bb544ba9 Merge branch 'rw_format' of git://github.com/rwelin/pgtype into rwelin-rw_format 2020-03-27 16:10:37 -05:00
Robert Welin
43bf713180 Use correct format verb for unknown type error 2020-03-27 13:20:33 +00:00
Jack Christensen
c7502af68b Add PostgreSQL time type support
fixes #15
2019-12-19 21:35:35 -06:00
jaltavilla
af517d68fc Scan into nullable custom types (pointers to pointers). 2019-10-21 17:21:42 -04:00
Jack Christensen
52ae698572 Fix daterange oid 2019-09-19 21:43:18 -05:00
Jack Christensen
b1e25e4ea4 Add format code helpers to ConnInfo 2019-08-25 00:32:11 -05:00
Jack Christensen
ab885b375b OID type should only be used for scanning and encoding values
It was a mistake to use it in other contexts. This made interop
difficult between pacakges that depended on pgtype such as pgx and
packages that did not like pgconn and pgproto3. In particular this was
awkward for prepared statements.

Because pgx depends on pgtype and the tests for pgtype depend on pgx
this change will require a couple back and forth commits to get the
go.mod dependecies correct.
2019-08-24 13:49:12 -05:00
Jack Christensen
4cf1c44817 Fix unknown OID scanning into string and []byte 2019-08-22 18:20:36 -05:00
Jack Christensen
f25878662d Use golang.org/x/xerrors 2019-04-20 17:43:44 -05:00
Jack Christensen
bd85fe870d Hard code standard PostgreSQL types
Instead of needing to instrospect the database on connection preload the
standard OID / type map. Types from extensions (like hstore) and custom
types can be registered by the application developer. Otherwise, they
will be treated as strings.
2019-04-13 16:45:52 -05:00
Jack Christensen
f779b05f36 Extract scan value to pgtype 2019-04-12 21:31:59 -05:00
Iurii Krasnoshchok
91bb74b526 Add support for bpchar type 2018-01-02 12:29:40 +01:00
Jack Christensen
a01653c3df Add support for bit type 2017-11-18 21:13:34 -06:00
Jack Christensen
4e6de12a62 Fix missing interval mapping 2017-11-17 09:37:57 -06:00
Kelsey Francis
2dfcf74f62 Add UUIDArray type
Also change UUID.Set() to convert nil to NULL in order for
UUIDArray.Set() to support converting [][]byte slices that contain nil.
2017-08-27 19:36:53 -07:00
Timothée Peignier
43c2b979d0 Add more ColumnType support 2017-08-18 18:22:08 -07:00
Jack Christensen
a5f166bd21 Use github.com/pkg/errors 2017-06-04 21:30:03 -05:00
Jack Christensen
654adbdd4a Use Go casing convention for CID/TID/XID/CIDR 2017-06-03 12:01:49 -05:00
Jack Christensen
01fa5960b2 Use Go casing convention for ACLItem 2017-06-03 11:58:40 -05:00
Jack Christensen
aab8b77215 Use Go casing convention for JSON(B) 2017-06-03 11:57:14 -05:00
Jack Christensen
496c5a4dff Use Go casing convention for UUID 2017-06-03 11:54:57 -05:00
Jack Christensen
2140814606 Use Go casing convention for OID 2017-06-03 11:53:49 -05:00
Jack Christensen
6b906ca870 Refactor pgio and types to append buffers 2017-05-02 20:38:26 -05:00
Jack Christensen
ab21bc4ec7 pgtype DecodeText and DecodeBinary do not copy
They now take ownership of the src argument.

Needed to change Scan to make a copy of []byte arguments as lib/pq apparently
gives Scan a shared memory buffer.
2017-04-29 12:23:51 -05:00
Jack Christensen
92474ef292 Add MarshalJSON to a few types 2017-04-13 21:58:19 -05:00