@rex0220/kintone-sql-tools 3.21.0 → 3.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist-cli/ksql.js CHANGED
@@ -13927,7 +13927,12 @@ async function execute(sql, client, options = {}) {
13927
13927
  cacheContext
13928
13928
  );
13929
13929
  metrics.elapsedMs = Date.now() - startedAt;
13930
- return { ...attachSearchAbortWarning(result, collector), metrics };
13930
+ const finalResult = { ...attachSearchAbortWarning(result, collector), metrics };
13931
+ if (result.type === "SELECT") {
13932
+ const columnMeta = materializedMetaBySelectResult.get(result);
13933
+ if (columnMeta) materializedMetaBySelectResult.set(finalResult, columnMeta);
13934
+ }
13935
+ return finalResult;
13931
13936
  }
13932
13937
  function createEmptyMetrics() {
13933
13938
  return {
@@ -14138,11 +14143,26 @@ async function executeParsedStatement(stmt, client, options, cacheContext) {
14138
14143
  case "VALIDATE":
14139
14144
  return executeExistingRecordValidation(stmt, client, options, cacheContext);
14140
14145
  case "SELECT":
14141
- return executeSelect(stmt, client, options, cacheContext);
14146
+ return executeSelect(
14147
+ stmt,
14148
+ client,
14149
+ options,
14150
+ cacheContext,
14151
+ void 0,
14152
+ options.captureColumnMeta === true,
14153
+ options.captureColumnMeta === true
14154
+ );
14142
14155
  case "UNION":
14143
- return executeUnion(stmt, client, options, cacheContext);
14156
+ return executeUnion(
14157
+ stmt,
14158
+ client,
14159
+ options,
14160
+ cacheContext,
14161
+ options.captureColumnMeta === true,
14162
+ options.captureColumnMeta === true
14163
+ );
14144
14164
  case "WITH":
14145
- return executeWith(stmt, client, options, cacheContext);
14165
+ return executeWith(stmt, client, options, cacheContext, void 0, options.captureColumnMeta === true);
14146
14166
  case "INSERT":
14147
14167
  return executeInsert(stmt, client, options, cacheContext);
14148
14168
  case "INSERT_SELECT":
@@ -15196,13 +15216,16 @@ async function assertDmlWhereCapability(stmt, client, cacheContext) {
15196
15216
  );
15197
15217
  }
15198
15218
  }
15199
- async function executeSelect(stmt, client, options, cacheContext, cteCache, captureColumnMeta = false) {
15219
+ async function executeSelect(stmt, client, options, cacheContext, cteCache, captureColumnMeta = false, forLibraryCapture = false) {
15200
15220
  let result;
15201
15221
  await validateSelectGroupingPlanning(stmt, client, cacheContext, cteCache);
15202
15222
  if (isNoFromSelect(stmt)) {
15203
15223
  result = executeNoFromSelect(stmt);
15204
15224
  if (captureColumnMeta) {
15205
- materializedMetaBySelectResult.set(result, await inferSelectColumnMeta(stmt, result.columns, client, cacheContext, cteCache));
15225
+ materializedMetaBySelectResult.set(
15226
+ result,
15227
+ await inferSelectColumnMeta(stmt, result.columns, client, cacheContext, cteCache, forLibraryCapture)
15228
+ );
15206
15229
  }
15207
15230
  return result;
15208
15231
  }
@@ -15257,7 +15280,10 @@ async function executeSelect(stmt, client, options, cacheContext, cteCache, capt
15257
15280
  throwCompleteInputError(completePolicy, error);
15258
15281
  }
15259
15282
  if (captureColumnMeta) {
15260
- materializedMetaBySelectResult.set(result, await inferSelectColumnMeta(stmt, result.columns, client, cacheContext, cteCache));
15283
+ materializedMetaBySelectResult.set(
15284
+ result,
15285
+ await inferSelectColumnMeta(stmt, result.columns, client, cacheContext, cteCache, forLibraryCapture)
15286
+ );
15261
15287
  }
15262
15288
  return result;
15263
15289
  }
@@ -16025,7 +16051,7 @@ function selectNeedsSourceColumnMeta(stmt) {
16025
16051
  (column) => column.type === "FIELD" || column.type === "WILDCARD" || column.type === "PARENT_WILDCARD" || column.type === "CASE_COL" || column.type === "AGGREGATE" && (column.func === "MIN" || column.func === "MAX" || column.func === "MODE")
16026
16052
  );
16027
16053
  }
16028
- async function inferSelectColumnMeta(stmt, outputColumns, client, cacheContext, materializedTables) {
16054
+ async function inferSelectColumnMeta(stmt, outputColumns, client, cacheContext, materializedTables, forLibraryCapture = false) {
16029
16055
  const physicalInfos = /* @__PURE__ */ new Map();
16030
16056
  if (selectNeedsSourceColumnMeta(stmt)) {
16031
16057
  await Promise.all(physicalSelectTables(stmt).map(async (table) => {
@@ -16035,6 +16061,7 @@ async function inferSelectColumnMeta(stmt, outputColumns, client, cacheContext,
16035
16061
  }));
16036
16062
  }
16037
16063
  const tables = [stmt.from, ...stmt.joins.map((join2) => join2.table)];
16064
+ const canExposePublicSource = forLibraryCapture && materializedTables === void 0 && tables.every((table) => table.cteName === null);
16038
16065
  const resolveField2 = (ref) => {
16039
16066
  if (ref.tableAlias !== null) {
16040
16067
  if (ref.tableAlias === "_p" && stmt.from.subtableCode && stmt.from.cteName === null) {
@@ -16064,18 +16091,33 @@ async function inferSelectColumnMeta(stmt, outputColumns, client, cacheContext,
16064
16091
  });
16065
16092
  return matches.length === 1 ? matches[0] : void 0;
16066
16093
  };
16094
+ const resolvePublicSourceApp = (ref) => {
16095
+ if (!canExposePublicSource) return void 0;
16096
+ const matches = tables.filter((table) => {
16097
+ if (ref.tableAlias !== null && effectiveTableAlias(table) !== ref.tableAlias) return false;
16098
+ const fieldCode = fieldCodeForTypeLookup(table, ref.field);
16099
+ return physicalInfos.get(table.appId)?.has(fieldCode) === true || systemColumnMeta(ref.field) !== void 0;
16100
+ });
16101
+ return matches.length === 1 ? matches[0].appId : void 0;
16102
+ };
16103
+ const withPublicSource = (meta, ref) => {
16104
+ const publicSourceApp = resolvePublicSourceApp(ref);
16105
+ return meta && publicSourceApp !== void 0 ? { ...meta, publicSourceApp } : meta;
16106
+ };
16067
16107
  const inferred = /* @__PURE__ */ new Map();
16068
16108
  const hasWildcard = stmt.columns.some((column) => column.type === "WILDCARD" || column.type === "PARENT_WILDCARD");
16069
16109
  if (stmt.columns.length === 1 && (stmt.columns[0].type === "WILDCARD" || stmt.columns[0].type === "PARENT_WILDCARD")) {
16070
16110
  for (const output of outputColumns) {
16071
- const meta = resolveField2(aggregateFieldRef(output));
16111
+ const ref = aggregateFieldRef(output);
16112
+ const meta = withPublicSource(resolveField2(ref), ref);
16072
16113
  if (meta) inferred.set(output, meta);
16073
16114
  }
16074
16115
  return inferred;
16075
16116
  }
16076
16117
  if (hasWildcard) {
16077
16118
  for (const output of outputColumns) {
16078
- const meta = resolveField2(aggregateFieldRef(output));
16119
+ const ref = aggregateFieldRef(output);
16120
+ const meta = withPublicSource(resolveField2(ref), ref);
16079
16121
  if (meta) inferred.set(output, meta);
16080
16122
  }
16081
16123
  }
@@ -16087,7 +16129,8 @@ async function inferSelectColumnMeta(stmt, outputColumns, client, cacheContext,
16087
16129
  if (!output) return;
16088
16130
  let meta;
16089
16131
  if (column.type === "FIELD") {
16090
- meta = resolveField2(aggregateFieldRef(column.field));
16132
+ const ref = aggregateFieldRef(column.field);
16133
+ meta = withPublicSource(resolveField2(ref), ref);
16091
16134
  } else if (column.type === "AGGREGATE") {
16092
16135
  if (column.func === "GROUP_CONCAT") {
16093
16136
  meta = syntheticColumnMeta("string");
@@ -16131,8 +16174,15 @@ function mergeUnionColumnMeta(left, right) {
16131
16174
  const a = leftMeta?.get(column);
16132
16175
  const rightColumn = right.columns[index];
16133
16176
  const b = rightColumn === void 0 ? void 0 : rightMeta?.get(rightColumn);
16134
- if (a && b) merged.set(column, mergeExpressionColumnMeta([a, b]));
16135
- else if (a || b) merged.set(column, unknownStringColumnMeta());
16177
+ if (a && b) {
16178
+ const combined = mergeExpressionColumnMeta([a, b]);
16179
+ const publicSourceApp = a.publicSourceApp === b.publicSourceApp ? a.publicSourceApp : void 0;
16180
+ const { publicSourceApp: _discarded, ...withoutPublicSource } = combined;
16181
+ merged.set(
16182
+ column,
16183
+ publicSourceApp === void 0 ? withoutPublicSource : { ...withoutPublicSource, publicSourceApp }
16184
+ );
16185
+ } else if (a || b) merged.set(column, unknownStringColumnMeta());
16136
16186
  });
16137
16187
  return merged;
16138
16188
  }
@@ -16295,10 +16345,10 @@ async function executeFullScanSelect(stmt, client, options, cacheContext, cteCac
16295
16345
  });
16296
16346
  return { type: "SELECT", rows, columns, rowCount: rows.length, warnings: [...warnings] };
16297
16347
  }
16298
- async function executeUnion(stmt, client, options, cacheContext) {
16348
+ async function executeUnion(stmt, client, options, cacheContext, captureColumnMeta = false, forLibraryCapture = false) {
16299
16349
  const [leftResult, rightResult] = await Promise.all([
16300
- stmt.left.type === "UNION" ? executeUnion(stmt.left, client, options, cacheContext) : executeSelect(stmt.left, client, options, cacheContext),
16301
- executeSelect(stmt.right, client, options, cacheContext)
16350
+ stmt.left.type === "UNION" ? executeUnion(stmt.left, client, options, cacheContext, captureColumnMeta, forLibraryCapture) : executeSelect(stmt.left, client, options, cacheContext, void 0, captureColumnMeta, forLibraryCapture),
16351
+ executeSelect(stmt.right, client, options, cacheContext, void 0, captureColumnMeta, forLibraryCapture)
16302
16352
  ]);
16303
16353
  const leftCols = leftResult.columns;
16304
16354
  const rightCols = rightResult.columns;
@@ -16311,7 +16361,11 @@ async function executeUnion(stmt, client, options, cacheContext) {
16311
16361
  });
16312
16362
  const combined = [...leftResult.rows, ...remappedRight];
16313
16363
  const rows = stmt.all ? combined : deduplicateRows(combined, leftCols);
16314
- return { type: "SELECT", rows, columns: leftCols, rowCount: rows.length };
16364
+ const result = { type: "SELECT", rows, columns: leftCols, rowCount: rows.length };
16365
+ if (captureColumnMeta) {
16366
+ materializedMetaBySelectResult.set(result, mergeUnionColumnMeta(leftResult, rightResult));
16367
+ }
16368
+ return result;
16315
16369
  }
16316
16370
  function deduplicateRows(rows, columns) {
16317
16371
  const seen = /* @__PURE__ */ new Set();
@@ -16324,7 +16378,7 @@ function deduplicateRows(rows, columns) {
16324
16378
  }
16325
16379
  async function executeWith(stmt, client, options, cacheContext, seed, captureColumnMeta = false) {
16326
16380
  if ((seed == null || seed.size === 0) && canInlineSingleCte(stmt)) {
16327
- return executeSelect(buildInlinedQuery(stmt), client, options, cacheContext, void 0, captureColumnMeta);
16381
+ return executeSelect(buildInlinedQuery(stmt), client, options, cacheContext, void 0, captureColumnMeta, false);
16328
16382
  }
16329
16383
  const cteCache = new Map(seed ?? []);
16330
16384
  for (const cte of stmt.ctes) {
@@ -16369,7 +16423,7 @@ async function executeQueryWithCte(query, client, options, cteCache, cacheContex
16369
16423
  }
16370
16424
  const hasCteRef = query.from.cteName != null && query.from.cteName !== NO_FROM_CTE_NAME || query.joins.some((j) => j.table.cteName != null);
16371
16425
  if (!hasCteRef) {
16372
- return executeSelect(query, client, options, cacheContext, cteCache, captureColumnMeta);
16426
+ return executeSelect(query, client, options, cacheContext, cteCache, captureColumnMeta, false);
16373
16427
  }
16374
16428
  const result = await executeFullScanWithCte(query, client, options, cteCache, cacheContext);
16375
16429
  if (captureColumnMeta) {