Skip to content

Commit 293c6da

Browse files
committed
Clarify examples in documentation
1 parent 1efcf45 commit 293c6da

File tree

4 files changed

+90
-39
lines changed

4 files changed

+90
-39
lines changed

docs/inserting.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,23 @@ type item struct {
185185
Address2 *string `db:"address2" goqu:"omitnil"`
186186
Address3 *string `db:"address3" goqu:"omitnil"`
187187
}
188-
testString := "Test"
189-
emptyString := ""
190-
i := item{FirstName: "Test", Address1: &testString, Address2: &emptyString}
188+
address1 := "111 Test Addr"
189+
var emptyString string
190+
i := item{
191+
FirstName: "Test First Name",
192+
LastName: "",
193+
Address1: &address1,
194+
Address2: &emptyString,
195+
Address3: nil, // will omit nil pointer
196+
}
191197

192198
insertSQL, args, _ := goqu.Insert("items").Rows(i).ToSQL()
193199
fmt.Println(insertSQL, args)
194200
```
195201

196202
Output:
197203
```
198-
INSERT INTO "items" ("address1", "address2", "first_name", "last_name") VALUES ('Test', '', 'Test', '') []
204+
INSERT INTO "items" ("address1", "address2", "first_name", "last_name") VALUES ('111 Test Addr', '', 'Test First Name', '') []
199205
```
200206

201207
If you do not want to set the database field when the struct field is a zero value (including nil pointers) you can use
@@ -211,17 +217,22 @@ type item struct {
211217
Address2 *string `db:"address2" goqu:"omitempty"`
212218
Address3 *string `db:"address3" goqu:"omitempty"`
213219
}
214-
testString := "Test"
215-
emptyString := ""
216-
i := item{FirstName: "Test", Address1: &testString, Address2: &emptyString}
217-
220+
address1 := "112 Test Addr"
221+
var emptyString string
222+
i := item{
223+
FirstName: "Test First Name",
224+
LastName: "", // will omit zero field
225+
Address1: &address1,
226+
Address2: &emptyString,
227+
Address3: nil, // will omit nil pointer
228+
}
218229
insertSQL, args, _ := goqu.Insert("items").Rows(i).ToSQL()
219230
fmt.Println(insertSQL, args)
220231
```
221232

222233
Output:
223234
```
224-
INSERT INTO "items" ("address1", "address2", "first_name") VALUES ('Test', '', 'Test') []
235+
INSERT INTO "items" ("address1", "address2", "first_name") VALUES ('112 Test Addr', '', 'Test First Name') []
225236
```
226237

227238
If you want to use the database `DEFAULT` when the struct field is a zero value you can use the `defaultifempty` tag.

