代码拉取完成,页面将自动刷新
同步操作将从 fifsky/gosql 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package gosql
import (
"fmt"
"strings"
)
type SQLBuilder struct {
dialect Dialect
fields string
table string
forceIndex string
where string
order string
limit string
offset string
hint string
// Extra args to be substituted in the *where* clause
args []interface{}
}
func (s *SQLBuilder) limitFormat() string {
if s.limit != "" {
return fmt.Sprintf("LIMIT %s", s.limit)
}
return ""
}
func (s *SQLBuilder) offsetFormat() string {
if s.offset != "" {
return fmt.Sprintf("OFFSET %s", s.offset)
}
return ""
}
func (s *SQLBuilder) orderFormat() string {
if s.order != "" {
return fmt.Sprintf("ORDER BY %s", s.order)
}
return ""
}
//queryString Assemble the query statement
func (s *SQLBuilder) queryString() string {
if s.fields == "" {
s.fields = "*"
}
table := s.dialect.Quote(s.table)
if s.forceIndex != "" {
table += fmt.Sprintf(" force index(%s)", s.forceIndex)
}
query := fmt.Sprintf("%sSELECT %s FROM %s %s %s %s %s", s.hint, s.fields, table, s.where, s.orderFormat(), s.limitFormat(), s.offsetFormat())
query = strings.TrimRight(query, " ")
query = query + ";"
return query
}
//countString Assemble the count statement
func (s *SQLBuilder) countString() string {
query := fmt.Sprintf("%sSELECT count(*) FROM %s %s", s.hint, s.dialect.Quote(s.table), s.where)
query = strings.TrimRight(query, " ")
query = query + ";"
return query
}
//insertString Assemble the insert statement
func (s *SQLBuilder) insertString(params map[string]interface{}) string {
var cols, vals []string
for _, k := range sortedParamKeys(params) {
cols = append(cols, s.dialect.Quote(k))
vals = append(vals, "?")
s.args = append(s.args, params[k])
}
return fmt.Sprintf("INSERT INTO %s (%s) VALUES(%s);", s.dialect.Quote(s.table), strings.Join(cols, ","), strings.Join(vals, ","))
}
//updateString Assemble the update statement
func (s *SQLBuilder) updateString(params map[string]interface{}) string {
var updateFields []string
args := make([]interface{}, 0)
for _, k := range sortedParamKeys(params) {
if e, ok := params[k].(*expr); ok {
updateFields = append(updateFields, fmt.Sprintf("%s=%s", s.dialect.Quote(k), e.expr))
args = append(args, e.args...)
} else {
updateFields = append(updateFields, fmt.Sprintf("%s=?", s.dialect.Quote(k)))
args = append(args, params[k])
}
}
args = append(args, s.args...)
s.args = args
query := fmt.Sprintf("UPDATE %s SET %s %s", s.dialect.Quote(s.table), strings.Join(updateFields, ","), s.where)
query = strings.TrimRight(query, " ")
query = query + ";"
return query
}
//deleteString Assemble the delete statement
func (s *SQLBuilder) deleteString() string {
query := fmt.Sprintf("DELETE FROM %s %s", s.dialect.Quote(s.table), s.where)
query = strings.TrimRight(query, " ")
query = query + ";"
return query
}
func (s *SQLBuilder) Where(str string, args ...interface{}) {
if s.where != "" {
s.where = fmt.Sprintf("%s AND (%s)", s.where, str)
} else {
s.where = fmt.Sprintf("WHERE (%s)", str)
}
// NB this assumes that args are only supplied for where clauses
// this may be an incorrect assumption!
if args != nil {
if s.args == nil {
s.args = args
} else {
s.args = append(s.args, args...)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。