Guest User

Untitled

a guest
Jan 24th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. select p.*,
  2. array(select url from image i where p.prod_id = i.prod_id) as images,
  3. (select v from prod_variation v where v.prod_id = p.prod_id limit 1) as variants
  4. from product p limit 1;
  5.  
  6. select p.*,
  7. array(select url from image i where p.prod_id = i.prod_id) as images,
  8. array(select v from prod_variation v where v.prod_id = p.prod_id) as variants
  9. from product p limit 1;
  10.  
  11. Images []string `sql:",array"`
  12. Variants ProdVariant `sql:"composite:prod_variation"`
  13.  
  14. Variants []ProdVariant `sql:",array,composite:prod_variation"`
  15.  
  16. CREATE TABLE product (
  17. prod_id integer primary key,
  18. name text,
  19. price real
  20. );
  21.  
  22. CREATE TABLE prod_variation (
  23. aid integer primary key,
  24. prod_id integer references product(prod_id),
  25. stock_count integer,
  26. color text
  27. );
  28.  
  29. CREATE TABLE image (
  30. prod_id integer references product(prod_id),
  31. url text,
  32. primary key (url,prod_id)
  33. );
  34.  
  35. CREATE INDEX on image (prod_id);
  36. CREATE INDEX on prod_variation (prod_id);
  37.  
  38. INSERT INTO product(prod_id, name, price) VALUES (1, 'Prod 1', 10.0);
  39. INSERT INTO image(prod_id, url) VALUES (1, 'http://url.com/p1-1.jpg');
  40. INSERT INTO image(prod_id, url) VALUES (1, 'http://url.com/p1-2.jpg');
  41. INSERT INTO prod_variation(aid,prod_id, stock_count, color) VALUES (11, 1, 5, 'red');
  42. INSERT INTO prod_variation(aid,prod_id, stock_count, color) VALUES (12, 1, 7, 'blue');
  43.  
  44. INSERT INTO product(prod_id,name, price) VALUES (2, 'Prod 2', 20.0);
  45. INSERT INTO image(prod_id, url) VALUES (2, 'http://url.com/p2-1.jpg');
  46. INSERT INTO image(prod_id, url) VALUES (2, 'http://url.com/p2-2.jpg');
  47. INSERT INTO prod_variation(aid,prod_id, stock_count, color) VALUES (21, 2, 10, 'black');
  48. INSERT INTO prod_variation(aid,prod_id, stock_count, color) VALUES (22, 2, 15, 'white');
  49.  
  50. package main
  51.  
  52. import (
  53. "log"
  54.  
  55. "github.com/go-pg/pg"
  56. _ "github.com/go-pg/pg/orm"
  57. "github.com/kylelemons/godebug/pretty"
  58. )
  59.  
  60. type Product struct {
  61. tableName struct{} `pg:",discard_unknown_columns"sql:"product"`
  62. ProdId uint64 `sql:",pk"`
  63. Name string
  64. Price float32 `sql:",notnull"`
  65. Images []string `sql:",array"`
  66. Variants ProdVariant `sql:"composite:prod_variation"`
  67. }
  68.  
  69. type ProdVariant struct {
  70. tableName struct{} `pg:",discard_unknown_columns"sql:"prod_variation"`
  71. AID string `sql:",pk"`
  72. ProdId string `pg:"fk:prod"`
  73. StockCount int `sql:",notnull"`
  74. Color string
  75. }
  76.  
  77. type ProductImage struct {
  78. tableName struct{} `pg:",discard_unknown_columns"sql:"image"`
  79. ProdId uint64 `pg:"fk:prod"`
  80. URL string
  81. }
  82.  
  83. func selectProducts(tx *pg.Tx) error {
  84. queryFormat := `select p.*,
  85. array(select url from image i where p.prod_id = i.prod_id) as images,
  86. (select v from prod_variation v where v.prod_id = p.prod_id limit 1) as variants
  87. from product p limit 1;`
  88. Res := []Product{}
  89. if _, err := tx.Query(&Res, queryFormat); err != nil {
  90. return err
  91. }
  92. log.Printf("Select result: %s", pretty.Sprint(&Res))
  93. return nil
  94. }
  95.  
  96. func main() {
  97. opt := pg.Options{Addr: "127.0.0.1:5432", User: "tester", Password: "test12345", Database: "test"}
  98. conn := pg.Connect(&opt)
  99.  
  100. if err := conn.RunInTransaction(selectProducts); err != nil {
  101. log.Fatalf("Request failed [product]: %s", err)
  102. }
  103. }
  104.  
  105. 2019/01/25 03:15:13 Select result: [{ProdId: 1,
  106. Name: "Prod 1",
  107. Price: 10,
  108. Images: ["http://url.com/p1-1.jpg",
  109. "http://url.com/p1-2.jpg"],
  110. Variants: {AID: "11",
  111. ProdId: "1",
  112. StockCount: 5,
  113. Color: "red"}}]
Add Comment
Please, Sign In to add comment