Skip to content

Commit

Permalink
feat: Ability to select DocumentIds (#16)
Browse files Browse the repository at this point in the history
* feat: Ability to select DocumentIds

closes #14
  • Loading branch information
pgollangi authored Nov 16, 2023
1 parent 006c8fc commit 6a84826
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ select * from users order by 'address.city' desc limit 10
select * from `users` where id > 50
select id, LENGTH(contacts) as total_contacts from `users`
select id, (age > 100) as centenarian as total_contacts from `users`
select __name__ from users // to select document id
```

`FireQL` depend on [govaluate](https://github.com/Knetic/govaluate) to evaluate expressions in `SELECT`. See list of possible expressions and operators [here](https://github.com/Knetic/govaluate/blob/master/MANUAL.md#operators).
Expand Down
24 changes: 14 additions & 10 deletions pkg/select/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (sel *SelectStatement) Execute() (*util.QueryResult, error) {

func (sel *SelectStatement) readResults(docs *firestore.DocumentIterator, selectedColumns []*selectColumn) (*util.QueryResult, error) {
document, err := docs.Next()
if err == iterator.Done {
if errors.Is(err, iterator.Done) {
var columns []string
for _, column := range selectedColumns {
columns = append(columns, column.alias)
Expand Down Expand Up @@ -147,7 +147,7 @@ func (sel *SelectStatement) readResults(docs *firestore.DocumentIterator, select
row[idx] = val
}
document, err = docs.Next()
if err == iterator.Done {
if errors.Is(err, iterator.Done) {
break
}
}
Expand All @@ -159,16 +159,20 @@ func readColumnValue(document *firestore.DocumentSnapshot, data *map[string]inte
var val interface{}
switch column.colType {
case Field:
fieldPaths := strings.Split(column.field, ".")
var colData interface{}
colData = *data
for _, fPath := range fieldPaths {
colData = colData.(map[string]interface{})[fPath]
if colData == nil {
return nil, fmt.Errorf(`unknown field "%s" in doc "%s"`, column.field, document.Ref.ID)
if column.field == firestore.DocumentID {
val = document.Ref.ID
} else {
fieldPaths := strings.Split(column.field, ".")
var colData interface{}
colData = *data
for _, fPath := range fieldPaths {
colData = colData.(map[string]interface{})[fPath]
if colData == nil {
return nil, fmt.Errorf(`unknown field "%s" in doc "%s"`, column.field, document.Ref.ID)
}
}
val = colData
}
val = colData
break
case Function:
params := make([]interface{}, len(column.params))
Expand Down
6 changes: 6 additions & 0 deletions pkg/select/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ var selectTests = []TestExpect{
length: "1",
records: [][]interface{}{[]interface{}{true}},
},
TestExpect{
query: "select __name__ from users where id = 1",
columns: []string{"__name__"},
length: "1",
records: [][]interface{}{[]interface{}{"1"}},
},
}

func newFirestoreTestClient(ctx context.Context) *firestore.Client {
Expand Down

0 comments on commit 6a84826

Please sign in to comment.