Commit Graph

380 Commits (f59e8bf5551f403e6b7ec0912097bce85ea21351)

Author SHA1 Message Date
Lev Zakharov c27b9b49ea support different bool string representations 2023-04-27 20:29:41 -05:00
Jack Christensen 67f2a41587 Fix scanning a table type into a struct
Table types have system / hidden columns like tableoid, cmax, xmax, etc.
These are not included when sending or receiving composite types.

https://github.com/jackc/pgx/issues/1576
2023-04-20 20:13:37 -05:00
Simon Paredes 2cf1541bb9 wrap error 2023-04-11 18:07:05 -05:00
Jack Christensen 847f888631 Fix scan array of record to pointer to slice of struct
https://github.com/jackc/pgx/issues/1570
2023-04-08 14:39:48 -05:00
cemre.mengu ca022267db add tests 2023-03-25 10:22:11 -05:00
Cemre Mengu 2a653b4a8d fix: handle null interface for json
When using `scany` I encountered the following case. This seems to fix it.

Looks like null `jsonb` columns cause the problem. If you create a table like below you can see that the following code fails. Is this expected?

```sql
CREATE TABLE test (
	a int4 NULL,
	b int4 NULL,
	c jsonb NULL
);

INSERT INTO test (a, b, c) VALUES (1, null, null);
```

```go
package main

import (
	"context"
	"log"

	"github.com/georgysavva/scany/v2/pgxscan"
	"github.com/jackc/pgx/v5"
)

func main() {
	var rows []map[string]interface{}
	conn, _ := pgx.Connect(context.Background(), , ts.PGURL().String())
	
	// this will fail with can't scan into dest[0]: cannot scan NULL into *interface {}
	err := pgxscan.Select(context.Background(), conn, &rows, `SELECT c from test`) 
	
	// this works
	// err = pgxscan.Select(context.Background(), conn, &rows, `SELECT a,b from test`)
	
	if err != nil {
		panic(err)
	}

	log.Printf("%+v", rows)
}
```
2023-03-25 10:22:11 -05:00
Sergej Brazdeikis 9ae852eb58 Fix typo in error message `uint32` -> `uint16` 2023-03-11 15:34:08 -06:00
Jack Christensen 38e09bda4c Fix *wrapSliceEncodePlan[T].Encode
It should pass a FlatArray[T] to the next step instead of a
anySliceArrayReflect. By using a anySliceArrayReflect, an encode of
[]github.com/google/uuid.UUID followed by []string into a PostgreSQL
uuid[] would crash. This was caused by a EncodePlan cache collision
where the second encoding used part of the cached plan of the first.

In proper usage a cache collision shouldn't be able to occur. If this
assertion proves incorrect it will be necessary to add an optional
interface to ScanPlan and EncodePlan that marks the plan as ineligable
for caching. But I have been unable to construct a failing case, and
given that ScanPlans have been cached for quite some time now without
incident I do not think it is possible. This issue only occurred due to
the bug in *wrapSliceEncodePlan[T].Encode.

https://github.com/jackc/pgx/issues/1502
2023-02-21 21:04:30 -06:00
Jack Christensen 42d327f660 Add text format jsonpath support 2023-02-14 19:52:47 -06:00
Jack Christensen a6ace8969b Fix: Prefer sql.Scanner before TryWrapScanPlanFuncs
This was already the case when the data type was unknown but should also
be the case when it is known.
2023-02-14 09:03:41 -06:00
Jack Christensen 2100a64dbe Fix broken benchmarks 2023-02-10 20:26:18 -06:00
Jack Christensen 4484831550 Prefer binary format for arrays
This improves performance decoding text[].
2023-02-10 20:21:25 -06:00
Jack Christensen 1f43e2e490 Fix text format array decoding with a string of "NULL"
It was incorrectly being treated as NULL instead of 'NULL'.

