Skip to content

Commit

Permalink
devoping sub queries
Browse files Browse the repository at this point in the history
  • Loading branch information
baxiry committed Jul 26, 2024
1 parent fe5845c commit f8694de
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 51 deletions.
96 changes: 55 additions & 41 deletions engine/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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 == "" {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -312,7 +326,7 @@ func getIds(query gjson.Result) (ids []int64) {
limit--
}
}
fmt.Println("\n", ids)
//fmt.Println("\n", ids)

return ids
}
Expand Down
20 changes: 10 additions & 10 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)
ok, err := match(mtch, record, subs)
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)
ok, err := match(mtch, record, subs)
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)
ok, err := match(mtch, record, subs)
if err != nil {
return err.Error()
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 ?

Expand All @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit f8694de

Please sign in to comment.