-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
124 lines (100 loc) · 3.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"context"
"database/sql"
"fmt"
"log"
dbmodels "github.com/gurleensethi/go-sql-boiler-example/db/models"
_ "github.com/lib/pq"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
func main() {
ctx := context.Background()
db := connectDB()
boil.SetDB(db)
author := createAuthor(ctx)
createArticle(ctx, author)
createArticle(ctx, author)
selectAuthorWithArticleJoin(ctx, author.ID)
}
func connectDB() *sql.DB {
db, err := sql.Open("postgres", "postgres://postgres:postgres@localhost:2345/postgres?sslmode=disable")
if err != nil {
log.Fatal(err)
}
return db
}
func createAuthor(ctx context.Context) dbmodels.Author {
author := dbmodels.Author{
Name: "John Doe",
Email: "johndoe@email.com",
}
err := author.InsertG(ctx, boil.Infer())
if err != nil {
log.Fatal(err)
}
return author
}
func createArticle(ctx context.Context, author dbmodels.Author) dbmodels.Article {
article := dbmodels.Article{
Title: "Hello World",
Body: null.StringFrom("Hello world, this is an article."),
AuthorID: author.ID,
}
err := article.InsertG(ctx, boil.Infer())
if err != nil {
log.Fatal(err)
}
return article
}
func selectAuthorWithArticle(ctx context.Context, authorID int) {
author, err := dbmodels.Authors(dbmodels.AuthorWhere.ID.EQ(authorID)).OneG(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Author: \n\tID:%d \n\tName:%s \n\tEmail:%s\n", author.ID, author.Name, author.Email)
articles, err := author.Articles().AllG(ctx)
if err != nil {
log.Fatal(err)
}
for _, a := range articles {
fmt.Printf("Article: \n\tID:%d \n\tTitle:%s \n\tBody:%s \n\tCreatedAt:%v\n", a.ID, a.Title, a.Body.String, a.CreatedAt.Time)
}
}
func selectAuthorWithArticleEager(ctx context.Context, authorID int) {
author, err := dbmodels.Authors(
dbmodels.AuthorWhere.ID.EQ(authorID),
qm.Load(dbmodels.AuthorRels.Articles),
).OneG(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Author: \n\tID:%d \n\tName:%s \n\tEmail:%s\n", author.ID, author.Name, author.Email)
for _, a := range author.R.Articles {
fmt.Printf("Article: \n\tID:%d \n\tTitle:%s \n\tBody:%s \n\tCreatedAt:%v\n", a.ID, a.Title, a.Body.String, a.CreatedAt.Time)
}
}
func selectAuthorWithArticleJoin(ctx context.Context, authorID int) {
type AuthorAndArticle struct {
Article dbmodels.Article `boil:"articles,bind"`
Author dbmodels.Author `boil:"author,bind"`
}
authorAndArticles := make([]AuthorAndArticle, 0)
err := dbmodels.NewQuery(
qm.Select("*"),
qm.From(dbmodels.TableNames.Author),
qm.InnerJoin("article on article.author_id = author.id"),
dbmodels.AuthorWhere.ID.EQ(authorID),
).BindG(ctx, &authorAndArticles)
if err != nil {
log.Fatal(err)
}
for _, authorAndArticle := range authorAndArticles {
author := authorAndArticle.Author
a := authorAndArticle.Article
fmt.Printf("Author: \n\tID:%d \n\tName:%s \n\tEmail:%s\n", author.ID, author.Name, author.Email)
fmt.Printf("Article: \n\tID:%d \n\tTitle:%s \n\tBody:%s \n\tCreatedAt:%v\n", a.ID, a.Title, a.Body.String, a.CreatedAt.Time)
}
}