trash_sql.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. use crate::errors::FlowyError;
  2. use diesel::sql_types::Integer;
  3. use flowy_database::{
  4. prelude::*,
  5. schema::{trash_table, trash_table::dsl},
  6. SqliteConnection,
  7. };
  8. use folder_model::{TrashRevision, TrashTypeRevision};
  9. pub struct TrashTableSql();
  10. impl TrashTableSql {
  11. pub(crate) fn create_trash(trashes: Vec<TrashRevision>, conn: &SqliteConnection) -> Result<(), FlowyError> {
  12. for trash_rev in trashes {
  13. let trash_table: TrashTable = trash_rev.into();
  14. match diesel_record_count!(trash_table, &trash_table.id, conn) {
  15. 0 => diesel_insert_table!(trash_table, trash_table.clone(), conn),
  16. _ => {
  17. let changeset = TrashChangeset::from(trash_table);
  18. diesel_update_table!(trash_table, changeset, conn)
  19. }
  20. }
  21. }
  22. Ok(())
  23. }
  24. pub(crate) fn read_all(conn: &SqliteConnection) -> Result<Vec<TrashRevision>, FlowyError> {
  25. let trash_tables = dsl::trash_table.load::<TrashTable>(conn)?;
  26. let items = trash_tables
  27. .into_iter()
  28. .map(TrashRevision::from)
  29. .collect::<Vec<TrashRevision>>();
  30. Ok(items)
  31. }
  32. pub(crate) fn delete_all(conn: &SqliteConnection) -> Result<(), FlowyError> {
  33. let _ = diesel::delete(dsl::trash_table).execute(conn)?;
  34. Ok(())
  35. }
  36. pub(crate) fn read(trash_id: &str, conn: &SqliteConnection) -> Result<TrashTable, FlowyError> {
  37. let trash_table = dsl::trash_table
  38. .filter(trash_table::id.eq(trash_id))
  39. .first::<TrashTable>(conn)?;
  40. Ok(trash_table)
  41. }
  42. pub(crate) fn delete_trash(trash_id: &str, conn: &SqliteConnection) -> Result<(), FlowyError> {
  43. diesel_delete_table!(trash_table, trash_id, conn);
  44. Ok(())
  45. }
  46. }
  47. #[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)]
  48. #[table_name = "trash_table"]
  49. pub(crate) struct TrashTable {
  50. pub id: String,
  51. pub name: String,
  52. pub desc: String,
  53. pub modified_time: i64,
  54. pub create_time: i64,
  55. pub ty: SqlTrashType,
  56. }
  57. // impl std::convert::From<TrashTable> for Trash {
  58. // fn from(table: TrashTable) -> Self {
  59. // Trash {
  60. // id: table.id,
  61. // name: table.name,
  62. // modified_time: table.modified_time,
  63. // create_time: table.create_time,
  64. // ty: table.ty.into(),
  65. // }
  66. // }
  67. // }
  68. //
  69. impl std::convert::From<TrashTable> for TrashRevision {
  70. fn from(trash: TrashTable) -> Self {
  71. TrashRevision {
  72. id: trash.id,
  73. name: trash.name,
  74. modified_time: trash.modified_time,
  75. create_time: trash.create_time,
  76. ty: trash.ty.into(),
  77. }
  78. }
  79. }
  80. impl std::convert::From<TrashRevision> for TrashTable {
  81. fn from(trash_rev: TrashRevision) -> Self {
  82. TrashTable {
  83. id: trash_rev.id,
  84. name: trash_rev.name,
  85. desc: "".to_string(),
  86. modified_time: trash_rev.modified_time,
  87. create_time: trash_rev.create_time,
  88. ty: trash_rev.ty.into(),
  89. }
  90. }
  91. }
  92. #[derive(AsChangeset, Identifiable, Clone, Default, Debug)]
  93. #[table_name = "trash_table"]
  94. pub(crate) struct TrashChangeset {
  95. pub id: String,
  96. pub name: Option<String>,
  97. pub modified_time: i64,
  98. }
  99. impl std::convert::From<TrashTable> for TrashChangeset {
  100. fn from(trash: TrashTable) -> Self {
  101. TrashChangeset {
  102. id: trash.id,
  103. name: Some(trash.name),
  104. modified_time: trash.modified_time,
  105. }
  106. }
  107. }
  108. #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, FromSqlRow, AsExpression)]
  109. #[repr(i32)]
  110. #[sql_type = "Integer"]
  111. pub(crate) enum SqlTrashType {
  112. Unknown = 0,
  113. View = 1,
  114. App = 2,
  115. }
  116. impl std::convert::From<i32> for SqlTrashType {
  117. fn from(value: i32) -> Self {
  118. match value {
  119. 0 => SqlTrashType::Unknown,
  120. 1 => SqlTrashType::View,
  121. 2 => SqlTrashType::App,
  122. _o => SqlTrashType::Unknown,
  123. }
  124. }
  125. }
  126. impl_sql_integer_expression!(SqlTrashType);
  127. impl std::convert::From<SqlTrashType> for TrashTypeRevision {
  128. fn from(ty: SqlTrashType) -> Self {
  129. match ty {
  130. SqlTrashType::Unknown => TrashTypeRevision::Unknown,
  131. SqlTrashType::View => TrashTypeRevision::TrashView,
  132. SqlTrashType::App => TrashTypeRevision::TrashApp,
  133. }
  134. }
  135. }
  136. impl std::convert::From<TrashTypeRevision> for SqlTrashType {
  137. fn from(ty: TrashTypeRevision) -> Self {
  138. match ty {
  139. TrashTypeRevision::Unknown => SqlTrashType::Unknown,
  140. TrashTypeRevision::TrashView => SqlTrashType::View,
  141. TrashTypeRevision::TrashApp => SqlTrashType::App,
  142. }
  143. }
  144. }