()
| 934 | } |
| 935 | |
| 936 | func ExampleRowToStructByNameLax() { |
| 937 | ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) |
| 938 | defer cancel() |
| 939 | |
| 940 | conn, err := pgx.Connect(ctx, os.Getenv("PGX_TEST_DATABASE")) |
| 941 | if err != nil { |
| 942 | fmt.Printf("Unable to establish connection: %v", err) |
| 943 | return |
| 944 | } |
| 945 | |
| 946 | if conn.PgConn().ParameterStatus("crdb_version") != "" { |
| 947 | // Skip test / example when running on CockroachDB. Since an example can't be skipped fake success instead. |
| 948 | fmt.Println(`Cheeseburger: $10 |
| 949 | Fries: $5 |
| 950 | Soft Drink: $3`) |
| 951 | return |
| 952 | } |
| 953 | |
| 954 | // Setup example schema and data. |
| 955 | _, err = conn.Exec(ctx, ` |
| 956 | create temporary table products ( |
| 957 | id int primary key generated by default as identity, |
| 958 | name varchar(100) not null, |
| 959 | price int not null |
| 960 | ); |
| 961 | |
| 962 | insert into products (name, price) values |
| 963 | ('Cheeseburger', 10), |
| 964 | ('Double Cheeseburger', 14), |
| 965 | ('Fries', 5), |
| 966 | ('Soft Drink', 3); |
| 967 | `) |
| 968 | if err != nil { |
| 969 | fmt.Printf("Unable to setup example schema and data: %v", err) |
| 970 | return |
| 971 | } |
| 972 | |
| 973 | type product struct { |
| 974 | ID int32 |
| 975 | Name string |
| 976 | Type string |
| 977 | Price int32 |
| 978 | } |
| 979 | |
| 980 | rows, _ := conn.Query(ctx, "select * from products where price < $1 order by price desc", 12) |
| 981 | products, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[product]) |
| 982 | if err != nil { |
| 983 | fmt.Printf("CollectRows error: %v", err) |
| 984 | return |
| 985 | } |
| 986 | |
| 987 | for _, p := range products { |
| 988 | fmt.Printf("%s: $%d\n", p.Name, p.Price) |
| 989 | } |
| 990 | |
| 991 | // Output: |
| 992 | // Cheeseburger: $10 |
| 993 | // Fries: $5 |
nothing calls this directly
no test coverage detected