neosqlite.collection.query_helper package

Submodules

Module contents

class neosqlite.collection.query_helper.QueryHelper(collection)[source]

Bases: CRUDOperationsMixin, UpdateOperationsMixin, QueryBuilderMixin, AggregationMixin, QueryOptimizerMixin

A helper class for the QueryEngine that provides methods for building queries, performing updates, and processing aggregation pipelines.

This class contains the core logic for translating MongoDB-like queries and operations into SQL statements that can be executed against the SQLite database. It handles both simple operations that can be done directly with SQL JSON functions and complex operations that require Python-based processing.

The class is composed of several mixins: - CRUDOperationsMixin: Insert, replace, delete operations - UpdateOperationsMixin: Update operations (SQL and Python-based) - QueryBuilderMixin: WHERE clause building and query application - AggregationMixin: Aggregation pipeline processing - QueryOptimizerMixin: Query optimization and cost estimation

__init__(collection)[source]

Initialize the QueryHelper with a collection.

Parameters:

collection – The collection instance this QueryHelper will operate on.

cleanup() None[source]

Clean up resources used by the QueryHelper.

_normalize_id_query(query: dict[str, Any]) dict[str, Any][source]

Normalize ID types in a query dictionary to correct common mismatches.

This method delegates to the centralized normalize_id_query_for_db function to ensure consistent ID handling across all NeoSQLite components.

Parameters:

query – The query dictionary to process

Returns:

A new query dictionary with corrected ID types

_get_integer_id_for_oid(oid: Any) int[source]

Get the integer ID for a given ObjectId or other ID type.

Parameters:

oid – The ID value (can be ObjectId, int, str, etc.)

Returns:

The integer ID from the database

Return type:

int

_validate_json_document(json_str: str) bool[source]

Validate JSON document using SQLite’s json_valid function.

Parameters:

json_str – The JSON string to validate

Returns:

True if valid, False otherwise

Return type:

bool

_get_json_error_position(json_str: str) int[source]

Get position of JSON error using json_error_position().

Parameters:

json_str – The JSON string to check

Returns:

Position of error, or -1 if valid/not supported

Return type:

int

_build_expr_where_clause(query: dict[str, Any]) tuple[str, list[Any], list[str]] | None[source]

Build a SQL WHERE clause for $expr queries using the 3-tier approach. Also handles other query fields combined with $expr.

Tier Selection Logic: - Tier 1 (Simple): Direct SQL WHERE with json_extract/jsonb_extract - Tier 2 (Complex): Temporary tables with pre-computed field extractions - Tier 3 (Fallback): Python evaluation for unsupported operations

Parameters:

query – Query dictionary containing $expr and potentially other fields

Returns:

Tuple of (SQL WHERE clause, parameters, tables) or None for Python fallback

_build_other_fields_clause(query: dict[str, Any], expr: dict[str, Any]) tuple[str, list[Any]] | None[source]

Helper to build WHERE clause for non-$expr fields.

_analyze_expr_complexity(expr: dict[str, Any]) int[source]

Analyze expression complexity to determine appropriate tier.

Complexity scoring: - Base expression: 1 point - Each nested operator: +1 point - Arithmetic operators: +1 point each - Conditional operators: +2 points each - Array operators: +2 points each - Type conversion: +1 point each

Tier thresholds: - 1-2: Tier 1 (simple SQL WHERE) - 3-8: Tier 2 (temporary tables) - 9+: Tier 3 (Python fallback)

Parameters:

expr – The $expr expression

Returns:

Complexity score

Return type:

int

_combine_expr_with_other_fields(sql_expr: str, params: list[Any], query: dict[str, Any], expr: dict[str, Any]) tuple[str, list[Any], list[str]] | None[source]

Combine $expr SQL with other query fields.

Parameters:
  • sql_expr – The $expr SQL expression

  • params – SQL parameters

  • query – Full query dictionary

  • expr – The $expr expression

Returns:

Tuple of (WHERE clause, parameters, tables) or None for Python fallback