1
1
mirror of https://github.com/go-gitea/gitea synced 2024-09-19 10:16:03 +00:00
gitea/vendor/xorm.io/xorm/internal/statements/expr.go
zeripath 73e5c36f25
Upgrade xorm to v1.2.2 (#16663) & Add test to ensure that dumping of login sources remains correct (#16847) (#16849)
* Upgrade xorm to v1.2.2 (#16663)

Backport #16663

Fix #16683

* Upgrade xorm to v1.2.2

* Change the Engine interface to match xorm v1.2.2

* Add test to ensure that dumping of login sources remains correct (#16847)

#16831 has occurred because of a missed regression. This PR adds a simple test to
try to prevent this occuring again.

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2021-08-28 13:15:21 +02:00

95 lines
1.9 KiB
Go
Vendored

// Copyright 2019 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package statements
import (
"fmt"
"strings"
"xorm.io/builder"
"xorm.io/xorm/schemas"
)
// ErrUnsupportedExprType represents an error with unsupported express type
type ErrUnsupportedExprType struct {
tp string
}
func (err ErrUnsupportedExprType) Error() string {
return fmt.Sprintf("Unsupported expression type: %v", err.tp)
}
// Expr represents an SQL express
type Expr struct {
ColName string
Arg interface{}
}
// WriteArgs writes args to the writer
func (expr *Expr) WriteArgs(w *builder.BytesWriter) error {
switch arg := expr.Arg.(type) {
case *builder.Builder:
if _, err := w.WriteString("("); err != nil {
return err
}
if err := arg.WriteTo(w); err != nil {
return err
}
if _, err := w.WriteString(")"); err != nil {
return err
}
case string:
if arg == "" {
arg = "''"
}
if _, err := w.WriteString(fmt.Sprintf("%v", arg)); err != nil {
return err
}
default:
if _, err := w.WriteString("?"); err != nil {
return err
}
w.Append(arg)
}
return nil
}
type exprParams []Expr
func (exprs exprParams) ColNames() []string {
var cols = make([]string, 0, len(exprs))
for _, expr := range exprs {
cols = append(cols, expr.ColName)
}
return cols
}
func (exprs *exprParams) Add(name string, arg interface{}) {
*exprs = append(*exprs, Expr{name, arg})
}
func (exprs exprParams) IsColExist(colName string) bool {
for _, expr := range exprs {
if strings.EqualFold(schemas.CommonQuoter.Trim(expr.ColName), schemas.CommonQuoter.Trim(colName)) {
return true
}
}
return false
}
func (exprs exprParams) WriteArgs(w *builder.BytesWriter) error {
for i, expr := range exprs {
if err := expr.WriteArgs(w); err != nil {
return err
}
if i != len(exprs)-1 {
if _, err := w.WriteString(","); err != nil {
return err
}
}
}
return nil
}