From f8694de884ea492cfb6ef0eec7850afea71f80ea Mon Sep 17 00:00:00 2001 From: baxiry Date: Fri, 26 Jul 2024 20:52:00 +0300 Subject: [PATCH] devoping sub queries --- engine/filter.go | 96 +++++++++++++++++++++++++++-------------------- engine/queries.go | 20 +++++----- 2 files changed, 65 insertions(+), 51 deletions(-) diff --git a/engine/filter.go b/engine/filter.go index b5e5487..2f857b4 100644 --- a/engine/filter.go +++ b/engine/filter.go @@ -10,8 +10,8 @@ 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, ids ...int64) (result bool, err error) { - // TODO should return syntax error if op unknown +func match(filter gjson.Result, data string, subs gjson.Result) (result bool, err error) { + // TODO should I return syntax error if op unknown ? result = true @@ -21,95 +21,98 @@ func match(filter gjson.Result, data string, ids ...int64) (result bool, err err if queryVal.Type == 5 { // 5:json // {name:{$eq:"adam"}, age:{$gt: 18}} - queryVal.ForEach(func(sQueryKey, sQueryVal gjson.Result) bool { if sQueryVal.Type == 3 { // 3:string, // from : {$eq:"adam"} , sQueryKey is $eq, sQueryVal is "adam" switch sQueryKey.Str { + // TODO check if checking sQueryKey is not []string is reduce cpu consome ? // compare sQueryKey - case "$gt": - if !(dataVal.Str > sQueryVal.Str) { + case "$st": // start with .. + if !strings.HasPrefix(dataVal.Str, sQueryVal.Str) { result = false } return result - case "$lt": - if !(dataVal.Str < sQueryVal.Str) { + case "$en": // end with .. + if !strings.HasSuffix(dataVal.Str, sQueryVal.Str) { result = false } return result - case "$gte": - if !(dataVal.Str >= sQueryVal.Str) { + case "$c": // contains .. + if !strings.Contains(dataVal.Str, sQueryVal.Str) { result = false } return result - case "$lte": - if !(dataVal.Str <= sQueryVal.Str) { + case "$nst": // start with .. + if strings.HasPrefix(dataVal.Str, sQueryVal.Str) { result = false } return result - case "$eq": - if dataVal.Str != sQueryVal.Str { + case "$nen": // end with .. + if strings.HasSuffix(dataVal.Str, sQueryVal.Str) { result = false } return result - case "$ne": - if dataVal.Str == sQueryVal.Str { + + case "$nc": // contains .. + if strings.Contains(dataVal.Str, sQueryVal.Str) { result = false } return result - case "$st": // start with .. - if !strings.HasPrefix(dataVal.Str, sQueryVal.Str) { + case "$gt": + if !(dataVal.Str > sQueryVal.Str) { result = false } return result - case "$en": // end with .. - if !strings.HasSuffix(dataVal.Str, sQueryVal.Str) { + case "$lt": + if !(dataVal.Str < sQueryVal.Str) { result = false } return result - case "$c": // contains .. - if !strings.Contains(dataVal.Str, sQueryVal.Str) { + case "$gte": + if !(dataVal.Str >= sQueryVal.Str) { result = false } return result - case "$nst": // start with .. - if strings.HasPrefix(dataVal.Str, sQueryVal.Str) { + case "$lte": + if !(dataVal.Str <= sQueryVal.Str) { result = false } return result - case "$nen": // end with .. - if strings.HasSuffix(dataVal.Str, sQueryVal.Str) { + case "$eq": + if dataVal.Str != sQueryVal.Str { result = false } return result - - case "$nc": // contains .. - if strings.Contains(dataVal.Str, sQueryVal.Str) { + case "$ne": + if dataVal.Str == sQueryVal.Str { result = false } return result + case "$sub": + fmt.Println() + fmt.Println("name sub her is : ", sQueryVal.Str) + fmt.Println("subs.Get: ", subs.Get(sQueryVal.Str)) + return result default: - err = fmt.Errorf("unknown %s operation", sQueryKey.Str) //fmt.Println("..wher here", sQueryKey.Value(), sQueryKey.Type) result = false return result } } - // if sQueryVal is number switch sQueryKey.Str { @@ -200,7 +203,7 @@ func match(filter gjson.Result, data string, ids ...int64) (result bool, err err if queryKey.Str == "$and" { for _, v := range queryVal.Array() { - res, _ := match(v, data) + res, _ := match(v, data, subs) if !res { result = false return result @@ -214,7 +217,7 @@ func match(filter gjson.Result, data string, ids ...int64) (result bool, err err for _, v := range queryVal.Array() { - res, _ := match(v, data) + res, _ := match(v, data, subs) if res { return result } @@ -223,13 +226,12 @@ func match(filter gjson.Result, data string, ids ...int64) (result bool, err err return result } - err = fmt.Errorf("unknown %s operation", sQueryKey.Str) result = false return result } }) - match(queryVal, queryVal.Str) + match(queryVal, queryVal.Str, subs) return result } @@ -251,7 +253,20 @@ func match(filter gjson.Result, data string, ids ...int64) (result bool, err err return result, err } -func getIds(query gjson.Result) (ids []int64) { +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 getsub(query gjson.Result) (ids []int64) { coll := query.Get("collection").Str if coll == "" { @@ -272,11 +287,10 @@ func getIds(query gjson.Result) (ids []int64) { stmt := `select rowid, record from ` + coll - sub := query.Get("sQuery") + subs := query.Get("subs") - if sub.Raw != "" { - fmt.Println("sub.Row is : ", sub.Raw) - ids = getIds(sub) + if subs.Raw != "" { + fmt.Println("sub.Row is : ", subs.Raw) } rows, err := db.db.Query(stmt) @@ -297,7 +311,7 @@ func getIds(query gjson.Result) (ids []int64) { rowid = 0 _ = rows.Scan(&rowid, &record) - ok, err := match(mtch, record) + ok, err := match(mtch, record, subs) if err != nil { fmt.Printf("match %s\n", err) return nil @@ -312,7 +326,7 @@ func getIds(query gjson.Result) (ids []int64) { limit-- } } - fmt.Println("\n", ids) + //fmt.Println("\n", ids) return ids } diff --git a/engine/queries.go b/engine/queries.go index 21b98c2..a807d58 100644 --- a/engine/queries.go +++ b/engine/queries.go @@ -38,7 +38,7 @@ func (db *DB) deleteMany(query gjson.Result) string { return err.Error() // TODO standaring errors } - ok, err := match(mtch, record) + ok, err := match(mtch, record, subs) if err != nil { return err.Error() } @@ -91,7 +91,7 @@ func (db *DB) updateMany(query gjson.Result) (result string) { return err.Error() // TODO standaring errors } - ok, err := match(mtch, record) + ok, err := match(mtch, record, subs) if err != nil { return err.Error() } @@ -145,7 +145,7 @@ func (db *DB) updateOne(query gjson.Result) (result string) { return err.Error() // TODO standaring errors } - ok, err := match(mtch, record) + ok, err := match(mtch, record, subs) if err != nil { return err.Error() } @@ -213,10 +213,9 @@ func (db *DB) findMany(query gjson.Result) (res string) { stmt := `select record from ` + coll - sub := query.Get("subQuery") - var ids []int64 - if sub.Raw != "" { - ids = getIds(sub) + subs = query.Get("subs") + if subs.Raw != "" { + //ids = getsub(sub) //stmt += ` where rowid in (` + ids + `);` //fmt.Println(stmt) } @@ -246,7 +245,7 @@ func (db *DB) findMany(query gjson.Result) (res string) { return err.Error() // TODO standaring errors } - ok, err := match(mtch, record, ids...) + ok, err := match(mtch, record, subs) if err != nil { return err.Error() } @@ -288,6 +287,7 @@ 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 ? @@ -311,7 +311,7 @@ func (db *DB) findOne(query gjson.Result) (res string) { if err != nil { return err.Error() } - b, err := match(mtch, record) + b, err := match(mtch, record, subs) if err != nil { return err.Error() } @@ -347,7 +347,7 @@ func (db *DB) deleteOne(query gjson.Result) string { fmt.Println(err.Error()) } - b, err := match(mtch, record) + b, err := match(mtch, record, subs) if err != nil { return err.Error() }