You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// More complex statements can be made with references to other tables.
229
+
230
+
typeRolestruct {
231
+
IDint`db:"id"`
232
+
Namestring`db:"name"`
233
+
}
234
+
235
+
// Roles repository.
236
+
rr:= sqluct.Table[Role](st, "roles")
237
+
238
+
// To be able to resolve "roles" columns, we need to attach roles repo to users repo.
239
+
ur.AddTableAlias(rr.R, "roles")
240
+
241
+
fmt.Println("Get users with role 'admin'.")
242
+
users, _ = ur.List(ctx, ur.SelectStmt().
243
+
LeftJoin(ur.Fmt("%s ON %s = %s", rr.R, &rr.R.ID, &ur.R.RoleID)).
244
+
Where(ur.Fmt("%s = ?", &rr.R.Name), "admin"),
245
+
)
246
+
247
+
_ = user
248
+
_ = users
249
+
250
+
// Output:
251
+
// Insert single user.
252
+
// exec INSERT INTO "users" ("id","role_id","name") VALUES ($1,$2,$3) [123 0 John Doe]
253
+
// Insert two users.
254
+
// exec INSERT INTO "users" ("id","role_id","name") VALUES ($1,$2,$3),($4,$5,$6) [124 0 Jane Doe 125 0 Richard Roe]
255
+
// Update a user with new name.
256
+
// exec UPDATE "users" SET "id" = $1, "role_id" = $2, "name" = $3 WHERE "users"."id" = $4 [123 0 John Doe, Jr. 123]
257
+
// Delete a user with id 123.
258
+
// exec DELETE FROM "users" WHERE "users"."id" = $1 [123]
259
+
// Get single user with id = 123.
260
+
// query SELECT "users"."id", "users"."role_id", "users"."name" FROM "users" WHERE "users"."id" = $1 [123]
261
+
// Get multiple users with names starting with 'John '.
262
+
// query SELECT "users"."id", "users"."role_id", "users"."name" FROM "users" WHERE "users"."name" LIKE $1 [John %]
263
+
// Get multiple users with id != 123.
264
+
// query SELECT "users"."id", "users"."role_id", "users"."name" FROM "users" WHERE "users"."id" <> $1 [123]
265
+
// Get all users.
266
+
// query SELECT "users"."id", "users"."role_id", "users"."name" FROM "users" []
267
+
// Get users with role 'admin'.
268
+
// query SELECT "users"."id", "users"."role_id", "users"."name" FROM "users" LEFT JOIN "roles" ON "roles"."id" = "users"."role_id" WHERE "roles"."name" = $1 [admin]
269
+
```
270
+
165
271
## Omitting Zero Values
166
272
167
273
When building `WHERE` conditions from row structure it is often needed skip empty fields from condition.
// More complex statements can be made with references to other tables.
67
+
68
+
typeRolestruct {
69
+
IDint`db:"id"`
70
+
Namestring`db:"name"`
71
+
}
72
+
73
+
// Roles repository.
74
+
rr:=sqluct.Table[Role](st, "roles")
75
+
76
+
// To be able to resolve "roles" columns, we need to attach roles repo to users repo.
77
+
ur.AddTableAlias(rr.R, "roles")
78
+
79
+
fmt.Println("Get users with role 'admin'.")
80
+
users, _=ur.List(ctx, ur.SelectStmt().
81
+
LeftJoin(ur.Fmt("%s ON %s = %s", rr.R, &rr.R.ID, &ur.R.RoleID)).
82
+
Where(ur.Fmt("%s = ?", &rr.R.Name), "admin"),
83
+
)
84
+
85
+
_=user
86
+
_=users
87
+
88
+
// Output:
89
+
// Insert single user.
90
+
// exec INSERT INTO "users" ("id","role_id","name") VALUES ($1,$2,$3) [123 0 John Doe]
91
+
// Insert two users.
92
+
// exec INSERT INTO "users" ("id","role_id","name") VALUES ($1,$2,$3),($4,$5,$6) [124 0 Jane Doe 125 0 Richard Roe]
93
+
// Update a user with new name.
94
+
// exec UPDATE "users" SET "id" = $1, "role_id" = $2, "name" = $3 WHERE "users"."id" = $4 [123 0 John Doe, Jr. 123]
95
+
// Delete a user with id 123.
96
+
// exec DELETE FROM "users" WHERE "users"."id" = $1 [123]
97
+
// Get single user with id = 123.
98
+
// query SELECT "users"."id", "users"."role_id", "users"."name" FROM "users" WHERE "users"."id" = $1 [123]
99
+
// Get multiple users with names starting with 'John '.
100
+
// query SELECT "users"."id", "users"."role_id", "users"."name" FROM "users" WHERE "users"."name" LIKE $1 [John %]
101
+
// Get multiple users with id != 123.
102
+
// query SELECT "users"."id", "users"."role_id", "users"."name" FROM "users" WHERE "users"."id" <> $1 [123]
103
+
// Get all users.
104
+
// query SELECT "users"."id", "users"."role_id", "users"."name" FROM "users" []
105
+
// Get users with role 'admin'.
106
+
// query SELECT "users"."id", "users"."role_id", "users"."name" FROM "users" LEFT JOIN "roles" ON "roles"."id" = "users"."role_id" WHERE "roles"."name" = $1 [admin]
0 commit comments