sage.database.postgres_backends.postgres package¶
Submodules¶
sage.database.postgres_backends.postgres.connector module¶
-
class
sage.database.postgres_backends.postgres.connector.
DefaultPostgresConnector
(table_name: str, dbname: str, user: str, password: str, host: str = '', port: int = 5432, fetch_size: int = 500)¶ Bases:
sage.database.postgres_backends.connector.PostgresConnector
A DefaultPostgresConnector search for RDF triples in a PostgreSQL database.
- Args:
table_name: Name of the SQL table containing RDF data.
dbname: the database name.
user: user name used to authenticate.
password: password used to authenticate.
host: database host address (defaults to UNIX socket if not provided).
port: connection port number (defaults to 5432 if not provided).
fetch_size: The number of SQL rows/RDF triples to fetch per batch (defaults to 500).
-
delete
(subject: str, predicate: str, obj: str) → None¶ Delete a RDF triple from the RDF graph.
- Args:
subject: Subject of the RDF triple.
predicate: Predicate of the RDF triple.
obj: Object of the RDF triple.
-
from_config
() → sage.database.postgres_backends.connector.PostgresConnector¶ Build a DefaultPostgresConnector from a configuration object.
The configuration object must contains the following fields: ‘dbname’, ‘name’, ‘user’ and ‘password’. Optional fields are: ‘host’, ‘port’ and ‘fetch_size’.
-
insert
(subject: str, predicate: str, obj: str) → None¶ Insert a RDF triple into the RDF graph.
- Args:
subject: Subject of the RDF triple.
predicate: Predicate of the RDF triple.
obj: Object of the RDF triple.
-
search
(subject: str, predicate: str, obj: str, last_read: Optional[str] = None, as_of: Optional[datetime.datetime] = None) → Tuple[sage.database.postgres_backends.postgres.iterator.PostgresIterator, int]¶ Get an iterator over all RDF triples matching a triple pattern.
- Args:
subject: Subject of the triple pattern.
predicate: Predicate of the triple pattern.
object: Object of the triple pattern.
last_read: A RDF triple ID. When set, the search is resumed for this RDF triple.
as_of: A version timestamp. When set, perform all reads against a consistent snapshot represented by this timestamp.
- Returns:
A tuple (iterator, cardinality), where iterator is a Python iterator over RDF triples matching the given triples pattern, and cardinality is the estimated cardinality of the triple pattern.
sage.database.postgres_backends.postgres.iterator module¶
-
class
sage.database.postgres_backends.postgres.iterator.
PostgresIterator
(cursor, connection, start_query: str, start_params: List[str], pattern: Dict[str, str], fetch_size: int = 500)¶ Bases:
sage.database.db_iterator.DBIterator
A PostgresIterator fetches RDF triples from a PostgreSQL table using batch queries and lazy loading.
- Args:
cursor: A psycopg cursor. This cursor must only be used for this iterator, to avoid side-effects.
connection: A psycopg connection.
start_query: Prepared SQL query executed to fetch RDF triples as SQL rows.
start_params: Parameters to use with the prepared SQL query.
pattern: Triple pattern scanned.
fetch_size: The number of SQL rows/RDF triples to fetch per batch.
-
has_next
() → bool¶ Return True if there is still results to read, False otherwise
-
last_read
() → str¶ Return the index ID of the last element read
-
next
() → Optional[Dict[str, str]]¶ Return the next solution mapping or None if there are no more solutions
sage.database.postgres_backends.postgres.queries module¶
-
sage.database.postgres_backends.postgres.queries.
get_delete_query
(table_name: str) → str¶ Build a SQL query to delete a RDF triple from a PostgreSQL table.
Argument: Name of the SQL table from which the triple will be deleted.
Returns: A prepared SQL query that can be executed with a tuple (subject, predicate, object).
-
sage.database.postgres_backends.postgres.queries.
get_insert_many_query
(table_name: str) → str¶ Build a SQL query to insert several RDF triples into a PostgreSQL table.
Argument: Name of the SQL table in which the triples will be inserted.
Returns: A prepared SQL query that can be executed with a list of tuples (subject, predicate, object).
-
sage.database.postgres_backends.postgres.queries.
get_insert_query
(table_name: str) → str¶ Build a SQL query to insert a RDF triple into a PostgreSQL table.
Argument: Name of the SQL table in which the triple will be inserted.
Returns: A prepared SQL query that can be executed with a tuple (subject, predicate, object).
-
sage.database.postgres_backends.postgres.queries.
get_resume_query
(subj: str, pred: str, obj: str, last_read: Tuple[str, str, str], table_name: str, symbol: str = '>=') → Tuple[str, str]¶ Get a prepared SQL query which resumes scanning for a triple pattern.
The SQL query rely on keyset pagination to resume query processing using an optimized Index Scan.
- Args:
subj: Subject of the triple pattern.
pred: Predicate of the triple pattern.
obj: Object of the triple pattern.
last_read: The SQL row from whoch to resume scanning.
table_name: Name of the SQL table to scan for RDF triples.
symbol: Symbol used to perform the keyset pagination. Defaults to “>=”.
- Returns:
A tuple with the prepared SQL query and its parameters.
-
sage.database.postgres_backends.postgres.queries.
get_start_query
(subj: str, pred: str, obj: str, table_name: str) → Tuple[str, List[str]]¶ Get a prepared SQL query which starts scanning for a triple pattern.
- Args:
subj: Subject of the triple pattern.
pred: Predicate of the triple pattern.
obj: Object of the triple pattern.
table_name: Name of the SQL table to scan for RDF triples.
- Returns:
A tuple with the prepared SQL query and its parameters.