client.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package dbconnect
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "strings"
  8. "time"
  9. "unicode"
  10. "unicode/utf8"
  11. )
  12. // Client is an interface to talk to any database.
  13. //
  14. // Currently, the only implementation is SQLClient, but its structure
  15. // should be designed to handle a MongoClient or RedisClient in the future.
  16. type Client interface {
  17. Ping(context.Context) error
  18. Submit(context.Context, *Command) (interface{}, error)
  19. }
  20. // NewClient creates a database client based on its URL scheme.
  21. func NewClient(ctx context.Context, originURL *url.URL) (Client, error) {
  22. return NewSQLClient(ctx, originURL)
  23. }
  24. // Command is a standard, non-vendor format for submitting database commands.
  25. //
  26. // When determining the scope of this struct, refer to the following litmus test:
  27. // Could this (roughly) conform to SQL, Document-based, and Key-value command formats?
  28. type Command struct {
  29. Statement string `json:"statement"`
  30. Arguments Arguments `json:"arguments,omitempty"`
  31. Mode string `json:"mode,omitempty"`
  32. Isolation string `json:"isolation,omitempty"`
  33. Timeout time.Duration `json:"timeout,omitempty"`
  34. }
  35. // Validate enforces the contract of Command: non empty statement (both in length and logic),
  36. // lowercase mode and isolation, non-zero timeout, and valid Arguments.
  37. func (cmd *Command) Validate() error {
  38. if cmd.Statement == "" {
  39. return fmt.Errorf("cannot provide an empty statement")
  40. }
  41. if strings.Map(func(char rune) rune {
  42. if char == ';' || unicode.IsSpace(char) {
  43. return -1
  44. }
  45. return char
  46. }, cmd.Statement) == "" {
  47. return fmt.Errorf("cannot provide a statement with no logic: '%s'", cmd.Statement)
  48. }
  49. cmd.Mode = strings.ToLower(cmd.Mode)
  50. cmd.Isolation = strings.ToLower(cmd.Isolation)
  51. if cmd.Timeout.Nanoseconds() <= 0 {
  52. cmd.Timeout = 24 * time.Hour
  53. }
  54. return cmd.Arguments.Validate()
  55. }
  56. // UnmarshalJSON converts a byte representation of JSON into a Command, which is also validated.
  57. func (cmd *Command) UnmarshalJSON(data []byte) error {
  58. // Alias is required to avoid infinite recursion from the default UnmarshalJSON.
  59. type Alias Command
  60. alias := &struct {
  61. *Alias
  62. }{
  63. Alias: (*Alias)(cmd),
  64. }
  65. err := json.Unmarshal(data, &alias)
  66. if err == nil {
  67. err = cmd.Validate()
  68. }
  69. return err
  70. }
  71. // Arguments is a wrapper for either map-based or array-based Command arguments.
  72. //
  73. // Each field is mutually-exclusive and some Client implementations may not
  74. // support both fields (eg. MySQL does not accept named arguments).
  75. type Arguments struct {
  76. Named map[string]interface{}
  77. Positional []interface{}
  78. }
  79. // Validate enforces the contract of Arguments: non nil, mutually exclusive, and no empty or reserved keys.
  80. func (args *Arguments) Validate() error {
  81. if args.Named == nil {
  82. args.Named = map[string]interface{}{}
  83. }
  84. if args.Positional == nil {
  85. args.Positional = []interface{}{}
  86. }
  87. if len(args.Named) > 0 && len(args.Positional) > 0 {
  88. return fmt.Errorf("both named and positional arguments cannot be specified: %+v and %+v", args.Named, args.Positional)
  89. }
  90. for key := range args.Named {
  91. if key == "" {
  92. return fmt.Errorf("named arguments cannot contain an empty key: %+v", args.Named)
  93. }
  94. if !utf8.ValidString(key) {
  95. return fmt.Errorf("named argument does not conform to UTF-8 encoding: %s", key)
  96. }
  97. if strings.HasPrefix(key, "_") {
  98. return fmt.Errorf("named argument cannot start with a reserved keyword '_': %s", key)
  99. }
  100. if unicode.IsNumber([]rune(key)[0]) {
  101. return fmt.Errorf("named argument cannot start with a number: %s", key)
  102. }
  103. }
  104. return nil
  105. }
  106. // UnmarshalJSON converts a byte representation of JSON into Arguments, which is also validated.
  107. func (args *Arguments) UnmarshalJSON(data []byte) error {
  108. var obj interface{}
  109. err := json.Unmarshal(data, &obj)
  110. if err != nil {
  111. return err
  112. }
  113. named, ok := obj.(map[string]interface{})
  114. if ok {
  115. args.Named = named
  116. } else {
  117. positional, ok := obj.([]interface{})
  118. if ok {
  119. args.Positional = positional
  120. } else {
  121. return fmt.Errorf("arguments must either be an object {\"0\":\"val\"} or an array [\"val\"]: %s", string(data))
  122. }
  123. }
  124. return args.Validate()
  125. }