1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-23 02:38:35 +00:00

Upgrade xorm to v1.1.0 (#15869)

This commit is contained in:
Lunny Xiao
2021-05-15 03:17:06 +08:00
committed by GitHub
parent e2f39c2b64
commit f6be429781
55 changed files with 1309 additions and 438 deletions

View File

@@ -6,15 +6,15 @@ package json
import "encoding/json"
// JSONInterface represents an interface to handle json data
type JSONInterface interface {
// Interface represents an interface to handle json data
type Interface interface {
Marshal(v interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
}
var (
// DefaultJSONHandler default json handler
DefaultJSONHandler JSONInterface = StdJSON{}
DefaultJSONHandler Interface = StdJSON{}
)
// StdJSON implements JSONInterface via encoding/json

View File

@@ -12,6 +12,7 @@ import (
"xorm.io/xorm/schemas"
)
// ConvertIDSQL converts SQL with id
func (statement *Statement) ConvertIDSQL(sqlStr string) string {
if statement.RefTable != nil {
cols := statement.RefTable.PKColumns()
@@ -37,6 +38,7 @@ func (statement *Statement) ConvertIDSQL(sqlStr string) string {
return ""
}
// ConvertUpdateSQL converts update SQL
func (statement *Statement) ConvertUpdateSQL(sqlStr string) (string, string) {
if statement.RefTable == nil || len(statement.RefTable.PrimaryKeys) != 1 {
return "", ""

View File

@@ -12,6 +12,7 @@ import (
"xorm.io/xorm/schemas"
)
// ErrUnsupportedExprType represents an error with unsupported express type
type ErrUnsupportedExprType struct {
tp string
}

View File

@@ -14,6 +14,7 @@ import (
"xorm.io/xorm/schemas"
)
// GenQuerySQL generate query SQL
func (statement *Statement) GenQuerySQL(sqlOrArgs ...interface{}) (string, []interface{}, error) {
if len(sqlOrArgs) > 0 {
return statement.ConvertSQLOrArgs(sqlOrArgs...)
@@ -72,6 +73,7 @@ func (statement *Statement) GenQuerySQL(sqlOrArgs ...interface{}) (string, []int
return sqlStr, args, nil
}
// GenSumSQL generates sum SQL
func (statement *Statement) GenSumSQL(bean interface{}, columns ...string) (string, []interface{}, error) {
if statement.RawSQL != "" {
return statement.GenRawSQL(), statement.RawParams, nil
@@ -102,6 +104,7 @@ func (statement *Statement) GenSumSQL(bean interface{}, columns ...string) (stri
return sqlStr, append(statement.joinArgs, condArgs...), nil
}
// GenGetSQL generates Get SQL
func (statement *Statement) GenGetSQL(bean interface{}) (string, []interface{}, error) {
v := rValue(bean)
isStruct := v.Kind() == reflect.Struct
@@ -316,6 +319,7 @@ func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderB
return buf.String(), condArgs, nil
}
// GenExistSQL generates Exist SQL
func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interface{}, error) {
if statement.RawSQL != "" {
return statement.GenRawSQL(), statement.RawParams, nil
@@ -385,6 +389,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
return sqlStr, args, nil
}
// GenFindSQL generates Find SQL
func (statement *Statement) GenFindSQL(autoCond builder.Cond) (string, []interface{}, error) {
if statement.RawSQL != "" {
return statement.GenRawSQL(), statement.RawParams, nil

View File

@@ -90,19 +90,17 @@ func NewStatement(dialect dialects.Dialect, tagParser *tags.Parser, defaultTimeZ
return statement
}
// SetTableName set table name
func (statement *Statement) SetTableName(tableName string) {
statement.tableName = tableName
}
func (statement *Statement) omitStr() string {
return statement.dialect.Quoter().Join(statement.OmitColumnMap, " ,")
}
// GenRawSQL generates correct raw sql
func (statement *Statement) GenRawSQL() string {
return statement.ReplaceQuote(statement.RawSQL)
}
// GenCondSQL generates condition SQL
func (statement *Statement) GenCondSQL(condOrBuilder interface{}) (string, []interface{}, error) {
condSQL, condArgs, err := builder.ToSQL(condOrBuilder)
if err != nil {
@@ -111,6 +109,7 @@ func (statement *Statement) GenCondSQL(condOrBuilder interface{}) (string, []int
return statement.ReplaceQuote(condSQL), condArgs, nil
}
// ReplaceQuote replace sql key words with quote
func (statement *Statement) ReplaceQuote(sql string) string {
if sql == "" || statement.dialect.URI().DBType == schemas.MYSQL ||
statement.dialect.URI().DBType == schemas.SQLITE {
@@ -119,11 +118,12 @@ func (statement *Statement) ReplaceQuote(sql string) string {
return statement.dialect.Quoter().Replace(sql)
}
// SetContextCache sets context cache
func (statement *Statement) SetContextCache(ctxCache contexts.ContextCache) {
statement.Context = ctxCache
}
// Init reset all the statement's fields
// Reset reset all the statement's fields
func (statement *Statement) Reset() {
statement.RefTable = nil
statement.Start = 0
@@ -163,7 +163,7 @@ func (statement *Statement) Reset() {
statement.LastError = nil
}
// NoAutoCondition if you do not want convert bean's field as query condition, then use this function
// SetNoAutoCondition if you do not want convert bean's field as query condition, then use this function
func (statement *Statement) SetNoAutoCondition(no ...bool) *Statement {
statement.NoAutoCondition = true
if len(no) > 0 {
@@ -271,6 +271,7 @@ func (statement *Statement) NotIn(column string, args ...interface{}) *Statement
return statement
}
// SetRefValue set ref value
func (statement *Statement) SetRefValue(v reflect.Value) error {
var err error
statement.RefTable, err = statement.tagParser.ParseWithCache(reflect.Indirect(v))
@@ -285,6 +286,7 @@ func rValue(bean interface{}) reflect.Value {
return reflect.Indirect(reflect.ValueOf(bean))
}
// SetRefBean set ref bean
func (statement *Statement) SetRefBean(bean interface{}) error {
var err error
statement.RefTable, err = statement.tagParser.ParseWithCache(rValue(bean))
@@ -390,6 +392,7 @@ func (statement *Statement) Cols(columns ...string) *Statement {
return statement
}
// ColumnStr returns column string
func (statement *Statement) ColumnStr() string {
return statement.dialect.Quoter().Join(statement.ColumnMap, ", ")
}
@@ -493,11 +496,12 @@ func (statement *Statement) Asc(colNames ...string) *Statement {
return statement
}
// Conds returns condtions
func (statement *Statement) Conds() builder.Cond {
return statement.cond
}
// Table tempororily set table name, the parameter could be a string or a pointer of struct
// SetTable tempororily set table name, the parameter could be a string or a pointer of struct
func (statement *Statement) SetTable(tableNameOrBean interface{}) error {
v := rValue(tableNameOrBean)
t := v.Type()
@@ -564,7 +568,7 @@ func (statement *Statement) Join(joinOP string, tablename interface{}, condition
return statement
}
// tbName get some table's table name
// tbNameNoSchema get some table's table name
func (statement *Statement) tbNameNoSchema(table *schemas.Table) string {
if len(statement.AltTableName) > 0 {
return statement.AltTableName
@@ -585,12 +589,13 @@ func (statement *Statement) Having(conditions string) *Statement {
return statement
}
// Unscoped always disable struct tag "deleted"
// SetUnscoped always disable struct tag "deleted"
func (statement *Statement) SetUnscoped() *Statement {
statement.unscoped = true
return statement
}
// GetUnscoped return true if it's unscoped
func (statement *Statement) GetUnscoped() bool {
return statement.unscoped
}
@@ -636,6 +641,7 @@ func (statement *Statement) genColumnStr() string {
return buf.String()
}
// GenCreateTableSQL generated create table SQL
func (statement *Statement) GenCreateTableSQL() []string {
statement.RefTable.StoreEngine = statement.StoreEngine
statement.RefTable.Charset = statement.Charset
@@ -643,6 +649,7 @@ func (statement *Statement) GenCreateTableSQL() []string {
return s
}
// GenIndexSQL generated create index SQL
func (statement *Statement) GenIndexSQL() []string {
var sqls []string
tbName := statement.TableName()
@@ -659,6 +666,7 @@ func uniqueName(tableName, uqeName string) string {
return fmt.Sprintf("UQE_%v_%v", tableName, uqeName)
}
// GenUniqueSQL generates unique SQL
func (statement *Statement) GenUniqueSQL() []string {
var sqls []string
tbName := statement.TableName()
@@ -671,6 +679,7 @@ func (statement *Statement) GenUniqueSQL() []string {
return sqls
}
// GenDelIndexSQL generate delete index SQL
func (statement *Statement) GenDelIndexSQL() []string {
var sqls []string
tbName := statement.TableName()
@@ -896,6 +905,7 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{},
return builder.And(conds...), nil
}
// BuildConds builds condition
func (statement *Statement) BuildConds(table *schemas.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, addedTableName bool) (builder.Cond, error) {
return statement.buildConds2(table, bean, includeVersion, includeUpdated, includeNil, includeAutoIncr, statement.allUseBool, statement.useAllCols,
statement.unscoped, statement.MustColumnMap, statement.TableName(), statement.TableAlias, addedTableName)
@@ -911,12 +921,10 @@ func (statement *Statement) mergeConds(bean interface{}) error {
statement.cond = statement.cond.And(autoCond)
}
if err := statement.ProcessIDParam(); err != nil {
return err
}
return nil
return statement.ProcessIDParam()
}
// GenConds generates conditions
func (statement *Statement) GenConds(bean interface{}) (string, []interface{}, error) {
if err := statement.mergeConds(bean); err != nil {
return "", nil, err
@@ -930,6 +938,7 @@ func (statement *Statement) quoteColumnStr(columnStr string) string {
return statement.dialect.Quoter().Join(columns, ",")
}
// ConvertSQLOrArgs converts sql or args
func (statement *Statement) ConvertSQLOrArgs(sqlOrArgs ...interface{}) (string, []interface{}, error) {
sql, args, err := convertSQLOrArgs(sqlOrArgs...)
if err != nil {

View File

@@ -77,6 +77,7 @@ func convertArg(arg interface{}, convertFunc func(string) string) string {
const insertSelectPlaceHolder = true
// WriteArg writes an arg
func (statement *Statement) WriteArg(w *builder.BytesWriter, arg interface{}) error {
switch argv := arg.(type) {
case *builder.Builder:
@@ -116,6 +117,7 @@ func (statement *Statement) WriteArg(w *builder.BytesWriter, arg interface{}) er
return nil
}
// WriteArgs writes args
func (statement *Statement) WriteArgs(w *builder.BytesWriter, args []interface{}) error {
for i, arg := range args {
if err := statement.WriteArg(w, arg); err != nil {

View File

@@ -8,6 +8,7 @@ import (
"fmt"
)
// IndexName returns index name
func IndexName(tableName, idxName string) string {
return fmt.Sprintf("IDX_%v_%v", tableName, idxName)
}

View File

@@ -8,6 +8,7 @@ import (
"reflect"
)
// ReflectValue returns value of a bean
func ReflectValue(bean interface{}) reflect.Value {
return reflect.Indirect(reflect.ValueOf(bean))
}

View File

@@ -8,6 +8,7 @@ import (
"strings"
)
// IsSubQuery returns true if it contains a sub query
func IsSubQuery(tbName string) bool {
const selStr = "select"
if len(tbName) <= len(selStr)+1 {

View File

@@ -8,10 +8,12 @@ import (
"strings"
)
// IndexNoCase index a string in a string with no care of capitalize
func IndexNoCase(s, sep string) int {
return strings.Index(strings.ToLower(s), strings.ToLower(sep))
}
// SplitNoCase split a string by a seperator with no care of capitalize
func SplitNoCase(s, sep string) []string {
idx := IndexNoCase(s, sep)
if idx < 0 {
@@ -20,6 +22,7 @@ func SplitNoCase(s, sep string) []string {
return strings.Split(s, s[idx:idx+len(sep)])
}
// SplitNNoCase split n by a seperator with no care of capitalize
func SplitNNoCase(s, sep string, n int) []string {
idx := IndexNoCase(s, sep)
if idx < 0 {

View File

@@ -9,6 +9,7 @@ import (
"time"
)
// Zeroable represents an interface which could know if it's a zero value
type Zeroable interface {
IsZero() bool
}
@@ -21,39 +22,39 @@ func IsZero(k interface{}) bool {
return true
}
switch k.(type) {
switch t := k.(type) {
case int:
return k.(int) == 0
return t == 0
case int8:
return k.(int8) == 0
return t == 0
case int16:
return k.(int16) == 0
return t == 0
case int32:
return k.(int32) == 0
return t == 0
case int64:
return k.(int64) == 0
return t == 0
case uint:
return k.(uint) == 0
return t == 0
case uint8:
return k.(uint8) == 0
return t == 0
case uint16:
return k.(uint16) == 0
return t == 0
case uint32:
return k.(uint32) == 0
return t == 0
case uint64:
return k.(uint64) == 0
return t == 0
case float32:
return k.(float32) == 0
return t == 0
case float64:
return k.(float64) == 0
return t == 0
case bool:
return k.(bool) == false
return !t
case string:
return k.(string) == ""
return t == ""
case *time.Time:
return k.(*time.Time) == nilTime || IsTimeZero(*k.(*time.Time))
return t == nilTime || IsTimeZero(*t)
case time.Time:
return IsTimeZero(k.(time.Time))
return IsTimeZero(t)
case Zeroable:
return k.(Zeroable) == nil || k.(Zeroable).IsZero()
case reflect.Value: // for go version less than 1.13 because reflect.Value has no method IsZero
@@ -65,6 +66,7 @@ func IsZero(k interface{}) bool {
var zeroType = reflect.TypeOf((*Zeroable)(nil)).Elem()
// IsValueZero returns true if the reflect Value is a zero
func IsValueZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice:
@@ -88,6 +90,7 @@ func IsValueZero(v reflect.Value) bool {
return false
}
// IsStructZero returns true if the Value is a struct and all fields is zero
func IsStructZero(v reflect.Value) bool {
if !v.IsValid() || v.NumField() == 0 {
return true
@@ -120,6 +123,7 @@ func IsStructZero(v reflect.Value) bool {
return true
}
// IsArrayZero returns true is a slice of array is zero
func IsArrayZero(v reflect.Value) bool {
if !v.IsValid() || v.Len() == 0 {
return true
@@ -134,11 +138,13 @@ func IsArrayZero(v reflect.Value) bool {
return true
}
// represents all zero times
const (
ZeroTime0 = "0000-00-00 00:00:00"
ZeroTime1 = "0001-01-01 00:00:00"
)
// IsTimeZero return true if a time is zero
func IsTimeZero(t time.Time) bool {
return t.IsZero() || t.Format("2006-01-02 15:04:05") == ZeroTime0 ||
t.Format("2006-01-02 15:04:05") == ZeroTime1