6 Star 28 Fork 7

艾润物联/go-sqlbuilder

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
insert.go 1.55 KB
一键复制 编辑 原始数据 按行查看 历史
Yoojia Chen 提交于 2018-05-17 21:33 +08:00 . Rename package from sqlx to gsb
package gsb
import (
"bytes"
)
//
// Author: 陈永佳 chenyongjia@parkingwang.com, yoojiachen@gmail.com
//
type InsertBuilder struct {
table string
columns []string
values []interface{}
}
func Insert(table string) *InsertBuilder {
return &InsertBuilder{
table: table,
columns: make([]string, 0),
values: make([]interface{}, 0),
}
}
func (slf *InsertBuilder) Table(table string) *InsertBuilder {
slf.table = table
return slf
}
func (slf *InsertBuilder) Columns(columns ...string) *InsertBuilder {
for _, col := range columns {
slf.columns = append(slf.columns, col)
slf.values = append(slf.values, SQLPlaceHolder)
}
return slf
}
func (slf *InsertBuilder) Values(values ...interface{}) *InsertBuilder {
// check columns and values
if len(slf.columns) != len(slf.values) {
panic("length of columns and values NOT MATCH")
}
for i, newVal := range values {
slf.values[i] = newVal
}
return slf
}
func (slf *InsertBuilder) compile() *bytes.Buffer {
if "" == slf.table {
panic("table not found, you should call 'Table(table)' method to set it")
}
buf := new(bytes.Buffer)
buf.WriteString("INSERT INTO ")
buf.WriteString(EscapeName(slf.table))
buf.WriteByte('(')
buf.WriteString(joinNames(slf.columns))
buf.WriteByte(')')
buf.WriteString(" VALUES ")
buf.WriteByte('(')
buf.WriteString(joinValues(slf.values))
buf.WriteByte(')')
return buf
}
func (slf *InsertBuilder) GetSQL() string {
return endOfSQL(slf.compile())
}
func (slf *InsertBuilder) Execute(prepare SQLPrepare) *Executor {
return newExecute(slf.GetSQL(), prepare)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/iRainIoT/go-sqlbuilder.git
git@gitee.com:iRainIoT/go-sqlbuilder.git
iRainIoT
go-sqlbuilder
go-sqlbuilder
37658dd0e549

搜索帮助