sqlparser: Support jsonb_each and jsonb_tree

This commit is contained in:
Simon Binder
2025-11-04 23:15:36 +01:00
parent 58f5d9bc11
commit 0978a266e6
4 changed files with 38 additions and 25 deletions
+4
View File
@@ -1,3 +1,7 @@
## 0.42.1-dev
- Support new features introduced in SQLite version 3.51.0.
## 0.42.0
- Fix unresolved references in CTEs resolving against the scope of the main query.
+19 -20
View File
@@ -9,8 +9,19 @@ class Json1Extension implements Extension {
engine
..registerFunctionHandler(_Json1Functions(supportsJsonb))
..registerTableValuedFunctionHandler(const _JsonEachFunction())
..registerTableValuedFunctionHandler(const _JsonTreeFunction());
..registerTableValuedFunctionHandler(
const _JsonTableValuedFunction('json_each'))
..registerTableValuedFunctionHandler(
const _JsonTableValuedFunction('json_tree'));
if (engine.options.version >= SqliteVersion.v3_51) {
// These have been added in 3.51.
engine
..registerTableValuedFunctionHandler(
const _JsonTableValuedFunction('jsonb_each'))
..registerTableValuedFunctionHandler(
const _JsonTableValuedFunction('jsonb_tree'));
}
}
}
@@ -108,8 +119,7 @@ final _jsonFunctionResultSet = CustomResultSet([
// https://www.sqlite.org/json1.html#the_json_each_and_json_tree_table_valued_functions
// we use string for any
TableColumn('key', const ResolvedType(type: BasicType.text)),
TableColumn(
'value', const ResolvedType(type: BasicType.text, nullable: true)),
TableColumn('value', const ResolvedType()),
TableColumn('type', const ResolvedType(type: BasicType.text)),
TableColumn('atom', const ResolvedType(type: BasicType.text)),
TableColumn('type', const ResolvedType(type: BasicType.text)),
@@ -119,8 +129,11 @@ final _jsonFunctionResultSet = CustomResultSet([
TableColumn('fullkey', const ResolvedType(type: BasicType.text)),
]);
abstract class _JsonTableValuedFunction implements TableValuedFunctionHandler {
const _JsonTableValuedFunction();
final class _JsonTableValuedFunction implements TableValuedFunctionHandler {
@override
final String functionName;
const _JsonTableValuedFunction(this.functionName);
@override
ResultSet resolveTableValued(
@@ -128,17 +141,3 @@ abstract class _JsonTableValuedFunction implements TableValuedFunctionHandler {
return _jsonFunctionResultSet;
}
}
class _JsonEachFunction extends _JsonTableValuedFunction {
const _JsonEachFunction();
@override
String get functionName => 'json_each';
}
class _JsonTreeFunction extends _JsonTableValuedFunction {
const _JsonTreeFunction();
@override
String get functionName => 'json_tree';
}
+8 -5
View File
@@ -93,6 +93,14 @@ class SqliteVersion implements Comparable<SqliteVersion> {
/// can't provide analysis warnings when using recent sqlite3 features.
static const SqliteVersion minimum = SqliteVersion.v3(34);
/// The highest sqlite version supported by this `sqlparser` package.
///
/// Newer features in `sqlite3` may not be recognized by this library.
static const SqliteVersion current = v3_51;
/// Version `3.51.0` added the `jsonb_each` and `jsonb_tree` functions.
static const SqliteVersion v3_51 = SqliteVersion.v3(48);
/// Version `3.50.0` added the `unistr` and `unistr_quote` functions.
static const SqliteVersion v3_50 = SqliteVersion.v3(48);
@@ -134,11 +142,6 @@ class SqliteVersion implements Comparable<SqliteVersion> {
/// Version `3.35.0` of `sqlite3`.
static const SqliteVersion v3_35 = SqliteVersion.v3(35);
/// The highest sqlite version supported by this `sqlparser` package.
///
/// Newer features in `sqlite3` may not be recognized by this library.
static const SqliteVersion current = v3_48;
/// The major version of sqlite.
///
/// This will always be `3` in the foreseeable future.
@@ -88,6 +88,13 @@ SELECT DISTINCT user.name
expect(result.errors, isEmpty);
});
test('can use jsonb table-valued functions', () {
final engine = SqlEngine(EngineOptions(version: SqliteVersion.v3_51));
final result = engine.analyze('SELECT * FROM jsonb_each(?)');
expect(result.errors, isEmpty);
});
});
test('does not allow jsonb functions before 3.45', () {