1 Star 0 Fork 8

jqmtony/toyorm

forked from bigpigeon/toyorm 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
exec.go 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
bigpigeon 提交于 2018-05-22 16:52 . 1. Insert operation optimization
/*
* Copyright 2018. bigpigeon. All rights reserved.
* Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file.
*/
package toyorm
import (
"bytes"
"encoding/json"
"strconv"
)
type ExecValue interface {
Source() string // exec source code
Query() string // exec encode code
Args() []interface{}
Append(query string, args ...interface{}) ExecValue
JsonArgs() string
}
type BasicExec struct {
query string
args []interface{}
}
// not process source str, Source() == Query()
type DefaultExec struct {
query string
args []interface{}
}
func (e DefaultExec) Source() string {
return e.query
}
func (e DefaultExec) Query() string {
return e.query
}
func (e DefaultExec) Args() []interface{} {
return e.args
}
func (e DefaultExec) Append(query string, args ...interface{}) ExecValue {
e.query += query
e.args = append(e.args, args...)
return e
}
func (e DefaultExec) JsonArgs() string {
s, err := json.Marshal(e.args)
if err != nil {
panic(err)
}
return string(s)
}
// when call Query() method, all '?' in query will replace to '$1','$2'...
type QToSExec struct {
DefaultExec
}
// go-bug DefaultExec will return ExecValue(DefaultExec), so must to implement this func
func (e QToSExec) Append(query string, args ...interface{}) ExecValue {
e.query += query
e.args = append(e.args, args...)
return e
}
func (e QToSExec) Query() string {
data := []byte(e.query)
buff := bytes.Buffer{}
isEscaping := false
pNum := 1 // number of placeholder
pre, i := 0, 0
for ; i < len(data); i++ {
switch e.query[i] {
case '?':
if isEscaping == false {
buff.Write(data[pre:i])
buff.Write(append([]byte{'$'}, []byte(strconv.Itoa(pNum))...))
pre = i + 1
pNum++
} else {
buff.Write(data[pre : i-1])
buff.WriteByte(data[i])
pre = i + 1
}
case '\\':
isEscaping = true
continue
}
isEscaping = false
}
buff.Write(data[pre:i])
return buff.String()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jqmtony/toyorm.git
git@gitee.com:jqmtony/toyorm.git
jqmtony
toyorm
toyorm
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385