|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/jackc/pgx/v5" |
| 10 | +) |
| 11 | + |
| 12 | +func runBasicQueries(connStr string) { |
| 13 | + |
| 14 | + fmt.Println("# runBasicQueries") |
| 15 | + |
| 16 | + ctx := context.Background() |
| 17 | + conn, err := pgx.Connect(ctx, connStr) |
| 18 | + if err != nil { |
| 19 | + log.Fatal(err) |
| 20 | + } |
| 21 | + defer conn.Close(ctx) |
| 22 | + var name string |
| 23 | + err = conn.QueryRow(ctx, "select name || $1 from sys.cluster", "foo").Scan(&name) |
| 24 | + if err != nil { |
| 25 | + log.Fatal(err) |
| 26 | + } |
| 27 | + fmt.Println(name) |
| 28 | + commandTag, err := conn.Exec(ctx, "create table if not exists t1 (x integer, ts timestamp)") |
| 29 | + if err != nil { |
| 30 | + log.Fatal(err) |
| 31 | + } |
| 32 | + fmt.Println(commandTag) |
| 33 | + ts := time.Now() |
| 34 | + // Convert to UTC |
| 35 | + loc, _ := time.LoadLocation("UTC") |
| 36 | + ts = ts.In(loc) |
| 37 | + commandTag, err = conn.Exec(ctx, "insert into t1 (x, ts) values (?, ?)", 1, ts) |
| 38 | + if err != nil { |
| 39 | + log.Fatal(err) |
| 40 | + } |
| 41 | + fmt.Println(commandTag) |
| 42 | + commandTag, err = conn.Exec(ctx, "refresh table t1") |
| 43 | + if err != nil { |
| 44 | + log.Fatal(err) |
| 45 | + } |
| 46 | + fmt.Println(commandTag) |
| 47 | + var tsRead time.Time |
| 48 | + err = conn.QueryRow(ctx, "select ts from t1").Scan(&tsRead) |
| 49 | + if err != nil { |
| 50 | + log.Fatal(err) |
| 51 | + } |
| 52 | + if tsRead.Sub(ts) > (1 * time.Second) { |
| 53 | + log.Fatal("Inserted ts doesn't match read ts: ", ts, tsRead) |
| 54 | + } |
| 55 | + commandTag, err = conn.Exec(ctx, "update t1 set x = ?", 2) |
| 56 | + if err != nil { |
| 57 | + log.Fatal(err) |
| 58 | + } |
| 59 | + fmt.Println(commandTag) |
| 60 | + commandTag, err = conn.Exec(ctx, "refresh table t1") |
| 61 | + if err != nil { |
| 62 | + log.Fatal(err) |
| 63 | + } |
| 64 | + fmt.Println(commandTag) |
| 65 | + commandTag, err = conn.Exec(ctx, "delete from t1 where x = ?", 2) |
| 66 | + if err != nil { |
| 67 | + log.Fatal(err) |
| 68 | + } |
| 69 | + fmt.Println(commandTag) |
| 70 | + |
| 71 | + fmt.Println() |
| 72 | + |
| 73 | +} |
0 commit comments