@rex0220/kintone-sql-tools 3.37.0 → 3.38.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
@@ -1925,24 +1925,25 @@ var Parser = class {
1925
1925
  }
1926
1926
  if (this.isGroupingFunctionStart()) {
1927
1927
  const ref = this.parseGroupingRef();
1928
- const alias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1929
- return { type: "GROUPING_COL", ref, alias: alias2 };
1928
+ const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1929
+ return this.withAliasDisplay({ type: "GROUPING_COL", ref, alias: parsedAlias2?.alias ?? null }, parsedAlias2);
1930
1930
  }
1931
1931
  if (this.tryAggregateFunc() === null && this.hasTopLevelTokenBeforeValueEnd("||" /* CONCAT_OP */)) {
1932
1932
  const expr = this.parseScalarValueExpr({ allowAggregateArgs: true });
1933
- const alias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1934
- return { type: "SCALAR_VALUE_COL", expr, alias: alias2 };
1933
+ const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1934
+ return this.withAliasDisplay({ type: "SCALAR_VALUE_COL", expr, alias: parsedAlias2?.alias ?? null }, parsedAlias2);
1935
1935
  }
1936
1936
  if (this.peek().kind === "VARIABLE" /* VARIABLE */) {
1937
1937
  const variable = this.advance();
1938
1938
  if (!this.consume("AS" /* AS */)) {
1939
1939
  throw new ParseError("SELECT \u5217\u306E\u30D0\u30C3\u30C1\u5909\u6570\u306B\u306F AS alias \u304C\u5FC5\u8981\u3067\u3059", this.peek());
1940
1940
  }
1941
- return {
1941
+ const parsedAlias2 = this.parseAliasName();
1942
+ return this.withAliasDisplay({
1942
1943
  type: "VARIABLE_COL",
1943
1944
  name: variable.value.slice(1).toLowerCase(),
1944
- alias: this.parseAliasName()
1945
- };
1945
+ alias: parsedAlias2.alias
1946
+ }, parsedAlias2);
1946
1947
  }
1947
1948
  const windowFunc = this.tryWindowFunc();
