mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 12:52:27 +00:00
store_sqlite: Implement put_object overwrite parameter
This commit is contained in:
@@ -275,13 +275,13 @@ impl AddressbookStore for SqliteStore {
|
|||||||
principal: String,
|
principal: String,
|
||||||
addressbook_id: String,
|
addressbook_id: String,
|
||||||
object: AddressObject,
|
object: AddressObject,
|
||||||
// TODO: implement
|
|
||||||
overwrite: bool,
|
overwrite: bool,
|
||||||
) -> Result<(), rustical_store::Error> {
|
) -> Result<(), rustical_store::Error> {
|
||||||
let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
|
let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
let (object_id, vcf) = (object.get_id(), object.get_vcf());
|
let (object_id, vcf) = (object.get_id(), object.get_vcf());
|
||||||
|
|
||||||
|
(if overwrite {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"REPLACE INTO addressobjects (principal, addressbook_id, id, vcf) VALUES (?, ?, ?, ?)",
|
"REPLACE INTO addressobjects (principal, addressbook_id, id, vcf) VALUES (?, ?, ?, ?)",
|
||||||
principal,
|
principal,
|
||||||
@@ -289,6 +289,16 @@ impl AddressbookStore for SqliteStore {
|
|||||||
object_id,
|
object_id,
|
||||||
vcf
|
vcf
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
// If the object already exists a database error is thrown and handled in error.rs
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO addressobjects (principal, addressbook_id, id, vcf) VALUES (?, ?, ?, ?)",
|
||||||
|
principal,
|
||||||
|
addressbook_id,
|
||||||
|
object_id,
|
||||||
|
vcf
|
||||||
|
)
|
||||||
|
})
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.map_err(crate::Error::from)?;
|
.map_err(crate::Error::from)?;
|
||||||
|
|||||||
@@ -223,13 +223,13 @@ impl CalendarStore for SqliteStore {
|
|||||||
principal: String,
|
principal: String,
|
||||||
cal_id: String,
|
cal_id: String,
|
||||||
object: CalendarObject,
|
object: CalendarObject,
|
||||||
// TODO: implement
|
|
||||||
overwrite: bool,
|
overwrite: bool,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
|
let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
|
||||||
|
|
||||||
let (object_id, ics) = (object.get_id(), object.get_ics());
|
let (object_id, ics) = (object.get_id(), object.get_ics());
|
||||||
|
|
||||||
|
(if overwrite {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"REPLACE INTO calendarobjects (principal, cal_id, id, ics) VALUES (?, ?, ?, ?)",
|
"REPLACE INTO calendarobjects (principal, cal_id, id, ics) VALUES (?, ?, ?, ?)",
|
||||||
principal,
|
principal,
|
||||||
@@ -237,6 +237,15 @@ impl CalendarStore for SqliteStore {
|
|||||||
object_id,
|
object_id,
|
||||||
ics
|
ics
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO calendarobjects (principal, cal_id, id, ics) VALUES (?, ?, ?, ?)",
|
||||||
|
principal,
|
||||||
|
cal_id,
|
||||||
|
object_id,
|
||||||
|
ics
|
||||||
|
)
|
||||||
|
})
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.map_err(crate::Error::from)?;
|
.map_err(crate::Error::from)?;
|
||||||
|
|||||||
@@ -11,6 +11,13 @@ impl From<sqlx::Error> for Error {
|
|||||||
fn from(value: sqlx::Error) -> Self {
|
fn from(value: sqlx::Error) -> Self {
|
||||||
match value {
|
match value {
|
||||||
sqlx::Error::RowNotFound => Error::StoreError(rustical_store::Error::NotFound),
|
sqlx::Error::RowNotFound => Error::StoreError(rustical_store::Error::NotFound),
|
||||||
|
sqlx::Error::Database(err) => {
|
||||||
|
if err.is_unique_violation() {
|
||||||
|
Error::StoreError(rustical_store::Error::AlreadyExists)
|
||||||
|
} else {
|
||||||
|
Error::SqlxError(sqlx::Error::Database(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
err => Error::SqlxError(err),
|
err => Error::SqlxError(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user