function set_property
Inserts or updates a property value for a specific file ID and version in a SQLite database table named 'filedata'.
/tf/active/vicechatdev/rmcl/datacache.py
45 - 50
simple
Purpose
This function provides a database persistence layer for storing metadata properties associated with file identifiers and versions. It uses SQLite's INSERT OR REPLACE functionality to either create a new record or update an existing one if a record with the same id, version, and property already exists. The function is designed to work with a caching system, likely storing file metadata in the XDG cache directory.
Source Code
def set_property(id_, version, property_, value):
conn = _get_conn()
c = conn.cursor()
c.execute('INSERT OR REPLACE INTO filedata VALUES(?, ?, ?, ?)',
(id_, version, property_, value))
conn.commit()
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
id_ |
- | - | positional_or_keyword |
version |
- | - | positional_or_keyword |
property_ |
- | - | positional_or_keyword |
value |
- | - | positional_or_keyword |
Parameter Details
id_: A unique identifier for the file or object. This is the primary key component that identifies which file's property is being set. Expected to be a string or integer that can be stored in SQLite.
version: The version number or identifier for the file. This allows tracking properties across different versions of the same file. Expected to be a string or integer.
property_: The name of the property being set (e.g., 'size', 'checksum', 'last_modified'). This acts as a key in a key-value store pattern. Expected to be a string.
value: The value to store for the specified property. Can be any type that SQLite can handle (string, integer, float, bytes, None). The actual type depends on the property being stored.
Return Value
This function returns None. It performs a database write operation and commits the transaction, but does not return any value to indicate success. Database errors would be raised as exceptions rather than returned.
Dependencies
sqlite3xdg
Required Imports
import sqlite3
from xdg import xdg_cache_home
Usage Example
# Assuming _get_conn() is defined to return a database connection
# and the filedata table exists with columns (id, version, property, value)
import sqlite3
from xdg import xdg_cache_home
# Example usage:
set_property('file123', '1.0', 'checksum', 'abc123def456')
set_property('file123', '1.0', 'size', 2048)
set_property('file456', '2.1', 'last_modified', '2024-01-15')
# This will insert or update the property in the database
# If a record with the same id_, version, and property_ exists, it will be replaced
Best Practices
- Ensure the _get_conn() function is properly implemented and returns a valid SQLite connection
- The function commits after each insert, which may be inefficient for bulk operations. Consider batching multiple set_property calls within a single transaction for better performance
- No error handling is present - consider wrapping calls in try-except blocks to handle database errors gracefully
- The function does not validate input parameters - ensure id_, version, property_, and value are appropriate types before calling
- The INSERT OR REPLACE operation requires a unique constraint on the combination of id_, version, and property_ columns in the filedata table
- Consider adding connection pooling or connection reuse if this function is called frequently to avoid connection overhead
- The function does not close the cursor after use, though this is typically handled by Python's garbage collector
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_property 72.1% similar
-
function _get_conn 48.7% similar
-
function push_changes 44.1% similar
-
class DatabaseManager_v1 43.9% similar
-
function direct_fix 43.8% similar