Skip to content

generating query bulk insert to support library gorm.io

Notifications You must be signed in to change notification settings

jauharibill/gobulkinsert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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 := <this value belongs to gobulkrequest>
  i.Exec(query) // you could put any db transaction also
}

Contribute

Please pull request if you have any improvement, Cheers!