ImagingTools Core SDK
Public Member Functions | List of all members
imtdb::ISqlJsonXPathExtractorabstract

Database-specific JSON field extraction abstraction. More...

#include <ISqlJsonXPathExtractor.h>

Inherits istd::IPolymorphic.

Inherited by imtdb::CPostgresXPathExtractorComp [virtual], and imtdb::CSqliteXPathExtractorComp [virtual].

Public Member Functions

virtual QString ExtractXPath (const QString &jsonKey, const QString &fieldId, QMetaType::Type metaType=QMetaType::QString, const QString &tableAlias=QString()) const =0
 Extracts a JSON field using database-specific syntax.
 

Detailed Description

Database-specific JSON field extraction abstraction.

ISqlJsonXPathExtractor provides a database-agnostic interface for extracting values from JSON/JSONB columns in SQL databases. Different databases use different syntax for JSON operations (PostgreSQL uses operators, SQLite uses functions), and this interface abstracts those differences.

Purpose

Enables consistent JSON field access across database backends:

Implementations

Usage Pattern

The extractor is used internally by delegates to generate database-specific JSON queries in WHERE clauses and SELECT statements:

// PostgreSQL output: metadata->>'userName'
// SQLite output: json_extract(metadata, '$.userName')
QString extracted = extractor->ExtractXPath("metadata", "userName");
// Use in SQL query
QString whereClause = QString("WHERE %1 = 'john'").arg(extracted);

Path Syntax

The fieldId parameter uses dot notation for nested fields:

Note
Automatically handles database-specific escaping and type casting
See also
CPostgresXPathExtractorComp for PostgreSQL implementation
CSqliteXPathExtractorComp for SQLite implementation

Definition at line 62 of file ISqlJsonXPathExtractor.h.

Member Function Documentation

◆ ExtractXPath()

virtual QString imtdb::ISqlJsonXPathExtractor::ExtractXPath ( const QString &  jsonKey,
const QString &  fieldId,
QMetaType::Type  metaType = QMetaType::QString,
const QString &  tableAlias = QString() 
) const
pure virtual

Extracts a JSON field using database-specific syntax.

Generates the appropriate SQL expression to extract a value from a JSON column based on the target database's syntax.

Parameters
jsonKeyThe name of the JSON/JSONB column in the table (e.g., "metadata", "dataMetaInfo")
fieldIdThe path to the field within the JSON (e.g., "userName", "address.city", "tags[0]")
metaTypeExpected Qt type of the extracted value (for type casting)
tableAliasOptional table alias prefix (e.g., "t1" for "t1.metadata")
Returns
SQL expression string for extracting the JSON field

Examples:

PostgreSQL:

// Extract string value
QString expr = extractor->ExtractXPath("metadata", "userName");
// Returns: metadata->>'userName'
// Extract nested value
QString expr = extractor->ExtractXPath("metadata", "address.city");
// Returns: metadata#>>'{address,city}'
// Extract with table alias
QString expr = extractor->ExtractXPath("metadata", "age", QMetaType::Int, "users");
// Returns: (users.metadata->>'age')::integer

SQLite:

// Extract string value
QString expr = extractor->ExtractXPath("metadata", "userName");
// Returns: json_extract(metadata, '$.userName')
// Extract nested value
QString expr = extractor->ExtractXPath("metadata", "address.city");
// Returns: json_extract(metadata, '$.address.city')
// Extract with table alias
QString expr = extractor->ExtractXPath("metadata", "age", QMetaType::Int, "users");
// Returns: CAST(json_extract(users.metadata, '$.age') AS INTEGER)
Note
The metaType parameter enables proper type casting (important for numeric comparisons)
Array access syntax varies by database and is handled internally
Nested fields are properly escaped to prevent SQL injection
See also
CPostgresXPathExtractorComp, CSqliteXPathExtractorComp