graflo.db.util¶
Database utilities for graph operations.
This module provides utility functions for working with database operations, including cursor handling, data serialization, and schema management.
Key Functions
- get_data_from_cursor: Retrieve data from a cursor with optional limit
- serialize_value: Serialize non-serializable values (datetime, Decimal, etc.)
- serialize_document: Serialize all values in a document dictionary
- load_reserved_words: Load reserved words for a database flavor
- sanitize_attribute_name: Sanitize attribute names to avoid reserved words
Example
from graflo.onto import DBType >>> # ArangoDB-specific AQL query (collection is ArangoDB terminology) cursor = db.execute("FOR doc IN vertex_class RETURN doc") batch = get_data_from_cursor(cursor, limit=100)
Serialize datetime objects in a document¶
doc = {"id": 1, "created_at": datetime.now()} serialized = serialize_document(doc)
Sanitize reserved words¶
reserved = load_reserved_words(DBType.TIGERGRAPH) sanitized = sanitize_attribute_name("SELECT", reserved)
get_data_from_cursor(cursor, limit=None)
¶
Retrieve data from a cursor with optional limit.
This function iterates over a database cursor and collects the results into a batch. It handles cursor iteration errors and supports an optional limit on the number of items retrieved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cursor
|
Database cursor to iterate over |
required | |
limit
|
Optional maximum number of items to retrieve |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
Batch of items retrieved from the cursor |
Note
The function will stop iteration if: - The limit is reached - The cursor is exhausted - A CursorNextError occurs
Source code in graflo/db/util.py
json_serializer(obj)
¶
JSON serializer for objects not serializable by default json code.
This function is designed to be used as the default parameter for json.dumps().
It handles datetime, date, time, and Decimal objects by converting them to
JSON-serializable types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Object to serialize |
required |
Returns:
| Type | Description |
|---|---|
|
JSON-serializable representation |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the value type is not serializable |
Example
import json from datetime import datetime data = {"id": 1, "created_at": datetime.now()} json.dumps(data, default=json_serializer) '{"id": 1, "created_at": "2023-12-25T14:30:45"}'
Source code in graflo/db/util.py
load_reserved_words(db_flavor)
¶
Load reserved words for a given database flavor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_flavor
|
DBType
|
The database flavor to load reserved words for |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of reserved words (uppercase) for the database flavor. |
set[str]
|
Returns empty set if no reserved words file exists or for unsupported flavors. |
Source code in graflo/db/util.py
sanitize_attribute_name(name, reserved_words, suffix='_attr')
¶
Sanitize an attribute name to avoid reserved words.
This function deterministically replaces reserved attribute names with modified versions. The algorithm: 1. Checks if the name (case-insensitive) is in the reserved words set 2. If reserved, appends a suffix (default: "_attr") 3. If the modified name is still reserved, appends a numeric suffix incrementally until a non-reserved name is found
The algorithm is deterministic: the same input always produces the same output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The attribute name to sanitize |
required |
reserved_words
|
set[str]
|
Set of reserved words (uppercase) to avoid |
required |
suffix
|
str
|
Suffix to append if name is reserved (default: "_attr") |
'_attr'
|
Returns:
| Type | Description |
|---|---|
str
|
Sanitized attribute name that is not in the reserved words set |
Examples:
>>> reserved = {"SELECT", "FROM", "WHERE"}
>>> sanitize_attribute_name("name", reserved)
'name'
>>> sanitize_attribute_name("SELECT", reserved)
'SELECT_attr'
>>> sanitize_attribute_name("SELECT_attr", reserved)
'SELECT_attr_1'
Source code in graflo/db/util.py
serialize_document(doc)
¶
Serialize all values in a document dictionary.
Recursively serializes all values in a document, converting datetime objects and other non-serializable types to JSON-serializable formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
dict
|
Document dictionary to serialize |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with all values serialized |
Example
from datetime import datetime doc = {"id": 1, "created_at": datetime.now(), "name": "test"} serialized = serialize_document(doc) assert isinstance(serialized["created_at"], str)
Source code in graflo/db/util.py
serialize_value(value)
¶
Serialize non-serializable values for database operations.
Converts datetime, date, time, and Decimal objects to JSON-serializable types. This is useful for databases that require JSON-serializable parameters or when serializing data for storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Value to serialize |
required |
Returns:
| Type | Description |
|---|---|
|
Serialized value: |
|
|
|
|
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If the value type is not serializable and not handled |
Example
from datetime import datetime serialize_value(datetime(2023, 12, 25, 14, 30, 45)) '2023-12-25T14:30:45' from decimal import Decimal serialize_value(Decimal('123.456')) 123.456