rsiot/components/cmp_tsdb_writer/
row_builder.rs1use sqlx::types::time::OffsetDateTime;
2use time::format_description::well_known::Iso8601;
3
4use super::Error;
5
6fn row_to_sql(time: &str, values: &[impl AsRef<str>]) -> String {
7 let values = values
8 .iter()
9 .map(|v| v.as_ref())
10 .collect::<Vec<_>>()
11 .join(", ");
12 format!("('{}', {})", time, values)
13}
14
15pub fn row_with_ts(time: &OffsetDateTime, values: &[impl AsRef<str>]) -> Result<String, Error> {
17 let time = time.format(&Iso8601::DEFAULT)?;
18 let sql = row_to_sql(&time, values);
19 Ok(sql)
20}
21
22pub fn row_without_ts(values: &[impl AsRef<str>]) -> Result<String, Error> {
24 let time = OffsetDateTime::now_local()?.format(&Iso8601::DEFAULT)?;
25 let sql = row_to_sql(&time, values);
26 Ok(sql)
27}