Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello World tutorial for Go pgx #7698

Merged
merged 1 commit into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _includes/sidebar-data-v19.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"title": "Go",
"urls": [
"/${VERSION}/build-a-go-app-with-cockroachdb.html",
"/${VERSION}/build-a-go-app-with-cockroachdb-pq.html",
"/${VERSION}/build-a-go-app-with-cockroachdb-gorm.html"
]
},
Expand Down
1 change: 1 addition & 0 deletions _includes/sidebar-data-v20.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@
"title": "Go",
"urls": [
"/${VERSION}/build-a-go-app-with-cockroachdb.html",
"/${VERSION}/build-a-go-app-with-cockroachdb-pq.html",
"/${VERSION}/build-a-go-app-with-cockroachdb-gorm.html"
]
},
Expand Down
1 change: 1 addition & 0 deletions _includes/sidebar-data-v20.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@
"title": "Go",
"urls": [
"/${VERSION}/build-a-go-app-with-cockroachdb.html",
"/${VERSION}/build-a-go-app-with-cockroachdb-pq.html",
"/${VERSION}/build-a-go-app-with-cockroachdb-gorm.html"
]
},
Expand Down
52 changes: 52 additions & 0 deletions _includes/v19.2/app/basic-sample-pgx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"context"
"fmt"
"log"

"github.com/jackc/pgx/v4"
)

func main() {
config, err := pgx.ParseConfig("postgresql://maxroach@localhost:26257/bank?sslmode=require&sslrootcert=certs/ca.crt&sslkey=certs/client.maxroach.key&sslcert=certs/client.maxroach.crt")
if err != nil {
log.Fatal("error configuring the database: ", err)
}

config.TLSConfig.ServerName = "localhost"

// Connect to the "bank" database.
conn, err := pgx.ConnectConfig(context.Background(), config)
if err != nil {
log.Fatal("error connecting to the database: ", err)
}
defer conn.Close(context.Background())

// Create the "accounts" table.
if _, err := conn.Exec(context.Background(),
"CREATE TABLE IF NOT EXISTS accounts (id INT PRIMARY KEY, balance INT)"); err != nil {
log.Fatal(err)
}

// Insert two rows into the "accounts" table.
if _, err := conn.Exec(context.Background(),
"INSERT INTO accounts (id, balance) VALUES (1, 1000), (2, 250)"); err != nil {
log.Fatal(err)
}

// Print out the balances.
rows, err := conn.Query(context.Background(), "SELECT id, balance FROM accounts")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
fmt.Println("Initial balances:")
for rows.Next() {
var id, balance int
if err := rows.Scan(&id, &balance); err != nil {
log.Fatal(err)
}
fmt.Printf("%d %d\n", id, balance)
}
}
50 changes: 50 additions & 0 deletions _includes/v19.2/app/insecure/basic-sample-pgx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"context"
"fmt"
"log"

"github.com/jackc/pgx/v4"
)

func main() {
config, err := pgx.ParseConfig("postgresql://maxroach@localhost:26257/bank?sslmode=disable")
if err != nil {
log.Fatal("error configuring the database: ", err)
}

// Connect to the "bank" database.
conn, err := pgx.ConnectConfig(context.Background(), config)
if err != nil {
log.Fatal("error connecting to the database: ", err)
}
defer conn.Close(context.Background())

// Create the "accounts" table.
if _, err := conn.Exec(context.Background(),
"CREATE TABLE IF NOT EXISTS accounts (id INT PRIMARY KEY, balance INT)"); err != nil {
log.Fatal(err)
}

// Insert two rows into the "accounts" table.
if _, err := conn.Exec(context.Background(),
"INSERT INTO accounts (id, balance) VALUES (1, 1000), (2, 250)"); err != nil {
log.Fatal(err)
}

// Print out the balances.
rows, err := conn.Query(context.Background(), "SELECT id, balance FROM accounts")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
fmt.Println("Initial balances:")
for rows.Next() {
var id, balance int
if err := rows.Scan(&id, &balance); err != nil {
log.Fatal(err)
}
fmt.Printf("%d %d\n", id, balance)
}
}
58 changes: 58 additions & 0 deletions _includes/v19.2/app/insecure/txn-sample-pgx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"
"fmt"
"log"

"github.com/cockroachdb/cockroach-go/crdb/crdbpgx"
"github.com/jackc/pgx/v4"
)

func transferFunds(ctx context.Context, tx pgx.Tx, from int, to int, amount int) error {
// Read the balance.
var fromBalance int
if err := tx.QueryRow(ctx,
"SELECT balance FROM accounts WHERE id = $1", from).Scan(&fromBalance); err != nil {
return err
}

if fromBalance < amount {
return fmt.Errorf("insufficient funds")
}

// Perform the transfer.
if _, err := tx.Exec(ctx,
"UPDATE accounts SET balance = balance - $1 WHERE id = $2", amount, from); err != nil {
return err
}
if _, err := tx.Exec(ctx,
"UPDATE accounts SET balance = balance + $1 WHERE id = $2", amount, to); err != nil {
return err
}
return nil
}

