Skip to content

Commit

Permalink
feat sub query
Browse files Browse the repository at this point in the history
  • Loading branch information
baxiry committed Jul 12, 2024
1 parent b1300ea commit 1fa72a8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ and then open : `localhost:1111`
this docs is for libs dev, but it good for everyone :
[zara api](https://github.com/baxiry/zaradb/wiki/Zara-API)

> [!WARNING]
> [!NOTE]
> It is possible that some things will change within the API,
> The changes will be partial. Then it stabilizes completely in version 1.0.0
> The changes will be partial. Then it stabilizes completely in version 1.0.0.
> This documentation is intended for library developers, but is useful to anyone interested in documentary databases
#### Limitations:
Expand Down
19 changes: 7 additions & 12 deletions engine/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func match(filter gjson.Result, data string) (result bool, err error) {
if subQueryVal.Type == 3 { // 3:string,
//fmt.Println("here with: ", subQueryKey.String())

switch subQueryKey.Str { // .String()
switch subQueryKey.Str {

// comparition
case "$gt":
Expand Down Expand Up @@ -70,19 +70,19 @@ func match(filter gjson.Result, data string) (result bool, err error) {
result = false
return result

case "$st": // is it start with ?
case "$st": // start with ..
if !strings.HasPrefix(dataVal.Str, subQueryVal.Str) {
result = false
}
return result

case "$en": // is it end with
case "$en": // end with ..
if !strings.HasSuffix(dataVal.Str, subQueryVal.Str) {
result = false
}
return result

case "$c": // is it contains
case "$c": // contains ..
if !strings.Contains(dataVal.Str, subQueryVal.Str) {
result = false
}
Expand Down Expand Up @@ -135,7 +135,8 @@ func match(filter gjson.Result, data string) (result bool, err error) {

case "$in": // in array
for _, v := range subQueryVal.Array() {
if dataVal.Str == v.Str {
//fmt.Println("subQueryVal", subQueryVal)
if dataVal.Num == v.Num {
return result
}
}
Expand All @@ -144,20 +145,14 @@ func match(filter gjson.Result, data string) (result bool, err error) {

case "$nin": // not in
for _, v := range subQueryVal.Array() {
if dataVal.Str == v.Str {
if dataVal.Num == v.Num {
result = false
return result
}
}
return result

//case "$ins": // is it is sub query ?

default:
if subQueryKey.Str == "$ins" {
fmt.Println("queryVal : ", subQueryVal.Raw)
fmt.Println("ids: ", getIds(subQueryVal))
}

// {$and:[{name:{$eq:"adam"}},{name:{$eq:"jawad"}}]}
// {$or: [{name:{$eq:"adam"}}, {name:{$eq:"jawad"}}]}
Expand Down
38 changes: 27 additions & 11 deletions engine/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ func (db *DB) updateById(query gjson.Result) (result string) {
return id + " updated"
}

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

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

mtch := query.Get("match")
Expand All @@ -215,9 +215,17 @@ func getIds(query gjson.Result) string {

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 err.Error()
return "", fmt.Errorf("db.Query %s", err)
}
defer rows.Close()

Expand All @@ -238,12 +246,12 @@ func getIds(query gjson.Result) string {
rowid = ""
err := rows.Scan(&rowid, &record)
if err != nil {
return err.Error() // TODO standaring errors
return "", fmt.Errorf("row.Scan %s", err)
}

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

if ok {
Expand All @@ -253,9 +261,10 @@ func getIds(query gjson.Result) string {
}

if rowids == "" {
return ""
return "", fmt.Errorf("str zero val rowids")
}
return rowids[:len(rowids)-1]

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

// Find finds any obs match creteria.
Expand All @@ -269,10 +278,6 @@ func (db *DB) findMany(query gjson.Result) (res string) {

mtch := query.Get("match")

if mtch.Str == "" {

}

skip := query.Get("skip").Int()
limit := query.Get("limit").Int()
if limit == 0 {
Expand All @@ -281,6 +286,13 @@ func (db *DB) findMany(query gjson.Result) (res string) {

stmt := `select record from ` + coll

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

rows, err := db.db.Query(stmt)
if err != nil {
return err.Error()
Expand All @@ -289,7 +301,9 @@ func (db *DB) findMany(query gjson.Result) (res string) {

record := ""
listData := make([]string, 0)

for rows.Next() {
fmt.Println("next")
if limit == 0 {
break
}
Expand All @@ -309,6 +323,8 @@ func (db *DB) findMany(query gjson.Result) (res string) {
return err.Error()
}

fmt.Println("record: ", record)

if ok {
listData = append(listData, record)
limit--
Expand Down

0 comments on commit 1fa72a8

Please sign in to comment.