Skip to content

Commit 591bcf2

Browse files
authored
Merge pull request #1 from ultd/batch-write
add batch write method
2 parents a730a44 + 3de9309 commit 591bcf2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

client.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"math/big"
1414
"net"
15+
"strings"
1516

1617
_ "github.com/lib/pq"
1718
)
@@ -189,6 +190,35 @@ func (c *Client) Write(a interface{}, options ...option) error {
189190
return nil
190191
}
191192

193+
func (c *Client) WriteBatch(rows []interface{}, options ...option) error {
194+
var models []*Model
195+
for _, row := range rows {
196+
m, err := NewModel(row)
197+
if err != nil {
198+
return err
199+
}
200+
if len(options) > 0 {
201+
for _, opt := range options {
202+
// check and set all options here
203+
if opt.tableName != "" {
204+
m.tableName = opt.tableName
205+
}
206+
}
207+
}
208+
models = append(models, m)
209+
}
210+
211+
var sb strings.Builder
212+
for _, m := range models {
213+
sb.Write(m.MarshalLine())
214+
}
215+
_, err := c.ilpConn.Write([]byte(sb.String()))
216+
if err != nil {
217+
return err
218+
}
219+
return nil
220+
}
221+
192222
// DB func returns the underlying *sql.DB struct for DB operations over the Postgres wire protocol
193223
func (c *Client) DB() *sql.DB {
194224
return c.pgSqlDB

0 commit comments

Comments
 (0)