1948
1949
  if (windowFunc !== null) {
@@ -1950,58 +1951,58 @@ var Parser = class {
1950
1951
  }
1951
1952
  if (this.peek().kind === "CASE" /* CASE */) {
1952
1953
  const expr = this.parseCaseWhenExpr(true);
1953
- const alias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1954
- return { type: "CASE_COL", expr, alias: alias2 };
1954
+ const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1955
+ return this.withAliasDisplay({ type: "CASE_COL", expr, alias: parsedAlias2?.alias ?? null }, parsedAlias2);
1955
1956
  }
1956
1957
  if (this.peek().kind === "IF" /* IF */) {
1957
1958
  const expr = this.parseIfExpr(true);
1958
- const alias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1959
- return { type: "CASE_COL", expr, alias: alias2 };
1959
+ const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1960
+ return this.withAliasDisplay({ type: "CASE_COL", expr, alias: parsedAlias2?.alias ?? null }, parsedAlias2);
1960
1961
  }
1961
1962
  if (this.tryStringFuncName() !== null) {
1962
1963
  const funcExpr = this.parseStringFuncExpr();
1963
1964
  if (this.isArithOp(this.peek().kind)) {
1964
1965
  const node = this.parseSelectArith(() => this.continueArith(funcExpr));
1965
- const alias3 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1966
- return { type: "ARITH_COL", expr: node, alias: alias3 };
1966
+ const parsedAlias3 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1967
+ return this.withAliasDisplay({ type: "ARITH_COL", expr: node, alias: parsedAlias3?.alias ?? null }, parsedAlias3);
1967
1968
  }
1968
- const alias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1969
- return { type: "STRFUNC_COL", expr: funcExpr, alias: alias2 };
1969
+ const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1970
+ return this.withAliasDisplay({ type: "STRFUNC_COL", expr: funcExpr, alias: parsedAlias2?.alias ?? null }, parsedAlias2);
1970
1971
  }
1971
1972
  const aggFunc = this.tryAggregateFunc();
1972
1973
  if (aggFunc !== null) {
1973
1974
  const ref = this.parseAggregateRef(aggFunc);
1974
1975
  if (this.isArithOp(this.peek().kind)) {
1975
1976
  const expr = this.continueAggArith(ref);
1976
- const alias3 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1977
- return { type: "ARITH_AGG_COL", expr, alias: alias3 };
1977
+ const parsedAlias3 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1978
+ return this.withAliasDisplay({ type: "ARITH_AGG_COL", expr, alias: parsedAlias3?.alias ?? null }, parsedAlias3);
1978
1979
  }
1979
- const alias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1980
- return {
1980
+ const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1981
+ return this.withAliasDisplay({
1981
1982
  type: "AGGREGATE",
1982
1983
  func: ref.func,
1983
1984
  distinct: ref.distinct,
1984
1985
  arg: ref.arg,
1985
1986
  ...ref.separator !== void 0 ? { separator: ref.separator } : {},
1986
- alias: alias2
1987
- };
1987
+ alias: parsedAlias2?.alias ?? null
1988
+ }, parsedAlias2);
1988
1989
  }
1989
1990
  if (this.peek().kind === "(" /* LPAREN */ && this.peekAt(1).kind === "SELECT" /* SELECT */) {
1990
1991
  this.advance();
1991
1992
  const query = this.parseSelect();
1992
1993
  this.expect(")" /* RPAREN */);
1993
- const alias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1994
- return { type: "SCALAR_SUBQUERY_COL", query, alias: alias2 };
1994
+ const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1995
+ return this.withAliasDisplay({ type: "SCALAR_SUBQUERY_COL", query, alias: parsedAlias2?.alias ?? null }, parsedAlias2);
1995
1996
  }
1996
1997
  if (this.peek().kind === "STRING" /* STRING */) {
1997
1998
  const value = this.expect("STRING" /* STRING */).value;
1998
- const alias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
1999
- return { type: "LITERAL_COL", value, alias: alias2 };
1999
+ const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
2000
+ return this.withAliasDisplay({ type: "LITERAL_COL", value, alias: parsedAlias2?.alias ?? null }, parsedAlias2);
2000
2001
  }
2001
2002
  if (this.peek().kind === "(" /* LPAREN */ || this.peek().kind === "NUMBER" /* NUMBER */) {
2002
2003
  const node = this.parseSelectArith(() => this.parseArithAddSub());
2003
- const alias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
2004
- return { type: "ARITH_COL", expr: node, alias: alias2 };
2004
+ const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
2005
+ return this.withAliasDisplay({ type: "ARITH_COL", expr: node, alias: parsedAlias2?.alias ?? null }, parsedAlias2);
2005
2006
  }
2006
2007
  const field = this.parseColumnFieldRef();
2007
2008
  if (field === "_p.*") {
@@ -2013,11 +2014,11 @@ var Parser = class {
2013
2014
  if (this.isArithOp(this.peek().kind)) {
2014
2015
  const left = { type: "FIELD_REF", field };
2015
2016
  const node = this.parseSelectArith(() => this.continueArith(left));
2016
- const alias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
2017
- return { type: "ARITH_COL", expr: node, alias: alias2 };
2017
+ const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
2018
+ return this.withAliasDisplay({ type: "ARITH_COL", expr: node, alias: parsedAlias2?.alias ?? null }, parsedAlias2);
2018
2019
  }
2019
- const alias = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
2020
- return { type: "FIELD", field, alias };
2020
+ const parsedAlias = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
2021
+ return this.withAliasDisplay({ type: "FIELD", field, alias: parsedAlias?.alias ?? null }, parsedAlias);
2021
2022
  }
2022
2023
  tryWindowFunc() {
2023
2024
  return PARSER_WINDOW_FUNCTION_TOKEN_MAP[this.peek().kind] ?? null;
@@ -2045,8 +2046,8 @@ var Parser = class {
2045
2046
  if (!this.consume("AS" /* AS */)) {
2046
2047
  throw new ParseError("\u30A6\u30A3\u30F3\u30C9\u30A6\u95A2\u6570\u306B\u306F AS alias \u304C\u5FC5\u8981\u3067\u3059", this.peek());
2047
2048
  }
2048
- const alias = this.parseAliasName();
2049
- return { type: "WINDOW_COL", func, partitionBy, orderBy, alias };
2049
+ const parsedAlias = this.parseAliasName();
2050
+ return this.withAliasDisplay({ type: "WINDOW_COL", func, partitionBy, orderBy, alias: parsedAlias.alias }, parsedAlias);
2050
2051
  }
2051
2052
  selectColumnHasAggregate(column) {
2052
2053
  if (column.type === "AGGREGATE" || column.type === "ARITH_AGG_COL") return true;
@@ -4195,10 +4196,24 @@ var Parser = class {
4195
4196
  }
4196
4197
  if (tok.kind === "IDENT" /* IDENT */ || tok.kind === "BIDENT" /* BIDENT */ || KEYWORDS.has(tok.value.toUpperCase())) {
4197
4198
  this.advance();
4198
- return tok.value.toLowerCase();
4199
+ return {
4200
+ alias: tok.value.toLowerCase(),
4201
+ // alias は小文字で統一
4202
+ display: tok.value
4203
+ };
4199
4204
  }
4200
4205
  throw new ParseError("\u30A8\u30A4\u30EA\u30A2\u30B9\u540D\u304C\u5FC5\u8981\u3067\u3059", tok);
4201
4206
  }
4207
+ /** 互換 snapshot を変えず、SELECT 列 AST に表示表記を保持する。 */
4208
+ withAliasDisplay(column, parsedAlias) {
4209
+ if (parsedAlias !== null) {
4210
+ Object.defineProperty(column, "aliasDisplay", {
4211
+ value: parsedAlias.display,
4212
+ enumerable: false
4213
+ });
4214
+ }
4215
+ return column;
4216
+ }
4202
4217
  /** フィールド名または修飾フィールド名(alias.field)を解析する */
4203
4218
  parseFieldPath() {
4204
4219
  const first = this.parseIdentifier();
@@ -16089,7 +16104,7 @@ function materializedColumnMetaEqual(left, right) {
16089
16104
  if (!left || !right || left.size !== right.size) return false;
16090
16105
  for (const [column, meta] of left) {
16091
16106
  const candidate = right.get(column);
16092
- if (!candidate || candidate.sortKind !== meta.sortKind || candidate.fieldType !== meta.fieldType || !fieldSemanticsEqual(candidate.semantics, meta.semantics)) return false;
16107
+ if (!candidate || candidate.displayName !== meta.displayName || candidate.sortKind !== meta.sortKind || candidate.fieldType !== meta.fieldType || !fieldSemanticsEqual(candidate.semantics, meta.semantics)) return false;
16093
16108
  }
16094
16109
  return true;
16095
16110
  }
@@ -16368,7 +16383,16 @@ async function executeBatchStatement(stmt, info, client, options, cacheContext,
16368
16383
  }
16369
16384
  if (info.tempTablesReferenced.length > 0) {
16370
16385
  if (resolvedStmt.type === "SELECT" || resolvedStmt.type === "UNION") {
16371
- return { result: await executeQueryWithCte(resolvedStmt, client, options, tempTables, cacheContext) };
16386
+ return {
16387
+ result: await executeQueryWithCte(
16388
+ resolvedStmt,
16389
+ client,
16390
+ options,
16391
+ tempTables,
16392
+ cacheContext,
16393
+ options.captureColumnMeta === true
16394
+ )
16395
+ };
16372
16396
  }
16373
16397
  if (resolvedStmt.type === "WITH") {
16374
16398
  return { result: await executeWith(resolvedStmt, client, options, cacheContext, tempTables) };
@@ -16530,7 +16554,8 @@ function resolveBatchVariableReferencesInternal(node, variables, numericArithmet
16530
16554
  const value = variables.get(obj["name"]);
16531
16555
  if (value === void 0) throw new Error(`ParseError: variable @${obj["name"]} is not defined in this batch.`);
16532
16556
  if (value.type === "array") throw new Error(`ParseError: array variable @${obj["name"]} cannot be used as a SELECT column.`);
16533
- return value.type === "number" ? { type: "ARITH_COL", expr: { type: "NUMBER", value: value.value, raw: value.raw ?? String(value.value) }, alias: obj["alias"] } : { type: "LITERAL_COL", value: value.value, alias: obj["alias"] };
16557
+ const aliasDisplay = typeof obj["aliasDisplay"] === "string" ? { aliasDisplay: obj["aliasDisplay"] } : {};
16558
+ return value.type === "number" ? { type: "ARITH_COL", expr: { type: "NUMBER", value: value.value, raw: value.raw ?? String(value.value) }, alias: obj["alias"], ...aliasDisplay } : { type: "LITERAL_COL", value: value.value, alias: obj["alias"], ...aliasDisplay };
16534
16559
  }
16535
16560
  if (obj["type"] === "VARIABLE_IN_LIST") return obj;
16536
16561
  const resolved = Object.fromEntries(
@@ -16543,6 +16568,9 @@ function resolveBatchVariableReferencesInternal(node, variables, numericArithmet
16543
16568
  )
16544
16569
  ])
16545
16570
  );
16571
+ if (typeof obj["aliasDisplay"] === "string") {
16572
+ resolved["aliasDisplay"] = obj["aliasDisplay"];
16573
+ }
16546
16574
  if (resolved["type"] === "BINARY") {
16547
16575
  const right = resolved["right"];
16548
16576
  if (right?.["type"] === "VARIABLE_IN_LIST" && typeof right["name"] === "string") {
@@ -18068,6 +18096,9 @@ function inferAggregateArgMeta(arg, resolveField2) {
18068
18096
  if (arg.elseResult) results.push(caseResultColumnMeta(arg.elseResult, resolveField2));
18069
18097
  return mergeExpressionColumnMeta(results);
18070
18098
  }
18099
+ function withDisplayName(meta, displayName) {
18100
+ return { ...meta ?? {}, displayName };
18101
+ }
18071
18102
  function selectNeedsSourceColumnMeta(stmt) {
18072
18103
  return stmt.columns.some(
18073
18104
  (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")
@@ -18132,7 +18163,7 @@ async function inferSelectColumnMeta(stmt, outputColumns, client, cacheContext,
18132
18163
  for (const output of outputColumns) {
18133
18164
  const ref = aggregateFieldRef(output);
18134
18165
  const meta = withPublicSource(resolveField2(ref), ref);
18135
- if (meta) inferred.set(output, meta);
18166
+ inferred.set(output, withDisplayName(meta, meta?.displayName ?? output));
18136
18167
  }
18137
18168
  return inferred;
18138
18169
  }
@@ -18140,7 +18171,7 @@ async function inferSelectColumnMeta(stmt, outputColumns, client, cacheContext,
18140
18171
  for (const output of outputColumns) {
18141
18172
  const ref = aggregateFieldRef(output);
18142
18173
  const meta = withPublicSource(resolveField2(ref), ref);
18143
- if (meta) inferred.set(output, meta);
18174
+ inferred.set(output, withDisplayName(meta, meta?.displayName ?? output));
18144
18175
  }
18145
18176
  }
18146
18177
  const explicitColumns = stmt.columns.filter(
@@ -18184,7 +18215,7 @@ async function inferSelectColumnMeta(stmt, outputColumns, client, cacheContext,
18184
18215
  } else if (column.type === "SCALAR_SUBQUERY_COL") {
18185
18216
  meta = unknownStringColumnMeta();
18186
18217
  }
18187
- if (meta) inferred.set(output, meta);
18218
+ inferred.set(output, withDisplayName(meta, column.aliasDisplay ?? output));
18188
18219
  });
18189
18220
  return inferred;
18190
18221
  }
@@ -18196,15 +18227,14 @@ function mergeUnionColumnMeta(left, right) {
18196
18227
  const a = leftMeta?.get(column);
18197
18228
  const rightColumn = right.columns[index];
18198
18229
  const b = rightColumn === void 0 ? void 0 : rightMeta?.get(rightColumn);
18230
+ let meta;
18199
18231
  if (a && b) {
18200
18232
  const combined = mergeExpressionColumnMeta([a, b]);
18201
18233
  const publicSourceApp = a.publicSourceApp === b.publicSourceApp ? a.publicSourceApp : void 0;
18202
18234
  const { publicSourceApp: _discarded, ...withoutPublicSource } = combined;
18203
- merged.set(
18204
- column,
18205
- publicSourceApp === void 0 ? withoutPublicSource : { ...withoutPublicSource, publicSourceApp }
18206
- );
18207
- } else if (a || b) merged.set(column, unknownStringColumnMeta());
18235
+ meta = publicSourceApp === void 0 ? withoutPublicSource : { ...withoutPublicSource, publicSourceApp };
18236
+ } else meta = unknownStringColumnMeta();
18237
+ merged.set(column, withDisplayName(meta, a?.displayName ?? column));
18208
18238
  });
18209
18239
  return merged;
18210
18240
  }
@@ -25332,35 +25362,41 @@ function tryParseStatements(sql) {
25332
25362
  }
25333
25363
  }
25334
25364
 
25335
- // src/node/sqlDiagnostics.ts
25336
- function restoreSqlDiagnosticValue(value, bindings) {
25365
+ // src/core/sqlDiagnostics.ts
25366
+ function restoreSqlDiagnosticValue(value, bindings, options = {}) {
25367
+ const displayMode = options.logicalAppDisplay ?? "profile";
25337
25368
  if (typeof value === "string") {
25338
25369
  const dmlTarget = value.match(/^(\s*target:\s*)APP(\d+) \((\d+)\)\s*$/);
25339
25370
  if (dmlTarget && dmlTarget[2] === dmlTarget[3]) {
25340
25371
  const binding = bindings.get(Number(dmlTarget[2]));
25341
25372
  if (binding) {
25342
- const target = binding.source === "logical" ? `LAPP_${binding.logicalName} -> APP${binding.appId}@${binding.profile}` : `APP${binding.appId}@${binding.profile}`;
25373
+ const target = binding.source === "logical" ? displayMode === "physical" ? `LAPP_${binding.logicalName} -> APP${binding.appId}` : `LAPP_${binding.logicalName} -> APP${binding.appId}@${binding.profile}` : displayMode === "physical" ? `APP${binding.appId}` : `APP${binding.appId}@${binding.profile}`;
25343
25374
  return `${dmlTarget[1]}${target}`;
25344
25375
  }
25345
25376
  }
25346
25377
  let restored = value;
25347
25378
  for (const binding of bindings.values()) {
25348
25379
  const internal = `APP${binding.mappedAppId}`;
25349
- const display = binding.source === "logical" ? `LAPP_${binding.logicalName}@${binding.profile}` : `APP${binding.appId}@${binding.profile}`;
25380
+ const display = binding.source === "logical" ? displayMode === "physical" ? `LAPP_${binding.logicalName} -> APP${binding.appId}` : `LAPP_${binding.logicalName}@${binding.profile}` : displayMode === "physical" ? `APP${binding.appId}` : `APP${binding.appId}@${binding.profile}`;
25350
25381
  restored = restored.split(`${internal} (${binding.mappedAppId})`).join(display).split(internal).join(display);
25351
25382
  }
25352
25383
  return restored;
25353
25384
  }
25354
25385
  if (Array.isArray(value)) {
25355
- return value.map((item) => restoreSqlDiagnosticValue(item, bindings));
25386
+ return value.map((item) => restoreSqlDiagnosticValue(item, bindings, options));
25356
25387
  }
25357
25388
  if (value !== null && typeof value === "object") {
25358
25389
  return Object.fromEntries(
25359
- Object.entries(value).map(([key, item]) => [key, restoreSqlDiagnosticValue(item, bindings)])
25390
+ Object.entries(value).map(([key, item]) => [
25391
+ key,
25392
+ restoreSqlDiagnosticValue(item, bindings, options)
25393
+ ])
25360
25394
  );
25361
25395
  }
25362
25396
  return value;
25363
25397
  }
25398
+
25399
+ // src/node/sqlDiagnostics.ts
25364
25400
  function restoreSqlContextError(err, sourceSql, context) {
25365
25401
  if (!(err instanceof Error)) return err;
25366
25402
  let message = err.message;