view_sql.rs 6.1 KB

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