docs/updating.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,23 @@ type item struct {
165165
Address2 *string `db:"address2" goqu:"omitnil"`
166166
Address3 *string `db:"address3" goqu:"omitnil"`
167167
}
168-
testString := "Test"
169-
emptyString := ""
170-
query, args, _ := goqu.Update("items").Set(
171-
item{FirstName: "Test", Address1: &testString, Address2: &emptyString},
168+
address1 := "113 Test Addr"
169+
var emptyString string
170+
sql, args, _ := goqu.Update("items").Set(
171+
item{
172+
FirstName: "Test First Name",
173+
LastName: "",
174+
Address1: &address1,
175+
Address2: &emptyString,
176+
Address3: nil, // will omit nil pointer
177+
},
172178
).ToSQL()
173-
fmt.Println(query, args)
179+
fmt.Println(sql, args)
174180
```
175181

176182
Output:
177183
```
178-
UPDATE "items" SET "address1"='Test',"address2"='',"first_name"='Test',"last_name"='' []
184+
UPDATE "items" SET "address1"='113 Test Addr',"address2"='',"first_name"='Test First Name',"last_name"='' []
179185
```
180186

181187
If you do not want to update the database field when the struct field is a zero value (including nil pointers) you can
@@ -191,17 +197,23 @@ type item struct {
191197
Address2 *string `db:"address2" goqu:"omitempty"`
192198
Address3 *string `db:"address3" goqu:"omitempty"`
193199
}
194-
testString := "Test"
195-
emptyString := ""
196-
query, args, _ := goqu.Update("items").Set(
197-
item{FirstName: "Test", Address1: &testString, Address2: &emptyString},
200+
address1 := "114 Test Addr"
201+
var emptyString string
202+
sql, args, _ := goqu.Update("items").Set(
203+
item{
204+
FirstName: "Test First Name",
205+
LastName: "", // will omit zero field
206+
Address1: &address1,
207+
Address2: &emptyString,
208+
Address3: nil, // will omit nil pointer
209+
},
198210
).ToSQL()
199-
fmt.Println(query, args)
211+
fmt.Println(sql, args)
200212
```
201213

202214
Output:
203215
```
204-
UPDATE "items" SET "address1"='Test',"address2"='',"first_name"='Test' []
216+
UPDATE "items" SET "address1"='114 Test Addr',"address2"='',"first_name"='Test First Name' []
205217
```
206218

207219
If you want to use the database `DEFAULT` when the struct field is a zero value you can use the `defaultifempty` tag.

insert_dataset_example_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,21 @@ func ExampleInsertDataset_Rows_withOmitNilTag() {
385385
Address2 *string `db:"address2" goqu:"omitnil"`
386386
Address3 *string `db:"address3" goqu:"omitnil"`
387387
}
388-
address := "111 Test Addr"
388+
address1 := "111 Test Addr"
389389
var emptyString string
390-
i := item{FirstName: "Test", Address1: &address, Address2: &emptyString}
390+
i := item{
391+
FirstName: "Test First Name",
392+
LastName: "",
393+
Address1: &address1,
394+
Address2: &emptyString,
395+
Address3: nil, // will omit nil pointer
396+
}
391397

392398
insertSQL, args, _ := goqu.Insert("items").Rows(i).ToSQL()
393399
fmt.Println(insertSQL, args)
394400

395401
// Output:
396-
// INSERT INTO "items" ("address1", "address2", "first_name", "last_name") VALUES ('111 Test Addr', '', 'Test', '') []
402+
// INSERT INTO "items" ("address1", "address2", "first_name", "last_name") VALUES ('111 Test Addr', '', 'Test First Name', '') []
397403
}
398404

399405
func ExampleInsertDataset_Rows_withOmitEmptyTag() {
@@ -404,14 +410,20 @@ func ExampleInsertDataset_Rows_withOmitEmptyTag() {
404410
Address2 *string `db:"address2" goqu:"omitempty"`
405411
Address3 *string `db:"address3" goqu:"omitempty"`
406412
}
407-
address := "112 Test Addr"
413+
address1 := "112 Test Addr"
408414
var emptyString string
409-
i := item{FirstName: "Test", Address1: &address, Address2: &emptyString}
415+
i := item{
416+
FirstName: "Test First Name",
417+
LastName: "", // will omit zero field
418+
Address1: &address1,
419+
Address2: &emptyString,
420+
Address3: nil, // will omit nil pointer
421+
}
410422
insertSQL, args, _ := goqu.Insert("items").Rows(i).ToSQL()
411423
fmt.Println(insertSQL, args)
412424

413425
// Output:
414-
// INSERT INTO "items" ("address1", "address2", "first_name") VALUES ('112 Test Addr', '', 'Test') []
426+
// INSERT INTO "items" ("address1", "address2", "first_name") VALUES ('112 Test Addr', '', 'Test First Name') []
415427
}
416428

417429
func ExampleInsertDataset_Rows_withOmitEmptyTag_Valuer() {
@@ -425,17 +437,19 @@ func ExampleInsertDataset_Rows_withOmitEmptyTag_Valuer() {
425437
Address4 *sql.NullString `db:"address4" goqu:"omitempty"`
426438
}
427439
i := item{
428-
FirstName: sql.NullString{Valid: true, String: "Test"},
440+
FirstName: sql.NullString{Valid: true, String: "Test First Name"},
429441
MiddleName: sql.NullString{Valid: true, String: ""},
430-
Address1: &sql.NullString{Valid: true, String: "Test"},
442+
LastName: sql.NullString{}, // will omit zero valuer struct
443+
Address1: &sql.NullString{Valid: true, String: "Test Address 1"},
431444
Address2: &sql.NullString{Valid: true, String: ""},
432445
Address3: &sql.NullString{},
446+
Address4: nil, // will omit nil pointer
433447
}
434448
insertSQL, args, _ := goqu.Insert("items").Rows(i).ToSQL()
435449
fmt.Println(insertSQL, args)
436450

437451
// Output:
438-
// INSERT INTO "items" ("address1", "address2", "address3", "first_name", "middle_name") VALUES ('Test', '', NULL, 'Test', '') []
452+
// INSERT INTO "items" ("address1", "address2", "address3", "first_name", "middle_name") VALUES ('Test Address 1', '', NULL, 'Test First Name', '') []
439453
}
440454

