Skip to content

Commit

Permalink
updateOne & updateMany
Browse files Browse the repository at this point in the history
  • Loading branch information
baxiry committed Dec 27, 2023
1 parent 2246a5d commit 2c9f8a4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
11 changes: 9 additions & 2 deletions dblite/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ func HandleQueries(query string) string {
case "insert":
return insert(query)

case "update":
return update(query)
// update
case "updateById":
return updateById(query)

case "updateOne":
return updateOne(query)

case "updateMany":
return updateMany(query)

case "deleteById":
return deleteById(query)
Expand Down
56 changes: 55 additions & 1 deletion dblite/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func deleteById(query string) string {
}

// Update update document data
func update(query string) (result string) {
func updateById(query string) (result string) {
collection := gjson.Get(query, "collection").String() // + slash
if collection == "" {
return "ERROR! select no collection "
Expand All @@ -162,4 +162,58 @@ func update(query string) (result string) {
return "Success update"
}

// TODO updateOne one update document data
func updateOne(query string) (result string) {
collection := gjson.Get(query, "collection").String() // + slash
if collection == "" {
return "ERROR! select no collection "
}

filter := gjson.Get(query, "filter").String()
newData := gjson.Get(query, "data").String()

for i := 0; i <= db.lastId; i++ {

data := db.Get(i, collection)

if match(filter, data) {

data = gjson.Get("["+data+","+newData+"]", "@join").String()
db.Update(i, collection, data)
return "success updated"
}
}
return "nothing to update"
}

// TODO updateMany update document data
func updateMany(query string) (result string) {
collection := gjson.Get(query, "collection").String() // + slash
if collection == "" {
return "ERROR! select no collection "
}

filter := gjson.Get(query, "filter").String()
newData := gjson.Get(query, "data").String()

tot := 0

for i := 0; i <= db.lastId; i++ {
if db.indexs[i].coll != collection {
continue
}

data := db.Get(i, collection)

if match(filter, data) {
id := gjson.Get(data, "_id").Int()

data = gjson.Get("["+data+","+newData+"]", "@join").String()
db.Update(int(id), collection, data)
tot++
}
}
return str(tot) + " item updated"
}

// end
4 changes: 2 additions & 2 deletions web/shell.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<div id="data">
</div>

<div class="reconnecte">
<div id="reconnecte">
<h6>desconnected</h6>
<a style="color: #808b8a;" href="/shell">reload</a>
<a href="/shell">reload</a>
<p>alt + r</p>
</div>

Expand Down
2 changes: 1 addition & 1 deletion web/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ textarea {
width: 100%;
}

.reconnecte {
#reconnecte {
display: none;
color: #808b8a;
padding-left:35%;
Expand Down

0 comments on commit 2c9f8a4

Please sign in to comment.