https://github.com/jauharibill/gobulkinsert
generating query bulk insert to support library gorm.io
https://github.com/jauharibill/gobulkinsert
golang gorm mysql query query-builder sql
Last synced: about 1 month ago
JSON representation
generating query bulk insert to support library gorm.io
- Host: GitHub
- URL: https://github.com/jauharibill/gobulkinsert
- Owner: jauharibill
- Created: 2020-08-24T09:58:37.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-27T07:24:45.000Z (almost 6 years ago)
- Last Synced: 2025-08-01T14:52:09.531Z (11 months ago)
- Topics: golang, gorm, mysql, query, query-builder, sql
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GO BULK INSERT
## Introduction
this library is used for creating raw query insert in bulk mode. this library created because gorm library doesnt support bulk request, so this library created for supporting any golang query builder library especially GORM.
### How to install
```
go get -u github.com/jauharibill/gobulkinsert
```
### How to use
```
package main
import (
"fmt"
"github.com/jauharibill/gobulkinsert"
)
type UserModel struct {
FullName string `json:"fullname"`
Age string `json:"age"`
Phone string `json:"phone"`
}
func main() {
var user UserModel
value := [][]interface{}{
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
{"bill", 12, "17-09-1945", "082245xxxx"},
}
query := gobulkinsert.Service().
ExtractModelField(user, []string{}).
ArrangeValue(value, 6).
InsertQuery("users").
GetQuery()
fmt.Println(query)
}
```
### Output
```
INSERT INTO `users` (`fullname`, `age`, `phone`) VALUES ('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx');
INSERT INTO `users` (`fullname`, `age`, `phone`) VALUES ('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx');
INSERT INTO `users` (`fullname`, `age`, `phone`) VALUES ('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx'),('bill', '12', '17-09-1945', '082245xxxx');
```
### How to implement in GORM Library
put generated query in variable, and then execute it with `Exec()` method in query builder gorm. for example :
```
func (i *gorm.DB) test () {
query :=
i.Exec(query) // you could put any db transaction also
}
```
## Contribute
Please pull request if you have any improvement, Cheers!