diff --git a/src/db.rs b/src/db.rs index f109f04..023242d 100644 --- a/src/db.rs +++ b/src/db.rs @@ -9,6 +9,8 @@ use std::collections::HashMap; use std::path::PathBuf; use std::rc::Rc; +/// Database schema migrations for the Keep application + lazy_static! { static ref MIGRATIONS: Migrations<'static> = Migrations::new(vec![ M::up( @@ -38,27 +40,48 @@ lazy_static! { ]); } +/// Represents an item stored in the database #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Item { + /// Unique identifier for the item, None for new items pub id: Option, + /// Timestamp when the item was created pub ts: DateTime, + /// Size of the item content in bytes, None if not set pub size: Option, + /// Compression algorithm used for the item content pub compression: String, } +/// Represents a tag associated with an item #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Tag { + /// ID of the item this tag belongs to pub id: i64, + /// Name of the tag pub name: String, } +/// Represents metadata associated with an item #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Meta { + /// ID of the item this metadata belongs to pub id: i64, + /// Name of the metadata field pub name: String, + /// Value of the metadata field pub value: String, } +/// Opens a database connection and ensures the schema is up to date +/// +/// # Arguments +/// +/// * `path` - Path to the SQLite database file +/// +/// # Returns +/// +/// * `Result` - A SQLite connection on success, or an error if opening or migration fails pub fn open(path: PathBuf) -> Result { debug!("DB: Opening file: {:?}", path); let mut conn = Connection::open_with_flags( @@ -79,6 +102,16 @@ pub fn open(path: PathBuf) -> Result { Ok(conn) } +/// Inserts a new item into the database +/// +/// # Arguments +/// +/// * `conn` - Database connection +/// * `item` - Item to insert +/// +/// # Returns +/// +/// * `Result` - The ID of the newly inserted item on success, or an error if insertion fails pub fn insert_item(conn: &Connection, item: Item) -> Result { debug!("DB: Inserting item: {:?}", item); conn.execute( @@ -88,6 +121,16 @@ pub fn insert_item(conn: &Connection, item: Item) -> Result { Ok(conn.last_insert_rowid()) } +/// Creates a new item in the database with the current timestamp +/// +/// # Arguments +/// +/// * `conn` - Database connection +/// * `compression_type` - Compression type to use for the item +/// +/// # Returns +/// +/// * `Result` - The created item with its ID set, or an error if creation fails pub fn create_item(conn: &Connection, compression_type: crate::compression_engine::CompressionType) -> Result { let item = Item { id: None, @@ -102,6 +145,17 @@ pub fn create_item(conn: &Connection, compression_type: crate::compression_engin }) } +/// Adds a tag to an item +/// +/// # Arguments +/// +/// * `conn` - Database connection +/// * `item_id` - ID of the item to tag +/// * `tag_name` - Name of the tag to add +/// +/// # Returns +/// +/// * `Result<()>` - Success or error if the operation fails pub fn add_tag(conn: &Connection, item_id: i64, tag_name: &str) -> Result<()> { let tag = Tag { id: item_id, @@ -110,6 +164,18 @@ pub fn add_tag(conn: &Connection, item_id: i64, tag_name: &str) -> Result<()> { insert_tag(conn, tag) } +/// Adds metadata to an item +/// +/// # Arguments +/// +/// * `conn` - Database connection +/// * `item_id` - ID of the item +/// * `name` - Name of the metadata field +/// * `value` - Value of the metadata field +/// +/// # Returns +/// +/// * `Result<()>` - Success or error if the operation fails pub fn add_meta(conn: &Connection, item_id: i64, name: &str, value: &str) -> Result<()> { let meta = Meta { id: item_id,