#[cfg(feature = "decimal")]
use crate::proto::com::arcadedb::grpc::GrpcDecimal;
use crate::proto::com::arcadedb::grpc::{
grpc_value::Kind, GrpcEmbedded, GrpcLink, GrpcList, GrpcMap, GrpcRecord, GrpcValue,
};
fn gv(kind: Kind) -> GrpcValue {
GrpcValue {
kind: Some(kind),
logical_type: String::new(),
}
}
pub fn str_v(s: impl Into<String>) -> GrpcValue {
gv(Kind::StringValue(s.into()))
}
pub fn i64_v(n: i64) -> GrpcValue {
gv(Kind::Int64Value(n))
}
pub fn i32_v(n: i32) -> GrpcValue {
gv(Kind::Int32Value(n))
}
pub fn f32_v(n: f32) -> GrpcValue {
gv(Kind::FloatValue(n))
}
pub fn bool_v(b: bool) -> GrpcValue {
gv(Kind::BoolValue(b))
}
pub fn f64_v(d: f64) -> GrpcValue {
gv(Kind::DoubleValue(d))
}
pub fn bytes_v(b: impl Into<Vec<u8>>) -> GrpcValue {
gv(Kind::BytesValue(b.into()))
}
pub fn embedded_v(type_name: &str, fields: Vec<(&str, GrpcValue)>) -> GrpcValue {
gv(Kind::EmbeddedValue(GrpcEmbedded {
r#type: type_name.into(),
fields: fields
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect(),
}))
}
pub fn link_v(rid: impl Into<String>) -> GrpcValue {
gv(Kind::LinkValue(GrpcLink {
rid: rid.into(),
r#type: String::new(),
}))
}
pub fn rec<T: IntoGrpcValue>(type_name: &str, props: Vec<(&str, T)>) -> GrpcRecord {
GrpcRecord {
rid: String::new(),
r#type: type_name.to_string(),
properties: props
.into_iter()
.map(|(k, v)| (k.to_string(), v.into_value()))
.collect(),
}
}
pub fn list_v(values: impl IntoIterator<Item = GrpcValue>) -> GrpcValue {
gv(Kind::ListValue(GrpcList {
values: values.into_iter().collect(),
}))
}
pub fn map_v(entries: impl IntoIterator<Item = (String, GrpcValue)>) -> GrpcValue {
gv(Kind::MapValue(GrpcMap {
entries: entries.into_iter().collect(),
}))
}
pub fn timestamp_v(t: chrono::DateTime<chrono::Utc>) -> GrpcValue {
use chrono::Timelike;
gv(Kind::TimestampValue(prost_types::Timestamp {
seconds: t.timestamp(),
nanos: t.nanosecond() as i32,
}))
}
pub fn date_v(d: chrono::NaiveDate) -> GrpcValue {
timestamp_v(d.and_hms_opt(0, 0, 0).expect("midnight is valid").and_utc())
}
#[cfg(feature = "decimal")]
pub fn decimal_v(d: rust_decimal::Decimal) -> GrpcValue {
let mantissa = d.mantissa();
let (unscaled, unscaled_bytes) = match i64::try_from(mantissa) {
Ok(n) => (n, Vec::new()),
Err(_) => (0, mantissa.to_be_bytes().to_vec()),
};
gv(Kind::DecimalValue(GrpcDecimal {
unscaled,
scale: d.scale() as i32,
unscaled_bytes,
}))
}
#[derive(Debug, Clone, Default)]
pub struct Params(pub std::collections::HashMap<String, GrpcValue>);
impl<T: IntoGrpcValue, const N: usize> From<[(&str, T); N]> for Params {
fn from(pairs: [(&str, T); N]) -> Self {
Params(
pairs
.into_iter()
.map(|(k, v)| (k.to_string(), v.into_value()))
.collect(),
)
}
}
impl From<std::collections::HashMap<String, GrpcValue>> for Params {
fn from(map: std::collections::HashMap<String, GrpcValue>) -> Self {
Params(map)
}
}
#[cfg(feature = "ahash")]
impl From<ahash::AHashMap<String, GrpcValue>> for Params {
fn from(map: ahash::AHashMap<String, GrpcValue>) -> Self {
Params(map.into_iter().collect())
}
}
#[cfg(feature = "fxhash")]
impl From<rustc_hash::FxHashMap<String, GrpcValue>> for Params {
fn from(map: rustc_hash::FxHashMap<String, GrpcValue>) -> Self {
Params(map.into_iter().collect())
}
}
impl From<()> for Params {
fn from((): ()) -> Self {
Params(std::collections::HashMap::new())
}
}
impl core::ops::Deref for Params {
type Target = std::collections::HashMap<String, GrpcValue>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Params {
pub fn from_map<M: IntoIterator<Item = (String, GrpcValue)>>(map: M) -> Self {
Params(map.into_iter().collect())
}
}
impl FromIterator<(String, GrpcValue)> for Params {
fn from_iter<T: IntoIterator<Item = (String, GrpcValue)>>(iter: T) -> Self {
Params(iter.into_iter().collect())
}
}
pub trait IntoGrpcValue {
fn into_value(self) -> GrpcValue;
}
impl IntoGrpcValue for GrpcValue {
fn into_value(self) -> GrpcValue {
self
}
}
impl IntoGrpcValue for bool {
fn into_value(self) -> GrpcValue {
bool_v(self)
}
}
impl IntoGrpcValue for i64 {
fn into_value(self) -> GrpcValue {
i64_v(self)
}
}
impl IntoGrpcValue for f64 {
fn into_value(self) -> GrpcValue {
f64_v(self)
}
}
impl IntoGrpcValue for &str {
fn into_value(self) -> GrpcValue {
str_v(self)
}
}
impl IntoGrpcValue for &String {
fn into_value(self) -> GrpcValue {
str_v(self.as_str())
}
}
impl IntoGrpcValue for String {
fn into_value(self) -> GrpcValue {
str_v(self)
}
}
impl IntoGrpcValue for std::borrow::Cow<'_, str> {
fn into_value(self) -> GrpcValue {
str_v(self.into_owned())
}
}
impl IntoGrpcValue for chrono::DateTime<chrono::Utc> {
fn into_value(self) -> GrpcValue {
timestamp_v(self)
}
}
impl IntoGrpcValue for chrono::NaiveDate {
fn into_value(self) -> GrpcValue {
date_v(self)
}
}
impl<T: IntoGrpcValue> IntoGrpcValue for Vec<T> {
fn into_value(self) -> GrpcValue {
list_v(self.into_iter().map(IntoGrpcValue::into_value))
}
}
impl IntoGrpcValue for i32 {
fn into_value(self) -> GrpcValue {
i32_v(self)
}
}
impl<V: IntoGrpcValue> IntoGrpcValue for std::collections::HashMap<String, V> {
fn into_value(self) -> GrpcValue {
map_v(self.into_iter().map(|(k, v)| (k, v.into_value())))
}
}
#[cfg(feature = "ahash")]
impl<V: IntoGrpcValue> IntoGrpcValue for ahash::AHashMap<String, V> {
fn into_value(self) -> GrpcValue {
map_v(self.into_iter().map(|(k, v)| (k, v.into_value())))
}
}
#[cfg(feature = "fxhash")]
impl<V: IntoGrpcValue> IntoGrpcValue for rustc_hash::FxHashMap<String, V> {
fn into_value(self) -> GrpcValue {
map_v(self.into_iter().map(|(k, v)| (k, v.into_value())))
}
}
impl<V: IntoGrpcValue> IntoGrpcValue for std::collections::BTreeMap<String, V> {
fn into_value(self) -> GrpcValue {
map_v(self.into_iter().map(|(k, v)| (k, v.into_value())))
}
}
impl IntoGrpcValue for char {
fn into_value(self) -> GrpcValue {
str_v(self.to_string())
}
}
impl IntoGrpcValue for i8 {
fn into_value(self) -> GrpcValue {
i32_v(i32::from(self))
}
}
impl IntoGrpcValue for i16 {
fn into_value(self) -> GrpcValue {
i32_v(i32::from(self))
}
}
impl IntoGrpcValue for u16 {
fn into_value(self) -> GrpcValue {
i32_v(i32::from(self))
}
}
impl IntoGrpcValue for u32 {
fn into_value(self) -> GrpcValue {
i64_v(i64::from(self))
}
}
impl IntoGrpcValue for u64 {
fn into_value(self) -> GrpcValue {
i64_v(self as i64)
}
}
impl IntoGrpcValue for f32 {
fn into_value(self) -> GrpcValue {
f32_v(self)
}
}
impl IntoGrpcValue for Vec<u8> {
fn into_value(self) -> GrpcValue {
bytes_v(self)
}
}
impl IntoGrpcValue for serde_json::Value {
fn into_value(self) -> GrpcValue {
crate::decode::json_to_grpc_value(&self)
}
}
#[cfg(feature = "decimal")]
impl IntoGrpcValue for rust_decimal::Decimal {
fn into_value(self) -> GrpcValue {
decimal_v(self)
}
}
impl IntoGrpcValue for crate::Link {
fn into_value(self) -> GrpcValue {
link_v(self.to_rid_string())
}
}
#[macro_export]
macro_rules! params {
($($tokens:tt)+) => {
$crate::__params_acc!([] $($tokens)+)
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __params_acc {
([$($acc:tt)*]) => {
$crate::Params(::core::iter::FromIterator::from_iter([$($acc)*]))
};
([$($acc:tt)*] $key:ident : { $($mk:tt : $mv:expr),+ $(,)? } , $($rest:tt)+) => {
$crate::__params_acc!([
$($acc)*
(::std::stringify!($key).to_string(), $crate::__private::map_v([
$(($mk.to_string(), $crate::IntoGrpcValue::into_value($mv))),+
])),
] $($rest)+)
};
([$($acc:tt)*] $key:ident : { $($mk:tt : $mv:expr),+ $(,)? } $(,)?) => {
$crate::__params_acc!([
$($acc)*
(::std::stringify!($key).to_string(), $crate::__private::map_v([
$(($mk.to_string(), $crate::IntoGrpcValue::into_value($mv))),+
])),
])
};
([$($acc:tt)*] $key:ident , $($rest:tt)+) => {
$crate::__params_acc!([
$($acc)*
(::std::stringify!($key).to_string(), $crate::IntoGrpcValue::into_value($key)),
] $($rest)+)
};
([$($acc:tt)*] $key:ident $(,)?) => {
$crate::__params_acc!([
$($acc)*
(::std::stringify!($key).to_string(), $crate::IntoGrpcValue::into_value($key)),
])
};
([$($acc:tt)*] $key:ident : $value:expr , $($rest:tt)+) => {
$crate::__params_acc!([
$($acc)*
(::std::stringify!($key).to_string(), $crate::IntoGrpcValue::into_value($value)),
] $($rest)+)
};
([$($acc:tt)*] $key:ident : $value:expr $(,)?) => {
$crate::__params_acc!([
$($acc)*
(::std::stringify!($key).to_string(), $crate::IntoGrpcValue::into_value($value)),
])
};
}
#[macro_export]
macro_rules! embedded {
($type:expr $(, $($tokens:tt)+)?) => {
$crate::__embedded_acc!($type, [] $($($tokens)+)?)
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __embedded_acc {
($type:expr, [$($acc:tt)*]) => {
$crate::__private::embedded_v(
$type,
::core::iter::FromIterator::from_iter([$($acc)*]),
)
};
($type:expr, [$($acc:tt)*] $key:ident : $value:expr , $($rest:tt)+) => {
$crate::__embedded_acc!(
$type, [$($acc)* (::std::stringify!($key), $crate::IntoGrpcValue::into_value($value)),]
$($rest)+
)
};
($type:expr, [$($acc:tt)*] $key:ident : $value:expr $(,)?) => {
$crate::__embedded_acc!(
$type, [$($acc)* (::std::stringify!($key), $crate::IntoGrpcValue::into_value($value)),]
)
};
}
#[derive(Debug, Clone, Default)]
pub struct SetClause {
frags: Vec<String>,
params: std::collections::HashMap<String, GrpcValue>,
}
impl SetClause {
pub fn new() -> Self {
Self::default()
}
pub fn is_empty(&self) -> bool {
self.frags.is_empty()
}
pub fn set(mut self, col: &str, value: impl IntoGrpcValue) -> Self {
self.frags.push(format!("{col} = :{col}"));
self.params.insert(col.to_string(), value.into_value());
self
}
pub fn set_opt<T: IntoGrpcValue>(self, col: &str, value: Option<T>) -> Self {
match value {
Some(v) => self.set(col, v),
None => self,
}
}
pub fn set_when(self, cond: bool, col: &str, value: impl IntoGrpcValue) -> Self {
if cond {
self.set(col, value)
} else {
self
}
}
pub fn set_raw(
mut self,
fragment: impl Into<String>,
name: &str,
value: impl IntoGrpcValue,
) -> Self {
self.frags.push(fragment.into());
self.params.insert(name.to_string(), value.into_value());
self
}
pub fn set_expr(mut self, col: &str, expr: &str) -> Self {
self.frags.push(format!("{col} = {expr}"));
self
}
pub fn bind(mut self, name: &str, value: impl IntoGrpcValue) -> Self {
self.params.insert(name.to_string(), value.into_value());
self
}
pub fn apply_if<T>(self, option: Option<T>, f: impl FnOnce(Self, T) -> Self) -> Self {
match option {
Some(v) => f(self, v),
None => self,
}
}
pub fn append(mut self, col: &str, value: impl IntoGrpcValue) -> Self {
let param = format!("{col}__append");
match self.params.get_mut(¶m) {
Some(GrpcValue {
kind: Some(Kind::ListValue(list)),
..
}) => list.values.push(value.into_value()),
_ => {
self.frags.push(format!("{col} = {col} || :{param}"));
self.params.insert(param, list_v([value.into_value()]));
}
}
self
}
pub fn set_or_null<T: IntoGrpcValue>(mut self, col: &str, value: Option<T>) -> Self {
match value {
Some(v) => self.set(col, v),
None => {
self.frags.push(format!("{col} = null"));
self
}
}
}
pub fn sql(&self) -> String {
self.frags.join(", ")
}
pub fn into_params(self) -> Params {
Params(self.params)
}
}