Guest User

Untitled

a guest
Feb 25th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8.  
  9. _ "github.com/lib/pq"
  10. )
  11.  
  12. // Person is an employee
  13. type Person struct {
  14. Name string
  15. Children []*Child
  16. Job
  17. }
  18.  
  19. // Child is a child of an employee
  20. type Child struct {
  21. Name string
  22. // other fields
  23. }
  24.  
  25. // Job is the employment of a person
  26. type Job struct {
  27. Position string
  28. // other fields
  29. }
  30.  
  31. func main() {
  32. db, err := sql.Open("postgres",
  33. fmt.Sprintf(
  34. "user=%s password=%s host=%s database=%s sslmode=require",
  35. os.Getenv("user"), os.Getenv("pword"), os.Getenv("h"), os.Getenv("db"),
  36. ),
  37. )
  38. if err != nil {
  39. panic(err)
  40. }
  41.  
  42. defer db.Close()
  43. db.SetMaxIdleConns(0)
  44.  
  45. // create table
  46. if _, err = db.Exec("DROP table mytable"); err != nil {
  47. fmt.Printf("cannot delete table %v", err)
  48. }
  49.  
  50. if _, err = db.Exec("CREATE TABLE mytable (name text, children jsonb, job jsonb)"); err != nil {
  51. fmt.Printf("cannot create table %v", err)
  52. }
  53.  
  54. // insert some rows
  55. for _, person := range []Person{
  56. Person{"Bob", []*Child{&Child{"Fred"}, &Child{"Mary"}}, Job{"welder"}},
  57. Person{"Jane", []*Child{&Child{"Ben"}, &Child{"Emily"}}, Job{"machinist"}},
  58. } {
  59. c, e := json.Marshal(person.Children)
  60. if e != nil {
  61. fmt.Printf("cannot marshal children %v", err)
  62. }
  63.  
  64. j, e := json.Marshal(person.Job)
  65. if e != nil {
  66. fmt.Printf("cannot marshal job %v", err)
  67. }
  68.  
  69. if _, err = db.Exec("INSERT INTO mytable (name, children, job) VALUES ($1,$2,$3)", person.Name, string(c), string(j)); err != nil {
  70. fmt.Printf("cannot insert value %v", err)
  71. }
  72. }
  73.  
  74. //selectJob(db)
  75. selectChildrenAndJob(db)
  76.  
  77. }
  78.  
  79. func selectJob(db *sql.DB) {
  80. p := &Person{}
  81.  
  82. err := db.QueryRow("SELECT job FROM mytable LIMIT 1").Scan(&p.Job)
  83. switch {
  84. case err == sql.ErrNoRows:
  85. fmt.Println("No rows.")
  86. case err != nil:
  87. fmt.Println("cannot retrieve rows", err)
  88. default:
  89. fmt.Printf("job %vn", p.Job)
  90. }
  91. }
  92.  
  93. func selectChildrenAndJob(db *sql.DB) {
  94. p := &Person{}
  95.  
  96. err := db.QueryRow("SELECT children, job FROM mytable LIMIT 1").Scan(&p.Children, &p.Job)
  97. switch {
  98. case err == sql.ErrNoRows:
  99. fmt.Println("No rows.")
  100. case err != nil:
  101. fmt.Println("cannot retrieve rows", err)
  102. default:
  103. fmt.Printf("children %v; job %vn", p.Children, p.Job)
  104. }
  105. }
  106.  
  107. // Scan scans for Child
  108. func (c *Child) Scan(value interface{}) error {
  109. return json.Unmarshal(value.([]byte), c)
  110. }
  111.  
  112. // Scan scans for Job
  113. func (j *Job) Scan(value interface{}) error {
  114. return json.Unmarshal(value.([]byte), j)
  115. }
  116.  
  117. Scan error on column index 0: unsupported Scan, storing driver.Value type []uint8 into type *[]*main.Child
Add Comment
Please, Sign In to add comment