feat(sql): opt-in writes — allowWrites + per-tag writable, single-row parameterised UPDATE (lmxopcua#753)
v2-ci / unit-tests (push) Waiting to run
v2-ci / unit-tests (push) Waiting to run
allowWrites is no longer inert: SqlDriver implements IWritable, default off. - Gates (outermost first): node Writable + WriteOperate (server), driver allowWrites, the tag's own TagConfig "writable": true (strict JSON boolean; anything else refuses the tag, and the deploy gate says why), and a model that names exactly one row — KeyValue, or WideRow by where-pair. topByTimestamp and Query are BadNotWritable. - SqlWritePlanner: the only statement a write can become is UPDATE <table> SET <value col> = @v WHERE <key col> = @k, built from the catalog-gate-canonical identifiers (dialect-quoted) with value and key bound; value-before-key marker order so positional ODBC binds correctly. No authored write SQL, INSERT, UPSERT or procedure. - Executed in a transaction that commits only when exactly one row was touched: 0 rows -> BadNoData (never inserts), >1 -> rolled back, BadConfigurationError, unknown count -> rolled back, BadNotSupported. Bounded by operationTimeout; provider errors -> BadCommunicationError; health untouched. - Discovery advertises Operate only where a write could succeed. - AdminUI: driver form's Allow writes switch is live; tag editor gains a per-tag write switch; model validates writable strictly (parity corpus). - Factory no longer warns "allowWrites ignored"; it logs whether writes are on. - Tests: SqlWriteTests (planner SQL/bind order/injection/coercion, every gate, readback, absent key, two-row rollback, gate-rejected tag, discovery); MySqlReadTests write legs against InnoDB (keyed row only, two-row rollback, server-side grant refusal, default-off). Seed adds a setpoints table and the one UPDATE grant. (lmxopcua#753) Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -44,10 +44,10 @@ public sealed class SqlDriverConfigDto
|
||||
public bool? NullIsBad { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Master write kill-switch. <b>v1 is read-only and this field is inert</b> — the driver does not
|
||||
/// implement <c>IWritable</c>, so no value here can produce a write. The factory <em>warns</em> when it
|
||||
/// is authored <see langword="true"/> rather than failing the driver, since the flag cannot do harm but
|
||||
/// an operator who believes writes are enabled can.
|
||||
/// Master write switch (design §3.4, lmxopcua#753). Absent or <see langword="false"/> ⇒ every write is
|
||||
/// refused. <see langword="true"/> ⇒ a tag may be written when its own TagConfig also says
|
||||
/// <c>"writable": true</c> and its model names exactly one row; the node additionally needs its Writable
|
||||
/// flag and the caller the WriteOperate role.
|
||||
/// </summary>
|
||||
public bool? AllowWrites { get; init; }
|
||||
|
||||
|
||||
@@ -56,6 +56,16 @@ public static class SqlEquipmentTagParser
|
||||
|
||||
var timestampColumn = ReadString(root, "timestampColumn");
|
||||
|
||||
// The per-tag write opt-in (design §3.4). Strict like the enums: absent ⇒ read-only, a JSON boolean ⇒
|
||||
// that, anything else (a "true" string, a number) ⇒ the tag is refused rather than guessed at — a
|
||||
// write permission is the last field to coerce leniently.
|
||||
var writable = false;
|
||||
if (root.TryGetProperty("writable", out var writableEl))
|
||||
{
|
||||
if (writableEl.ValueKind is not (JsonValueKind.True or JsonValueKind.False)) return false;
|
||||
writable = writableEl.GetBoolean();
|
||||
}
|
||||
|
||||
if (model == SqlTagModel.Query)
|
||||
{
|
||||
// A named query reads no table of its own: the tag names the driver-config query, the result
|
||||
@@ -77,7 +87,7 @@ public static class SqlEquipmentTagParser
|
||||
Name: rawPath, Model: model, Table: string.Empty,
|
||||
TimestampColumn: timestampColumn, ColumnName: column,
|
||||
DeclaredType: declaredType, QueryName: queryName,
|
||||
RowKeyColumn: rowKeyColumn, RowKeyValue: rowKeyValue);
|
||||
RowKeyColumn: rowKeyColumn, RowKeyValue: rowKeyValue, Writable: writable);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -98,7 +108,7 @@ public static class SqlEquipmentTagParser
|
||||
def = new SqlTagDefinition(
|
||||
Name: rawPath, Model: model, Table: table!,
|
||||
KeyColumn: keyColumn, KeyValue: keyValue, ValueColumn: valueColumn,
|
||||
TimestampColumn: timestampColumn, DeclaredType: declaredType);
|
||||
TimestampColumn: timestampColumn, DeclaredType: declaredType, Writable: writable);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -122,7 +132,7 @@ public static class SqlEquipmentTagParser
|
||||
RowSelectorColumn: hasWherePair ? selectorColumn : null,
|
||||
RowSelectorValue: hasWherePair ? selectorValue : null,
|
||||
RowSelectorTopByTimestamp: hasWherePair ? null : topByTimestamp,
|
||||
DeclaredType: declaredType);
|
||||
DeclaredType: declaredType, Writable: writable);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,11 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts;
|
||||
/// return exactly one row. Matched against the result set, never quoted into SQL.
|
||||
/// </param>
|
||||
/// <param name="RowKeyValue"><see cref="SqlTagModel.Query"/>: the <see cref="RowKeyColumn"/> value (matched as text).</param>
|
||||
/// <param name="Writable">
|
||||
/// The tag's own write opt-in (<c>"writable": true</c> in its TagConfig, design §3.4). Necessary but not
|
||||
/// sufficient: the driver also needs <c>allowWrites</c>, the model must name one row to update (KeyValue, or
|
||||
/// a WideRow where-pair), and the node needs its Writable flag plus the caller's WriteOperate role.
|
||||
/// </param>
|
||||
/// <param name="DeclaredType">
|
||||
/// Optional explicit type override from the blob's <c>type</c> field. <see langword="null"/> ⇒ the
|
||||
/// driver infers the type from the result set's column metadata.
|
||||
@@ -53,4 +58,5 @@ public sealed record SqlTagDefinition(
|
||||
DriverDataType? DeclaredType = null,
|
||||
string? QueryName = null,
|
||||
string? RowKeyColumn = null,
|
||||
string? RowKeyValue = null);
|
||||
string? RowKeyValue = null,
|
||||
bool Writable = false);
|
||||
|
||||
@@ -110,6 +110,12 @@ public static class SqlTagDefinitionFactory
|
||||
if (typeWarning is not null) warnings.Add(typeWarning);
|
||||
if (modelWarning is not null) return; // the model is unknown; the per-model checks cannot run
|
||||
|
||||
if (root.TryGetProperty("writable", out var writable)
|
||||
&& writable.ValueKind is not (JsonValueKind.True or JsonValueKind.False))
|
||||
{
|
||||
warnings.Add("Sql TagConfig 'writable' must be a JSON boolean (true/false) — the driver will skip this tag.");
|
||||
}
|
||||
|
||||
var model = TagConfigJson.ReadEnumOrDefault(root, "model", SqlTagModel.KeyValue);
|
||||
if (model == SqlTagModel.Query)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user