Provides high-level helpers for SQLite's JSON1 and FTS5 extensions: compose JSON paths, build SQL snippets, detect availability, and register helper SQL functions.
Use cases
- Keep JSON path building type-safe—no stray quotes or typos when drilling into deeply nested structures.
- Detect feature availability at runtime so you can gracefully fall back on builds compiled without JSON1/FTS5.
- Install deterministic helper functions exactly once per connection to keep SQL migrations tidy.
Example usage
SELECT json_extract(blob, '$.user.email') FROM events;
auto path = sqlite::json::path().key("user").key("email");
auto expr = sqlite::json::extract_expression("events.blob", path);
sqlite::query q{db,
"SELECT " + expr + " AS email "
"FROM events "
"WHERE " + sqlite::fts::match_expression("events_fts") + " "
"ORDER BY fts_rank(matchinfo(events_fts)) DESC"
};
sqlite::json::register_contains_function(db);
sqlite::fts::register_rank_function(db);
These helpers just spit out SQL strings, so you can drop them straight into migrations, ORMs, or whatever query builder you already use.
sqlite::json::path_builder
| Member | Description |
|---|---|
path_builder() | Initializes the internal string with "$". |
path_builder& key(std::string_view segment) | Appends .segment (quoting automatically when needed). |
path_builder& index(std::size_t idx) | Appends array access ([idx]). |
std::string const& str() const noexcept | Returns the accumulated JSON path. |
inline path_builder path() returns a new builder rooted at $.
JSON utilities
| Function | Description |
|---|---|
std::string extract_expression(std::string_view json_expr, path_builder const& path) | Produces "json_extract(json_expr, 'path')" snippets. |
std::string contains_expression(std::string_view json_expr, path_builder const& path, std::string_view value_expr) | Returns an equality comparison between a JSON slot and value_expr. |
bool json::available(connection&) | Checks whether JSON1 is compiled into the current connection. |
void json::register_contains_function(connection&, std::string_view function_name = "json_contains_value") | Installs a deterministic scalar function that tests whether a JSON path equals the given value. |
FTS utilities
| Function | Description |
|---|---|
bool fts::available(connection&) | Verifies that FTS5 APIs are present. |
std::string fts::match_expression(std::string_view column_or_table, std::string_view query_expr = "?") | Generates "column MATCH query" fragments. |
void fts::register_rank_function(connection&, std::string_view function_name = "fts_rank") | Registers a simple ranking function that counts matched phrases. |