eql.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 dbfunc
  11. import (
  12. "fmt"
  13. "devt.de/krotik/ecal/parser"
  14. "devt.de/krotik/eliasdb/eql"
  15. "devt.de/krotik/eliasdb/graph"
  16. )
  17. /*
  18. QueryFunc runs an EQL query.
  19. */
  20. type QueryFunc struct {
  21. GM *graph.Manager
  22. }
  23. /*
  24. Run executes the ECAL function.
  25. */
  26. func (f *QueryFunc) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  27. var err error
  28. var cols, rows []interface{}
  29. if arglen := len(args); arglen != 2 {
  30. err = fmt.Errorf("Function requires 2 parameters: partition and a query string")
  31. }
  32. if err == nil {
  33. var res eql.SearchResult
  34. part := fmt.Sprint(args[0])
  35. query := fmt.Sprint(args[1])
  36. res, err = eql.RunQuery("db.query", part, query, f.GM)
  37. if err != nil {
  38. return nil, err
  39. }
  40. // Convert result to rumble data structure
  41. labels := res.Header().Labels()
  42. cols = make([]interface{}, len(labels))
  43. for i, v := range labels {
  44. cols[i] = v
  45. }
  46. rrows := res.Rows()
  47. rows = make([]interface{}, len(rrows))
  48. for i, v := range rrows {
  49. rows[i] = v
  50. }
  51. }
  52. return map[interface{}]interface{}{
  53. "cols": cols,
  54. "rows": rows,
  55. }, err
  56. }
  57. /*
  58. DocString returns a descriptive string.
  59. */
  60. func (f *QueryFunc) DocString() (string, error) {
  61. return "Run an EQL query.", nil
  62. }