Skip to content

Commit

Permalink
fix skip logic bug
Browse files Browse the repository at this point in the history
  • Loading branch information
baxiry committed Jul 14, 2024
1 parent 0d36de5 commit c7dd0ac
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 73 deletions.
73 changes: 73 additions & 0 deletions engine/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,79 @@ import (
"github.com/tidwall/gjson"
)

func getIds(query gjson.Result) (string, error) {

coll := query.Get("collection").Str
if coll == "" {
return "", fmt.Errorf("no collection")
}

mtch := query.Get("match")

if mtch.String() == "" {
fmt.Println("match.Str is empty")
}

skip := query.Get("skip").Int()
limit := query.Get("limit").Int()
if limit == 0 {
limit = 100
}

stmt := `select rowid, record from ` + coll

sub := query.Get("subQuery")
if sub.Raw != "" {
fmt.Println("sub.Row is : ", sub.Raw)
ids, _ := getIds(sub)
stmt += ` where rowid in (` + ids + `);`
fmt.Println(stmt)
}

rows, err := db.db.Query(stmt)
if err != nil {
return "", fmt.Errorf("db.Query %s", err)
}
defer rows.Close()

record := ""
rowids := ""
rowid := ""

for rows.Next() {
if limit == 0 {
break
}

record = ""
rowid = ""
err := rows.Scan(&rowid, &record)
if err != nil {
return "", fmt.Errorf("row.Scan %s", err)
}

ok, err := match(mtch, record)
if err != nil {
return "", fmt.Errorf("match %s", err)
}

if ok {
if skip != 0 {
skip--
continue
}
rowids += rowid + ","
limit--
}
}

if rowids == "" {
return "", fmt.Errorf("zero value")
}

return rowids[:len(rowids)-1], nil
}

// gjson.Type :
// json:5, array:5, int:2, string:3

Expand Down
73 changes: 0 additions & 73 deletions engine/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,79 +194,6 @@ func (db *DB) updateById(query gjson.Result) (result string) {
return id + " updated"
}

func getIds(query gjson.Result) (string, error) {

coll := query.Get("collection").Str
if coll == "" {
return "", fmt.Errorf("no collection")
}

mtch := query.Get("match")

if mtch.String() == "" {
fmt.Println("match.Str is empty")
}

skip := query.Get("skip").Int()
limit := query.Get("limit").Int()
if limit == 0 {
limit = 100
}

stmt := `select rowid, record from ` + coll

sub := query.Get("subQuery")
if sub.Raw != "" {
fmt.Println("sub.Row is : ", sub.Raw)
ids, _ := getIds(sub)
stmt += ` where rowid in (` + ids + `);`
fmt.Println(stmt)
}

rows, err := db.db.Query(stmt)
if err != nil {
return "", fmt.Errorf("db.Query %s", err)
}
defer rows.Close()

record := ""
rowids := ""
rowid := ""

for rows.Next() {
if limit == 0 {
break
}
if skip != 0 {
skip--
continue
}

record = ""
rowid = ""
err := rows.Scan(&rowid, &record)
if err != nil {
return "", fmt.Errorf("row.Scan %s", err)
}

ok, err := match(mtch, record)
if err != nil {
return "", fmt.Errorf("match %s", err)
}

if ok {
rowids += rowid + ","
limit--
}
}

if rowids == "" {
return "", fmt.Errorf("zero value")
}

return rowids[:len(rowids)-1], nil
}

// Find finds any obs match creteria.
func (db *DB) findMany(query gjson.Result) (res string) {

Expand Down

0 comments on commit c7dd0ac

Please sign in to comment.