view_sql.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. use crate::{
  2. entities::{
  3. trash::{Trash, TrashType},
  4. view::UpdateViewParams,
  5. },
  6. errors::FlowyError,
  7. services::persistence::version_1::app_sql::AppTable,
  8. };
  9. use diesel::sql_types::Integer;
  10. use flowy_database::{
  11. prelude::*,
  12. schema::{view_table, view_table::dsl},
  13. SqliteConnection,
  14. };
  15. use flowy_folder_data_model::revision::{ViewDataTypeRevision, ViewRevision};
  16. use lib_infra::util::timestamp;
  17. pub struct ViewTableSql();
  18. impl ViewTableSql {
  19. pub(crate) fn create_view(view_rev: ViewRevision, conn: &SqliteConnection) -> Result<(), FlowyError> {
  20. let view_table = ViewTable::new(view_rev);
  21. match diesel_record_count!(view_table, &view_table.id, conn) {
  22. 0 => diesel_insert_table!(view_table, &view_table, conn),
  23. _ => {
  24. let changeset = ViewChangeset::from_table(view_table);
  25. diesel_update_table!(view_table, changeset, conn)
  26. }
  27. }
  28. Ok(())
  29. }
  30. pub(crate) fn read_view(view_id: &str, conn: &SqliteConnection) -> Result<ViewTable, FlowyError> {
  31. // https://docs.diesel.rs/diesel/query_builder/struct.UpdateStatement.html
  32. // let mut filter =
  33. // dsl::view_table.filter(view_table::id.eq(view_id)).into_boxed();
  34. // if let Some(is_trash) = is_trash {
  35. // filter = filter.filter(view_table::is_trash.eq(is_trash));
  36. // }
  37. // let repeated_view = filter.first::<ViewTable>(conn)?;
  38. let view_table = dsl::view_table
  39. .filter(view_table::id.eq(view_id))
  40. .first::<ViewTable>(conn)?;
  41. Ok(view_table)
  42. }
  43. // belong_to_id will be the app_id or view_id.
  44. pub(crate) fn read_views(belong_to_id: &str, conn: &SqliteConnection) -> Result<Vec<ViewTable>, FlowyError> {
  45. let view_tables = dsl::view_table
  46. .filter(view_table::belong_to_id.eq(belong_to_id))
  47. .order(view_table::create_time.asc())
  48. .into_boxed()
  49. .load::<ViewTable>(conn)?;
  50. Ok(view_tables)
  51. }
  52. pub(crate) fn update_view(changeset: ViewChangeset, conn: &SqliteConnection) -> Result<(), FlowyError> {
  53. diesel_update_table!(view_table, changeset, conn);
  54. Ok(())
  55. }
  56. pub(crate) fn delete_view(view_id: &str, conn: &SqliteConnection) -> Result<(), FlowyError> {
  57. diesel_delete_table!(view_table, view_id, conn);
  58. Ok(())
  59. }
  60. }
  61. #[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)]
  62. #[belongs_to(AppTable, foreign_key = "belong_to_id")]
  63. #[table_name = "view_table"]
  64. pub(crate) struct ViewTable {
  65. pub id: String,
  66. pub belong_to_id: String,
  67. pub name: String,
  68. pub desc: String,
  69. pub modified_time: i64,
  70. pub create_time: i64,
  71. pub thumbnail: String,
  72. pub view_type: SqlViewDataType,
  73. pub version: i64,
  74. pub is_trash: bool,
  75. pub ext_data: String,
  76. }
  77. impl ViewTable {
  78. pub fn new(view_rev: ViewRevision) -> Self {
  79. let data_type = match view_rev.data_type {
  80. ViewDataTypeRevision::TextBlock => SqlViewDataType::Block,
  81. ViewDataTypeRevision::Grid => SqlViewDataType::Grid,
  82. };
  83. ViewTable {
  84. id: view_rev.id,
  85. belong_to_id: view_rev.belong_to_id,
  86. name: view_rev.name,
  87. desc: view_rev.desc,
  88. modified_time: view_rev.modified_time,
  89. create_time: view_rev.create_time,
  90. thumbnail: view_rev.thumbnail,
  91. view_type: data_type,
  92. ext_data: view_rev.ext_data,
  93. version: view_rev.version,
  94. is_trash: false,
  95. }
  96. }
  97. }
  98. impl std::convert::From<ViewTable> for ViewRevision {
  99. fn from(table: ViewTable) -> Self {
  100. let data_type = match table.view_type {
  101. SqlViewDataType::Block => ViewDataTypeRevision::TextBlock,
  102. SqlViewDataType::Grid => ViewDataTypeRevision::Grid,
  103. };
  104. ViewRevision {
  105. id: table.id,
  106. belong_to_id: table.belong_to_id,
  107. name: table.name,
  108. desc: table.desc,
  109. data_type,
  110. belongings: vec![],
  111. modified_time: table.modified_time,
  112. version: table.version,
  113. create_time: table.create_time,
  114. ext_data: "".to_string(),
  115. thumbnail: table.thumbnail,
  116. // Store the view in ViewTable was deprecated since v0.0.2.
  117. // No need to worry about plugin_type.
  118. plugin_type: 0,
  119. }
  120. }
  121. }
  122. impl std::convert::From<ViewTable> for Trash {
  123. fn from(table: ViewTable) -> Self {
  124. Trash {
  125. id: table.id,
  126. name: table.name,
  127. modified_time: table.modified_time,
  128. create_time: table.create_time,
  129. ty: TrashType::TrashView,
  130. }
  131. }
  132. }
  133. #[derive(AsChangeset, Identifiable, Clone, Default, Debug)]
  134. #[table_name = "view_table"]
  135. pub struct ViewChangeset {
  136. pub id: String,
  137. pub name: Option<String>,
  138. pub desc: Option<String>,
  139. pub thumbnail: Option<String>,
  140. pub modified_time: i64,
  141. }
  142. impl ViewChangeset {
  143. pub(crate) fn new(params: UpdateViewParams) -> Self {
  144. ViewChangeset {
  145. id: params.view_id,
  146. name: params.name,
  147. desc: params.desc,
  148. thumbnail: params.thumbnail,
  149. modified_time: timestamp(),
  150. }
  151. }
  152. pub(crate) fn from_table(table: ViewTable) -> Self {
  153. ViewChangeset {
  154. id: table.id,
  155. name: Some(table.name),
  156. desc: Some(table.desc),
  157. thumbnail: Some(table.thumbnail),
  158. modified_time: table.modified_time,
  159. }
  160. }
  161. }
  162. #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, FromSqlRow, AsExpression)]
  163. #[repr(i32)]
  164. #[sql_type = "Integer"]
  165. pub enum SqlViewDataType {
  166. Block = 0,
  167. Grid = 1,
  168. }
  169. impl std::default::Default for SqlViewDataType {
  170. fn default() -> Self {
  171. SqlViewDataType::Block
  172. }
  173. }
  174. impl std::convert::From<i32> for SqlViewDataType {
  175. fn from(value: i32) -> Self {
  176. match value {
  177. 0 => SqlViewDataType::Block,
  178. 1 => SqlViewDataType::Grid,
  179. o => {
  180. log::error!("Unsupported view type {}, fallback to ViewType::Block", o);
  181. SqlViewDataType::Block
  182. }
  183. }
  184. }
  185. }
  186. impl SqlViewDataType {
  187. pub fn value(&self) -> i32 {
  188. *self as i32
  189. }
  190. }
  191. impl_sql_integer_expression!(SqlViewDataType);