Help us shape the future of Go! Take the 2026 Go Developer Survey

sg

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2021 License: MIT Imports: 3 Imported by: 0

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 []rowValues
}

func (*InsertData) Add

func (d *InsertData) Add(values []string)

type MysqlSqlGenerator

type MysqlSqlGenerator struct {
}

func (MysqlSqlGenerator) GetInsertSql

func (sg MysqlSqlGenerator) GetInsertSql(data InsertData) (string, []interface{}, error)

GetInsertSql - bind params and values to sql query

func (MysqlSqlGenerator) GetSelectSql

func (sg MysqlSqlGenerator) GetSelectSql(data SelectData) (string, []interface{}, error)

func (MysqlSqlGenerator) GetUpdateSql

func (sg MysqlSqlGenerator) GetUpdateSql(data UpdateData) (string, []interface{}, error)

func (MysqlSqlGenerator) GetUpsertSql

func (sg MysqlSqlGenerator) GetUpsertSql(data UpsertData) (string, []interface{}, error)

type ReplaceData

type ReplaceData struct {
	Field     string
	Type      string
	Condition string
}

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 []string)

Jump to

Keyboard shortcuts

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