@rex0220/kintone-sql-tools 2.1.0 → 2.1.1

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
@@ -4462,8 +4462,12 @@ function project(rows, columns, scalarCache) {
4462
4462
  const cols = projected2.length > 0 ? Object.keys(projected2[0]) : [];
4463
4463
  return { rows: projected2, columns: cols };
4464
4464
  }
4465
- const orderedKeys = [];
4466
4465
  const defaultFieldKeys = buildDefaultFieldOutputKeys(columns);
4466
+ const hasWildcard = columns.some(
4467
+ (col) => col.type === "WILDCARD" || col.type === "PARENT_WILDCARD"
4468
+ );
4469
+ const outputKeys = hasWildcard ? null : computeOutputKeys(columns, defaultFieldKeys);
4470
+ const orderedKeys = outputKeys ?? [];
4467
4471
  const projected = rows.map((row, rowIdx) => {
4468
4472
  const out = {};
4469
4473
  for (const [colIdx, col] of columns.entries()) {
@@ -4480,58 +4484,58 @@ function project(rows, columns, scalarCache) {
4480
4484
  break;
4481
4485
  }
4482
4486
  case "FIELD": {
4483
- const key = col.alias ?? defaultFieldKeys.get(colIdx) ?? col.field;
4487
+ const key = outputKeys?.[colIdx] ?? col.alias ?? defaultFieldKeys.get(colIdx) ?? col.field;
4484
4488
  out[key] = resolveFieldRef(row, col.field);
4485
- if (rowIdx === 0) orderedKeys.push(key);
4489
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
4486
4490
  break;
4487
4491
  }
4488
4492
  case "LITERAL_COL": {
4489
- const key = col.alias ?? `'${col.value}'`;
4493
+ const key = outputKeys?.[colIdx] ?? col.alias ?? `'${col.value}'`;
4490
4494
  out[key] = col.value;
4491
- if (rowIdx === 0) orderedKeys.push(key);
4495
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
4492
4496
  break;
4493
4497
  }
4494
4498
  case "AGGREGATE": {
4495
4499
  const srcKey = aggregateSyntheticName2(col.func, col.distinct, col.arg);
4496
- const dstKey = col.alias ?? srcKey;
4500
+ const dstKey = outputKeys?.[colIdx] ?? col.alias ?? srcKey;
4497
4501
  out[dstKey] = row[col.alias ?? srcKey] ?? row[srcKey] ?? "0";
4498
- if (rowIdx === 0) orderedKeys.push(dstKey);
4502
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(dstKey);
4499
4503
  break;
4500
4504
  }
4501
4505
  case "ARITH_AGG_COL": {
4502
- const key = col.alias ?? aggArithDefaultKey(col.expr);
4506
+ const key = outputKeys?.[colIdx] ?? col.alias ?? aggArithDefaultKey(col.expr);
4503
4507
  out[key] = row[key] ?? "0";
4504
- if (rowIdx === 0) orderedKeys.push(key);
4508
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
4505
4509
  break;
4506
4510
  }
4507
4511
  case "ARITH_COL": {
4508
4512
  const val = evalArithExpr(col.expr, row);
4509
- const key = col.alias ?? arithColDefaultKey(col.expr);
4513
+ const key = outputKeys?.[colIdx] ?? col.alias ?? arithColDefaultKey(col.expr);
4510
4514
  out[key] = String(val);
4511
- if (rowIdx === 0) orderedKeys.push(key);
4515
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
4512
4516
  break;
4513
4517
  }
4514
4518
  case "CASE_COL": {
4515
- const key = col.alias ?? "case";
4519
+ const key = outputKeys?.[colIdx] ?? col.alias ?? "case";
4516
4520
  out[key] = evalCaseWhen(col.expr, row);
4517
- if (rowIdx === 0) orderedKeys.push(key);
4521
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
4518
4522
  break;
4519
4523
  }
4520
4524
  case "STRFUNC_COL": {
4521
- const key = col.alias ?? stringFuncDefaultKey(col.expr);
4525
+ const key = outputKeys?.[colIdx] ?? col.alias ?? stringFuncDefaultKey(col.expr);
4522
4526
  if (hasAggregateInStringFuncExpr2(col.expr)) {
4523
4527
  const srcKey = stringFuncDefaultKey(col.expr);
4524
4528
  out[key] = row[col.alias ?? srcKey] ?? row[srcKey] ?? evalStringFunc(col.expr, row);
4525
4529
  } else {
4526
4530
  out[key] = evalStringFunc(col.expr, row);
4527
4531
  }
4528
- if (rowIdx === 0) orderedKeys.push(key);
4532
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
4529
4533
  break;
4530
4534
  }
4531
4535
  case "SCALAR_SUBQUERY_COL": {
4532
- const key = col.alias ?? "(subquery)";
4536
+ const key = outputKeys?.[colIdx] ?? col.alias ?? "(subquery)";
4533
4537
  out[key] = scalarCache?.get(colIdx) ?? "";
4534
- if (rowIdx === 0) orderedKeys.push(key);
4538
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
4535
4539
  break;
4536
4540
  }
4537
4541
  }
@@ -4540,6 +4544,31 @@ function project(rows, columns, scalarCache) {
4540
4544
  });
4541
4545
  return { rows: projected, columns: orderedKeys };
4542
4546
  }
