Skip to content

Commit

Permalink
trying new ways for sub queries
Browse files Browse the repository at this point in the history
  • Loading branch information
baxiry committed Jul 28, 2024
1 parent f8694de commit fbe547a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
37 changes: 21 additions & 16 deletions engine/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// gjson.Type => json:5, array:5, int:2, string:3

// match verifies that data matches the conditions
func match(filter gjson.Result, data string, subs gjson.Result) (result bool, err error) {
func match(filter gjson.Result, data string) (result bool, err error) {
// TODO should I return syntax error if op unknown ?

result = true
Expand Down Expand Up @@ -102,8 +102,8 @@ func match(filter gjson.Result, data string, subs gjson.Result) (result bool, er
return result
case "$sub":
fmt.Println()
fmt.Println("name sub her is : ", sQueryVal.Str)
fmt.Println("subs.Get: ", subs.Get(sQueryVal.Str))
fmt.Println("in str switch. Raw: ", sQueryVal.Raw)
fmt.Println("sub: ", sQueryVal.Str)
return result

default:
Expand All @@ -116,6 +116,14 @@ func match(filter gjson.Result, data string, subs gjson.Result) (result bool, er
// if sQueryVal is number
switch sQueryKey.Str {

case "$sub":
//if Subs[]
fmt.Println()
fmt.Println("op: ", sQueryKey.Str)

getSubs(dataVal, sQueryVal)
return result

case "$gt":
if !(dataVal.Num > sQueryVal.Num) {
result = false
Expand Down Expand Up @@ -203,7 +211,7 @@ func match(filter gjson.Result, data string, subs gjson.Result) (result bool, er
if queryKey.Str == "$and" {

for _, v := range queryVal.Array() {
res, _ := match(v, data, subs)
res, _ := match(v, data)
if !res {
result = false
return result
Expand All @@ -217,7 +225,7 @@ func match(filter gjson.Result, data string, subs gjson.Result) (result bool, er

for _, v := range queryVal.Array() {

res, _ := match(v, data, subs)
res, _ := match(v, data)
if res {
return result
}
Expand All @@ -231,7 +239,7 @@ func match(filter gjson.Result, data string, subs gjson.Result) (result bool, er
}
})

match(queryVal, queryVal.Str, subs)
match(queryVal, queryVal.Str)
return result
}

Expand All @@ -253,17 +261,14 @@ func match(filter gjson.Result, data string, subs gjson.Result) (result bool, er
return result, err
}

var subs = gjson.Result{}
//var subs = make(map[string]interface{})
//var subs = gjson.Result{}

// fsubs is field of subQueries
func getSubs(fsubs gjson.Result) gjson.Result {

subs := fsubs.Get("subs")
if subs.Raw == "" {
fmt.Println("no subs")
}

return subs
func getSubs(dataVal, subQuery gjson.Result) (bool, error) {
fmt.Println("data Val : ", dataVal.Raw)
fmt.Println("sub query : ", subQuery.Raw)
return true, nil
}

func getsub(query gjson.Result) (ids []int64) {
Expand Down Expand Up @@ -311,7 +316,7 @@ func getsub(query gjson.Result) (ids []int64) {
rowid = 0
_ = rows.Scan(&rowid, &record)

ok, err := match(mtch, record, subs)
ok, err := match(mtch, record)
if err != nil {
fmt.Printf("match %s\n", err)
return nil
Expand Down
20 changes: 6 additions & 14 deletions engine/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (db *DB) deleteMany(query gjson.Result) string {
return err.Error() // TODO standaring errors
}

ok, err := match(mtch, record, subs)
ok, err := match(mtch, record)
if err != nil {
return err.Error()
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (db *DB) updateMany(query gjson.Result) (result string) {
return err.Error() // TODO standaring errors
}

ok, err := match(mtch, record, subs)
ok, err := match(mtch, record)
if err != nil {
return err.Error()
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (db *DB) updateOne(query gjson.Result) (result string) {
return err.Error() // TODO standaring errors
}

ok, err := match(mtch, record, subs)
ok, err := match(mtch, record)
if err != nil {
return err.Error()
}
Expand Down Expand Up @@ -213,13 +213,6 @@ func (db *DB) findMany(query gjson.Result) (res string) {

stmt := `select record from ` + coll

subs = query.Get("subs")
if subs.Raw != "" {
//ids = getsub(sub)
//stmt += ` where rowid in (` + ids + `);`
//fmt.Println(stmt)
}

rows, err := db.db.Query(stmt)
if err != nil {
return err.Error()
Expand All @@ -245,7 +238,7 @@ func (db *DB) findMany(query gjson.Result) (res string) {
return err.Error() // TODO standaring errors
}

ok, err := match(mtch, record, subs)
ok, err := match(mtch, record)
if err != nil {
return err.Error()
}
Expand Down Expand Up @@ -287,7 +280,6 @@ func (db *DB) findMany(query gjson.Result) (res string) {
func (db *DB) findOne(query gjson.Result) (res string) {
coll := query.Get("collection").Str
skip := query.Get("skip").Int()
//subs := query.Get("subs")

// TODO are skyp useful here ?

Expand All @@ -311,7 +303,7 @@ func (db *DB) findOne(query gjson.Result) (res string) {
if err != nil {
return err.Error()
}
b, err := match(mtch, record, subs)
b, err := match(mtch, record)
if err != nil {
return err.Error()
}
Expand Down Expand Up @@ -347,7 +339,7 @@ func (db *DB) deleteOne(query gjson.Result) string {
fmt.Println(err.Error())
}

b, err := match(mtch, record, subs)
b, err := match(mtch, record)
if err != nil {
return err.Error()
}
Expand Down

0 comments on commit fbe547a

Please sign in to comment.