arangodb / go-driver

The official ArangoDB go driver.
Apache License 2.0
339 stars 78 forks source link

AQL Queries not working with bindVars. #228

Closed its-smesh closed 1 year ago

its-smesh commented 4 years ago

Environment:

Issue: driver.Database.Query() with bindVars is always empty. Verified same AQL query via ArangoDB, and it's successfully returned 2 documents.

Code Snipper:

` func (s *Service) searchProduct(offset int, count int) (interface{},int64, error){ //FOR doc IN explore SORT BM25(doc) DESC LIMIT 0,10 RETURN doc queryBuilder := query.NewForQuery(exploreViewName, "doc") qs := queryBuilder.SortBM25(true). LIMIT(offset, count). Return().String() s.logFactory.Fields().Done().Info(qs)

ctx := context.Background()

// Open database
db, err := s.dbClient.Database(ctx, databaseName)
if err != nil {
    return nil, 0, errors.New(err, fmt.Sprintf("failed to open %s database", databaseName))
}

bindVars := map[string]interface{}{}

cursor, err := db.Query(ctx, qs, bindVars)
if err != nil {
    return nil, 0, errors.New(err, fmt.Sprintf("failed to query [%s] on %s database", qs, databaseName))
}

defer cursor.Close()
s.logFactory.Fields().Done().Info(fmt.Sprintf("%d",cursor.Count()))
return bindVars, cursor.Count(),nil

} `

  1. bindVars is empty.
  2. cursor.Count() is 0
  3. However while debugging, I found cursor.cursorData.Result has 2 items in the list.

This is a blocker, given we can't execute a simple AQL query.

Fruchtgummi commented 4 years ago

bindVars := map[string]interface{}{}

you have nothing tied down

bindVars["Key"]= "12345" ...

or

bindVars = map[string]interface{}{
        "Lang":       mylang,
        "Company": "12345",,
        "Key":        key,
    }

You need as example fullcount:

ctx := driver.WithQueryFullCount(ctx := context.Background())

you get the value with cursor.Statistics().FullCount()

irridia commented 4 years ago

bindVars does not contain the query result. As its name suggests, you specify @foo bind variables by passing in the map to Query.

You need to iterate over cursor.ReadDocument():

defer cursor.Close()
var results []interface{}   // Use whatever type you want.
for cursor.HasMore() {
    var result interface{}
    if _, err := cursor.ReadDocument(ctx, &result); err != nil {
        return err
    }
    results = append(results, result)
}
return results, nil