🔍 Code Extractor

function get_property

Maturity: 29

Retrieves a specific property value from a SQLite database table 'filedata' based on id, version, and property name.

File:
/tf/active/vicechatdev/rmcl/datacache.py
Lines:
35 - 43
Complexity:
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

  • sqlite3
  • xdg

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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function set_property 72.1% similar

    Inserts or updates a property value for a specific file ID and version in a SQLite database table named 'filedata'.

    From: /tf/active/vicechatdev/rmcl/datacache.py
  • function _get_conn 48.5% similar

    Lazy initialization function that returns a singleton SQLite database connection, creating the database and required table schema if they don't exist.

    From: /tf/active/vicechatdev/rmcl/datacache.py
  • function inspect_database 45.4% similar

    Inspects a SQLite database at a hardcoded path, examining table structure, row counts, and identifying potentially corrupted chat_session_id values in the text_sections table.

    From: /tf/active/vicechatdev/vice_ai/database_inspector.py
  • function get_file_version 44.2% similar

    Generates a version string for static files to enable cache busting, using current time in debug mode or file modification time in production.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function get_dbo_product_by_id 43.0% similar

    Retrieves a single dbo_Product node from a Neo4j graph database by its unique ID.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
← Back to Browse