fixes https://github.com/jackc/pgx/issues/1494
2023-02-10 19:59:03 -06:00
Felix Röhrich a47e836471 make TestPointerPointerStructScan easier to read 2023-02-10 19:06:20 -06:00
Felix Röhrich 5cd8468b99 replace erroneous reflect.New with reflect.Zero in TryWrapStructScanPlan 2023-02-10 19:06:20 -06:00
Jack Christensen c875abea84 Fix encode []any to array
https://github.com/jackc/pgx/issues/1488
2023-02-04 07:28:52 -06:00
Yumin Xia 766d2bba4f add UnmarshalJSON for pgtype Numeric 2023-01-30 21:33:02 -06:00
Jack Christensen 42a47194a2 Memoize encode plans
This significantly reduces memory allocations in paths that repeatedly
encode the same type of values such as CopyFrom.

https://github.com/jackc/pgx/issues/1481
2023-01-27 20:19:06 -06:00
Alexey Palazhchenko f839d501a7 Apply `gofmt -s`
And add CI check for that.
2023-01-24 07:55:00 -06:00
Jack Christensen e48e7a7189 Fix scanning json column into **string
refs https://github.com/jackc/pgx/issues/1470
2023-01-20 18:38:11 -06:00
Mark Chambers c46d792c93 Numeric numberTextBytes() workaround...
This seems a bit of a hack. It fixes the problems demonstrated in my previous commit.

Maybe there's a cleaner way?

Associated: https://github.com/jackc/pgx/issues/1426
2023-01-14 08:42:42 -06:00
Mark Chambers 37c6f97b11 pgtype.Numeric numberTextBytes() encoding bug
Demonstrate the problem with the tests:

...for negative decimal values e.g. -0.01

This causes errors when encoding to JSON:

    "json: error calling MarshalJSON for type pgtype.Numeric"

It also causes scan failures of sql.NullFloat64:

    "converting driver.Value type string ("0.-1") to a float64"

As reported here: https://github.com/jackc/pgx/issues/1426
2023-01-14 08:42:42 -06:00
Jack Christensen d4fcd4a897 Support sql.Scanner on renamed base type
https://github.com/jackc/pgtype/issues/197
2022-12-23 14:22:59 -06:00
Jack Christensen e66ad1bcec Fix encode to json ignoring driver.Valuer
https://github.com/jackc/pgx/issues/1430
2022-12-23 13:44:09 -06:00
Jack Christensen 456a242f5c Unregistered OIDs are handled the same as unknown OIDs
This improves handling of unregistered types. In general, they should
"just work". But there are performance benefits gained and some edge
cases avoided by registering types. Updated documentation to mention
this.

https://github.com/jackc/pgx/issues/1445
2022-12-23 13:14:56 -06:00
Jack Christensen d737852654 Fix: driver.Value representation of bytea should be []byte not string
https://github.com/jackc/pgx/issues/1445
2022-12-21 17:54:42 -06:00
Jack Christensen f42af35884 Add support for single dimensional arrays
https://github.com/jackc/pgx/issues/1442
2022-12-20 20:12:12 -06:00
Mark Chambers 1ce3e0384a pgtype Int fix minimum error message.
Previously on the minimum condition the error would be:

  "is greater than maximum"

Also add encoding/json import into the .erb template as the import was
missing after running rake generate.
2022-12-17 09:10:02 -06:00
Jack Christensen 279c3c0a20 Fix: json values work with sql.Scanner
https://github.com/jackc/pgx/issues/1418
2022-12-06 19:44:55 -06:00
Jack Christensen f0a73424b1 Fix: Scan uint and uint64 ScanNumeric
fixes https://github.com/jackc/pgx/issues/1414
2022-12-05 20:34:46 -06:00
Jack Christensen b265fedd75 Correct error message 2022-11-12 07:06:54 -06:00
Jack Christensen 871f14e43b Fix text decoding of dates with 5 digit years 2022-11-12 07:01:11 -06:00
Jack Christensen 071d1c9467 DateCodec.DecodeValue can return pgtype.InfinityModifier
Previously, an infinite value was returned as a string. Other types
that can be infinite such as Timestamptz return a
pgtype.InfinityModifier. This change brings them into alignment.
2022-11-12 06:27:41 -06:00
Jack Christensen 29109487ec DateCodec.DecodeDatabaseSQLValue returns time.Time when possible
Previously it returned a string. However, this was an unintended
behavior change from pgx v4.

