pki_ca/ca_core/property.py

34 lines
1003 B
Python

def set_property(cursor, entity_id: int, property_name: str):
"""
Adds a property for the entity. If it already exists, does nothing.
"""
cursor.execute("""
INSERT INTO property (id, property_name)
VALUES (%s, %s)
ON CONFLICT (id, property_name) DO NOTHING
""", (entity_id, property_name))
def delete_property(cursor, entity_id, property_name):
"""
Remove a property from an entity.
Raises ValueError if the property does not exist.
"""
cursor.execute(
"DELETE FROM property WHERE id=%s AND property_name=%s",
(entity_id, property_name)
)
if cursor.rowcount == 0:
raise ValueError("Property not found")
def get_properties(cursor, entity_id: int):
"""
Returns a list of property names for the given entity.
"""
cursor.execute("""
SELECT property_name
FROM property
WHERE id = %s
""", (entity_id,))
return [row['property_name'] for row in cursor.fetchall()]