sql.go 758 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package sqlutil
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. )
  7. var escapeArray = []string{"\\", "%"}
  8. func LikeFormat(name string) string {
  9. return "%" + escape(name) + "%"
  10. }
  11. func LeftLikeFormat(name string) string {
  12. return "%" + escape(name)
  13. }
  14. func RightLikeFormat(name string) string {
  15. return escape(name) + "%"
  16. }
  17. func escape(name string) string {
  18. for i := range escapeArray {
  19. name = strings.ReplaceAll(name, escapeArray[i], "\\"+escapeArray[i])
  20. }
  21. return name
  22. }
  23. func OrderByFieldInt(column string, arg ...int) string {
  24. of := bytes.NewBufferString("FIELD(" + column + ", ")
  25. for i := range arg {
  26. if i < len(arg)-1 {
  27. of.WriteString(fmt.Sprintf("%v, ", arg[i]))
  28. } else {
  29. of.WriteString(fmt.Sprintf("%v) ", arg[i]))
  30. }
  31. }
  32. return of.String()
  33. }