Skip to content

Commit 0ecb6ab

Browse files
zachvictorZach Victor
and
Zach Victor
authored
Examples for Insert.SQL and Insert.Args (#11)
Co-authored-by: Zach Victor <zach@zvictor.com>
1 parent 5f9300a commit 0ecb6ab

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

README.md

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,47 @@ ins := sqlinsert.Insert{`candy`, &rec}
6565
_, err := ins.Insert(db)
6666
```
6767

68-
## This is not an ORM
68+
### I want to see the SQL
69+
Question-mark (?) VALUES-tokens are the default:
70+
```go
71+
fmt.Println(ins.SQL())
72+
// INSERT INTO candy (id,candy_name,form_factor,description,manufacturer,weight_grams,ts) VALUES (?,?,?,?,?,?,?)
73+
```
74+
75+
You can change the token type. For example, for PostgreSQL:
76+
```go
77+
sqlinsert.UseTokenType = OrdinalNumberTokenType
78+
fmt.Println(ins.SQL())
79+
// INSERT INTO candy (id,candy_name,form_factor,description,manufacturer,weight_grams,ts) VALUES ($1,$2,$3,$4,$5,$6,$7)
80+
```
81+
82+
### I want to see the args
83+
84+
```go
85+
pretty.Println(ins.Args())
86+
```
87+
yields (using [github.com/kr/pretty](https://github.com/kr/pretty)):
88+
```
89+
[]interface {}{
90+
"c0600afd-78a7-4a1a-87c5-1bc48cafd14e",
91+
"Gougat",
92+
"Package",
93+
"tastes like gopher feed",
94+
"Gouggle",
95+
float64(1.1618),
96+
time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC),
97+
}
98+
```
99+
100+
### I want to use database/sql apparatus
101+
```go
102+
stmt, _ := db.Prepare(ins.SQL())
103+
result, _ := stmt.Exec(ins.Args()...)
104+
```
105+
106+
107+
## This is a helper
69108

70-
### Hide nothing
71-
Unlike ORMs, `sqlinsert` does **not** create an abstraction layer over SQL relations, nor does it restructure SQL
72-
functions.
73-
The aim is to keep it simple and hide nothing.
74109
`sqlinsert` is fundamentally a helper for [database/sql](https://pkg.go.dev/database/sql).
75110
It simply maps struct fields to INSERT elements:
76111
* struct tags
@@ -88,7 +123,12 @@ All aspects of SQL INSERT remain in your control:
88123
* _I just want the bind args for my Exec() call._ `Insert.Args()`
89124
* _I just want a Prepare-Exec wrapper._ `Insert.Insert()`
90125

91-
## This is a helper
126+
## This is not an ORM
127+
128+
### Hide nothing
129+
Unlike ORMs, `sqlinsert` does **not** create an abstraction layer over SQL relations, nor does it restructure SQL
130+
functions.
131+
The aim is to keep it simple and hide nothing.
92132

93133
### Let SQL be great
94134
SQL’s INSERT is already as close to functionally pure as possible. Why would we change that? Its simplicity and

0 commit comments

Comments
 (0)