Skip to content

Commit

Permalink
Release 0.12.2
Browse files Browse the repository at this point in the history
  • Loading branch information
photino committed Aug 31, 2023
1 parent d75ecee commit de1ae61
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 93 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ cargo run -- --env=dev

This project is licensed under the [MIT license][license].

## Community

If you have any poroblems or ideas, please don't hesitate to [open an issue][zino-issue].
For Chineses users, you can also follow our WeChat official account to cantact us:

![zino-web](https://foruda.gitee.com/images/1693469268337449782/f4089986_145641.png)

[`zino-core`]: https://github.com/photino/zino/tree/main/zino-core
[`zino-derive`]: https://github.com/photino/zino/tree/main/zino-derive
[`zino-model`]: https://github.com/photino/zino/tree/main/zino-model
Expand All @@ -63,3 +70,4 @@ This project is licensed under the [MIT license][license].
[`axum-app`]: https://github.com/photino/zino/tree/main/examples/axum-app
[`dioxus-desktop`]: https://github.com/photino/zino/tree/main/examples/dioxus-desktop
[license]: https://github.com/photino/zino/blob/main/LICENSE
[zino-issue]: https://github.com/photino/zino/issues/new
100 changes: 7 additions & 93 deletions zino-core/src/database/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,101 +45,15 @@ impl MutationExt<DatabaseDriver> for Mutation {
let mutation = format!(r#"{key} = {key} * {value}"#);
mutations.push(mutation);
}
"$add" => {
if let Some(values) = value.as_array() && values.len() >= 2 {
let value = values.iter()
.map(|v| {
if let Some(s) = v.as_str() && M::has_column(s) {
Query::format_field(s)
} else {
col.encode_value(Some(v))
}
})
.collect::<Vec<_>>()
.join(" + ");
let mutation = format!(r#"{key} = {value}"#);
mutations.push(mutation);
}
}
"$multiply" => {
if let Some(values) = value.as_array() && values.len() >= 2 {
let value = values.iter()
.map(|v| {
if let Some(s) = v.as_str() && M::has_column(s) {
Query::format_field(s)
} else {
col.encode_value(Some(v))
}
})
.collect::<Vec<_>>()
.join(" * ");
let mutation = format!(r#"{key} = {value}"#);
mutations.push(mutation);
}
}
"$subtract" => {
if let Some(values) = value.as_array() && values.len() == 2 {
let value = values.iter()
.map(|v| {
if let Some(s) = v.as_str() && M::has_column(s) {
Query::format_field(s)
} else {
col.encode_value(Some(v))
}
})
.collect::<Vec<_>>()
.join(" - ");
let mutation = format!(r#"{key} = {value}"#);
mutations.push(mutation);
}
}
"$divide" => {
if let Some(values) = value.as_array() && values.len() == 2 {
let value = values.iter()
.map(|v| {
if let Some(s) = v.as_str() && M::has_column(s) {
Query::format_field(s)
} else {
col.encode_value(Some(v))
}
})
.collect::<Vec<_>>()
.join(" / ");
let mutation = format!(r#"{key} = {value}"#);
mutations.push(mutation);
}
}
"$min" => {
if let Some(values) = value.as_array() && values.len() >= 2 {
let value = values.iter()
.map(|v| {
if let Some(s) = v.as_str() && M::has_column(s) {
Query::format_field(s)
} else {
col.encode_value(Some(v))
}
})
.collect::<Vec<_>>()
.join(", ");
let mutation = format!(r#"{key} = LEAST({value})"#);
mutations.push(mutation);
}
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = LEAST({key}, {value})"#);
mutations.push(mutation);
}
"$max" => {
if let Some(values) = value.as_array() && values.len() >= 2 {
let value = values.iter()
.map(|v| {
if let Some(s) = v.as_str() && M::has_column(s) {
Query::format_field(s)
} else {
col.encode_value(Some(v))
}
})
.collect::<Vec<_>>()
.join(", ");
let mutation = format!(r#"{key} = GREATEST({value})"#);
mutations.push(mutation);
}
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = GREATEST({key}, {value})"#);
mutations.push(mutation);
}
_ => ()
}
Expand All @@ -149,7 +63,7 @@ impl MutationExt<DatabaseDriver> for Mutation {
}
}
if set_json_object {
let value = col.encode_value(Some(&value));
let value = col.encode_value(Some(value));
let mutation = format!(r#"{key} = {value}"#);
mutations.push(mutation);
}
Expand Down
22 changes: 22 additions & 0 deletions zino-core/src/model/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ impl MutationBuilder {
self
}

/// Increments the field by a specified value
#[inline]
pub fn inc<S, T>(mut self, field: S, value: T) -> Self
where
S: Into<String>,
T: Into<JsonValue>,
{
self.updates.upsert(field, Map::from_entry("$inc", value));
self
}

/// Multiplies the value of a field by a number.
#[inline]
pub fn mul<S, T>(mut self, field: S, value: T) -> Self
where
S: Into<String>,
T: Into<JsonValue>,
{
self.updates.upsert(field, Map::from_entry("$mul", value));
self
}

/// Constructs an instance of `Mutation`.
#[inline]
pub fn build(self) -> Mutation {
Expand Down

0 comments on commit de1ae61

Please sign in to comment.