feat(sql): named-query tag model — read-only SELECTs declared on the driver (lmxopcua#753)
SqlTagModel.Query (design §5.4) is now served end to end instead of refused. The driver config declares `queries` (name -> sql, params, maxRows); a tag names the query, the result `column` it reads and optionally a `rowKey` picking its row. - Parser/deploy gate: query + column required, rowKey all-or-nothing; Inspect explains each refusal. Table is not read for this model. - SqlReadOnlyStatement: single SELECT/WITH only, no data/schema-changing keyword outside literals/comments (refuses SELECT INTO, FOR UPDATE, DML CTEs). Shared by the factory and the AdminUI driver form (SqlNamedQueryRules.Describe), so the form refuses what the driver refuses. One bad entry fails the driver. - Parameters are always bound; the factory refuses a declared parameter whose dialect marker the statement never uses. - Reader: one execution per query per poll, inside a transaction that is always rolled back; result columns matched exact-then-unique-CI; no row -> BadNoData, ambiguous row / missing column / over maxRows -> BadConfigurationError. - Catalog gate passes Query tags through (nothing is quoted into SQL); a Query-only driver skips the catalog load. A tag naming an undeclared query is skipped with the reason on /hosts. - AdminUI: typed editor offers Query (query/column/rowKey); driver form edits `queries` as JSON and keeps the last valid set on a typo. (lmxopcua#753) Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -56,12 +56,18 @@ public static class SqlGroupPlanner
|
||||
/// <see cref="ISqlDialect.QuoteIdentifier"/>). <see cref="SqlEquipmentTagParser"/> rejects all of these
|
||||
/// up front, so reaching one here means a caller built a definition by hand with a broken invariant.
|
||||
/// </exception>
|
||||
/// <exception cref="NotSupportedException">
|
||||
/// A tag carries <see cref="SqlTagModel.Query"/> (design §5.4, deferred to P3).
|
||||
/// <b>Deliberately loud rather than skipped:</b> silently dropping a tag would leave an authored node
|
||||
/// permanently unfed with nothing in the log, and the parser makes this branch unreachable in practice.
|
||||
/// </exception>
|
||||
public static IReadOnlyList<SqlQueryPlan> Plan(IEnumerable<SqlTagDefinition> tags, ISqlDialect dialect)
|
||||
/// <exception cref="NotSupportedException">A tag carries a model this planner does not know.</exception>
|
||||
/// <param name="queries">
|
||||
/// The driver's validated named queries, which a <see cref="SqlTagModel.Query"/> tag needs: every tag
|
||||
/// naming one query folds into ONE plan running that query's own statement (design §5.4). Null when the
|
||||
/// driver declares none; a Query tag naming a query this does not hold throws
|
||||
/// <see cref="ArgumentException"/> (the driver drops such tags at Initialize, so that is a broken caller
|
||||
/// invariant).
|
||||
/// </param>
|
||||
public static IReadOnlyList<SqlQueryPlan> Plan(
|
||||
IEnumerable<SqlTagDefinition> tags,
|
||||
ISqlDialect dialect,
|
||||
IReadOnlyDictionary<string, SqlNamedQuery>? queries = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(tags);
|
||||
ArgumentNullException.ThrowIfNull(dialect);
|
||||
@@ -97,6 +103,9 @@ public static class SqlGroupPlanner
|
||||
case SqlTagModel.WideRow:
|
||||
plans.Add(BuildWideRowPlan(key, members, dialect));
|
||||
break;
|
||||
case SqlTagModel.Query:
|
||||
plans.Add(BuildQueryPlan(key, members, dialect, queries));
|
||||
break;
|
||||
default:
|
||||
throw UnsupportedModel(members[0]);
|
||||
}
|
||||
@@ -106,8 +115,8 @@ public static class SqlGroupPlanner
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Composes the equality key that decides which tags share a query. Both models produce a 5-tuple whose
|
||||
/// first element is the model, so a key-value key can never compare equal to a wide-row key.
|
||||
/// Composes the equality key that decides which tags share a query. Every model produces a 5-tuple whose
|
||||
/// first element is the model, so keys of different models can never compare equal.
|
||||
/// <list type="bullet">
|
||||
/// <item>
|
||||
/// <see cref="SqlTagModel.KeyValue"/> → <c>(model, table, keyColumn, valueColumn, timestampColumn)</c>.
|
||||
@@ -131,9 +140,49 @@ public static class SqlGroupPlanner
|
||||
SqlTagModel.KeyValue => (tag.Model, tag.Table, tag.KeyColumn, tag.ValueColumn, tag.TimestampColumn),
|
||||
SqlTagModel.WideRow => (tag.Model, tag.Table, tag.RowSelectorColumn, tag.RowSelectorValue,
|
||||
tag.RowSelectorTopByTimestamp),
|
||||
// One execution per named query, whatever columns or rows its members pick out of the result.
|
||||
SqlTagModel.Query => (tag.Model, tag.QueryName, (string?)null, (string?)null, (string?)null),
|
||||
_ => throw UnsupportedModel(tag),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Emits one plan for a named query: the query's own (already-validated, read-only) statement, with
|
||||
/// each declared parameter spelled by the dialect's marker and bound under its bind name.
|
||||
/// <para><b>No identifier from a tag reaches this text.</b> A Query tag's <c>column</c>,
|
||||
/// <c>rowKey.column</c> and <c>timestampColumn</c> are matched against the result set's column names
|
||||
/// by the reader; they are never quoted into SQL, so they need no catalog gate.</para>
|
||||
/// <para><b>The row cap is client-side only.</b> An arbitrary statement cannot be wrapped in a
|
||||
/// provider row limit portably (a T-SQL CTE cannot sit in a derived table), so
|
||||
/// <see cref="SqlQueryPlan.RowCap"/> is the query's <c>maxRows</c> and the reader stops reading one row
|
||||
/// past it.</para>
|
||||
/// </summary>
|
||||
private static SqlQueryPlan BuildQueryPlan(
|
||||
object groupKey,
|
||||
List<SqlTagDefinition> members,
|
||||
ISqlDialect dialect,
|
||||
IReadOnlyDictionary<string, SqlNamedQuery>? queries)
|
||||
{
|
||||
var first = members[0];
|
||||
var name = first.QueryName;
|
||||
if (string.IsNullOrWhiteSpace(name)) throw MissingField(nameof(SqlTagDefinition.QueryName), first);
|
||||
if (queries is null || !queries.TryGetValue(name, out var query))
|
||||
throw new ArgumentException(
|
||||
$"Sql tag '{first.Name}' reads named query '{name}', which the driver config does not define.",
|
||||
nameof(queries));
|
||||
|
||||
var markers = new List<string>(query.ParameterNames.Count);
|
||||
var bindNames = new List<string>(query.ParameterNames.Count);
|
||||
foreach (var parameter in query.ParameterNames)
|
||||
{
|
||||
markers.Add(dialect.ParameterMarker(parameter));
|
||||
bindNames.Add(dialect.ParameterBindName(parameter));
|
||||
}
|
||||
|
||||
return new SqlQueryPlan(
|
||||
SqlTagModel.Query, groupKey, query.Sql, markers, bindNames, query.ParameterValues, members,
|
||||
SelectedColumns: [], RowCap: query.MaxRows);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Emits one key-value plan per <b>chunk</b> of the group's distinct keys, sized by
|
||||
/// <see cref="ISqlDialect.MaxBindParameters"/>. A group at or under the ceiling — every ordinary group
|
||||
@@ -346,6 +395,5 @@ public static class SqlGroupPlanner
|
||||
=> new($"Sql tag '{tag.Name}' ({tag.Model}) is missing the required '{field}'.");
|
||||
|
||||
private static NotSupportedException UnsupportedModel(SqlTagDefinition tag)
|
||||
=> new($"Sql tag '{tag.Name}' uses the {tag.Model} model, which the poll planner does not support. " +
|
||||
"v1 plans KeyValue and WideRow only; the named-query model is deferred (design §5.4).");
|
||||
=> new($"Sql tag '{tag.Name}' uses the {tag.Model} model, which the poll planner does not support.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user