QueryPostgres runs the query on a Postgres server and returns the results, along with the OIDs of each result column. This assumes that a valid Postgres (not Doltgres) instance is on port 5432.
(query string)
| 27 | // QueryPostgres runs the query on a Postgres server and returns the results, along with the OIDs of each result column. |
| 28 | // This assumes that a valid Postgres (not Doltgres) instance is on port 5432. |
| 29 | func QueryPostgres(query string) ([]sql.Row, []uint32, error) { |
| 30 | var err error |
| 31 | ctx := context.Background() |
| 32 | if postgresConnection == nil { |
| 33 | connectionString := fmt.Sprintf("postgres://postgres:password@127.0.0.1:%d/", 5432) |
| 34 | postgresConnection, err = pgx.Connect(ctx, connectionString) |
| 35 | if err != nil { |
| 36 | return nil, nil, err |
| 37 | } |
| 38 | } |
| 39 | r, err := postgresConnection.Query(ctx, query) |
| 40 | if err != nil { |
| 41 | return nil, nil, err |
| 42 | } |
| 43 | var oids []uint32 |
| 44 | for _, desc := range r.FieldDescriptions() { |
| 45 | oids = append(oids, desc.DataTypeOID) |
| 46 | } |
| 47 | var allRows []sql.Row |
| 48 | for r.Next() { |
| 49 | if err = r.Err(); err != nil { |
| 50 | return nil, nil, err |
| 51 | } |
| 52 | row, err := r.Values() |
| 53 | if err != nil { |
| 54 | return nil, nil, err |
| 55 | } |
| 56 | allRows = append(allRows, row) |
| 57 | } |
| 58 | return allRows, oids, r.Err() |
| 59 | } |
| 60 | |
| 61 | // ExecPostgres runs the query on a Postgres server without checking the results. This assumes that a valid Postgres |
| 62 | // (not Doltgres) instance is on port 5432. |