|
1 | 1 | import { SyntaxKind, NodeFlags, TypeNode, factory } from "typescript";
|
2 | 2 |
|
3 | 3 | import { Parameter, Column } from "../gen/plugin/codegen_pb";
|
4 |
| -import { argName } from "./utlis"; |
| 4 | +import { argName, colName } from "./utlis"; |
5 | 5 | import { log } from "../logger";
|
6 | 6 |
|
7 | 7 | export function columnType(column?: Column): TypeNode {
|
@@ -393,33 +393,62 @@ export function manyDecl(
|
393 | 393 | factory.createBlock(
|
394 | 394 | [
|
395 | 395 | factory.createReturnStatement(
|
396 |
| - factory.createAwaitExpression( |
397 |
| - factory.createCallExpression( |
398 |
| - factory.createPropertyAccessExpression( |
399 |
| - factory.createIdentifier("sql"), |
400 |
| - factory.createIdentifier("unsafe") |
| 396 | + factory.createCallExpression( |
| 397 | + factory.createPropertyAccessExpression( |
| 398 | + factory.createAwaitExpression( |
| 399 | + factory.createCallExpression( |
| 400 | + factory.createPropertyAccessExpression( |
| 401 | + factory.createCallExpression( |
| 402 | + factory.createPropertyAccessExpression( |
| 403 | + factory.createIdentifier("sql"), |
| 404 | + factory.createIdentifier("unsafe") |
| 405 | + ), |
| 406 | + undefined, |
| 407 | + [ |
| 408 | + factory.createIdentifier(queryName), |
| 409 | + factory.createArrayLiteralExpression( |
| 410 | + params.map((param, i) => |
| 411 | + factory.createPropertyAccessExpression( |
| 412 | + factory.createIdentifier("args"), |
| 413 | + factory.createIdentifier(argName(i, param.column)) |
| 414 | + ) |
| 415 | + ), |
| 416 | + false |
| 417 | + ), |
| 418 | + ] |
| 419 | + ), |
| 420 | + factory.createIdentifier("values"), |
| 421 | + ), |
| 422 | + undefined, |
| 423 | + undefined, |
| 424 | + ) |
401 | 425 | ),
|
402 |
| - [ |
403 |
| - factory.createArrayTypeNode( |
404 |
| - factory.createTypeReferenceNode( |
405 |
| - factory.createIdentifier(returnIface), |
406 |
| - undefined |
407 |
| - ) |
408 |
| - ), |
409 |
| - ], |
410 |
| - [ |
411 |
| - factory.createIdentifier(queryName), |
412 |
| - factory.createArrayLiteralExpression( |
413 |
| - params.map((param, i) => |
414 |
| - factory.createPropertyAccessExpression( |
415 |
| - factory.createIdentifier("args"), |
416 |
| - factory.createIdentifier(argName(i, param.column)) |
| 426 | + factory.createIdentifier("map"), |
| 427 | + ), |
| 428 | + undefined, |
| 429 | + [ |
| 430 | + factory.createArrowFunction( |
| 431 | + undefined, |
| 432 | + undefined, |
| 433 | + [ |
| 434 | + factory.createParameterDeclaration(undefined, undefined, "row"), |
| 435 | + ], |
| 436 | + undefined, |
| 437 | + factory.createToken(SyntaxKind.EqualsGreaterThanToken), |
| 438 | + factory.createObjectLiteralExpression( |
| 439 | + columns.map((col, i) => |
| 440 | + factory.createPropertyAssignment( |
| 441 | + factory.createIdentifier(colName(i, col)), |
| 442 | + factory.createElementAccessExpression( |
| 443 | + factory.createIdentifier("row"), |
| 444 | + factory.createNumericLiteral(`${i}`) |
| 445 | + ) |
417 | 446 | )
|
418 | 447 | ),
|
419 |
| - false |
420 |
| - ), |
421 |
| - ] |
422 |
| - ) |
| 448 | + true |
| 449 | + ) |
| 450 | + ), |
| 451 | + ] |
423 | 452 | )
|
424 | 453 | ),
|
425 | 454 | ],
|
@@ -469,29 +498,29 @@ export function oneDecl(
|
469 | 498 | factory.createAwaitExpression(
|
470 | 499 | factory.createCallExpression(
|
471 | 500 | factory.createPropertyAccessExpression(
|
472 |
| - factory.createIdentifier("sql"), |
473 |
| - factory.createIdentifier("unsafe") |
474 |
| - ), |
475 |
| - [ |
476 |
| - factory.createArrayTypeNode( |
477 |
| - factory.createTypeReferenceNode( |
478 |
| - factory.createIdentifier(returnIface), |
479 |
| - undefined |
480 |
| - ) |
481 |
| - ), |
482 |
| - ], |
483 |
| - [ |
484 |
| - factory.createIdentifier(queryName), |
485 |
| - factory.createArrayLiteralExpression( |
486 |
| - params.map((param, i) => |
487 |
| - factory.createPropertyAccessExpression( |
488 |
| - factory.createIdentifier("args"), |
489 |
| - factory.createIdentifier(argName(i, param.column)) |
490 |
| - ) |
| 501 | + factory.createCallExpression( |
| 502 | + factory.createPropertyAccessExpression( |
| 503 | + factory.createIdentifier("sql"), |
| 504 | + factory.createIdentifier("unsafe") |
491 | 505 | ),
|
492 |
| - false |
| 506 | + undefined, |
| 507 | + [ |
| 508 | + factory.createIdentifier(queryName), |
| 509 | + factory.createArrayLiteralExpression( |
| 510 | + params.map((param, i) => |
| 511 | + factory.createPropertyAccessExpression( |
| 512 | + factory.createIdentifier("args"), |
| 513 | + factory.createIdentifier(argName(i, param.column)) |
| 514 | + ) |
| 515 | + ), |
| 516 | + false |
| 517 | + ), |
| 518 | + ] |
493 | 519 | ),
|
494 |
| - ] |
| 520 | + factory.createIdentifier("values") |
| 521 | + ), |
| 522 | + undefined, |
| 523 | + undefined, |
495 | 524 | )
|
496 | 525 | )
|
497 | 526 | ),
|
@@ -519,10 +548,27 @@ export function oneDecl(
|
519 | 548 | ),
|
520 | 549 | undefined
|
521 | 550 | ),
|
| 551 | + factory.createVariableStatement( |
| 552 | + undefined, |
| 553 | + factory.createVariableDeclarationList([ |
| 554 | + factory.createVariableDeclaration("row", undefined, undefined, factory.createElementAccessExpression( |
| 555 | + factory.createIdentifier("rows"), |
| 556 | + factory.createNumericLiteral("0") |
| 557 | + )), |
| 558 | + ], NodeFlags.Const) |
| 559 | + ), |
522 | 560 | factory.createReturnStatement(
|
523 |
| - factory.createElementAccessExpression( |
524 |
| - factory.createIdentifier("rows"), |
525 |
| - factory.createNumericLiteral("0") |
| 561 | + factory.createObjectLiteralExpression( |
| 562 | + columns.map((col, i) => |
| 563 | + factory.createPropertyAssignment( |
| 564 | + factory.createIdentifier(colName(i, col)), |
| 565 | + factory.createElementAccessExpression( |
| 566 | + factory.createIdentifier("row"), |
| 567 | + factory.createNumericLiteral(`${i}`) |
| 568 | + ) |
| 569 | + ) |
| 570 | + ), |
| 571 | + true |
526 | 572 | )
|
527 | 573 | ),
|
528 | 574 | ],
|
|
0 commit comments