main.go 2.9 KB

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