Skip to content

Commit

Permalink
extract query builder for bulk insert
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Tanthowi Jauhari committed Aug 24, 2020
1 parent 780d524 commit 7b75dde
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 54 deletions.
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -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())
}
8 changes: 4 additions & 4 deletions models/user_model.go
Original file line number Diff line number Diff line change
@@ -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"`
}
32 changes: 32 additions & 0 deletions utils/agregator.go
Original file line number Diff line number Diff line change
@@ -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
}
74 changes: 74 additions & 0 deletions utils/collector.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 0 additions & 48 deletions utils/helper.go

This file was deleted.

0 comments on commit 7b75dde

Please sign in to comment.