|
| 1 | +use crate::{ |
| 2 | + enums::ReturnClause, |
| 3 | + internal_macros::push_clause, |
| 4 | + types::create::{ContentMode, CreateData, SetField}, |
| 5 | +}; |
| 6 | +use std::fmt::Write; |
| 7 | + |
| 8 | +pub struct CreateBuilder { |
| 9 | + pub data: CreateData, |
| 10 | +} |
| 11 | + |
| 12 | +impl CreateBuilder { |
| 13 | + /// Switches the statement from `CREATE ...` to `CREATE ONLY ...`. |
| 14 | + /// |
| 15 | + /// **Note:** SurrealDB expects a single-result `RETURN` when using `ONLY`. |
| 16 | + /// The builder does not enforce this — the server will validate it at runtime. |
| 17 | + pub fn only(mut self) -> Self { |
| 18 | + self.data.only = true; |
| 19 | + self |
| 20 | + } |
| 21 | + |
| 22 | + /// Sets the data-setting mode to `CONTENT @value`. |
| 23 | + /// |
| 24 | + /// This replaces any previous `CONTENT` or `SET` clause. |
| 25 | + /// |
| 26 | + /// # Example |
| 27 | + /// ``` |
| 28 | + /// # use surrealex::QueryBuilder; |
| 29 | + /// let sql = QueryBuilder::create("person") |
| 30 | + /// .content("{ name: 'Tobie', company: 'SurrealDB' }") |
| 31 | + /// .build(); |
| 32 | + /// assert_eq!(sql, "CREATE person CONTENT { name: 'Tobie', company: 'SurrealDB' }"); |
| 33 | + /// ``` |
| 34 | + pub fn content(mut self, value: &str) -> Self { |
| 35 | + self.data.content = Some(ContentMode::Content(value.to_string())); |
| 36 | + self |
| 37 | + } |
| 38 | + |
| 39 | + /// Adds a `SET field = value` assignment. |
| 40 | + /// |
| 41 | + /// Multiple calls accumulate assignments. If a `CONTENT` clause was previously |
| 42 | + /// set, it is replaced by the `SET` clause. |
| 43 | + /// |
| 44 | + /// # Example |
| 45 | + /// ``` |
| 46 | + /// # use surrealex::QueryBuilder; |
| 47 | + /// let sql = QueryBuilder::create("person") |
| 48 | + /// .set("name", "'Tobie'") |
| 49 | + /// .set("company", "'SurrealDB'") |
| 50 | + /// .build(); |
| 51 | + /// assert_eq!(sql, "CREATE person SET name = 'Tobie', company = 'SurrealDB'"); |
| 52 | + /// ``` |
| 53 | + pub fn set(mut self, field: &str, value: &str) -> Self { |
| 54 | + match &mut self.data.content { |
| 55 | + Some(ContentMode::Set(fields)) => { |
| 56 | + fields.push(SetField { |
| 57 | + field: field.to_string(), |
| 58 | + value: value.to_string(), |
| 59 | + }); |
| 60 | + } |
| 61 | + _ => { |
| 62 | + self.data.content = Some(ContentMode::Set(vec![SetField { |
| 63 | + field: field.to_string(), |
| 64 | + value: value.to_string(), |
| 65 | + }])); |
| 66 | + } |
| 67 | + } |
| 68 | + self |
| 69 | + } |
| 70 | + |
| 71 | + /// Sets the RETURN clause to `RETURN NONE`. |
| 72 | + pub fn return_none(mut self) -> Self { |
| 73 | + self.data.return_clause = Some(ReturnClause::None); |
| 74 | + self |
| 75 | + } |
| 76 | + |
| 77 | + /// Sets the RETURN clause to `RETURN BEFORE`. |
| 78 | + pub fn return_before(mut self) -> Self { |
| 79 | + self.data.return_clause = Some(ReturnClause::Before); |
| 80 | + self |
| 81 | + } |
| 82 | + |
| 83 | + /// Sets the RETURN clause to `RETURN AFTER`. |
| 84 | + pub fn return_after(mut self) -> Self { |
| 85 | + self.data.return_clause = Some(ReturnClause::After); |
| 86 | + self |
| 87 | + } |
| 88 | + |
| 89 | + /// Sets the RETURN clause to `RETURN DIFF`. |
| 90 | + pub fn return_diff(mut self) -> Self { |
| 91 | + self.data.return_clause = Some(ReturnClause::Diff); |
| 92 | + self |
| 93 | + } |
| 94 | + |
| 95 | + /// Sets the RETURN clause to `RETURN <param1>, <param2>, ...`. |
| 96 | + /// |
| 97 | + /// # Example |
| 98 | + /// ``` |
| 99 | + /// # use surrealex::QueryBuilder; |
| 100 | + /// let sql = QueryBuilder::create("person") |
| 101 | + /// .set("name", "'Tobie'") |
| 102 | + /// .return_params(vec!["name", "id"]) |
| 103 | + /// .build(); |
| 104 | + /// assert_eq!(sql, "CREATE person SET name = 'Tobie' RETURN name, id"); |
| 105 | + /// ``` |
| 106 | + pub fn return_params<S: Into<String>>(mut self, params: Vec<S>) -> Self { |
| 107 | + self.data.return_clause = Some(ReturnClause::Params( |
| 108 | + params.into_iter().map(|s| s.into()).collect(), |
| 109 | + )); |
| 110 | + self |
| 111 | + } |
| 112 | + |
| 113 | + /// Sets the RETURN clause to `RETURN VALUE <field>`. |
| 114 | + /// |
| 115 | + /// # Example |
| 116 | + /// ``` |
| 117 | + /// # use surrealex::QueryBuilder; |
| 118 | + /// let sql = QueryBuilder::create("person") |
| 119 | + /// .set("name", "'Tobie'") |
| 120 | + /// .return_value("name") |
| 121 | + /// .build(); |
| 122 | + /// assert_eq!(sql, "CREATE person SET name = 'Tobie' RETURN VALUE name"); |
| 123 | + /// ``` |
| 124 | + pub fn return_value(mut self, field: &str) -> Self { |
| 125 | + self.data.return_clause = Some(ReturnClause::Value(field.to_string())); |
| 126 | + self |
| 127 | + } |
| 128 | + |
| 129 | + /// Sets the TIMEOUT clause with a raw SurrealQL duration string. |
| 130 | + /// |
| 131 | + /// Accepts SurrealQL duration syntax such as `"500ms"`, `"2s"`, `"1m"`. |
| 132 | + pub fn timeout(mut self, duration: &str) -> Self { |
| 133 | + self.data.timeout = Some(duration.to_string()); |
| 134 | + self |
| 135 | + } |
| 136 | + |
| 137 | + /// Builds the final CREATE query string. |
| 138 | + pub fn build(self) -> String { |
| 139 | + let mut query = String::with_capacity(128); |
| 140 | + let targets = &self.data.targets; |
| 141 | + |
| 142 | + if self.data.only { |
| 143 | + push_clause!(query, "CREATE ONLY {targets}"); |
| 144 | + } else { |
| 145 | + push_clause!(query, "CREATE {targets}"); |
| 146 | + } |
| 147 | + |
| 148 | + if let Some(ref content) = self.data.content { |
| 149 | + match content { |
| 150 | + ContentMode::Content(value) => { |
| 151 | + push_clause!(query, "CONTENT {value}"); |
| 152 | + } |
| 153 | + ContentMode::Set(fields) => { |
| 154 | + let assignments: String = fields |
| 155 | + .iter() |
| 156 | + .map(|f| format!("{} = {}", f.field, f.value)) |
| 157 | + .collect::<Vec<String>>() |
| 158 | + .join(", "); |
| 159 | + push_clause!(query, "SET {assignments}"); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if let Some(ref rc) = self.data.return_clause { |
| 165 | + push_clause!(query, "RETURN {rc}"); |
| 166 | + } |
| 167 | + |
| 168 | + if let Some(ref duration) = self.data.timeout { |
| 169 | + push_clause!(query, "TIMEOUT {duration}"); |
| 170 | + } |
| 171 | + |
| 172 | + query |
| 173 | + } |
| 174 | +} |
0 commit comments