Skip to content

Commit 369f6df

Browse files
committed
use insert or on conflict
1 parent 1ba7004 commit 369f6df

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

src/dcli/src/activitystoreinterface.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,24 +1479,21 @@ impl ActivityStoreInterface {
14791479
return Ok(row_id);
14801480
}
14811481

1482-
// here we need to either insert or update.
1483-
// sqlite has REPLACE or UPDATE but it deletes the row and then inserts
1484-
// instead of updating. With the way we have our primary keys setup, this
1485-
// causes issues, so we have to manually check and run the right command.
1486-
// if we just used member_id and character_id as primary keys, instead of
1487-
// auto-incrementing we would be fine (but we don't do that right now)
1488-
if row_id == -1 {
1489-
sqlx::query(
1490-
r#"
1491-
INSERT into "character" ("character_id", "member", "class") VALUES (?, ?, ?)
1492-
"#,
1493-
)
1494-
.bind(character_id.to_string())
1495-
.bind(member_rowid)
1496-
.bind(class_type.as_id().to_string())
1497-
.execute(&mut self.db)
1498-
.await?;
1482+
sqlx::query(
1483+
r#"
1484+
INSERT into "character" ("character_id", "member", "class") VALUES (?, ?, ?)
1485+
ON CONFLICT (character_id, member) DO UPDATE
1486+
set class = ?
1487+
"#,
1488+
)
1489+
.bind(character_id.to_string())
1490+
.bind(member_rowid)
1491+
.bind(class_type.as_id().to_string())
1492+
.bind(class_type.as_id().to_string())
1493+
.execute(&mut self.db)
1494+
.await?;
14991495

1496+
if row_id == -1 {
15001497
let row = sqlx::query(
15011498
r#"
15021499
SELECT id from "character" where character_id=? and member=?
@@ -1508,17 +1505,6 @@ impl ActivityStoreInterface {
15081505
.await?;
15091506

15101507
row_id = row.try_get("id")?;
1511-
} else {
1512-
sqlx::query(
1513-
r#"
1514-
UPDATE "character" set class=? where member=? AND character_id=?
1515-
"#,
1516-
)
1517-
.bind(character_id.to_string())
1518-
.bind(member_rowid)
1519-
.bind(class_type.as_id().to_string())
1520-
.execute(&mut self.db)
1521-
.await?;
15221508
}
15231509

15241510
Ok(row_id)

0 commit comments

Comments
 (0)