function get_property
Retrieves a specific property value from a SQLite database table 'filedata' based on id, version, and property name.
/tf/active/vicechatdev/rmcl/datacache.py
35 - 43
simple
Purpose
This function queries a SQLite database to fetch a single property value associated with a specific file or object identified by its id and version. It's designed for retrieving metadata or attributes stored in a normalized database structure where properties are stored as key-value pairs. Returns None if no matching record is found.
Source Code
def get_property(id_, version, property_):
conn = _get_conn()
c = conn.cursor()
c.execute('SELECT value FROM filedata WHERE id=? AND version=? and property=?',
(id_, version, property_))
res = c.fetchone()
if res:
return res[0]
return None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
id_ |
- | - | positional_or_keyword |
version |
- | - | positional_or_keyword |
property_ |
- | - | positional_or_keyword |
Parameter Details
id_: The unique identifier for the file or object whose property is being retrieved. Expected to be a value that matches the 'id' column in the 'filedata' table (likely string or integer).
version: The version number or identifier of the file/object. Used to distinguish between different versions of the same entity. Expected type matches the 'version' column in the database (likely string or integer).
property_: The name of the property to retrieve. This is the key in a key-value storage pattern. Expected to be a string matching values in the 'property' column of the 'filedata' table.
Return Value
Returns the value of the requested property as stored in the database (type depends on what was stored, but typically string or could be any SQLite-compatible type). Returns None if no matching record is found for the given id, version, and property combination.
Dependencies
sqlite3xdg
Required Imports
import sqlite3
from xdg import xdg_cache_home
Usage Example
# Assuming _get_conn() is defined and returns a valid SQLite connection
# and the database has the required 'filedata' table
# Example usage:
file_id = "document_123"
file_version = "1.0"
property_name = "author"
author = get_property(file_id, file_version, property_name)
if author:
print(f"Author: {author}")
else:
print("Property not found")
# Retrieve another property
file_size = get_property("document_123", "1.0", "size")
if file_size:
print(f"File size: {file_size}")
Best Practices
- This function depends on an external _get_conn() function that must be defined in the same module
- The function uses parameterized queries which prevents SQL injection attacks
- Consider adding error handling for database connection failures or query errors
- The function does not close the cursor or connection, assuming connection pooling or external management
- Returns None for missing properties, so callers should check for None before using the result
- Consider adding type hints for better code documentation and IDE support
- The database schema must have a 'filedata' table with columns: id, version, property, and value
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function set_property 72.1% similar
-
function _get_conn 48.5% similar
-
function inspect_database 45.4% similar
-
function get_file_version 44.2% similar
-
function get_dbo_product_by_id 43.0% similar