Skip to content

Commit 4bfd8ff

Browse files
committed
orm variant
1 parent 0c45b34 commit 4bfd8ff

File tree

1 file changed

+77
-55
lines changed

1 file changed

+77
-55
lines changed

syncstorage-postgres/src/db/db_impl.rs

Lines changed: 77 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -384,61 +384,83 @@ impl Db for PgDb {
384384
// This method is an upsert operation, which allows the update of an existing row
385385
// or inserts a new one if it doesn’t exist. Postgres does not have `UPSERT` but
386386
// achieves this though `INSERT...ON CONFLICT`.
387-
let q: String = r#"
388-
INSERT INTO bso (user_id, collection_id, bso_id, sortindex, payload, modified, expiry)
389-
VALUES ($1, $2, $3, $4, $5, $6, $7)
390-
ON CONFLICT (user_id, collection_id, bso_id)
391-
DO UPDATE SET
392-
user_id = EXCLUDED.user_id,
393-
collection_id = EXCLUDED.collection_id,
394-
bso_id = EXCLUDED.bso_id,
395-
"#
396-
.to_string();
397-
398-
let q = format!(
399-
"{}{}",
400-
q,
401-
if bso.sortindex.is_some() {
402-
", sortindex = VALUES(sortindex)"
403-
} else {
404-
""
405-
},
406-
);
407-
let q = format!(
408-
"{}{}",
409-
q,
410-
if bso.payload.is_some() {
411-
", payload = VALUES(payload)"
412-
} else {
413-
""
414-
},
415-
);
416-
let q = format!(
417-
"{}{}",
418-
q,
419-
if bso.ttl.is_some() {
420-
"expiry = VALUES(expiry)"
421-
} else {
422-
""
423-
},
424-
);
425-
let q = format!(
426-
"{}{}",
427-
q,
428-
if bso.payload.is_some() || bso.sortindex.is_some() {
429-
"modified = VALUES(modified)"
430-
} else {
431-
""
432-
},
433-
);
434-
sql_query(q)
435-
.bind::<BigInt, _>(user_id as i64) // XXX:
436-
.bind::<Integer, _>(&collection_id)
437-
.bind::<Text, _>(&bso.id)
438-
.bind::<Nullable<Integer>, _>(sortindex)
439-
.bind::<Text, _>(payload)
440-
.bind::<BigInt, _>(timestamp)
441-
.bind::<BigInt, _>(timestamp + (i64::from(ttl) * 1000)) // remember: this is in millis
387+
// let q: String = r#"
388+
// INSERT INTO bso (user_id, collection_id, bso_id, sortindex, payload, modified, expiry)
389+
// VALUES ($1, $2, $3, $4, $5, $6, $7)
390+
// ON CONFLICT (user_id, collection_id, bso_id)
391+
// DO UPDATE SET
392+
// user_id = EXCLUDED.user_id,
393+
// collection_id = EXCLUDED.collection_id,
394+
// bso_id = EXCLUDED.bso_id,
395+
// "#
396+
// .to_string();
397+
398+
// let q = format!(
399+
// "{}{}",
400+
// q,
401+
// if bso.sortindex.is_some() {
402+
// ", sortindex = VALUES(sortindex)"
403+
// } else {
404+
// ""
405+
// },
406+
// );
407+
// let q = format!(
408+
// "{}{}",
409+
// q,
410+
// if bso.payload.is_some() {
411+
// ", payload = VALUES(payload)"
412+
// } else {
413+
// ""
414+
// },
415+
// );
416+
// let q = format!(
417+
// "{}{}",
418+
// q,
419+
// if bso.ttl.is_some() {
420+
// "expiry = VALUES(expiry)"
421+
// } else {
422+
// ""
423+
// },
424+
// );
425+
// let q = format!(
426+
// "{}{}",
427+
// q,
428+
// if bso.payload.is_some() || bso.sortindex.is_some() {
429+
// "modified = VALUES(modified)"
430+
// } else {
431+
// ""
432+
// },
433+
// );
434+
// sql_query(q)
435+
// .bind::<BigInt, _>(user_id as i64) // XXX:
436+
// .bind::<Integer, _>(&collection_id)
437+
// .bind::<Text, _>(&bso.id)
438+
// .bind::<Nullable<Integer>, _>(sortindex)
439+
// .bind::<Text, _>(payload)
440+
// .bind::<BigInt, _>(timestamp)
441+
// .bind::<BigInt, _>(timestamp + (i64::from(ttl) * 1000)) // remember: this is in millis
442+
// .execute(&mut self.conn)
443+
// .await?;
444+
445+
let expiry_ts = SyncTimestamp::from_i64(timestamp + (i64::from(ttl) * 1000))?; // remember: original milli conversion
446+
let modified_ts = SyncTimestamp::from_i64(timestamp)?;
447+
diesel::insert_into(bsos::table)
448+
.values((
449+
bsos::user_id.eq(user_id as i64),
450+
bsos::collection_id.eq(&collection_id),
451+
bsos::bso_id.eq(&bso.id),
452+
bsos::sortindex.eq(sortindex),
453+
bsos::payload.eq(payload),
454+
bsos::modified.eq(modified_ts.as_naive_datetime()?),
455+
bsos::expiry.eq(expiry_ts.as_naive_datetime()?),
456+
))
457+
.on_conflict((bsos::user_id, bsos::collection_id, bsos::bso_id))
458+
.do_update()
459+
.set((
460+
bsos::user_id.eq(user_id as i64),
461+
bsos::collection_id.eq(&collection_id),
462+
bsos::bso_id.eq(&bso.id),
463+
))
442464
.execute(&mut self.conn)
443465
.await?;
444466

0 commit comments

Comments
 (0)