4547
+ function computeOutputKeys(columns, defaultFieldKeys) {
4548
+ return columns.map((col, colIdx) => {
4549
+ switch (col.type) {
4550
+ case "FIELD":
4551
+ return col.alias ?? defaultFieldKeys.get(colIdx) ?? col.field;
4552
+ case "LITERAL_COL":
4553
+ return col.alias ?? `'${col.value}'`;
4554
+ case "AGGREGATE":
4555
+ return col.alias ?? aggregateSyntheticName2(col.func, col.distinct, col.arg);
4556
+ case "ARITH_AGG_COL":
4557
+ return col.alias ?? aggArithDefaultKey(col.expr);
4558
+ case "ARITH_COL":
4559
+ return col.alias ?? arithColDefaultKey(col.expr);
4560
+ case "CASE_COL":
4561
+ return col.alias ?? "case";
4562
+ case "STRFUNC_COL":
4563
+ return col.alias ?? stringFuncDefaultKey(col.expr);
4564
+ case "SCALAR_SUBQUERY_COL":
4565
+ return col.alias ?? "(subquery)";
4566
+ case "WILDCARD":
4567
+ case "PARENT_WILDCARD":
4568
+ throw new Error("internal: computeOutputKeys received a wildcard column");
4569
+ }
4570
+ });
4571
+ }
4543
4572
  function buildDefaultFieldOutputKeys(columns) {
4544
4573
  const qualifierCollisionCount = /* @__PURE__ */ new Map();
4545
4574
  for (const col of columns) {
@@ -5930,8 +5959,9 @@ async function executeInsertSelect(stmt, client, options, cacheContext, cteCache
5930
5959
  const selectResult = cteCache !== void 0 && cteCache.size > 0 ? await executeQueryWithCte(stmt.select, client, options, cteCache, cacheContext) : await executeSelect(stmt.select, client, options, cacheContext);
5931
5960
  const { rows, columns } = selectResult;
5932
5961
  if (columns.length !== stmt.fields.length) {
5962
+ const emptySourceHint = columns.length === 0 && rows.length === 0 ? "\u3002\u7D50\u679C\u304C 0 \u884C\u306E\u305F\u3081\u5217\u3092\u7279\u5B9A\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F\uFF08SELECT * \u3092\u7A7A\u30BD\u30FC\u30B9\u306B\u4F7F\u3046\u3068\u5217\u3092\u6C7A\u5B9A\u3067\u304D\u307E\u305B\u3093\u3002\u660E\u793A\u5217\u3067\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044\uFF09" : "";
5933
5963
  throw new Error(
5934
- `SELECT \u306E\u5217\u6570\uFF08${columns.length}\uFF09\u3068 INSERT \u306E\u30D5\u30A3\u30FC\u30EB\u30C9\u6570\uFF08${stmt.fields.length}\uFF09\u304C\u4E00\u81F4\u3057\u307E\u305B\u3093`
5964
+ `SELECT \u306E\u5217\u6570\uFF08${columns.length}\uFF09\u3068 INSERT \u306E\u30D5\u30A3\u30FC\u30EB\u30C9\u6570\uFF08${stmt.fields.length}\uFF09\u304C\u4E00\u81F4\u3057\u307E\u305B\u3093${emptySourceHint}`
5935
5965
  );
5936
5966
  }
5937
5967
  if (options.confirm) {
@@ -6404,8 +6434,9 @@ async function executeUpsertSelect(stmt, client, options, cacheContext, cteCache
6404
6434
  const selectResult = cteCache !== void 0 && cteCache.size > 0 ? await executeQueryWithCte(stmt.select, client, options, cteCache, cacheContext) : await executeSelect(stmt.select, client, options, cacheContext);
6405
6435
  const { rows, columns } = selectResult;
6406
6436
  if (columns.length !== stmt.fields.length) {
6437
+ const emptySourceHint = columns.length === 0 && rows.length === 0 ? "\u3002\u7D50\u679C\u304C 0 \u884C\u306E\u305F\u3081\u5217\u3092\u7279\u5B9A\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F\uFF08SELECT * \u3092\u7A7A\u30BD\u30FC\u30B9\u306B\u4F7F\u3046\u3068\u5217\u3092\u6C7A\u5B9A\u3067\u304D\u307E\u305B\u3093\u3002\u660E\u793A\u5217\u3067\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044\uFF09" : "";
6407
6438
  throw new Error(
6408
- `SELECT \u306E\u5217\u6570\uFF08${columns.length}\uFF09\u3068 UPSERT \u306E\u30D5\u30A3\u30FC\u30EB\u30C9\u6570\uFF08${stmt.fields.length}\uFF09\u304C\u4E00\u81F4\u3057\u307E\u305B\u3093`
6439
+ `SELECT \u306E\u5217\u6570\uFF08${columns.length}\uFF09\u3068 UPSERT \u306E\u30D5\u30A3\u30FC\u30EB\u30C9\u6570\uFF08${stmt.fields.length}\uFF09\u304C\u4E00\u81F4\u3057\u307E\u305B\u3093${emptySourceHint}`
6409
6440
  );
6410
6441
  }
6411
6442
  for (const key of stmt.keyFields) {
@@ -35363,8 +35363,12 @@ function project(rows, columns, scalarCache) {
35363
35363
  const cols = projected2.length > 0 ? Object.keys(projected2[0]) : [];
35364
35364
  return { rows: projected2, columns: cols };
35365
35365
  }
35366
- const orderedKeys = [];
35367
35366
  const defaultFieldKeys = buildDefaultFieldOutputKeys(columns);
35367
+ const hasWildcard = columns.some(
35368
+ (col) => col.type === "WILDCARD" || col.type === "PARENT_WILDCARD"
35369
+ );
35370
+ const outputKeys = hasWildcard ? null : computeOutputKeys(columns, defaultFieldKeys);
35371
+ const orderedKeys = outputKeys ?? [];
35368
35372
  const projected = rows.map((row, rowIdx) => {
35369
35373
  const out = {};
35370
35374
  for (const [colIdx, col] of columns.entries()) {
@@ -35381,58 +35385,58 @@ function project(rows, columns, scalarCache) {
35381
35385
  break;
35382
35386
  }
35383
35387
  case "FIELD": {
35384
- const key = col.alias ?? defaultFieldKeys.get(colIdx) ?? col.field;
35388
+ const key = outputKeys?.[colIdx] ?? col.alias ?? defaultFieldKeys.get(colIdx) ?? col.field;
35385
35389
  out[key] = resolveFieldRef(row, col.field);
35386
- if (rowIdx === 0) orderedKeys.push(key);
35390
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
35387
35391
  break;
35388
35392
  }
35389
35393
  case "LITERAL_COL": {
35390
- const key = col.alias ?? `'${col.value}'`;
35394
+ const key = outputKeys?.[colIdx] ?? col.alias ?? `'${col.value}'`;
35391
35395
  out[key] = col.value;
35392
- if (rowIdx === 0) orderedKeys.push(key);
35396
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
35393
35397
  break;
35394
35398
  }
35395
35399
  case "AGGREGATE": {
35396
35400
  const srcKey = aggregateSyntheticName2(col.func, col.distinct, col.arg);
35397
- const dstKey = col.alias ?? srcKey;
35401
+ const dstKey = outputKeys?.[colIdx] ?? col.alias ?? srcKey;
35398
35402
  out[dstKey] = row[col.alias ?? srcKey] ?? row[srcKey] ?? "0";
35399
- if (rowIdx === 0) orderedKeys.push(dstKey);
35403
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(dstKey);
35400
35404
  break;
35401
35405
  }
35402
35406
  case "ARITH_AGG_COL": {
35403
- const key = col.alias ?? aggArithDefaultKey(col.expr);
35407
+ const key = outputKeys?.[colIdx] ?? col.alias ?? aggArithDefaultKey(col.expr);
35404
35408
  out[key] = row[key] ?? "0";
35405
- if (rowIdx === 0) orderedKeys.push(key);
35409
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
35406
35410
  break;
35407
35411
  }
35408
35412
  case "ARITH_COL": {
35409
35413
  const val = evalArithExpr(col.expr, row);
35410
- const key = col.alias ?? arithColDefaultKey(col.expr);
35414
+ const key = outputKeys?.[colIdx] ?? col.alias ?? arithColDefaultKey(col.expr);
35411
35415
  out[key] = String(val);
35412
- if (rowIdx === 0) orderedKeys.push(key);
35416
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
35413
35417
  break;
35414
35418
  }
35415
35419
  case "CASE_COL": {
35416
- const key = col.alias ?? "case";
35420
+ const key = outputKeys?.[colIdx] ?? col.alias ?? "case";
35417
35421
  out[key] = evalCaseWhen(col.expr, row);
35418
- if (rowIdx === 0) orderedKeys.push(key);
35422
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
35419
35423
  break;
35420
35424
  }
35421
35425
  case "STRFUNC_COL": {
35422
- const key = col.alias ?? stringFuncDefaultKey(col.expr);
35426
+ const key = outputKeys?.[colIdx] ?? col.alias ?? stringFuncDefaultKey(col.expr);
35423
35427
  if (hasAggregateInStringFuncExpr2(col.expr)) {
35424
35428
  const srcKey = stringFuncDefaultKey(col.expr);
35425
35429
  out[key] = row[col.alias ?? srcKey] ?? row[srcKey] ?? evalStringFunc(col.expr, row);
35426
35430
  } else {
35427
35431
  out[key] = evalStringFunc(col.expr, row);
35428
35432
  }
35429
- if (rowIdx === 0) orderedKeys.push(key);
35433
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
35430
35434
  break;
35431
35435
  }
35432
35436
  case "SCALAR_SUBQUERY_COL": {
35433
- const key = col.alias ?? "(subquery)";
35437
+ const key = outputKeys?.[colIdx] ?? col.alias ?? "(subquery)";
35434
35438
  out[key] = scalarCache?.get(colIdx) ?? "";
35435
- if (rowIdx === 0) orderedKeys.push(key);
35439
+ if (outputKeys === null && rowIdx === 0) orderedKeys.push(key);
35436
35440
  break;
35437
35441
  }
35438
35442
  }
@@ -35441,6 +35445,31 @@ function project(rows, columns, scalarCache) {
35441
35445
  });
35442
35446
  return { rows: projected, columns: orderedKeys };
35443
35447
  }
35448
+ function computeOutputKeys(columns, defaultFieldKeys) {
35449
+ return columns.map((col, colIdx) => {
35450
+ switch (col.type) {
35451
+ case "FIELD":
35452
+ return col.alias ?? defaultFieldKeys.get(colIdx) ?? col.field;
35453
+ case "LITERAL_COL":
35454
+ return col.alias ?? `'${col.value}'`;
35455
+ case "AGGREGATE":
35456
+ return col.alias ?? aggregateSyntheticName2(col.func, col.distinct, col.arg);
35457
+ case "ARITH_AGG_COL":
35458
+ return col.alias ?? aggArithDefaultKey(col.expr);
35459
+ case "ARITH_COL":
35460
+ return col.alias ?? arithColDefaultKey(col.expr);
35461
+ case "CASE_COL":
35462
+ return col.alias ?? "case";
35463
+ case "STRFUNC_COL":
35464
+ return col.alias ?? stringFuncDefaultKey(col.expr);
35465
+ case "SCALAR_SUBQUERY_COL":
35466
+ return col.alias ?? "(subquery)";
35467
+ case "WILDCARD":
35468
+ case "PARENT_WILDCARD":
35469
+ throw new Error("internal: computeOutputKeys received a wildcard column");
35470
+ }
35471
+ });
35472
+ }
35444
35473
  function buildDefaultFieldOutputKeys(columns) {
35445
35474
  const qualifierCollisionCount = /* @__PURE__ */ new Map();
35446
35475
  for (const col of columns) {
@@ -36831,8 +36860,9 @@ async function executeInsertSelect(stmt, client, options, cacheContext, cteCache
36831
36860
  const selectResult = cteCache !== void 0 && cteCache.size > 0 ? await executeQueryWithCte(stmt.select, client, options, cteCache, cacheContext) : await executeSelect(stmt.select, client, options, cacheContext);
36832
36861
  const { rows, columns } = selectResult;
36833
36862
  if (columns.length !== stmt.fields.length) {
36863
+ const emptySourceHint = columns.length === 0 && rows.length === 0 ? "\u3002\u7D50\u679C\u304C 0 \u884C\u306E\u305F\u3081\u5217\u3092\u7279\u5B9A\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F\uFF08SELECT * \u3092\u7A7A\u30BD\u30FC\u30B9\u306B\u4F7F\u3046\u3068\u5217\u3092\u6C7A\u5B9A\u3067\u304D\u307E\u305B\u3093\u3002\u660E\u793A\u5217\u3067\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044\uFF09" : "";
36834
36864
  throw new Error(
36835
- `SELECT \u306E\u5217\u6570\uFF08${columns.length}\uFF09\u3068 INSERT \u306E\u30D5\u30A3\u30FC\u30EB\u30C9\u6570\uFF08${stmt.fields.length}\uFF09\u304C\u4E00\u81F4\u3057\u307E\u305B\u3093`
36865
+ `SELECT \u306E\u5217\u6570\uFF08${columns.length}\uFF09\u3068 INSERT \u306E\u30D5\u30A3\u30FC\u30EB\u30C9\u6570\uFF08${stmt.fields.length}\uFF09\u304C\u4E00\u81F4\u3057\u307E\u305B\u3093${emptySourceHint}`
36836
36866
  );
36837
36867
  }
36838
36868
  if (options.confirm) {
@@ -37305,8 +37335,9 @@ async function executeUpsertSelect(stmt, client, options, cacheContext, cteCache
37305
37335
  const selectResult = cteCache !== void 0 && cteCache.size > 0 ? await executeQueryWithCte(stmt.select, client, options, cteCache, cacheContext) : await executeSelect(stmt.select, client, options, cacheContext);
37306
37336
  const { rows, columns } = selectResult;
37307
37337
  if (columns.length !== stmt.fields.length) {
37338
+ const emptySourceHint = columns.length === 0 && rows.length === 0 ? "\u3002\u7D50\u679C\u304C 0 \u884C\u306E\u305F\u3081\u5217\u3092\u7279\u5B9A\u3067\u304D\u307E\u305B\u3093\u3067\u3057\u305F\uFF08SELECT * \u3092\u7A7A\u30BD\u30FC\u30B9\u306B\u4F7F\u3046\u3068\u5217\u3092\u6C7A\u5B9A\u3067\u304D\u307E\u305B\u3093\u3002\u660E\u793A\u5217\u3067\u6307\u5B9A\u3057\u3066\u304F\u3060\u3055\u3044\uFF09" : "";
37308
37339
  throw new Error(
37309
- `SELECT \u306E\u5217\u6570\uFF08${columns.length}\uFF09\u3068 UPSERT \u306E\u30D5\u30A3\u30FC\u30EB\u30C9\u6570\uFF08${stmt.fields.length}\uFF09\u304C\u4E00\u81F4\u3057\u307E\u305B\u3093`
37340
+ `SELECT \u306E\u5217\u6570\uFF08${columns.length}\uFF09\u3068 UPSERT \u306E\u30D5\u30A3\u30FC\u30EB\u30C9\u6570\uFF08${stmt.fields.length}\uFF09\u304C\u4E00\u81F4\u3057\u307E\u305B\u3093${emptySourceHint}`
37310
37341
  );
37311
37342
  }
37312
37343
  for (const key of stmt.keyFields) {
@@ -39946,7 +39977,7 @@ Options:
39946
39977
  -h, --help Show help
39947
39978
  `);
39948
39979
  }
39949
- var SERVER_VERSION = true ? "2.1.0" : "0.0.0-dev";
39980
+ var SERVER_VERSION = true ? "2.1.1" : "0.0.0-dev";
39950
39981
  function createServer(args) {
39951
39982
  const server = new McpServer({
39952
39983
  name: "ksql-mcp",
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rex0220/kintone-sql-tools",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "kintone SQL plugin, CLI, and MCP tools (ksql)",
5
5
  "publishConfig": {
6
6
  "access": "public"