mysql_connector/model/active_model/
named_value.rs1use crate::{
2 error::SerializeError,
3 types::{SimpleValue, Value},
4};
5
6#[derive(Debug)]
7pub struct NamedValue(pub &'static str, pub Value);
8
9impl NamedValue {
10 pub fn name(&self) -> &'static str {
11 self.0
12 }
13
14 pub fn value(&self) -> &Value {
15 &self.1
16 }
17
18 pub(super) fn into_insert(
19 values: &[NamedValue],
20 table: &str,
21 ) -> Result<String, SerializeError> {
22 let mut stmt = String::from("insert into `");
23 stmt += table;
24 stmt += "` (";
25 for (i, value) in values.iter().enumerate() {
26 if i != 0 {
27 stmt += ", ";
28 }
29 stmt += "`";
30 stmt += value.name();
31 stmt += "`";
32 }
33 stmt += ") values (";
34 for i in 0..values.len() {
35 if i != 0 {
36 stmt += ", ";
37 }
38 stmt += "?";
39 }
40 stmt += ")";
41 Ok(stmt)
42 }
43
44 pub(super) fn into_update(
45 values: &[NamedValue],
46 table: &str,
47 primary: &str,
48 ) -> Result<String, SerializeError> {
49 let mut stmt = String::from("update `");
50 stmt += table;
51 stmt += "` set ";
52 for (i, value) in values.iter().enumerate() {
53 if i != 0 {
54 stmt += ", ";
55 }
56 stmt += "`";
57 stmt += value.name();
58 stmt += "` = ?";
59 }
60 stmt += " where `";
61 stmt += primary;
62 stmt += "` = ?";
63 Ok(stmt)
64 }
65}
66
67impl SimpleValue for NamedValue {
68 fn value(&self) -> &Value {
69 &self.1
70 }
71}