golangã§DBæ¥ç¶ï¼PostgreSQLï¼ã試ããã®ã§ãã®ãµã³ãã«ã§ãã
Â
以ä¸ãµã³ãã«ã
ã¾ãã¯æ¥ç¶å
ã®PostgreSQLç°å¢ãæºåãã¾ãã
ããã¯æã£åãæ©ããDocker ã§ç°å¢ãä½ãã¾ããã
ï¼åæï¼
Docker ãã¤ã³ã¹ãã¼ã«æ¸ã¿ã§ãããã¨
postgresql ãã¤ã³ã¹ãã¼ã«æ¸ã¿ã§ãããã¨ï¼psql ã³ãã³ãã使ç¨ã㦠DBæ¥ç¶ã確èªããããï¼
ä¸è¨ã® docker-compose.yml ãä½æãããdocker-compose up -dãã§èµ·åãã¾ãã
Â
[docker-compose.yml]
version: '3'
services:
 postgres:
  image: postgres:14
  container_name: postgres
  environment:
   POSTGRES_USER: user
   POSTGRES_PASSWORD: password
   POSTGRES_DB: sample
   TZ: "Asia/Tokyo"
   - 5432:5432
  volumes:
Â
docker exec -it postgres psql -U user -d sample
ä¸è¨ã³ãã³ãã§PostgreSQLã«æ¥ç¶ãã§ãã¾ãã
ããã§PostgreSQLç°å¢æ§ç¯ã¯å®äºã§ãã
docker exec -it postgres psql -U user -d sample
psql (14.10 (Debian 14.10-1.pgdg120+1))
Type "help" for help.
sample=#Â
Â
Goããã°ã©ã ãã PostgreSQLã«æ¥ç¶
ç¶ãã¦ãGo ã§PostgreSQL ã«æ¥ç¶ããå¦çãæ¸ãã¦ããã¾ãã
pq ã©ã¤ãã©ãªã使ç¨ãã¾ããã
Â
ä¸è¨ã§å種ãã¡ã¤ã«ãç¨æãã¾ãã
mkdir sample
go mod init sample
touch main.go
Â
ã¾ã㯠main.go ã«ã¯ä¸è¨ã®å
容ãè¨è¼ãã¾ãã
Â
package main
import (
  "fmt"
  "log"
)
func main() {
  connStr := "user=user password=password dbname=sample sslmode=disable"
  _, err := sql.Open("postgres", connStr)
    log.Fatal(err)
  }
  fmt.Println("DBæ¥ç¶")
}
Â
ã_ "github.com/lib/pq"ãã®ã¨ããã§ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã®ã§ã
go mod tidy
ãå®è¡ãããã¨ã§ãpq ã go.mod ã®ä¾åé¢ä¿ã«è¿½å ããã¾ããï¼go.mod ãã¡ã¤ã«ãæ´æ°ãããï¼
Â
ããã¾ã§ã§ããããmain.go ãå®è¡ãã¾ãã
ãDBæ¥ç¶ãã¨è¡¨ç¤ºãããã°ãåé¡ãªãæ¥ç¶ãã§ãã¦ãã¾ãã
go run main.goÂ
DBæ¥ç¶
Â
æ¥ç¶ããã ãã ã¨é¢ç½ããªãã®ã§ãDBã¸ã®CRUDã試ãã¾ãã
Â
ã¾ãã¯åä½æ¤è¨¼ç¨ã®ãã¼ãã«ãç¨æã
CREATE TABLE "user" (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255),
  age INT
);
Â
ç¶ãã¦ãã¼ã¿ã®ç»é²ã§ãã
ãã¼ã¿ç»é²ç¨ã® dbInsert é¢æ°ã追å ãã¦ãã¾ãã
Â
package main
import (
  "fmt"
  "log"
)
func main() {
  connStr := "user=user password=password dbname=sample sslmode=disable"
  db, err := sql.Open("postgres", connStr)
    log.Fatal(err)
  }
  fmt.Println("DBæ¥ç¶")
  var command string
  fmt.Print("ã³ãã³ããå
¥å (insert/update/delete/select): ")
  fmt.Scan(&command)
  switch command {
  case "insert":
    dbInsert(db)
  default:
    fmt.Println("ç¡å¹ãªã³ãã³ã")
  }
}
func dbInsert(db *sql.DB) {
  var name string
  var age int
  fmt.Print("ååãå
¥å: ")
  fmt.Scan(&name)
  fmt.Print("å¹´é½¢ãå
¥å: ")
  fmt.Scan(&age)
  // INSERTæãå®è¡
  result, err := db.Exec("INSERT INTO \"user\" (name, age) VALUES ($1, $2)", name, age)
    log.Fatal(err)
  }
  // 追å æåæã®ã¡ãã»ã¼ã¸ã¨è¿½å ãããã¬ã³ã¼ãå
容ã表示
  fmt.Println("追å ãã¾ããã")
  lastInsertID, _ := result.LastInsertId()
  fmt.Printf("追å ãããã¬ã³ã¼ãã®ID: %d, åå: %s, å¹´é½¢: %d\n", lastInsertID, name, age)
}
Â
ã³ãã³ãã©ã¤ã³ãããinsert/update/delete/select ã®ããããã®å
¥åãåãä»ãã
insert ã®å ´åã«ãã¼ã¿ç»é²ãè¡ãããã«ãã¦ãã¾ãã
ãããå®è¡ãã¦ã¿ã¾ãã
go run main.goÂ
DBæ¥ç¶
ã³ãã³ããå
¥å (insert/update/delete/select): insert
ååãå
¥å: test
å¹´é½¢ãå
¥å: 10
追å ãã¾ããã
追å ãããã¬ã³ã¼ãã®ID: 0, åå: test, å¹´é½¢: 10
Â
PostgreSQLã«ãSelectå¥ãæãã¦ã¿ã¾ãã
select * from public.user;
 id | name | ageÂ
----+------+-----
 1 | test |  10
(1 row)
Â
ç¡äºã«ç»é²ã§ãã¦ãã¾ãã
Â
ç¶ãã¦ãã¼ã¿ã®æ´æ°ã§ãã
ãã¼ã¿æ´æ°ç¨ã® dbUpdate é¢æ°ã追å ãã¦ãã¾ãã
Â
func main() {
  connStr := "user=user password=password dbname=sample sslmode=disable"
  db, err := sql.Open("postgres", connStr)
    log.Fatal(err)
  }
  fmt.Println("DBæ¥ç¶")
  var command string
  fmt.Print("ã³ãã³ããå
¥å (insert/update/delete/select): ")
  fmt.Scan(&command)
  switch command {
  case "insert":
    dbInsert(db)
  case "update":
    dbUpdate(db)
  default:
    fmt.Println("ç¡å¹ãªã³ãã³ã")
  }
}
func dbUpdate(db *sql.DB) {
  var id int
  var name string
  var age int
  fmt.Print("æ´æ°å¯¾è±¡ã®ã¬ã³ã¼ãã®IDãå
¥å: ")
  fmt.Scan(&id)
  fmt.Print("ååãå
¥å: ")
  fmt.Scan(&name)
  fmt.Print("å¹´é½¢ãå
¥å: ")
  fmt.Scan(&age)
  // UPDATEæãå®è¡
  result, err := db.Exec("UPDATE \"user\" SET name = $1, age = $2 WHERE id = $3", name, age, id)
    log.Fatal(err)
  }
  // æ´æ°æåæã®ã¡ãã»ã¼ã¸ã¨æ´æ°ãããã¬ã³ã¼ãå
容ã表示
  rowsAffected, _ := result.RowsAffected()
  if rowsAffected > 0 {
    fmt.Println("æ´æ°ãã¾ããã")
    fmt.Printf("æ´æ°ãããã¬ã³ã¼ãã®ID: %d, æ°ããåå: %s, æ°ããå¹´é½¢: %d\n", id, name, age)
  } else {
    fmt.Println("æå®ãããIDã®ã¬ã³ã¼ããè¦ã¤ããã¾ããã§ããã")
  }
}
Â
ã³ãã³ãã©ã¤ã³ããã®å
¥åããupdate ã®å ´åã«ãã¼ã¿æ´æ°ãè¡ãããã«ãã¦ãã¾ãã
ãããå®è¡ãã¦ã¿ã¾ãã
go run main.goÂ
DBæ¥ç¶
ã³ãã³ããå
¥å (insert/update/delete/select): update
æ´æ°å¯¾è±¡ã®ã¬ã³ã¼ãã®IDãå
¥å: 1
ååãå
¥å: test_update
å¹´é½¢ãå
¥å: 20
æ´æ°ãã¾ããã
æ´æ°ãããã¬ã³ã¼ãã®ID: 1, æ°ããåå: test_update, æ°ããå¹´é½¢: 20
Â
PostgreSQLã«ãSelectå¥ãæãã¦ã¿ã¾ãã
select * from public.user where id = 1;
 id |   name   | ageÂ
----+-------------+-----
 1 | test_update |  20
(1 row)
Â
ç¡äºã«æ´æ°ã§ãã¦ãã¾ãã
Â
ç¶ãã¦ãã¼ã¿ã®æ¤ç´¢ã§ãã
ãã¼ã¿æ¤ç´¢ç¨ã® dbSelect é¢æ°ã追å ãã¦ãã¾ãã
Â
func main() {
  connStr := "user=user password=password dbname=sample sslmode=disable"
  db, err := sql.Open("postgres", connStr)
    log.Fatal(err)
  }
  fmt.Println("DBæ¥ç¶")
  var command string
  fmt.Print("ã³ãã³ããå
¥å (insert/update/delete/select): ")
  fmt.Scan(&command)
  switch command {
  case "insert":
    dbInsert(db)
  case "update":
    dbUpdate(db)
  case "select":
    dbSelect(db)
  default:
    fmt.Println("ç¡å¹ãªã³ãã³ã")
  }
}
Â
func dbSelect(db *sql.DB) {
  var id int
  fmt.Print("æ¤ç´¢å¯¾è±¡ã®ã¬ã³ã¼ãã®IDãå
¥å: ")
  fmt.Scan(&id)
  // SELECTæãå®è¡
  row := db.QueryRow("SELECT id, name, age FROM \"user\" WHERE id = $1", id)
  var selectedID int
  var selectedName string
  var selectedAge int
  // åå¾ããã¬ã³ã¼ãã®å¤ãã¹ãã£ã³
  err := row.Scan(&selectedID, &selectedName, &selectedAge)
    if err == sql.ErrNoRows {
      fmt.Println("æå®ãããIDã®ã¬ã³ã¼ããè¦ã¤ããã¾ããã§ããã")
    } else {
      log.Fatal(err)
    }
    return
  }
  // åå¾ããã¬ã³ã¼ãã®å
容ã表示
  fmt.Printf("ID: %d, åå: %s, å¹´é½¢: %d\n", selectedID, selectedName, selectedAge)
}
Â
ã³ãã³ãã©ã¤ã³ããã®å
¥åããselect ã®å ´åã«ãã¼ã¿æ¤ç´¢ãè¡ãããã«ãã¦ãã¾ãã
ãããå®è¡ãã¦ã¿ã¾ãã
go run main.goÂ
DBæ¥ç¶
ã³ãã³ããå
¥å (insert/update/delete/select): select
æ¤ç´¢å¯¾è±¡ã®ã¬ã³ã¼ãã®IDãå
¥å: 1
ID: 1, åå: test_update, å¹´é½¢: 20
Â
PostgreSQLã«ãSelectå¥ãæãã¦ã¿ã¾ãã
select * from public.user where id = 1;
 id |   name   | ageÂ
----+-------------+-----
 1 | test_update |  20
(1 row)
Â
ç¡äºã«æ¤ç´¢ã§ãã¦ãã¾ãã
Â
ç¶ãã¦ãã¼ã¿ã®åé¤ã§ãã
ãã¼ã¿åé¤ç¨ã® dbDelete é¢æ°ã追å ãã¦ãã¾ãã
Â
func main() {
  connStr := "user=user password=password dbname=sample sslmode=disable"
  db, err := sql.Open("postgres", connStr)
    log.Fatal(err)
  }
  fmt.Println("DBæ¥ç¶")
  var command string
  fmt.Print("ã³ãã³ããå
¥å (insert/update/delete/select): ")
  fmt.Scan(&command)
  switch command {
  case "insert":
    dbInsert(db)
  case "update":
    dbUpdate(db)
  case "select":
    dbSelect(db)
  case "delete":
    dbDelete(db)
  default:
    fmt.Println("ç¡å¹ãªã³ãã³ã")
  }
}
Â
func dbDelete(db *sql.DB) {
  var id int
  fmt.Print("åé¤å¯¾è±¡ã®ã¬ã³ã¼ãã®IDãå
¥å: ")
  fmt.Scan(&id)
  // DELETEæãå®è¡
  result, err := db.Exec("DELETE FROM \"user\" WHERE id = $1", id)
    log.Fatal(err)
  }
  // åé¤æåæã®ã¡ãã»ã¼ã¸ã表示
  rowsAffected, _ := result.RowsAffected()
  if rowsAffected > 0 {
    fmt.Println("åé¤ãã¾ããã")
  } else {
    fmt.Println("æå®ãããIDã®ã¬ã³ã¼ããè¦ã¤ããã¾ããã§ããã")
  }
}
Â
ã³ãã³ãã©ã¤ã³ããã®å
¥åããdelete ã®å ´åã«ãã¼ã¿åé¤ãè¡ãããã«ãã¦ãã¾ãã
ãããå®è¡ãã¦ã¿ã¾ãã
go run main.goÂ
DBæ¥ç¶
ã³ãã³ããå
¥å (insert/update/delete/select): delete
åé¤å¯¾è±¡ã®ã¬ã³ã¼ãã®IDãå
¥å: 1
åé¤ãã¾ããã
Â
PostgreSQLã«ãSelectå¥ãæãã¦ã¿ã¾ãã
select * from public.user where id = 1;
 id | name | ageÂ
----+------+-----
(0 rows)
Â
ç¡äºã«åé¤ã§ãã¦ãã¾ãã
Â
以ä¸ãµã³ãã«ã«ãªãã¾ãã
Â