main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "path/filepath"
  7. "github.com/vladimirok5959/golang-sql/gosql"
  8. )
  9. func main() {
  10. // Get temp file name
  11. f, err := ioutil.TempFile("", "go-sqlite-")
  12. if err != nil {
  13. panic(fmt.Sprintf("%s", err))
  14. }
  15. f.Close()
  16. // Set migration directory
  17. migrationsDir, err := filepath.Abs("./db/migrations")
  18. if err != nil {
  19. panic(fmt.Sprintf("%s", err))
  20. }
  21. // Open DB connection, SQLite is used as example
  22. // You can use here MySQL or PostgreSQL, just change dbURL
  23. db, err := gosql.Open("sqlite://"+f.Name(), migrationsDir, true)
  24. if err != nil {
  25. panic(fmt.Sprintf("%s", err))
  26. }
  27. // DB struct here ./db/migrations/20220527233113_test_migration.sql
  28. fmt.Println("Insert some data to users table")
  29. if _, err := db.Exec(
  30. context.Background(),
  31. "INSERT INTO users (id, name) VALUES ($1, $2)",
  32. 5, "John",
  33. ); err != nil {
  34. panic(fmt.Sprintf("%s", err))
  35. }
  36. fmt.Println("Select all rows from users table")
  37. if rows, err := db.Query(
  38. context.Background(),
  39. "SELECT id, name FROM users ORDER BY id ASC",
  40. ); err == nil {
  41. type rowStruct struct {
  42. ID int64
  43. Name string
  44. }
  45. defer rows.Close()
  46. for rows.Next() {
  47. var row rowStruct
  48. if err := rows.Scan(&row.ID, &row.Name); err != nil {
  49. panic(fmt.Sprintf("%s", err))
  50. }
  51. fmt.Printf("ID: %d, Name: %s\n", row.ID, row.Name)
  52. }
  53. if err := rows.Err(); err != nil {
  54. panic(fmt.Sprintf("%s", err))
  55. }
  56. } else {
  57. panic(fmt.Sprintf("%s", err))
  58. }
  59. fmt.Println("Update inside transaction")
  60. if err := db.Transaction(context.Background(), func(ctx context.Context, tx *gosql.Tx) error {
  61. if _, err := tx.Exec(ctx, "UPDATE users SET name=$1 WHERE id=$2", "John", 1); err != nil {
  62. return err
  63. }
  64. if _, err := tx.Exec(ctx, "UPDATE users SET name=$1 WHERE id=$2", "Alice", 5); err != nil {
  65. return err
  66. }
  67. return nil
  68. }); err != nil {
  69. panic(fmt.Sprintf("%s", err))
  70. }
  71. fmt.Println("Select all rows from users again")
  72. if rows, err := db.Query(
  73. context.Background(),
  74. "SELECT id, name FROM users ORDER BY id ASC",
  75. ); err == nil {
  76. type rowStruct struct {
  77. ID int64
  78. Name string
  79. }
  80. defer rows.Close()
  81. for rows.Next() {
  82. var row rowStruct
  83. if err := rows.Scan(&row.ID, &row.Name); err != nil {
  84. panic(fmt.Sprintf("%s", err))
  85. }
  86. fmt.Printf("ID: %d, Name: %s\n", row.ID, row.Name)
  87. }
  88. if err := rows.Err(); err != nil {
  89. panic(fmt.Sprintf("%s", err))
  90. }
  91. } else {
  92. panic(fmt.Sprintf("%s", err))
  93. }
  94. // Close DB connection
  95. if err := db.Close(); err != nil {
  96. panic(fmt.Sprintf("%s", err))
  97. }
  98. }