441455
func ExampleInsertDataset_Rows_withGoquDefaultIfEmptyTag() {

update_dataset_example_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,21 @@ func ExampleUpdate_withOmitNilTag() {
3131
Address2 *string `db:"address2" goqu:"omitnil"`
3232
Address3 *string `db:"address3" goqu:"omitnil"`
3333
}
34-
address := "113 Test Addr"
34+
address1 := "113 Test Addr"
3535
var emptyString string
3636
sql, args, _ := goqu.Update("items").Set(
37-
item{FirstName: "Test", Address1: &address, Address2: &emptyString},
37+
item{
38+
FirstName: "Test First Name",
39+
LastName: "",
40+
Address1: &address1,
41+
Address2: &emptyString,
42+
Address3: nil, // will omit nil pointer
43+
},
3844
).ToSQL()
3945
fmt.Println(sql, args)
4046

4147
// Output:
42-
// UPDATE "items" SET "address1"='113 Test Addr',"address2"='',"first_name"='Test',"last_name"='' []
48+
// UPDATE "items" SET "address1"='113 Test Addr',"address2"='',"first_name"='Test First Name',"last_name"='' []
4349
}
4450

4551
func ExampleUpdate_withOmitEmptyTag() {
@@ -50,15 +56,21 @@ func ExampleUpdate_withOmitEmptyTag() {
5056
Address2 *string `db:"address2" goqu:"omitempty"`
5157
Address3 *string `db:"address3" goqu:"omitempty"`
5258
}
53-
address := "114 Test Addr"
59+
address1 := "114 Test Addr"
5460
var emptyString string
5561
sql, args, _ := goqu.Update("items").Set(
56-
item{FirstName: "Test", Address1: &address, Address2: &emptyString},
62+
item{
63+
FirstName: "Test First Name",
64+
LastName: "", // will omit zero field
65+
Address1: &address1,
66+
Address2: &emptyString,
67+
Address3: nil, // will omit nil pointer
68+
},
5769
).ToSQL()
5870
fmt.Println(sql, args)
5971

6072
// Output:
61-
// UPDATE "items" SET "address1"='114 Test Addr',"address2"='',"first_name"='Test' []
73+
// UPDATE "items" SET "address1"='114 Test Addr',"address2"='',"first_name"='Test First Name' []
6274
}
6375

6476
func ExampleUpdate_withOmitEmptyTag_valuer() {
@@ -73,17 +85,19 @@ func ExampleUpdate_withOmitEmptyTag_valuer() {
7385
}
7486
query, args, _ := goqu.Update("items").Set(
7587
item{
76-
FirstName: dbsql.NullString{Valid: true, String: "Test"},
88+
FirstName: dbsql.NullString{Valid: true, String: "Test First Name"},
7789
MiddleName: dbsql.NullString{Valid: true, String: ""},
78-
Address1: &dbsql.NullString{Valid: true, String: "Test"},
90+
LastName: dbsql.NullString{}, // will omit zero valuer struct
91+
Address1: &dbsql.NullString{Valid: true, String: "Test Address 1"},
7992
Address2: &dbsql.NullString{Valid: true, String: ""},
8093
Address3: &dbsql.NullString{},
94+
Address4: nil, // will omit nil pointer
8195
},
8296
).ToSQL()
8397
fmt.Println(query, args)
8498

8599
// Output:
86-
// UPDATE "items" SET "address1"='Test',"address2"='',"address3"=NULL,"first_name"='Test',"middle_name"='' []
100+
// UPDATE "items" SET "address1"='Test Address 1',"address2"='',"address3"=NULL,"first_name"='Test First Name',"middle_name"='' []
87101
}
88102

89103
func ExampleUpdate_withGoquRecord() {

0 commit comments

Comments
 (0)