Skip to content

Commit 8284bb0

Browse files
committed
Add more inserting/updating operations
1 parent 8df2dd4 commit 8284bb0

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/builder.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct Builder {
99
}
1010

1111
impl Builder {
12-
// TODO: Schema
12+
// TODO: Switching schema
1313
pub fn new(url: &str) -> Self {
1414
Builder {
1515
method: None,
@@ -28,8 +28,8 @@ impl Builder {
2828
}
2929

3030
// TODO: Write-only tables
31-
// TODO: UPSERT
3231
// TODO: URL-encoded payload
32+
// TODO: Allow specifying columns
3333
pub fn insert(mut self, body: &str) -> Self {
3434
self.method = Some(Method::POST);
3535
self.headers
@@ -38,6 +38,33 @@ impl Builder {
3838
self
3939
}
4040

41+
pub fn insert_csv(mut self, body: &str) -> Self {
42+
self.headers
43+
.push(("Content-Type".to_string(), "text/csv".to_string()));
44+
self.insert(body)
45+
}
46+
47+
// TODO: Allow Prefer: resolution=ignore-duplicates
48+
// TODO: on_conflict (make UPSERT work on UNIQUE columns)
49+
pub fn upsert(mut self, body: &str) -> Self {
50+
self.method = Some(Method::POST);
51+
self.headers
52+
.push(("Prefer".to_string(),
53+
"return=representation; resolution=merge-duplicates".to_string()));
54+
self.body = Some(body.to_string());
55+
self
56+
}
57+
58+
pub fn single_upsert(mut self, primary_column: &str, key: &str, body: &str) -> Self {
59+
self.method = Some(Method::PUT);
60+
self.headers
61+
.push(("Prefer".to_string(), "return=representation".to_string()));
62+
self.queries.push((primary_column.to_string(),
63+
format!("eq.{}", key)));
64+
self.body = Some(body.to_string());
65+
self
66+
}
67+
4168
pub fn update(mut self, body: &str) -> Self {
4269
self.method = Some(Method::PATCH);
4370
self.headers

0 commit comments

Comments
 (0)