expr.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2016 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. import "fmt"
  6. // Expression represetns a SQL express with arguments
  7. type Expression struct {
  8. sql string
  9. args []interface{}
  10. }
  11. var _ Cond = &Expression{}
  12. // Expr generate customerize SQL
  13. func Expr(sql string, args ...interface{}) Cond {
  14. return &Expression{sql, args}
  15. }
  16. func (expr *Expression) Content() string {
  17. return expr.sql
  18. }
  19. func (expr *Expression) Args() []interface{} {
  20. return expr.args
  21. }
  22. // OpWriteTo implements UpdateCond interface
  23. func (expr *Expression) OpWriteTo(op string, w Writer) error {
  24. return expr.WriteTo(w)
  25. }
  26. // WriteTo implements Cond interface
  27. func (expr *Expression) WriteTo(w Writer) error {
  28. if _, err := fmt.Fprint(w, expr.sql); err != nil {
  29. return err
  30. }
  31. w.Append(expr.args...)
  32. return nil
  33. }
  34. // And implements Cond interface
  35. func (expr *Expression) And(conds ...Cond) Cond {
  36. return And(expr, And(conds...))
  37. }
  38. // Or implements Cond interface
  39. func (expr *Expression) Or(conds ...Cond) Cond {
  40. return Or(expr, Or(conds...))
  41. }
  42. // IsValid implements Cond interface
  43. func (expr *Expression) IsValid() bool {
  44. return len(expr.sql) > 0
  45. }