flavor.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2018 Huan Du. All rights reserved.
  2. // Licensed under the MIT license that can be found in the LICENSE file.
  3. package sqlbuilder
  4. import "fmt"
  5. // Supported flavors.
  6. const (
  7. invalidFlavor Flavor = iota
  8. MySQL
  9. PostgreSQL
  10. )
  11. var (
  12. // DefaultFlavor is the default flavor for all builders.
  13. DefaultFlavor = MySQL
  14. )
  15. // Flavor is the flag to control the format of compiled sql.
  16. type Flavor int
  17. // String returns the name of f.
  18. func (f Flavor) String() string {
  19. switch f {
  20. case MySQL:
  21. return "MySQL"
  22. case PostgreSQL:
  23. return "PostgreSQL"
  24. }
  25. return "<invalid>"
  26. }
  27. // NewSelectBuilder creates a new SELECT builder with flavor.
  28. func (f Flavor) NewSelectBuilder() *SelectBuilder {
  29. b := newSelectBuilder()
  30. b.SetFlavor(f)
  31. return b
  32. }
  33. // Quote adds quote for name to make sure the name can be used safely
  34. // as table name or field name.
  35. //
  36. // * For MySQL, use back quote (`) to quote name;
  37. // * For PostgreSQL, use double quote (") to quote name.
  38. func (f Flavor) Quote(name string) string {
  39. switch f {
  40. case MySQL:
  41. return fmt.Sprintf("`%v`", name)
  42. case PostgreSQL:
  43. return fmt.Sprintf(`"%v"`, name)
  44. }
  45. return name
  46. }