Add support for postgres arrays

This commit is contained in:
Arjen Brouwer 2024-09-03 15:39:36 +02:00 committed by go-jet
parent b835e25665
commit d3ada5361e
27 changed files with 558 additions and 74 deletions

View file

@ -2,6 +2,7 @@ package postgres
import (
"database/sql"
"github.com/lib/pq"
"testing"
"time"
@ -1361,11 +1362,11 @@ var allTypesRow0 = model.AllTypes{
JSON: `{"a": 1, "b": 3}`,
JsonbPtr: testutils.StringPtr(`{"a": 1, "b": 3}`),
Jsonb: `{"a": 1, "b": 3}`,
IntegerArrayPtr: testutils.StringPtr("{1,2,3}"),
IntegerArray: "{1,2,3}",
TextArrayPtr: testutils.StringPtr("{breakfast,consulting}"),
TextArray: "{breakfast,consulting}",
JsonbArray: `{"{\"a\": 1, \"b\": 2}","{\"a\": 3, \"b\": 4}"}`,
IntegerArrayPtr: &pq.Int32Array{1, 2, 3},
IntegerArray: pq.Int32Array{1, 2, 3},
TextArrayPtr: &pq.StringArray{"breakfast", "consulting"},
TextArray: pq.StringArray{"breakfast", "consulting"},
JsonbArray: pq.StringArray{`{"a": 1, "b": 2}`, `{"a": 3, "b": 4}`},
TextMultiDimArrayPtr: testutils.StringPtr("{{meeting,lunch},{training,presentation}}"),
TextMultiDimArray: "{{meeting,lunch},{training,presentation}}",
MoodPtr: &moodSad,
@ -1430,10 +1431,10 @@ var allTypesRow1 = model.AllTypes{
JsonbPtr: nil,
Jsonb: `{"a": 1, "b": 3}`,
IntegerArrayPtr: nil,
IntegerArray: "{1,2,3}",
IntegerArray: pq.Int32Array{1, 2, 3},
TextArrayPtr: nil,
TextArray: "{breakfast,consulting}",
JsonbArray: `{"{\"a\": 1, \"b\": 2}","{\"a\": 3, \"b\": 4}"}`,
TextArray: pq.StringArray{"breakfast", "consulting"},
JsonbArray: pq.StringArray{`{"a": 1, "b": 2}`, `{"a": 3, "b": 4}`},
TextMultiDimArrayPtr: nil,
TextMultiDimArray: "{{meeting,lunch},{training,presentation}}",
MoodPtr: nil,

View file

@ -447,7 +447,7 @@ func TestGeneratorTemplate_Model_ChangeFieldTypes(t *testing.T) {
require.Contains(t, data, "\"database/sql\"")
require.Contains(t, data, "Description sql.NullString")
require.Contains(t, data, "ReleaseYear sql.NullInt32")
require.Contains(t, data, "SpecialFeatures sql.NullString")
require.Contains(t, data, "SpecialFeatures *pq.StringArray")
}
func TestGeneratorTemplate_SQLBuilder_ChangeColumnTypes(t *testing.T) {

View file

@ -677,6 +677,7 @@ package model
import (
"github.com/google/uuid"
"github.com/lib/pq"
"time"
)
@ -735,11 +736,11 @@ type AllTypes struct {
JSON string
JsonbPtr *string
Jsonb string
IntegerArrayPtr *string
IntegerArray string
TextArrayPtr *string
TextArray string
JsonbArray string
IntegerArrayPtr *pq.Int32Array
IntegerArray pq.Int32Array
TextArrayPtr *pq.StringArray
TextArray pq.StringArray
JsonbArray pq.StringArray
TextMultiDimArrayPtr *string
TextMultiDimArray string
MoodPtr *Mood
@ -821,11 +822,11 @@ type allTypesTable struct {
JSON postgres.ColumnString
JsonbPtr postgres.ColumnString
Jsonb postgres.ColumnString
IntegerArrayPtr postgres.ColumnString
IntegerArray postgres.ColumnString
TextArrayPtr postgres.ColumnString
TextArray postgres.ColumnString
JsonbArray postgres.ColumnString
IntegerArrayPtr postgres.ColumnIntegerArray
IntegerArray postgres.ColumnIntegerArray
TextArrayPtr postgres.ColumnStringArray
TextArray postgres.ColumnStringArray
JsonbArray postgres.ColumnStringArray
TextMultiDimArrayPtr postgres.ColumnString
TextMultiDimArray postgres.ColumnString
MoodPtr postgres.ColumnString
@ -924,11 +925,11 @@ func newAllTypesTableImpl(schemaName, tableName, alias string) allTypesTable {
JSONColumn = postgres.StringColumn("json")
JsonbPtrColumn = postgres.StringColumn("jsonb_ptr")
JsonbColumn = postgres.StringColumn("jsonb")
IntegerArrayPtrColumn = postgres.StringColumn("integer_array_ptr")
IntegerArrayColumn = postgres.StringColumn("integer_array")
TextArrayPtrColumn = postgres.StringColumn("text_array_ptr")
TextArrayColumn = postgres.StringColumn("text_array")
JsonbArrayColumn = postgres.StringColumn("jsonb_array")
IntegerArrayPtrColumn = postgres.IntegerArrayColumn("integer_array_ptr")
IntegerArrayColumn = postgres.IntegerArrayColumn("integer_array")
TextArrayPtrColumn = postgres.StringArrayColumn("text_array_ptr")
TextArrayColumn = postgres.StringArrayColumn("text_array")
JsonbArrayColumn = postgres.StringArrayColumn("jsonb_array")
TextMultiDimArrayPtrColumn = postgres.StringColumn("text_multi_dim_array_ptr")
TextMultiDimArrayColumn = postgres.StringColumn("text_multi_dim_array")
MoodPtrColumn = postgres.StringColumn("mood_ptr")

View file

@ -2,6 +2,7 @@ package postgres
import (
"context"
"github.com/lib/pq"
"github.com/volatiletech/null/v8"
"testing"
"time"
@ -953,6 +954,7 @@ func TestScanIntoCustomBaseTypes(t *testing.T) {
type MyFloat32 float32
type MyFloat64 float64
type MyString string
type MyStringArray pq.StringArray
type MyTime = time.Time
type film struct {
@ -967,26 +969,25 @@ func TestScanIntoCustomBaseTypes(t *testing.T) {
ReplacementCost MyFloat64
Rating *model.MpaaRating
LastUpdate MyTime
SpecialFeatures *MyString
SpecialFeatures MyStringArray
Fulltext MyString
}
// We'll skip special features, because it's a slice and it does not implement sql.Scanner
stmt := SELECT(
Film.AllColumns,
Film.AllColumns.Except(Film.SpecialFeatures),
).FROM(
Film,
).ORDER_BY(
Film.FilmID.ASC(),
).LIMIT(3)
var films []model.Film
err := stmt.Query(db, &films)
var myFilms []film
err := stmt.Query(db, &myFilms)
require.NoError(t, err)
var myFilms []film
err = stmt.Query(db, &myFilms)
var films []model.Film
err = stmt.Query(db, &films)
require.NoError(t, err)
require.Equal(t, testutils.ToJSON(films), testutils.ToJSON(myFilms))
@ -1160,7 +1161,7 @@ var film1 = model.Film{
ReplacementCost: 20.99,
Rating: &pgRating,
LastUpdate: *testutils.TimestampWithoutTimeZone("2013-05-26 14:50:58.951", 3),
SpecialFeatures: testutils.StringPtr("{\"Deleted Scenes\",\"Behind the Scenes\"}"),
SpecialFeatures: &pq.StringArray{"Deleted Scenes", "Behind the Scenes"},
Fulltext: "'academi':1 'battl':15 'canadian':20 'dinosaur':2 'drama':5 'epic':4 'feminist':8 'mad':11 'must':14 'rocki':21 'scientist':12 'teacher':17",
}
@ -1176,7 +1177,7 @@ var film2 = model.Film{
ReplacementCost: 12.99,
Rating: &gRating,
LastUpdate: *testutils.TimestampWithoutTimeZone("2013-05-26 14:50:58.951", 3),
SpecialFeatures: testutils.StringPtr(`{Trailers,"Deleted Scenes"}`),
SpecialFeatures: &pq.StringArray{"Trailers", "Deleted Scenes"},
Fulltext: `'ace':1 'administr':9 'ancient':19 'astound':4 'car':17 'china':20 'databas':8 'epistl':5 'explor':12 'find':15 'goldfing':2 'must':14`,
}

View file

@ -3,6 +3,7 @@ package postgres
import (
"context"
"database/sql"
"github.com/lib/pq"
"testing"
"time"
@ -1837,7 +1838,7 @@ ORDER BY film.film_id ASC;
Rating: &gRating,
RentalDuration: 3,
LastUpdate: *testutils.TimestampWithoutTimeZone("2013-05-26 14:50:58.951", 3),
SpecialFeatures: testutils.StringPtr("{Trailers,\"Deleted Scenes\"}"),
SpecialFeatures: &pq.StringArray{"Trailers", "Deleted Scenes"},
Fulltext: "'ace':1 'administr':9 'ancient':19 'astound':4 'car':17 'china':20 'databas':8 'epistl':5 'explor':12 'find':15 'goldfing':2 'must':14",
})
}
@ -2793,7 +2794,7 @@ ORDER BY actor.actor_id ASC, film.film_id ASC;
err := stmt.Query(db, &dest)
require.NoError(t, err)
//jsonSave("./testdata/quick-start-dest.json", dest)
//testutils.SaveJSONFile(dest, "./testdata/results/postgres/quick-start-dest.json")
testutils.AssertJSONFile(t, dest, "./testdata/results/postgres/quick-start-dest.json")
var dest2 []struct {
@ -2806,7 +2807,7 @@ ORDER BY actor.actor_id ASC, film.film_id ASC;
err = stmt.Query(db, &dest2)
require.NoError(t, err)
//jsonSave("./testdata/quick-start-dest2.json", dest2)
//testutils.SaveJSONFile(dest, "./testdata/results/postgres/quick-start-dest2.json")
testutils.AssertJSONFile(t, dest2, "./testdata/results/postgres/quick-start-dest2.json")
}
@ -3382,7 +3383,10 @@ func TestRecursionScanNxM(t *testing.T) {
"ReplacementCost": 20.99,
"Rating": "PG",
"LastUpdate": "2013-05-26T14:50:58.951Z",
"SpecialFeatures": "{\"Deleted Scenes\",\"Behind the Scenes\"}",
"SpecialFeatures": [
"Deleted Scenes",
"Behind the Scenes"
],
"Fulltext": "'academi':1 'battl':15 'canadian':20 'dinosaur':2 'drama':5 'epic':4 'feminist':8 'mad':11 'must':14 'rocki':21 'scientist':12 'teacher':17",
"Actors": [
{
@ -3406,7 +3410,10 @@ func TestRecursionScanNxM(t *testing.T) {
"ReplacementCost": 9.99,
"Rating": "R",
"LastUpdate": "2013-05-26T14:50:58.951Z",
"SpecialFeatures": "{Trailers,\"Deleted Scenes\"}",
"SpecialFeatures": [
"Trailers",
"Deleted Scenes"
],
"Fulltext": "'anaconda':1 'australia':18 'confess':2 'dentist':8,11 'display':5 'fight':14 'girl':16 'lacklustur':4 'must':13",
"Actors": [
{
@ -3454,7 +3461,10 @@ func TestRecursionScanNxM(t *testing.T) {
"ReplacementCost": 20.99,
"Rating": "PG",
"LastUpdate": "2013-05-26T14:50:58.951Z",
"SpecialFeatures": "{\"Deleted Scenes\",\"Behind the Scenes\"}",
"SpecialFeatures": [
"Deleted Scenes",
"Behind the Scenes"
],
"Fulltext": "'academi':1 'battl':15 'canadian':20 'dinosaur':2 'drama':5 'epic':4 'feminist':8 'mad':11 'must':14 'rocki':21 'scientist':12 'teacher':17",
"Actors": null
},
@ -3470,7 +3480,10 @@ func TestRecursionScanNxM(t *testing.T) {
"ReplacementCost": 9.99,
"Rating": "R",
"LastUpdate": "2013-05-26T14:50:58.951Z",
"SpecialFeatures": "{Trailers,\"Deleted Scenes\"}",
"SpecialFeatures": [
"Trailers",
"Deleted Scenes"
],
"Fulltext": "'anaconda':1 'australia':18 'confess':2 'dentist':8,11 'display':5 'fight':14 'girl':16 'lacklustur':4 'must':13",
"Actors": null
}