Remove depreciated functions and move some functions to schemas

This commit is contained in:
Lunny Xiao 2020-02-26 20:04:23 +08:00
parent 5027038e64
commit b651431d4a
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
10 changed files with 42 additions and 101 deletions

View File

@ -191,11 +191,6 @@ func (engine *Engine) SupportInsertMany() bool {
return engine.dialect.SupportInsertMany() 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 // Quote Use QuoteStr quote the string sql
func (engine *Engine) Quote(value string) string { func (engine *Engine) Quote(value string) string {
value = strings.TrimSpace(value) value = strings.TrimSpace(value)
@ -222,18 +217,6 @@ func (engine *Engine) QuoteTo(buf *strings.Builder, value string) {
engine.dialect.Quoter().QuoteTo(buf, value) 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 // SQLType A simple wrapper to dialect's core.SqlType method
func (engine *Engine) SQLType(c *schemas.Column) string { func (engine *Engine) SQLType(c *schemas.Column) string {
return engine.dialect.SQLType(c) 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 // SQL method let's you manually write raw SQL and operate
// For example: // For example:
// //
@ -597,13 +572,6 @@ func (engine *Engine) Where(query interface{}, args ...interface{}) *Session {
return session.Where(query, args...) 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) = ? // ID method provoide a condition as (id) = ?
func (engine *Engine) ID(id interface{}) *Session { func (engine *Engine) ID(id interface{}) *Session {
session := engine.NewSession() session := engine.NewSession()
@ -1092,23 +1060,9 @@ func (engine *Engine) IsTableExist(beanOrTableName interface{}) (bool, error) {
return session.IsTableExist(beanOrTableName) 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 // IDOf get id from one struct
func (engine *Engine) IDOf(bean interface{}) schemas.PK { func (engine *Engine) IDOf(bean interface{}) schemas.PK {
return engine.IdOfV(reflect.ValueOf(bean)) 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)
} }
// IDOfV get id from one value of struct // 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) 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, "`"))
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 package schemas
import ( import (
"fmt"
"strings" "strings"
) )
@ -76,7 +75,7 @@ func (q Quoter) Trim(s string) string {
return s return s
} }
func TrimSpaceJoin(a []string, sep string) string { func (q Quoter) Join(a []string, sep string) string {
switch len(a) { switch len(a) {
case 0: case 0:
return "" return ""
@ -90,19 +89,21 @@ func TrimSpaceJoin(a []string, sep string) string {
var b strings.Builder var b strings.Builder
b.Grow(n) b.Grow(n)
b.WriteString(strings.TrimSpace(a[0])) for i, s := range a {
for _, s := range a[1:] { if i > 0 {
b.WriteString(sep) b.WriteString(sep)
}
if q[0] != "" {
b.WriteString(q[0])
}
b.WriteString(strings.TrimSpace(s)) b.WriteString(strings.TrimSpace(s))
if q[1] != "" {
b.WriteString(q[1])
}
} }
return b.String() 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) { func (q Quoter) QuoteTo(buf *strings.Builder, value string) {
if q.IsEmpty() { if q.IsEmpty() {
buf.WriteString(value) buf.WriteString(value)

View File

@ -45,3 +45,13 @@ func TestQuoteTo(t *testing.T) {
quoter.QuoteTo(buf, "noquote") quoter.QuoteTo(buf, "noquote")
assert.EqualValues(t, "noquote", buf.String()) 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" 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 // 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. // and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.
func (session *Session) SQL(query interface{}, args ...interface{}) *Session { func (session *Session) SQL(query interface{}, args ...interface{}) *Session {
@ -39,13 +31,6 @@ func (session *Session) Or(query interface{}, args ...interface{}) *Session {
return 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 // ID provides converting id as a query condition
func (session *Session) ID(id interface{}) *Session { func (session *Session) ID(id interface{}) *Session {
session.statement.ID(id) session.statement.ID(id)

View File

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

View File

@ -251,19 +251,21 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
} }
cleanupProcessorsClosures(&session.beforeClosures) cleanupProcessorsClosures(&session.beforeClosures)
quoter := session.engine.dialect.Quoter()
var sql string var sql string
colStr := quoter.Join(colNames, ",")
if session.engine.dialect.DBType() == schemas.ORACLE { if session.engine.dialect.DBType() == schemas.ORACLE {
temp := fmt.Sprintf(") INTO %s (%v) VALUES (", temp := fmt.Sprintf(") INTO %s (%v) VALUES (",
session.engine.Quote(tableName), quoter.Quote(tableName),
quoteColumns(colNames, session.engine.Quote, ",")) colStr)
sql = fmt.Sprintf("INSERT ALL INTO %s (%v) VALUES (%v) SELECT 1 FROM DUAL", sql = fmt.Sprintf("INSERT ALL INTO %s (%v) VALUES (%v) SELECT 1 FROM DUAL",
session.engine.Quote(tableName), quoter.Quote(tableName),
quoteColumns(colNames, session.engine.Quote, ","), colStr,
strings.Join(colMultiPlaces, temp)) strings.Join(colMultiPlaces, temp))
} else { } else {
sql = fmt.Sprintf("INSERT INTO %s (%v) VALUES (%v)", sql = fmt.Sprintf("INSERT INTO %s (%v) VALUES (%v)",
session.engine.Quote(tableName), quoter.Quote(tableName),
quoteColumns(colNames, session.engine.Quote, ","), colStr,
strings.Join(colMultiPlaces, "),(")) strings.Join(colMultiPlaces, "),("))
} }
res, err := session.exec(sql, args...) 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 session.statement.JoinStr == "" {
if columnStr == "" { if columnStr == "" {
if session.statement.GroupByStr != "" { if session.statement.GroupByStr != "" {
columnStr = session.engine.quoteColumns(session.statement.GroupByStr) columnStr = session.statement.quoteColumnStr(session.statement.GroupByStr)
} else { } else {
columnStr = session.statement.genColumnStr() columnStr = session.statement.genColumnStr()
} }
@ -44,7 +44,7 @@ func (session *Session) genQuerySQL(sqlOrArgs ...interface{}) (string, []interfa
} else { } else {
if columnStr == "" { if columnStr == "" {
if session.statement.GroupByStr != "" { if session.statement.GroupByStr != "" {
columnStr = session.engine.quoteColumns(session.statement.GroupByStr) columnStr = session.statement.quoteColumnStr(session.statement.GroupByStr)
} else { } else {
columnStr = "*" columnStr = "*"
} }

View File

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