-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract query builder for bulk insert
- Loading branch information
Bill Tanthowi Jauhari
committed
Aug 24, 2020
1 parent
780d524
commit 7b75dde
Showing
5 changed files
with
122 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.