sg

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2021 License: MIT Imports: 4 Imported by: 1

README

Sql generator

Обзор

Пакет служит для генерации сложных типовых запросов, таких как bulk insert, bulk update, bulk upsert

Примеры использования

sqlGenerator := sg.MysqlSqlGenerator{}
Select
selectData := sg.SelectData{
    TableName: "SearchText",
    Fields:    []string{"id", "country", "os"},
    Where:     map[string]string{"id": "189920059115387768", "country": "IT"},
}

query, args, err := sqlGenerator.GetSelectSql(selectData)
Insert
dataInsert := sg.InsertData{
    TableName: "SearchText",
    IsIgnore:  false,
    Fields:    []string{"id", "country"},
}

dataInsert.Add([]string{"123", "it"})
dataInsert.Add([]string{"345", "ua"})
dataInsert.Add([]string{"456", "by"})

query, args, err = sqlGenerator.GetInsertSql(dataInsert)
Update
dataUpdate := sg.UpdateData{
    TableName: "SearchText",
}

dataUpdate.Add(
    map[string]string{"country": "ru", "os": "Android"},
    map[string]string{"id": "189920059115387768", "country": "IT"},
)

dataUpdate.Add(
    map[string]string{"country": "FR", "os": "ios"},
    map[string]string{"id": "456", "country": "UA"},
)

query, args, err = sqlGenerator.GetUpdateSql(dataUpdate)
Upsert
dataUpsert := sg.UpsertData{
    TableName: "SearchText",
    Fields:    []string{"id", "country"},
    ReplaceDataList: []sg.ReplaceData{
        {Field: "id"},
        {Field: "country", Type: sg.CONDITION, Condition: "1"},
        {Field: "frequency", Type: sg.INCREMENT},
    },
}

dataUpsert.Add([]string{"123", "it"})
dataUpsert.Add([]string{"345", "ua"})
dataUpsert.Add([]string{"456", "by"})

query, args, err = sqlGenerator.GetUpsertSql(dataUpsert)

Documentation

Index

Constants

View Source
const (
	INCREMENT = "inc"
	DECREMENT = "dec"
	CONDITION = "cond"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type InsertData

type InsertData struct {
	TableName  string
	IsIgnore   bool
	Fields     []string
	ValuesList rows
	// contains filtered or unexported fields
}

InsertData - data for perform insert operation

func NewInsertData

func NewInsertData() InsertData

NewInsertData - create InsertData instance

func (*InsertData) Add

func (d *InsertData) Add(values []interface{})

Add - add row to struct

func (*InsertData) GetPrimaryKeyName

func (d *InsertData) GetPrimaryKeyName() string

GetPrimaryKeyName - get primary key field name

func (*InsertData) IsOptimize

func (d *InsertData) IsOptimize() bool

IsOptimize - return optimize state

func (*InsertData) Reset added in v1.0.0

func (d *InsertData) Reset()

func (*InsertData) SetOptimize

func (d *InsertData) SetOptimize(o bool)

SetOptimize - set support for sql optimize

func (*InsertData) SetPrimaryKeyName

func (d *InsertData) SetPrimaryKeyName(name string)

SetPrimaryKeyName - set primary key field name

type ReplaceData

type ReplaceData struct {
	Field     string
	Type      string
	Condition string
}

type SQLGenerator

type SQLGenerator interface {
	GetInsertSQL(InsertData) (string, []interface{}, error)
	GetUpdateSQL(UpdateData) (string, []interface{}, error)
	GetUpsertSQL(UpsertData) (string, []interface{}, error)
	GetSelectSQL(SelectData) (string, []interface{}, error)
}

SQLGenerator - generate queries operations for defined driver

func NewSQLGenerator

func NewSQLGenerator(driver string) SQLGenerator

NewSQLGenerator - return sqlGenarator interface for provided driver

type SelectData

type SelectData struct {
	TableName string
	Fields    []string
	Where     map[string]string
}

type UpdateData

type UpdateData struct {
	TableName string
	List      []updateDataList
}

func (*UpdateData) Add

func (d *UpdateData) Add(set map[string]string, where map[string]string)

type UpsertData

type UpsertData struct {
	TableName       string
	Fields          []string
	ValuesList      []rowValues
	ReplaceDataList []ReplaceData
}

func (*UpsertData) Add

func (d *UpsertData) Add(values []interface{})

func (*UpsertData) Reset added in v1.0.0

func (d *UpsertData) Reset()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL