Skip to content

Commit

Permalink
feat: Allow selecting columns with NULL values (#18)
Browse files Browse the repository at this point in the history
* feat: Allow selecting columns with NULL values
  • Loading branch information
pgollangi authored Nov 16, 2023
1 parent 0034fdd commit 2f30e21
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
8 changes: 6 additions & 2 deletions pkg/select/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ func readColumnValue(document *firestore.DocumentSnapshot, data *map[string]inte
var colData interface{}
colData = *data
for _, fPath := range fieldPaths {
colData = colData.(map[string]interface{})[fPath]
if colData == nil {
fieldVal, ok := colData.(map[string]interface{})[fPath]
if !ok {
return nil, fmt.Errorf(`unknown field "%s" in doc "%s"`, column.field, document.Ref.ID)
}
colData = fieldVal
if colData == nil {
break
}
}
val = colData
}
Expand Down
44 changes: 25 additions & 19 deletions pkg/select/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,76 +30,82 @@ type TestExpect struct {
const FirestoreEmulatorHost = "FIRESTORE_EMULATOR_HOST"

var selectTests = []TestExpect{
TestExpect{
{
query: "select * from users",
columns: []string{"id", "email", "username", "address", "name"},
length: "21",
},
TestExpect{
{
query: "select * from `users`",
columns: []string{"id", "email", "username", "address", "name"},
length: "21",
},
TestExpect{
{
query: "select id as uid, * from users",
columns: []string{"uid", "id", "email", "username", "address", "name"},
length: "21",
},
TestExpect{
{
query: "select *, username as uname from users",
columns: []string{"id", "email", "username", "address", "name", "uname"},
length: "21",
},
TestExpect{
{
query: "select id as uid, *, username as uname from users",
columns: []string{"uid", "id", "email", "username", "address", "name", "uname"},
length: "21",
},
TestExpect{
{
query: "select id, email, address from users",
columns: []string{"id", "email", "address"},
length: "21",
},
TestExpect{
{
query: "select id, email, address from users limit 5",
columns: []string{"id", "email", "address"},
length: "5",
},
TestExpect{
{
query: "select id from users where email='[email protected]'",
columns: []string{"id"},
length: "1",
records: [][]interface{}{[]interface{}{float64(20)}},
records: [][]interface{}{{float64(20)}},
},
TestExpect{
{
query: "select id from users order by id desc limit 1",
columns: []string{"id"},
length: "1",
records: [][]interface{}{[]interface{}{float64(21)}},
records: [][]interface{}{{float64(21)}},
},
TestExpect{
{
query: "select LENGTH(username) as uLen from users where id = 8",
columns: []string{"uLen"},
length: "1",
records: [][]interface{}{[]interface{}{float64(6)}},
records: [][]interface{}{{float64(6)}},
},
TestExpect{
{
query: "select id from users where `address.city` = 'Glendale' and name = 'Eleanora'",
columns: []string{"id"},
length: "1",
records: [][]interface{}{[]interface{}{float64(10)}},
records: [][]interface{}{{float64(10)}},
},
TestExpect{
{
query: "select id > 0 as has_id from users where `address.city` = 'Glendale' and name = 'Eleanora'",
columns: []string{"has_id"},
length: "1",
records: [][]interface{}{[]interface{}{true}},
records: [][]interface{}{{true}},
},
TestExpect{
{
query: "select __name__ from users where id = 1",
columns: []string{"__name__"},
length: "1",
records: [][]interface{}{[]interface{}{"1"}},
records: [][]interface{}{{"1"}},
},
{
query: "select id, email, username from users where id = 21",
columns: []string{"id", "email", "username"},
length: "1",
records: [][]interface{}{{float64(21), nil, "ckensleyk"}},
},
}

Expand Down
2 changes: 1 addition & 1 deletion test/data/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
"id": 21,
"name": "Doyle",
"username": "ckensleyk",
"email": "[email protected]",
"email": null,
"address": {
"city": "Fayetteville"
}
Expand Down

0 comments on commit 2f30e21

Please sign in to comment.