Session
ORM unit of work over a graph store (SPARQLSession).
- class sparqlmodel.session.SPARQLSession(store=None, *, prefixes=None, autoflush=True, close_on_exit=True, rollback_on_error=True)[source]
Bases:
objectORM session: CRUD, queries, and graph sync with the backing store.
Use as a context manager to flush pending writes on success, discard the pending queue on error, and close the backing store when it supports
HttpStore.close()when usingHttpStore:with SPARQLSession(store=HttpStore(endpoint)) as session: session.put(model)
Already-flushed writes are not rolled back on error; only the pending queue from
put(..., flush=False)is affected. Full transactional rollback may be added in a future release.Not thread-safe; use one session per thread or asyncio task.
- __init__(store=None, *, prefixes=None, autoflush=True, close_on_exit=True, rollback_on_error=True)[source]
- classmethod from_rdf_file(path, *, format='turtle', prefixes=None, autoflush=True, close_on_exit=True, rollback_on_error=True)[source]
Open a session over an on-disk RDF file.
Uses an in-memory
MemoryStore. Parsespathwith TripleModelload_graph(pass aPath, not file contents as a string). Use for tutorials, fixtures, and ETL that starts from Turtle, TriG, or other supported formats beforeput/query.- Parameters:
format (str) – RDF format hint (e.g.
"turtle","trig"); inferred from extension when omitted onload_graph.prefixes (Mapping[str, str] | None) – Namespace prefixes merged into the session registry and graph.
autoflush (bool) – Passed to
__init__().close_on_exit (bool) – Passed to
__init__().rollback_on_error (bool) – Passed to
__init__().
- Return type:
- refresh(model, *, depth=0)[source]
Reload
modelfrom the store atdepth(updates cached instance when present).- Return type:
- merge(model)[source]
Return the session instance for
model’s identity key (no store write).- Return type:
- get(model_cls, iri, *, depth=0)[source]
Load a model by IRI with optional relationship depth.
- Return type:
SPARQLModel | None
Session-level identity map, hydration cache, and pending writes.
- class sparqlmodel.session_state.SessionState[source]
Bases:
objectIdentity map, hydration cache, and pending
putqueue for a session.- invalidate_hydration_for_iri(iri_key)[source]
Drop hydration cache entries for all model classes at
iri_key.
ORM eager-load: hydrate query results and get by relationship depth.
- sparqlmodel.hydration.validate_depth(depth)[source]
Raise if hydration depth is outside the supported range (0–2).
- sparqlmodel.hydration.hydrate_from_bindings(model_cls, bindings, store, *, depth=0)[source]
Hydrate models from SPARQL SELECT bindings.
- Return type:
- sparqlmodel.hydration.hydrate_one(model_cls, iri, store, *, depth=0, polymorphic=False)[source]
Load a single model by IRI from the store.
- Return type:
SPARQLModel | None
Async ORM unit of work over a graph store (AsyncSPARQLSession).
- class sparqlmodel.async_session.AsyncSPARQLSession(store=None, *, prefixes=None, autoflush=True, close_on_exit=True, rollback_on_error=True)[source]
Bases:
objectAsync ORM session: CRUD, queries, and graph sync with the backing store.
Use as an async context manager:
async with AsyncSPARQLSession(store=AsyncHttpStore(endpoint)) as session: await session.put(model)
Same identity map, hydration, and cascade semantics as
SPARQLSession. Not safe to share across concurrent asyncio tasks; use one session per task.- __init__(store=None, *, prefixes=None, autoflush=True, close_on_exit=True, rollback_on_error=True)[source]
- async expunge(model)[source]
Detach
modelfrom the identity map and hydration cache (store unchanged).
- async refresh(model, *, depth=0)[source]
Reload
modelfrom the store atdepth(updates cached instance when present).- Return type:
- async merge(model)[source]
Return the session instance for
model’s identity key (no store write).- Return type:
- async get(model_cls, iri, *, depth=0)[source]
Load a model by IRI with optional relationship depth.
- Return type:
SPARQLModel | None
- async hydrate_bindings(model_cls, bindings, *, depth=0, polymorphic=False)[source]
Hydrate query results with identity map and session cache.
- Return type:
Async ORM query builder; compiles Python filters to SPARQL.
- class sparqlmodel.async_query.AsyncQuery(session, model_cls)[source]
Bases:
objectAsync ORM query builder for a
SPARQLModelclass.
Shared session CRUD, hydration, and graph-sync logic (sync and async).
- sparqlmodel.session_core.sparql_has_prefix_declarations(sparql)[source]
Return True if
sparqlalready declares at least one PREFIX.- Return type:
- sparqlmodel.session_core.depth_satisfied(model, depth)[source]
Return whether
modelhas relationships loaded throughdepth.- Return type:
- sparqlmodel.session_core.invalidate_subjects(state, subjects)[source]
Expire identity and hydration cache entries for cascade subjects.
- sparqlmodel.session_core.check_stale_add(store_graph, model)[source]
Warn when
addmay duplicate triples for an existing subject.
- sparqlmodel.session_core.expunge_impl(state, model)[source]
Remove
modelfrom the identity map and hydration cache.
- sparqlmodel.session_core.expunge_all_impl(state)[source]
Clear identity map and hydration cache (pending queue unchanged).
- sparqlmodel.session_core.refresh_impl(state, store, model, *, depth)[source]
Reload
modelfrom the store graph atdepth.- Return type:
- async sparqlmodel.session_core.refresh_impl_async(state, store, model, *, depth)[source]
Reload
modelfrom the async store mirror atdepth.- Return type: