From 7b75dde2664436555b53f6f15be93dacd8aef59c Mon Sep 17 00:00:00 2001 From: Bill Tanthowi Jauhari Date: Mon, 24 Aug 2020 19:44:34 +0700 Subject: [PATCH] extract query builder for bulk insert --- main.go | 14 +++++++-- models/user_model.go | 8 ++--- utils/agregator.go | 32 +++++++++++++++++++ utils/collector.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ utils/helper.go | 48 ---------------------------- 5 files changed, 122 insertions(+), 54 deletions(-) create mode 100644 utils/agregator.go create mode 100644 utils/collector.go delete mode 100644 utils/helper.go diff --git a/main.go b/main.go index 188fd5d..5266b55 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,22 @@ package main import ( + "fmt" "go-bulk-insert/models" "go-bulk-insert/utils" ) func main() { var user models.UserModel - var exceptField = []string{"fullname"} - utils.ExtractModelField(user, exceptField) + bulk := new(utils.BulkInsertStruct) + + value := [][]interface{}{ + {"bill", 12, "23-10-1994", "082245088948"}, + {"bill", 12, "23-10-1994", "082245088948"}, + {"bill", 12, "23-10-1994", "082245088948"}, + } + fmt.Println(bulk.ArrangeValue(value). + ExtractModelField(user, []string{}). + PutQueryTogether("users"). + GetQuery()) } diff --git a/models/user_model.go b/models/user_model.go index 2f99097..825ed1f 100644 --- a/models/user_model.go +++ b/models/user_model.go @@ -1,8 +1,8 @@ package models type UserModel struct { - FullName string `json:"fullname"` - Age uint16 `json:"age"` - BirthDate string `json:"birthdate"` - MSISDN string `json:"msisdn"` + FullName string `json:"fullname" gorm:"column:fullname"` + Age uint16 `json:"age" gorm:"column:age"` + BirthDate string `json:"birthdate" gorm:"column:birthdate"` + MSISDN string `json:"msisdn" gorm:"column:msisdn"` } diff --git a/utils/agregator.go b/utils/agregator.go new file mode 100644 index 0000000..30e550d --- /dev/null +++ b/utils/agregator.go @@ -0,0 +1,32 @@ +package utils + +func (s *BulkInsertStruct) Test() (i *BulkInsertStruct) { + return +} + +func (t *BulkInsertStruct) SetColumn(columns string) (s *BulkInsertStruct) { + t.Columns = &columns + return t +} + +func (t *BulkInsertStruct) SetValues(values string) (s *BulkInsertStruct) { + t.Values = &values + return t +} + +func (t *BulkInsertStruct) SetQuery(query string) (s *BulkInsertStruct) { + t.Query = &query + return t +} + +func (t *BulkInsertStruct) GetColumn() string { + return *t.Columns +} + +func (t *BulkInsertStruct) GetValues() string { + return *t.Values +} + +func (t *BulkInsertStruct) GetQuery() string { + return *t.Query +} diff --git a/utils/collector.go b/utils/collector.go new file mode 100644 index 0000000..afd6b7b --- /dev/null +++ b/utils/collector.go @@ -0,0 +1,74 @@ +package utils + +import ( + "fmt" + "reflect" +) + +type BulkInsertStruct struct { + Columns *string + Query *string + Values *string +} + +func (t *BulkInsertStruct) ExtractModelField(model interface{}, exceptionalField []string) (s *BulkInsertStruct) { + var reflectValue = reflect.ValueOf(model) + var columns string + + if reflectValue.Kind() == reflect.Ptr { + reflectValue = reflectValue.Elem() + } + + var reflectType = reflectValue.Type() + + for i := 0; i < reflectValue.NumField(); i++ { + current_field := reflectType.Field(i).Tag.Get("json") + if len(exceptionalField) > 0 { + for _, item := range exceptionalField { + if current_field != item { + if (i + 1) < reflectValue.NumField() { + columns += fmt.Sprintf("'%v', ", current_field) + } else { + columns += fmt.Sprintf("'%v'", current_field) + } + } + } + } else { + if (i + 1) < reflectValue.NumField() { + columns += fmt.Sprintf("'%v', ", current_field) + } else { + columns += fmt.Sprintf("'%v'", current_field) + } + } + + } + t.SetColumn(columns) + return t +} + +func (t *BulkInsertStruct) ArrangeValue(models [][]interface{}) (s *BulkInsertStruct) { + var values string + for indexCols, cols := range models { + var valueRows string + for indexRows, rows := range cols { + if (indexRows + 1) < len(cols) { + valueRows += fmt.Sprintf("'%v', ", rows) + } else { + valueRows += fmt.Sprintf("'%v'", rows) + } + } + + if (indexCols + 1) < len(models) { + values += fmt.Sprintf("(%v),", valueRows) + } else { + values += fmt.Sprintf("(%v);", valueRows) + } + } + t.SetValues(values) + return t +} + +func (t *BulkInsertStruct) PutQueryTogether(tableName string) (s *BulkInsertStruct) { + t.SetQuery(fmt.Sprintf("INSERT INTO %v (%v) VALUES %v", tableName, t.GetColumn(), t.GetValues())) + return t +} diff --git a/utils/helper.go b/utils/helper.go deleted file mode 100644 index da50d43..0000000 --- a/utils/helper.go +++ /dev/null @@ -1,48 +0,0 @@ -package utils - -import ( - "fmt" - "reflect" -) - -func ExtractModelField(model interface{}, exceptionalField []string) { - var reflectValue = reflect.ValueOf(model) - - if reflectValue.Kind() == reflect.Ptr { - reflectValue = reflectValue.Elem() - } - - var reflectType = reflectValue.Type() - - for i := 0; i < reflectValue.NumField(); i++ { - for _, item := range exceptionalField { - current_field := reflectType.Field(i).Tag.Get("json") - if current_field != item { - fmt.Println(current_field) - } - } - } -} - -func PutAllValueTogether(models [][]interface{}) string { - var values string - - for indexCols, cols := range models { - var valueRows string - for indexRows, rows := range cols { - if (indexRows + 1) < len(cols) { - valueRows += fmt.Sprintf("'%v', ", rows.(string)) - } else { - valueRows += fmt.Sprintf("'%v' ", rows.(string)) - } - } - - if (indexCols + 1) < len(cols) { - values += fmt.Sprintf("(%v),", valueRows) - } else { - values += fmt.Sprintf("(%v);", valueRows) - } - } - - return values -}