Sql
This commit is contained in:
144
cmd/exp/exp.go
Normal file
144
cmd/exp/exp.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/jackc/pgx/v4/stdlib"
|
||||
)
|
||||
|
||||
func AddUser(db *sql.DB, name string, email string) (error, int) {
|
||||
row := db.QueryRow(`
|
||||
INSERT INTO users(name, email)
|
||||
VALUES($1, $2)
|
||||
RETURNING id;
|
||||
`, name, email)
|
||||
var id int
|
||||
err := row.Scan(&id)
|
||||
return err, id
|
||||
}
|
||||
|
||||
func QueryUser(db *sql.DB, id int) (error, string, string) {
|
||||
row := db.QueryRow(`
|
||||
SELECT name, email
|
||||
FROM users
|
||||
WHERE id=$1;`, id)
|
||||
var name, email string
|
||||
err := row.Scan(&name, &email)
|
||||
return err, name, email
|
||||
}
|
||||
|
||||
type Order struct {
|
||||
ID int
|
||||
UID int
|
||||
Amount int
|
||||
Desc string
|
||||
}
|
||||
|
||||
func AddOrder(db *sql.DB, uid int, amount int, desc string) (error, int) {
|
||||
row := db.QueryRow(`
|
||||
insert into orders(user_id, amount, description)
|
||||
values($1, $2, $3)
|
||||
returning id;
|
||||
`, uid, amount, desc)
|
||||
var id int
|
||||
err := row.Scan(&id)
|
||||
return err, id
|
||||
}
|
||||
|
||||
func QueryOrders(db *sql.DB, uid int) ([]Order, error) {
|
||||
var orders []Order
|
||||
rows, err := db.Query(`
|
||||
select id, amount, description from orders where user_id=$1
|
||||
`, uid)
|
||||
if err != nil {
|
||||
return orders, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var order Order
|
||||
order.UID = uid
|
||||
err := rows.Scan(&order.ID, &order.Amount, &order.Desc)
|
||||
if err != nil {
|
||||
return orders, err
|
||||
}
|
||||
orders = append(orders, order)
|
||||
}
|
||||
err = rows.Err()
|
||||
return orders, err
|
||||
}
|
||||
|
||||
func main() {
|
||||
db, err := sql.Open("pgx", "host=localhost port=5432 user=baloo dbname=lenslocked sslmode=disable")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer db.Close()
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("Connected!")
|
||||
_, err = db.Exec(`
|
||||
create table if not exists users (
|
||||
id serial primary key,
|
||||
name text,
|
||||
email text not null
|
||||
);
|
||||
create table if not exists orders (
|
||||
id serial primary key,
|
||||
user_id int not null,
|
||||
amount int,
|
||||
description text
|
||||
);`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("Tables created")
|
||||
|
||||
var name string
|
||||
var email string
|
||||
|
||||
// Create User
|
||||
/*
|
||||
name = "Jon Rob Baker"
|
||||
email = "demo@user.com"
|
||||
err, id := AddUser(db, name, email)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("User ", id, " created")
|
||||
*/
|
||||
|
||||
// Query User
|
||||
err, name, email = QueryUser(db, 5)
|
||||
if err == sql.ErrNoRows {
|
||||
fmt.Println("Error, no rows!")
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("User information: name=%s, email=%s\n", name, email)
|
||||
|
||||
uid := 5
|
||||
|
||||
// Create fake orders
|
||||
/*
|
||||
for i := 1; i <= 5; i++ {
|
||||
amount := i * 100
|
||||
desc := fmt.Sprintf("Fake order #%d", i)
|
||||
err, _ := AddOrder(db, uid, amount, desc)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
fmt.Println("Created fake orders")
|
||||
*/
|
||||
|
||||
// Query Orders
|
||||
orders, err := QueryOrders(db, uid)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("Orders: ", orders)
|
||||
}
|
||||
Reference in New Issue
Block a user