func main() {
config, err := pgx.ParseConfig("postgresql://maxroach@localhost:26257/bank?sslmode=disable")
if err != nil {
log.Fatal("error configuring the database: ", err)
}

// Connect to the "bank" database.
conn, err := pgx.ConnectConfig(context.Background(), config)
if err != nil {
log.Fatal("error connecting to the database: ", err)
}
defer conn.Close(context.Background())

// Run a transfer in a transaction.
err = crdbpgx.ExecuteTx(context.Background(), conn, pgx.TxOptions{}, func(tx pgx.Tx) error {
return transferFunds(context.Background(), tx, 1 /* from acct# */, 2 /* to acct# */, 100 /* amount */)
})
if err == nil {
fmt.Println("Success")
} else {
log.Fatal("error: ", err)
}
}
60 changes: 60 additions & 0 deletions _includes/v19.2/app/txn-sample-pgx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"context"
"fmt"
"log"

"github.com/cockroachdb/cockroach-go/crdb/crdbpgx"
"github.com/jackc/pgx/v4"
)

func transferFunds(ctx context.Context, tx pgx.Tx, from int, to int, amount int) error {
// Read the balance.
var fromBalance int
if err := tx.QueryRow(ctx,
"SELECT balance FROM accounts WHERE id = $1", from).Scan(&fromBalance); err != nil {
return err
}

if fromBalance < amount {
return fmt.Errorf("insufficient funds")
}

// Perform the transfer.
if _, err := tx.Exec(ctx,
"UPDATE accounts SET balance = balance - $1 WHERE id = $2", amount, from); err != nil {
return err
}
if _, err := tx.Exec(ctx,
"UPDATE accounts SET balance = balance + $1 WHERE id = $2", amount, to); err != nil {
return err
}
return nil
}

func main() {
config, err := pgx.ParseConfig("postgresql://maxroach@localhost:26257/bank?sslmode=require&sslrootcert=certs/ca.crt&sslkey=certs/client.maxroach.key&sslcert=certs/client.maxroach.crt")
if err != nil {
log.Fatal("error configuring the database: ", err)
}

config.TLSConfig.ServerName = "localhost"

// Connect to the "bank" database.
conn, err := pgx.ConnectConfig(context.Background(), config)
if err != nil {
log.Fatal("error connecting to the database: ", err)
}
defer conn.Close(context.Background())

// Run a transfer in a transaction.
err = crdbpgx.ExecuteTx(context.Background(), conn, pgx.TxOptions{}, func(tx pgx.Tx) error {
return transferFunds(context.Background(), tx, 1 /* from acct# */, 2 /* to acct# */, 100 /* amount */)
})
if err == nil {
fmt.Println("Success")
} else {
log.Fatal("error: ", err)
}
}
52 changes: 52 additions & 0 deletions _includes/v20.1/app/basic-sample-pgx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"context"
"fmt"
"log"

"github.com/jackc/pgx/v4"
)

func main() {
config, err := pgx.ParseConfig("postgresql://maxroach@localhost:26257/bank?sslmode=require&sslrootcert=certs/ca.crt&sslkey=certs/client.maxroach.key&sslcert=certs/client.maxroach.crt")
if err != nil {
log.Fatal("error configuring the database: ", err)
}

config.TLSConfig.ServerName = "localhost"

// Connect to the "bank" database.
conn, err := pgx.ConnectConfig(context.Background(), config)
if err != nil {
log.Fatal("error connecting to the database: ", err)
}
defer conn.Close(context.Background())

// Create the "accounts" table.
if _, err := conn.Exec(context.Background(),
"CREATE TABLE IF NOT EXISTS accounts (id INT PRIMARY KEY, balance INT)"); err != nil {
log.Fatal(err)
}

// Insert two rows into the "accounts" table.
if _, err := conn.Exec(context.Background(),
"INSERT INTO accounts (id, balance) VALUES (1, 1000), (2, 250)"); err != nil {
log.Fatal(err)
}

// Print out the balances.
rows, err := conn.Query(context.Background(), "SELECT id, balance FROM accounts")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
fmt.Println("Initial balances:")
for rows.Next() {
var id, balance int
if err := rows.Scan(&id, &balance); err != nil {
log.Fatal(err)
}
fmt.Printf("%d %d\n", id, balance)
}
}
50 changes: 50 additions & 0 deletions _includes/v20.1/app/insecure/basic-sample-pgx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"context"
"fmt"
"log"

"github.com/jackc/pgx/v4"
)

func main() {
config, err := pgx.ParseConfig("postgresql://maxroach@localhost:26257/bank?sslmode=disable")
if err != nil {
log.Fatal("error configuring the database: ", err)
}

// Connect to the "bank" database.
conn, err := pgx.ConnectConfig(context.Background(), config)
if err != nil {
log.Fatal("error connecting to the database: ", err)
}
defer conn.Close(context.Background())

// Create the "accounts" table.
if _, err := conn.Exec(context.Background(),
"CREATE TABLE IF NOT EXISTS accounts (id INT PRIMARY KEY, balance INT)"); err != nil {
log.Fatal(err)
}

// Insert two rows into the "accounts" table.
if _, err := conn.Exec(context.Background(),
"INSERT INTO accounts (id, balance) VALUES (1, 1000), (2, 250)"); err != nil {
log.Fatal(err)
}

// Print out the balances.
rows, err := conn.Query(context.Background(), "SELECT id, balance FROM accounts")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
fmt.Println("Initial balances:")
for rows.Next() {
var id, balance int
if err := rows.Scan(&id, &balance); err != nil {
log.Fatal(err)
}
fmt.Printf("%d %d\n", id, balance)
}
}
Loading