1use rusqlite::{Connection, Row};
8
9use crate::catalog::CatalogVersion;
10use crate::content::Content;
11use crate::fromdb::FromDb;
12use crate::lrobject::LrId;
13
14pub struct Collection {
16 id: LrId,
18 pub name: String,
20 pub parent: LrId,
22 pub system_only: bool,
24 pub content: Option<Content>,
26}
27
28impl FromDb for Collection {
29 fn read_from(version: CatalogVersion, row: &Row) -> crate::Result<Self> {
30 match version {
31 CatalogVersion::Lr4 | CatalogVersion::Lr6 => Ok(Collection {
32 id: row.get(0)?,
33 name: row.get(2)?,
34 parent: row.get(3).unwrap_or(0),
35 system_only: !matches!(row.get::<usize, f64>(4)? as i64, 0),
36 content: None,
37 }),
38 CatalogVersion::Lr2 => {
39 let tag_type: Box<str> = row.get(3)?;
40 let name: String = row.get(1).unwrap_or_else(|_| {
41 if tag_type.as_ref() == "AgQuickCollectionTagKind" {
42 "Quick Collection"
43 } else {
44 ""
45 }
46 .to_owned()
47 });
48 match tag_type.as_ref() {
49 "AgQuickCollectionTagKind" | "AgCollectionTagKind" => Ok(Collection {
50 id: row.get(0)?,
51 name,
52 parent: row.get(2).unwrap_or(0),
53 system_only: matches!(tag_type.as_ref(), "AgQuickCollectionTagKind"),
54 content: None,
55 }),
56 _ => Err(crate::Error::Skip),
57 }
58 }
59 _ => Err(crate::Error::UnsupportedVersion),
60 }
61 }
62
63 fn read_db_tables(version: CatalogVersion) -> &'static str {
64 match version {
65 CatalogVersion::Lr4 | CatalogVersion::Lr6 => "AgLibraryCollection",
66 CatalogVersion::Lr2 => "AgLibraryTag",
67 _ => "",
68 }
69 }
70
71 fn read_db_columns(version: CatalogVersion) -> &'static str {
72 match version {
73 CatalogVersion::Lr4 | CatalogVersion::Lr6 => {
74 "id_local,genealogy,name,parent,systemOnly"
75 }
76 CatalogVersion::Lr2 => "id_local,name,parent,kindName",
77 _ => "",
78 }
79 }
80}
81
82impl Collection {
83 pub fn id(&self) -> LrId {
85 self.id
86 }
87
88 pub fn read_content(&self, conn: &Connection) -> Content {
90 Content::from_db(conn, "AgLibraryCollectionContent", "collection", self.id)
91 }
92}