Skip to content

Commit

Permalink
reat order by
Browse files Browse the repository at this point in the history
  • Loading branch information
baxiry committed Jul 5, 2024
1 parent c9abecc commit 4a3f2d7
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 27 deletions.
4 changes: 3 additions & 1 deletion engine/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

// ok
func HandleQueries(query string) string {

parsedQuery := gjson.Parse(query)
switch parsedQuery.Get("a").String() { // action

switch parsedQuery.Get("action").String() { // action

// database actions
case "findOne":
Expand Down
89 changes: 84 additions & 5 deletions engine/aggregate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package engine

import (
"fmt"
"strings"

"github.com/tidwall/gjson"
Expand All @@ -9,11 +10,6 @@ import (

const siparator = "_:_"

func orderBy(param string, data []gjson.Result) []gjson.Result {

return data
}

// reKey renames json feild
func reKey(oldkey, newkey, json string) string {

Expand Down Expand Up @@ -84,3 +80,86 @@ func reFields(data []string, fields gjson.Result) []string {

return data
}

func orderBy(param string, data []string) (list []string) {

objects := []gjson.Result{}
for _, v := range data {
objects = append(objects, gjson.Parse(v))
}
// sort here

// check type
typ := objects[0].Get(param).Type
fmt.Println("type is ", typ)

if typ == 2 {
list = sortInt(param, objects)
}
if typ == 3 {
list = sortString(param, objects)
}

return list
}

func sortInt(key string, list []gjson.Result) []string {
max := len(list)
var tmp gjson.Result

element := list[0]
for max != 0 {
for i := 0; i < max; i++ {
if element.Get(key).Int() < list[i].Get(key).Int() {
tmp = list[i]
list[i] = element
element = tmp
}

if i == max-1 {
tmp = list[i]
list[i] = element
element = tmp
}
}
max--
}

list[0] = element
res := []string{}
for i := 0; i < len(list); i++ {
res = append(res, list[i].String())
}
return res
}

// TODO consider specific type.
func sortString(key string, list []gjson.Result) []string {
max := len(list)
var tmp gjson.Result

element := list[0]
for max != 0 {
for i := 0; i < max; i++ {
if element.Get(key).String() < list[i].Get(key).String() {
tmp = list[i]
list[i] = element
element = tmp
}

if i == max-1 {
tmp = list[i]
list[i] = element
element = tmp
}
}
max--
}

list[0] = element
res := []string{}
for i := 0; i < len(list); i++ {
res = append(res, list[i].String())
}
return res
}
28 changes: 17 additions & 11 deletions engine/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type matched struct {
// deletes Many items
func (db *DB) deleteMany(query gjson.Result) string {

mtch := query.Get("m")
mtch := query.Get("match")

coll := query.Get("c").String()
coll := query.Get("collection").String()

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

Expand Down Expand Up @@ -68,7 +68,7 @@ func (db *DB) updateMany(query gjson.Result) (result string) {

newObj := query.Get("data").String()

coll := query.Get("c").String()
coll := query.Get("collection").String()

// updates exist value

Expand Down Expand Up @@ -125,7 +125,7 @@ func (db *DB) updateOne(query gjson.Result) (result string) {

newObj := query.Get("data").String()

coll := query.Get("c").String()
coll := query.Get("collection").String()

// updates exist value

Expand Down Expand Up @@ -195,12 +195,12 @@ func (db *DB) updateById(query gjson.Result) (result string) {
func (db *DB) findMany(query gjson.Result) (res string) {

// TODO parse hol qury one time
coll := query.Get("c").String()
coll := query.Get("collection").String()
if coll == "" {
return `{"error":"forgot collection name "}`
}

mtch := query.Get("m")
mtch := query.Get("match")

if mtch.String() == "" {

Expand Down Expand Up @@ -249,11 +249,16 @@ func (db *DB) findMany(query gjson.Result) (res string) {
}
}

// order :
order := query.Get("orderBy").String()

listData = orderBy(order, listData)

// TODO aggrigate here

// remove|rename some fields
flds := query.Get("fields")
listData = fields(listData, flds)
listData = reFields(listData, flds)

records := "["

Expand All @@ -270,7 +275,7 @@ func (db *DB) findMany(query gjson.Result) (res string) {

// Finds first obj match creteria.
func (db *DB) findOne(query gjson.Result) (res string) {
coll := query.Get("c").String()
coll := query.Get("collection").String()
skip := query.Get("skip").Int()

// TODO are skyp useful here ?
Expand Down Expand Up @@ -352,7 +357,8 @@ func (db *DB) deleteOne(query gjson.Result) string {

// Finds first obj match creteria.
func (db *DB) findById(query gjson.Result) (res string) {
coll := query.Get("c").String()
coll := query.Get("collection").String()

id := query.Get("_id").String()

stmt := `select record from ` + coll + ` where rowid = ` + id
Expand Down Expand Up @@ -380,7 +386,7 @@ func (db *DB) findById(query gjson.Result) (res string) {

// Insert
func (db *DB) insertOne(query gjson.Result) (res string) {
coll := query.Get("c").String()
coll := query.Get("collection").String()
data := query.Get("data").String()

err := db.insert(coll, data)
Expand All @@ -397,7 +403,7 @@ func (db *DB) deleteById(query gjson.Result) string {
if id == "" {
return `{"error": "there is no _id"}`
}
coll := query.Get("c").String()
coll := query.Get("collection").String()
if coll == "" {
return `{"error": "there is no collection"}`
}
Expand Down
8 changes: 0 additions & 8 deletions engine/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package engine

import (
"database/sql"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -123,10 +122,3 @@ func check(hint string, err error) {
//return
}
}

var (
ErrDuplicate = errors.New("record already exists")
ErrNotExists = errors.New("row not exists")
ErrUpdateFailed = errors.New("update failed")
ErrDeleteFailed = errors.New("delete failed")
)
10 changes: 10 additions & 0 deletions engine/vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package engine

import "errors"

var (
ErrDuplicate = errors.New("record already exists")
ErrNotExists = errors.New("row not exists")
ErrUpdateFailed = errors.New("update failed")
ErrDeleteFailed = errors.New("delete failed")
)
2 changes: 1 addition & 1 deletion static/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ for (let [key, value] of Object.entries(yourobject)) {


// This configuration is suitable for development situation
//const configs = {debug: false, reconnectInterval: 200, reconnectDecay:1, maxReconnectInterval:1000}
const configs = {debug: false, reconnectDecay:1 , reconnectInterval: 200, reconnectDecay:1, maxReconnectInterval:200}

// WebSocket
var ws = new ReconnectingWebSocket('ws://localhost:1111/ws');
Expand Down
2 changes: 1 addition & 1 deletion store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_update(t *testing.T) {
t.Error(data, err)
}

coll.update(l, "hello")
//coll.update(l, "hello")

data, _ = coll.getData(l)
if err != nil {
Expand Down

0 comments on commit 4a3f2d7

Please sign in to comment.