Skip to content

Commit 2259044

Browse files
committed
added InsertStructs
1 parent db17851 commit 2259044

File tree

8 files changed

+47
-2
lines changed

8 files changed

+47
-2
lines changed

connection.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,14 @@ type Connection interface {
7676
// Optional ColumnFilter can be passed to ignore mapped columns.
7777
InsertStruct(table string, rowStruct any, ignoreColumns ...ColumnFilter) error
7878

79+
// InsertStructs inserts a slice or array of structs
80+
// as new rows into table using the connection's
81+
// StructFieldMapper to map struct fields to column names.
82+
// Optional ColumnFilter can be passed to ignore mapped columns.
83+
//
7984
// TODO optimized version with single query if possible
8085
// split into multiple queries depending or maxArgs for query
81-
// InsertStructs(table string, rowStructs any, ignoreColumns ...ColumnFilter) error
86+
InsertStructs(table string, rowStructs any, ignoreColumns ...ColumnFilter) error
8287

8388
// InsertUniqueStruct inserts a new row into table using the connection's
8489
// StructFieldMapper to map struct fields to column names.

errors.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func IsOtherThanErrNoRows(err error) bool {
3333
// and is meant to be used to declare const sentinel errors.
3434
//
3535
// Example:
36-
// const ErrUserNotFound impl.sentinelError = "user not found"
36+
//
37+
// const ErrUserNotFound impl.sentinelError = "user not found"
3738
type sentinelError string
3839

3940
func (s sentinelError) Error() string {
@@ -128,6 +129,10 @@ func (e connectionWithError) InsertStruct(table string, rowStruct any, ignoreCol
128129
return e.err
129130
}
130131

132+
func (e connectionWithError) InsertStructs(table string, rowStructs any, ignoreColumns ...ColumnFilter) error {
133+
return e.err
134+
}
135+
131136
func (e connectionWithError) InsertUniqueStruct(table string, rowStruct any, onConflict string, ignoreColumns ...ColumnFilter) (inserted bool, err error) {
132137
return false, e.err
133138
}

impl/connection.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ func (conn *connection) InsertStruct(table string, rowStruct any, ignoreColumns
106106
return InsertStruct(conn, table, rowStruct, conn.structFieldNamer, conn.argFmt, ignoreColumns)
107107
}
108108

109+
func (conn *connection) InsertStructs(table string, rowStructs any, ignoreColumns ...sqldb.ColumnFilter) error {
110+
return InsertStructs(conn, table, rowStructs, ignoreColumns...)
111+
}
112+
109113
func (conn *connection) InsertUniqueStruct(table string, rowStruct any, onConflict string, ignoreColumns ...sqldb.ColumnFilter) (inserted bool, err error) {
110114
return InsertUniqueStruct(conn, table, rowStruct, onConflict, conn.structFieldNamer, conn.argFmt, ignoreColumns)
111115
}

impl/insert.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,18 @@ func insertStructValues(table string, rowStruct any, namer sqldb.StructFieldMapp
142142
columns, _, vals = ReflectStructValues(v, namer, append(ignoreColumns, sqldb.IgnoreReadOnly))
143143
return columns, vals, nil
144144
}
145+
146+
func InsertStructs(conn sqldb.Connection, table string, rowStructs any, ignoreColumns ...sqldb.ColumnFilter) error {
147+
v := reflect.ValueOf(rowStructs)
148+
if k := v.Type().Kind(); k != reflect.Slice && k != reflect.Array {
149+
return fmt.Errorf("InsertStructs needs slice or array, got %T", rowStructs)
150+
}
151+
l := v.Len()
152+
for i := 0; i < l; i++ {
153+
err := conn.InsertStruct(table, v.Index(i).Interface(), ignoreColumns...)
154+
if err != nil {
155+
return err
156+
}
157+
}
158+
return nil
159+
}

impl/transaction.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ func (conn *transaction) InsertStruct(table string, rowStruct any, ignoreColumns
8686
return InsertStruct(conn, table, rowStruct, conn.structFieldNamer, conn.parent.argFmt, ignoreColumns)
8787
}
8888

89+
func (conn *transaction) InsertStructs(table string, rowStructs any, ignoreColumns ...sqldb.ColumnFilter) error {
90+
return InsertStructs(conn, table, rowStructs, ignoreColumns...)
91+
}
92+
8993
func (conn *transaction) InsertUniqueStruct(table string, rowStruct any, onConflict string, ignoreColumns ...sqldb.ColumnFilter) (inserted bool, err error) {
9094
return InsertUniqueStruct(conn, table, rowStruct, onConflict, conn.structFieldNamer, conn.parent.argFmt, ignoreColumns)
9195
}

mockconn/connection.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ func (conn *connection) InsertStruct(table string, rowStruct any, ignoreColumns
107107
return impl.InsertStruct(conn, table, rowStruct, conn.structFieldNamer, conn.argFmt, ignoreColumns)
108108
}
109109

110+
func (conn *connection) InsertStructs(table string, rowStructs any, ignoreColumns ...sqldb.ColumnFilter) error {
111+
return impl.InsertStructs(conn, table, rowStructs, ignoreColumns...)
112+
}
113+
110114
func (conn *connection) InsertUniqueStruct(table string, rowStruct any, onConflict string, ignoreColumns ...sqldb.ColumnFilter) (inserted bool, err error) {
111115
return impl.InsertUniqueStruct(conn, table, rowStruct, onConflict, conn.structFieldNamer, conn.argFmt, ignoreColumns)
112116
}

pqconn/connection.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ func (conn *connection) InsertStruct(table string, rowStruct any, ignoreColumns
127127
return impl.InsertStruct(conn, table, rowStruct, conn.structFieldNamer, argFmt, ignoreColumns)
128128
}
129129

130+
func (conn *connection) InsertStructs(table string, rowStructs any, ignoreColumns ...sqldb.ColumnFilter) error {
131+
return impl.InsertStructs(conn, table, rowStructs, ignoreColumns...)
132+
}
133+
130134
func (conn *connection) InsertUniqueStruct(table string, rowStruct any, onConflict string, ignoreColumns ...sqldb.ColumnFilter) (inserted bool, err error) {
131135
return impl.InsertUniqueStruct(conn, table, rowStruct, onConflict, conn.structFieldNamer, argFmt, ignoreColumns)
132136
}

pqconn/transaction.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func (conn *transaction) UpsertStruct(table string, rowStruct any, ignoreColumns
110110
return impl.UpsertStruct(conn, table, rowStruct, conn.structFieldNamer, argFmt, ignoreColumns)
111111
}
112112

113+
func (conn *transaction) InsertStructs(table string, rowStructs any, ignoreColumns ...sqldb.ColumnFilter) error {
114+
return impl.InsertStructs(conn, table, rowStructs, ignoreColumns...)
115+
}
116+
113117
func (conn *transaction) QueryRow(query string, args ...any) sqldb.RowScanner {
114118
rows, err := conn.tx.QueryContext(conn.parent.ctx, query, args...)
115119
if err != nil {

0 commit comments

Comments
 (0)