From 3de930987ef483f10e79b362cc1069ff41327ce2 Mon Sep 17 00:00:00 2001 From: Murali Medisetty Date: Wed, 15 Jun 2022 22:43:11 -0500 Subject: [PATCH] add batch write method --- client.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/client.go b/client.go index bae7add..5fe66ae 100644 --- a/client.go +++ b/client.go @@ -12,6 +12,7 @@ import ( "fmt" "math/big" "net" + "strings" _ "github.com/lib/pq" ) @@ -189,6 +190,35 @@ func (c *Client) Write(a interface{}, options ...option) error { return nil } +func (c *Client) WriteBatch(rows []interface{}, options ...option) error { + var models []*Model + for _, row := range rows { + m, err := NewModel(row) + if err != nil { + return err + } + if len(options) > 0 { + for _, opt := range options { + // check and set all options here + if opt.tableName != "" { + m.tableName = opt.tableName + } + } + } + models = append(models, m) + } + + var sb strings.Builder + for _, m := range models { + sb.Write(m.MarshalLine()) + } + _, err := c.ilpConn.Write([]byte(sb.String())) + if err != nil { + return err + } + return nil +} + // DB func returns the underlying *sql.DB struct for DB operations over the Postgres wire protocol func (c *Client) DB() *sql.DB { return c.pgSqlDB