89f69aaea9 (commitcomment-89173737)
2022-11-12 06:21:48 -06:00
Jack Christensen daf570c752 Date text encoding pads year with 0 for at least 4 digits
e.g. 0007-01-02 instead of 7-01-02

89f69aaea9 (commitcomment-89173737)
2022-11-12 06:14:04 -06:00
Jack Christensen 6fabd8f5b1 Fix encoding uint64 larger than math.MaxInt64 into numeric
fixes https://github.com/jackc/pgx/issues/1357
2022-10-29 08:47:12 -05:00
Jack Christensen 48b4807b33 Fix some reflect Kind checks to first check for nil
fixes https://github.com/jackc/pgx/issues/1335
2022-10-22 08:57:49 -05:00
Jack Christensen f2e7c8144d reflect.TypeOf can return nil. Check before using
https://github.com/jackc/pgx/issues/1331
2022-10-12 20:03:51 -05:00
Jack Christensen aff180b192 Remove dead code 2022-10-12 19:58:06 -05:00
Jack Christensen a581124dea Encode with driver.Valuer after trying TryWrapEncodePlanFuncs
However, all builtin TryWrapEncodePlanFuncs check for driver.Valuer and
skip themselves if it is found.
2022-10-12 19:52:57 -05:00
Jack Christensen c4407fb36e Prevent infinite loop for driver.Valuer / Codec edge case
A `driver.Valuer()` results in a `string` that the `Codec` for the
PostgreSQL type doesn't know how to handle. That string is scanned into
whatever the default type for that `Codec` is. That new value is
encoded. If the new value is the same type as the original type than an
infinite loop occured. Check that the types are different.

https://github.com/jackc/pgx/issues/1331
2022-10-12 19:46:15 -05:00
Jack Christensen af0b896290 Allow scanning null even if PG and Go types are incompatible
refs https://github.com/jackc/pgx/issues/1326
2022-10-08 09:10:43 -05:00
Jack Christensen 5655f9d593 Fix scan to pointer to pointer to renamed type
refs https://github.com/jackc/pgx/issues/1326
2022-10-08 08:10:40 -05:00
Jack Christensen 222e3b37bc Prefer driver.Value over wrap plans when encoding
This is tricky due to driver.Valuer returning any. For example, we can
plan for fmt.Stringer because it always returns a string.

Because of this driver.Valuer was always handled as the last option. But
with pgx v5 now having the ability to find underlying types like a
string and supporting fmt.Stringer it meant that driver.Valuer was
often not getting called because something else was found first.

This change tries driver.Valuer immediately after the initial PlanScan
for the Codec. So a type that directly implements a pgx interface should
be used, but driver.Valuer will be prefered before all the attempts to
handle renamed types, pointer deferencing, etc.

fixes https://github.com/jackc/pgx/issues/1319
fixes https://github.com/jackc/pgx/issues/1311
2022-10-01 12:20:23 -05:00
Jack Christensen 89f69aaea9 Date text encoding includes leading zero for month and day
e.g. 2000-01-01 instead of 2000-1-1. PostgreSQL accepted it without
zeroes but our text decoder didn't. This caused a problem when we needed
to take a value and encode to text so something else could parse it as
if it had come from the PostgreSQL server in text format. e.g.
database/sql compatibility.
2022-10-01 10:41:40 -05:00
Jack Christensen 335c8621ff Fix sqlScannerWrapper NULL handling
https://github.com/jackc/pgx/issues/1312
2022-09-24 10:30:12 -05:00
Jack Christensen ac9d4f4d96 Encode text for Lseg includes [ and ]
https://github.com/jackc/pgtype/issues/187
2022-09-24 10:30:12 -05:00
yogipristiawan 72e4b88e56 feat: add marshalJSON for float8 type 2022-09-24 10:00:40 -05:00
Jack Christensen f8d088cfb6 Fix JSON scan not completely overwriting destination
See https://github.com/jackc/pgtype/pull/185 for original report in
pgx v4 / pgtype.
2022-09-02 18:37:02 -05:00
Jack Christensen fe3a4f3150 Standardize casing for NULL in error messages 2022-08-22 21:01:18 -05:00