-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdml_insert_struct_test.go
More file actions
72 lines (60 loc) · 2.12 KB
/
dml_insert_struct_test.go
File metadata and controls
72 lines (60 loc) · 2.12 KB
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
// Copyright 2025 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlx
import (
"fmt"
"time"
)
type InsertStruct struct {
Base2
DefaultField string
ModifiedField string `sql:"field"`
ZeroField string `sql:",omitempty"`
IgnoredField string `sql:"-"`
Valuer MyTime `sql:"time,omitempty"`
}
func ExampleInsertBuilder_Struct() {
_time := time.Date(2025, 1, 2, 3, 4, 5, 0, time.Local)
s1 := InsertStruct{DefaultField: "v1", IgnoredField: "v2", Valuer: NewMyTime(_time)}
insert1 := Insert().Into("table").Struct(s1)
sql1, args1 := insert1.Build()
s2 := InsertStruct{Base2: Base2{Id: 123}, DefaultField: "v1", ModifiedField: "v2", ZeroField: "v3", IgnoredField: "v4"}
insert2 := Insert().Into("table").Struct(s2)
sql2, args2 := insert2.Build()
fmt.Println(sql1)
fmt.Println(args1.Args())
fmt.Println(sql2)
fmt.Println(args2.Args())
// Output:
// INSERT INTO `table` (`DefaultField`, `field`, `time`) VALUES (?, ?, ?)
// [v1 2025-01-02/03:04:05]
// INSERT INTO `table` (`id`, `DefaultField`, `field`, `ZeroField`) VALUES (?, ?, ?, ?)
// [123 v1 v2 v3]
}
func ExampleInsertBuilder_ValuesFromStructs() {
_time := time.Date(2025, 1, 2, 3, 4, 5, 0, time.Local)
sql1, args1 := Insert().
Into("table").
Columns("field", "time", "ZeroField").
ValuesFromStructs([]InsertStruct{
{DefaultField: "v1", IgnoredField: "v2", Valuer: NewMyTime(_time)},
{ModifiedField: "v3", ZeroField: "v4"},
}).
Build()
fmt.Println(sql1)
fmt.Println(args1.Args())
// Output:
// INSERT INTO `table` (`field`, `time`, `ZeroField`) VALUES (?, ?, ?), (?, ?, ?)
// [ 2025-01-02/03:04:05 v3 0001-01-01/00:00:00 v4]
}