@@ -506,8 +506,15 @@ class GenericSqlWorkspaceState extends material.State<GenericSqlWorkspace> {
506506
507507 final Map <String , SqlResultGridSchema > _gridSchemaCache = {};
508508
509- static String _schemaCacheKey (String sql, List <String > cols) =>
510- '${cols .join ('\u 0001' )}\u 0000$sql ' ;
509+ /// Table schemas are cached per (database, schema, table), so any query on a
510+ /// table reuses the lookup, not only the same text. A query with no single
511+ /// target table is not cached.
512+ String ? _schemaCacheKey (String sql) {
513+ final target = SqlTableTargetExtractor .extract (sql);
514+ if (target == null ) return null ;
515+ return '$effectiveDatabase \u 0001${target .schema ?? '' }\u 0001'
516+ '${target .tableName }' ;
517+ }
511518
512519 static final _readQueryRegex = RegExp (
513520 r'^\s*(select|with|values|show|explain|pragma|describe)\b' ,
@@ -725,8 +732,8 @@ class GenericSqlWorkspaceState extends material.State<GenericSqlWorkspace> {
725732
726733 // Rows show now. The table schema (keys, types, edit hint) follows: from
727734 // the cache, or from the delegate in the background.
728- final cacheKey = _schemaCacheKey (shownSql, cols );
729- final cached = _gridSchemaCache[cacheKey];
735+ final cacheKey = _schemaCacheKey (shownSql);
736+ final cached = cacheKey == null ? null : _gridSchemaCache[cacheKey];
730737 invalidatePane (session);
731738 setState (() {
732739 session.columns = cols;
@@ -753,7 +760,11 @@ class GenericSqlWorkspaceState extends material.State<GenericSqlWorkspace> {
753760 widget.delegate.resolveTableSchema (shownSql, cols).then (
754761 (gridSchema) {
755762 if (! mounted) return ;
756- _gridSchemaCache[cacheKey] = gridSchema;
763+ // A result with no columns gets no schema from the delegate, so
764+ // it is not worth keeping for the table.
765+ if (cacheKey != null && cols.isNotEmpty) {
766+ _gridSchemaCache[cacheKey] = gridSchema;
767+ }
757768 _applyGridSchema (session, shownSql, cols, outRows, gridSchema,
758769 result, scriptStatus);
759770 },
0 commit comments