sql.rs 4.2 KB

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