eql.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package rumble
  11. import (
  12. "fmt"
  13. "devt.de/krotik/common/defs/rumble"
  14. "devt.de/krotik/eliasdb/api"
  15. "devt.de/krotik/eliasdb/eql"
  16. )
  17. // Function: query
  18. // ===============
  19. /*
  20. QueryFunc runs an EQL query.
  21. */
  22. type QueryFunc struct {
  23. }
  24. /*
  25. Name returns the name of the function.
  26. */
  27. func (f *QueryFunc) Name() string {
  28. return "db.query"
  29. }
  30. /*
  31. Validate is called for parameter validation and to reset the function state.
  32. */
  33. func (f *QueryFunc) Validate(argsNum int, rt rumble.Runtime) rumble.RuntimeError {
  34. var err rumble.RuntimeError
  35. if argsNum != 2 {
  36. err = rt.NewRuntimeError(rumble.ErrInvalidConstruct,
  37. "Function query requires 2 parameters: partition and a query string")
  38. }
  39. return err
  40. }
  41. /*
  42. Execute executes the rumble function.
  43. */
  44. func (f *QueryFunc) Execute(argsVal []interface{}, vars rumble.Variables,
  45. rt rumble.Runtime) (interface{}, rumble.RuntimeError) {
  46. part := fmt.Sprint(argsVal[0])
  47. query := fmt.Sprint(argsVal[1])
  48. res, err := eql.RunQuery("db.query", part, query, api.GM)
  49. if err != nil {
  50. // Wrap error message in RuntimeError
  51. return nil, rt.NewRuntimeError(rumble.ErrInvalidState,
  52. fmt.Sprintf(err.Error()))
  53. }
  54. // Convert result to rumble data structure
  55. labels := res.Header().Labels()
  56. cols := make([]interface{}, len(labels))
  57. for i, v := range labels {
  58. cols[i] = v
  59. }
  60. rrows := res.Rows()
  61. rows := make([]interface{}, len(rrows))
  62. for i, v := range rrows {
  63. rows[i] = v
  64. }
  65. return map[interface{}]interface{}{
  66. "cols": cols,
  67. "rows": rows,
  68. }, err
  69. }