Move zero functions to a standalone package #1548

Merged
lunny merged 2 commits from lunny/zero into master 2020-02-26 12:45:15 +00:00
10 changed files with 42 additions and 101 deletions
Showing only changes of commit b651431d4a - Show all commits

View File

@ -191,11 +191,6 @@ func (engine *Engine) SupportInsertMany() bool {
return engine.dialect.SupportInsertMany()
}
func (engine *Engine) quoteColumns(columnStr string) string {
columns := strings.Split(columnStr, ",")
return engine.dialect.Quoter().Join(columns, ",")
}
// Quote Use QuoteStr quote the string sql
func (engine *Engine) Quote(value string) string {
value = strings.TrimSpace(value)
@ -222,18 +217,6 @@ func (engine *Engine) QuoteTo(buf *strings.Builder, value string) {
engine.dialect.Quoter().QuoteTo(buf, value)
}
/*
func (engine *Engine) quote(sql string) string {
return engine.dialect.Quote(sql)
}*/
// SqlType will be deprecated, please use SQLType instead
//
// Deprecated: use SQLType instead
func (engine *Engine) SqlType(c *schemas.Column) string {
return engine.SQLType(c)
}
// SQLType A simple wrapper to dialect's core.SqlType method
func (engine *Engine) SQLType(c *schemas.Column) string {
return engine.dialect.SQLType(c)
@ -335,14 +318,6 @@ func (engine *Engine) logSQL(sqlStr string, sqlArgs ...interface{}) {
}
}
// Sql provides raw sql input parameter. When you have a complex SQL statement
// and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.
//
// Deprecated: use SQL instead.
func (engine *Engine) Sql(querystring string, args ...interface{}) *Session {
return engine.SQL(querystring, args...)
}
// SQL method let's you manually write raw SQL and operate
// For example:
//
@ -597,13 +572,6 @@ func (engine *Engine) Where(query interface{}, args ...interface{}) *Session {
return session.Where(query, args...)
}
// Id will be deprecated, please use ID instead
func (engine *Engine) Id(id interface{}) *Session {
session := engine.NewSession()
session.isAutoClose = true
return session.Id(id)
}
// ID method provoide a condition as (id) = ?
func (engine *Engine) ID(id interface{}) *Session {
session := engine.NewSession()
@ -1092,23 +1060,9 @@ func (engine *Engine) IsTableExist(beanOrTableName interface{}) (bool, error) {
return session.IsTableExist(beanOrTableName)
}
// IdOf get id from one struct
//
// Deprecated: use IDOf instead.
func (engine *Engine) IdOf(bean interface{}) schemas.PK {
return engine.IDOf(bean)
}
// IDOf get id from one struct
func (engine *Engine) IDOf(bean interface{}) schemas.PK {
return engine.IdOfV(reflect.ValueOf(bean))
}
// IdOfV get id from one value of struct
//
// Deprecated: use IDOfV instead.
func (engine *Engine) IdOfV(rv reflect.Value) schemas.PK {
return engine.IDOfV(rv)
return engine.IDOfV(reflect.ValueOf(bean))
}
// IDOfV get id from one value of struct

View File

@ -214,10 +214,3 @@ func eraseAny(value string, strToErase ...string) string {
return replacer.Replace(value)
}
func quoteColumns(cols []string, quoteFunc func(string) string, sep string) string {
for i := range cols {
cols[i] = quoteFunc(cols[i])
}
return strings.Join(cols, sep+" ")
}

View File

@ -16,12 +16,3 @@ func TestEraseAny(t *testing.T) {
assert.EqualValues(t, "SELECT * FROM table.[table_name]", eraseAny(raw, "`"))
assert.EqualValues(t, "SELECT * FROM table.table_name", eraseAny(raw, "`", "[", "]"))
}
func TestQuoteColumns(t *testing.T) {
cols := []string{"f1", "f2", "f3"}
quoteFunc := func(value string) string {
return "[" + value + "]"
}
assert.EqualValues(t, "[f1], [f2], [f3]", quoteColumns(cols, quoteFunc, ","))
}

View File

@ -5,7 +5,6 @@
package schemas
import (
"fmt"
"strings"
)
@ -76,7 +75,7 @@ func (q Quoter) Trim(s string) string {
return s
}
func TrimSpaceJoin(a []string, sep string) string {
func (q Quoter) Join(a []string, sep string) string {
switch len(a) {
case 0:
return ""
@ -90,19 +89,21 @@ func TrimSpaceJoin(a []string, sep string) string {
var b strings.Builder
b.Grow(n)
b.WriteString(strings.TrimSpace(a[0]))
for _, s := range a[1:] {
b.WriteString(sep)
for i, s := range a {
if i > 0 {
b.WriteString(sep)
}
if q[0] != "" {
b.WriteString(q[0])
}
b.WriteString(strings.TrimSpace(s))
if q[1] != "" {
b.WriteString(q[1])
}
}
return b.String()
}
func (q Quoter) Join(s []string, splitter string) string {
//return fmt.Sprintf("%s%s%s", q[0], TrimSpaceJoin(s, fmt.Sprintf("%s%s%s", q[1], splitter, q[0])), q[1])
return q.Quote(TrimSpaceJoin(s, fmt.Sprintf("%s%s%s", q[1], splitter, q[0])))
}
func (q Quoter) QuoteTo(buf *strings.Builder, value string) {
if q.IsEmpty() {
buf.WriteString(value)

View File

@ -45,3 +45,13 @@ func TestQuoteTo(t *testing.T) {
quoter.QuoteTo(buf, "noquote")
assert.EqualValues(t, "noquote", buf.String())
}
func TestJoin(t *testing.T) {
cols := []string{"f1", "f2", "f3"}
quoter := Quoter{"[", "]"}
assert.EqualValues(t, "[f1], [f2], [f3]", quoter.Join(cols, ", "))
quoter = Quoter{"", ""}
assert.EqualValues(t, "f1, f2, f3", quoter.Join(cols, ", "))
}

View File

@ -6,14 +6,6 @@ package xorm
import "xorm.io/builder"
// Sql provides raw sql input parameter. When you have a complex SQL statement
// and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.
//
// Deprecated: use SQL instead.
func (session *Session) Sql(query string, args ...interface{}) *Session {
return session.SQL(query, args...)
}
// SQL provides raw sql input parameter. When you have a complex SQL statement
// and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.
func (session *Session) SQL(query interface{}, args ...interface{}) *Session {
@ -39,13 +31,6 @@ func (session *Session) Or(query interface{}, args ...interface{}) *Session {
return session
}
// Id provides converting id as a query condition
//
// Deprecated: use ID instead
func (session *Session) Id(id interface{}) *Session {
return session.ID(id)
}
// ID provides converting id as a query condition
func (session *Session) ID(id interface{}) *Session {
session.statement.ID(id)

View File

@ -142,7 +142,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{})
if session.statement.JoinStr == "" {
if columnStr == "" {
if session.statement.GroupByStr != "" {
columnStr = session.engine.quoteColumns(session.statement.GroupByStr)
columnStr = session.statement.quoteColumnStr(session.statement.GroupByStr)
} else {
columnStr = session.statement.genColumnStr()
}
@ -150,7 +150,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{})
} else {
if columnStr == "" {
if session.statement.GroupByStr != "" {
columnStr = session.engine.quoteColumns(session.statement.GroupByStr)
columnStr = session.statement.quoteColumnStr(session.statement.GroupByStr)
} else {
columnStr = "*"
}
@ -417,7 +417,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in
} else {
session.engine.logger.Debug("[cacheFind] cache hit bean:", tableName, id, bean)
pk := session.engine.IdOf(bean)
pk := session.engine.IDOf(bean)
xid, err := pk.ToString()
if err != nil {
return err

View File

@ -251,19 +251,21 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
}
cleanupProcessorsClosures(&session.beforeClosures)
quoter := session.engine.dialect.Quoter()
var sql string
colStr := quoter.Join(colNames, ",")
if session.engine.dialect.DBType() == schemas.ORACLE {
temp := fmt.Sprintf(") INTO %s (%v) VALUES (",
session.engine.Quote(tableName),
quoteColumns(colNames, session.engine.Quote, ","))
quoter.Quote(tableName),
colStr)
sql = fmt.Sprintf("INSERT ALL INTO %s (%v) VALUES (%v) SELECT 1 FROM DUAL",
session.engine.Quote(tableName),
quoteColumns(colNames, session.engine.Quote, ","),
quoter.Quote(tableName),
colStr,
strings.Join(colMultiPlaces, temp))
} else {
sql = fmt.Sprintf("INSERT INTO %s (%v) VALUES (%v)",
session.engine.Quote(tableName),
quoteColumns(colNames, session.engine.Quote, ","),
quoter.Quote(tableName),
colStr,
strings.Join(colMultiPlaces, "),("))
}
res, err := session.exec(sql, args...)

View File

@ -36,7 +36,7 @@ func (session *Session) genQuerySQL(sqlOrArgs ...interface{}) (string, []interfa
if session.statement.JoinStr == "" {
if columnStr == "" {
if session.statement.GroupByStr != "" {
columnStr = session.engine.quoteColumns(session.statement.GroupByStr)
columnStr = session.statement.quoteColumnStr(session.statement.GroupByStr)
} else {
columnStr = session.statement.genColumnStr()
}
@ -44,7 +44,7 @@ func (session *Session) genQuerySQL(sqlOrArgs ...interface{}) (string, []interfa
} else {
if columnStr == "" {
if session.statement.GroupByStr != "" {
columnStr = session.engine.quoteColumns(session.statement.GroupByStr)
columnStr = session.statement.quoteColumnStr(session.statement.GroupByStr)
} else {
columnStr = "*"
}

View File

@ -940,6 +940,11 @@ func (statement *Statement) genConds(bean interface{}) (string, []interface{}, e
return builder.ToSQL(statement.cond)
}
func (statement *Statement) quoteColumnStr(columnStr string) string {
columns := strings.Split(columnStr, ",")
return statement.Engine.dialect.Quoter().Join(columns, ",")
}
func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{}, error) {
v := rValue(bean)
isStruct := v.Kind() == reflect.Struct
@ -955,7 +960,7 @@ func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{},
if len(statement.JoinStr) == 0 {
if len(columnStr) == 0 {
if len(statement.GroupByStr) > 0 {
columnStr = statement.Engine.quoteColumns(statement.GroupByStr)
columnStr = statement.quoteColumnStr(statement.GroupByStr)
} else {
columnStr = statement.genColumnStr()
}
@ -963,7 +968,7 @@ func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{},
} else {
if len(columnStr) == 0 {
if len(statement.GroupByStr) > 0 {
columnStr = statement.Engine.quoteColumns(statement.GroupByStr)
columnStr = statement.quoteColumnStr(statement.GroupByStr)
}
}
}