Skip to content

Commit 60cedea

Browse files
Copilotanalogrelay
andauthored
cosmos: Add Query::with_text() and Query::append_text() methods (Azure#3045)
Adds methods to modify query text after creation, enabling more ergonomic query building patterns when working with optional parameters. ## Problem The `Query` type only allowed adding parameters via `with_parameter()`, but the query text could not be changed once created. This made it cumbersome to build queries with optional filters: ```rust // Previous approach required recreating Query objects if let Some(high) = high_time { query_text.push_str(&format!(" AND c.time <= @high_time")); query = Query::from(query_text.as_str()); query = query.with_parameter("@high_time", high)?; } ``` ## Solution Added two new methods following the same consumption pattern as `with_parameter()`: - `Query::with_text(String) -> Self` - Replaces the current query text - `Query::append_text(&str) -> Self` - Appends text to the current query text Both methods preserve existing parameters and support method chaining: ```rust // New ergonomic approach if let Some(high) = high_time { query = query.append_text(" AND c.time <= @high_time") .with_parameter("@high_time", high)?; } ``` ## Changes - Added `Query::with_text()` and `Query::append_text()` methods - Added comprehensive unit tests covering functionality and method chaining - Updated documentation with usage examples - Updated CHANGELOG.md All existing functionality remains unchanged and fully backward compatible. <!-- START COPILOT CODING AGENT SUFFIX --> ---- **Additional instructions:** > Make sure you structure your commits in a sensible way and include a concise but descriptive PR description. PREFER concise descriptions and code. The team knows how this code should work, they don't need explanations of it. The code should be largely self-explanatory. Fixes Azure#3044 <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey3.medallia.com/?EAHeSx-AP01bZqG0Ld9QLQ) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: analogrelay <[email protected]>
1 parent 56fe61e commit 60cedea

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

sdk/cosmos/azure_data_cosmos/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Features Added
66

7+
* Added `Query::with_text()` and `Query::append_text()` methods to modify query text after creation ([#3044](https://github.com/Azure/azure-sdk-for-rust/pull/3044))
8+
79
### Breaking Changes
810

911
### Bugs Fixed

sdk/cosmos/azure_data_cosmos/src/query/mod.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ pub use engine::*;
2727
/// # assert_eq!(serde_json::to_string(&query).unwrap(), "{\"query\":\"SELECT * FROM c WHERE c.id = @customer_id\",\"parameters\":[{\"name\":\"@customer_id\",\"value\":42}]}");
2828
/// ```
2929
///
30+
/// You can also modify the query text using [`Query::with_text()`] to replace it entirely
31+
/// or [`Query::append_text()`] to add to the existing text:
32+
///
33+
/// ```rust
34+
/// # use azure_data_cosmos::Query;
35+
/// let query = Query::from("SELECT * FROM c")
36+
/// .append_text(" WHERE c.time >= @low_time")
37+
/// .with_parameter("@low_time", "2023-01-01").unwrap()
38+
/// .append_text(" AND c.time <= @high_time")
39+
/// .with_parameter("@high_time", "2023-12-31").unwrap();
40+
/// # // We can't directly access the text field as it's private, but we can serialize to verify
41+
/// # let serialized = serde_json::to_string(&query).unwrap();
42+
/// # assert!(serialized.contains("WHERE c.time >= @low_time AND c.time <= @high_time"));
43+
/// ```
44+
///
3045
/// # Specifying Parameters
3146
///
3247
/// Any JSON-serializable value, including an empty tuple (`()`), which indicates `null`, can be used as a parameter.
@@ -92,6 +107,18 @@ impl Query {
92107

93108
Ok(self)
94109
}
110+
111+
/// Consumes this [`Query`] instance, replaces its text with the provided value, and returns it.
112+
pub fn with_text(mut self, text: String) -> Self {
113+
self.text = text;
114+
self
115+
}
116+
117+
/// Consumes this [`Query`] instance, appends the provided text to its current text, and returns it.
118+
pub fn append_text(mut self, text: &str) -> Self {
119+
self.text.push_str(text);
120+
self
121+
}
95122
}
96123

97124
impl<T: Into<String>> From<T> for Query {
@@ -169,4 +196,56 @@ mod tests {
169196
);
170197
Ok(())
171198
}
199+
200+
#[test]
201+
pub fn with_text_replaces_query_text() {
202+
let query = Query::from("SELECT * FROM c").with_text("SELECT c.id FROM c".to_string());
203+
assert_eq!(query.text, "SELECT c.id FROM c");
204+
}
205+
206+
#[test]
207+
pub fn with_text_preserves_parameters() -> Result<(), Box<dyn Error>> {
208+
let query = Query::from("SELECT * FROM c")
209+
.with_parameter("@id", 42)?
210+
.with_text("SELECT c.name FROM c WHERE c.id = @id".to_string());
211+
212+
assert_eq!(query.text, "SELECT c.name FROM c WHERE c.id = @id");
213+
assert_eq!(query.parameters.len(), 1);
214+
assert_eq!(query.parameters[0].name, "@id");
215+
Ok(())
216+
}
217+
218+
#[test]
219+
pub fn append_text_adds_to_existing_text() {
220+
let query = Query::from("SELECT * FROM c").append_text(" WHERE c.id = @id");
221+
assert_eq!(query.text, "SELECT * FROM c WHERE c.id = @id");
222+
}
223+
224+
#[test]
225+
pub fn append_text_preserves_parameters() -> Result<(), Box<dyn Error>> {
226+
let query = Query::from("SELECT * FROM c")
227+
.with_parameter("@id", 42)?
228+
.append_text(" WHERE c.id = @id");
229+
230+
assert_eq!(query.text, "SELECT * FROM c WHERE c.id = @id");
231+
assert_eq!(query.parameters.len(), 1);
232+
assert_eq!(query.parameters[0].name, "@id");
233+
Ok(())
234+
}
235+
236+
#[test]
237+
pub fn method_chaining_works_with_new_methods() -> Result<(), Box<dyn Error>> {
238+
let query = Query::from("SELECT * FROM c")
239+
.append_text(" WHERE c.time >= @low_time")
240+
.with_parameter("@low_time", "2023-01-01")?
241+
.append_text(" AND c.time <= @high_time")
242+
.with_parameter("@high_time", "2023-12-31")?;
243+
244+
assert_eq!(
245+
query.text,
246+
"SELECT * FROM c WHERE c.time >= @low_time AND c.time <= @high_time"
247+
);
248+
assert_eq!(query.parameters.len(), 2);
249+
Ok(())
250+
}
172251
}

0 commit comments

Comments
 (0)