|
8 | 8 | "testing" |
9 | 9 |
|
10 | 10 | _ "github.com/lib/pq" |
| 11 | + "github.com/stretchr/testify/assert" |
11 | 12 | ) |
12 | 13 |
|
13 | 14 | // before running test, create user and database |
@@ -480,3 +481,250 @@ func GetT4ByPkContext(ctx context.Context, db Queryer, pk0 int, pk1 int) (*T4, e |
480 | 481 | }) |
481 | 482 | } |
482 | 483 | } |
| 484 | + |
| 485 | +func TestPgCreateStruct(t *testing.T) { |
| 486 | + conn, cleanup := testPgSetup(t) |
| 487 | + defer cleanup() |
| 488 | + assert := assert.New(t) |
| 489 | + |
| 490 | + schema := "public" |
| 491 | + src, err := PgCreateStruct(conn, schema, "", "mypkg", "", []string{}) |
| 492 | + if err != nil { |
| 493 | + t.Fatal(err) |
| 494 | + } |
| 495 | + |
| 496 | + expected := `// Code generated by dgw. DO NOT EDIT. |
| 497 | +
|
| 498 | +package mypkg |
| 499 | +
|
| 500 | +// T1 represents public.t1 |
| 501 | +type T1 struct { |
| 502 | + ID int64 // id |
| 503 | + I int // i |
| 504 | + Str string // str |
| 505 | + NullableStr sql.NullString // nullable_str |
| 506 | + TWithTz time.Time // t_with_tz |
| 507 | + TWithoutTz time.Time // t_without_tz |
| 508 | + Tm *time.Time // tm |
| 509 | +} |
| 510 | +// Create inserts the T1 to the database. |
| 511 | +func (r *T1) Create(db Queryer) error { |
| 512 | + return r.CreateContext(context.Background(), db) |
| 513 | +} |
| 514 | +
|
| 515 | +// GetT1ByPk select the T1 from the database. |
| 516 | +func GetT1ByPk(db Queryer, pk0 int64) (*T1, error) { |
| 517 | + return GetT1ByPkContext(context.Background(), db, pk0) |
| 518 | +} |
| 519 | +
|
| 520 | +// CreateContext inserts the T1 to the database. |
| 521 | +func (r *T1) CreateContext(ctx context.Context, db Queryer) error { |
| 522 | + err := db.QueryRowContext(ctx, |
| 523 | + ` + "`INSERT INTO t1 (i, str, nullable_str, t_with_tz, t_without_tz, tm) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`" + `, |
| 524 | + &r.I, &r.Str, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.Tm).Scan(&r.ID) |
| 525 | + if err != nil { |
| 526 | + return errors.WithStack(err) |
| 527 | + } |
| 528 | + return nil |
| 529 | +} |
| 530 | +
|
| 531 | +// CreateOnConflictDoNothing inserts the T1 to the database. |
| 532 | +// If a conflict occurs (e.g., unique constraint violation), the insert is skipped without error. |
| 533 | +// Returns true if the row was inserted, false if it was skipped due to conflict. |
| 534 | +func (r *T1) CreateOnConflictDoNothing(ctx context.Context, db Queryer) (bool, error) { |
| 535 | + err := db.QueryRowContext(ctx, |
| 536 | + ` + "`INSERT INTO t1 (i, str, nullable_str, t_with_tz, t_without_tz, tm) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT DO NOTHING RETURNING id`" + `, |
| 537 | + &r.I, &r.Str, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.Tm).Scan(&r.ID) |
| 538 | + if err != nil { |
| 539 | + if err == sql.ErrNoRows { |
| 540 | + return false, nil |
| 541 | + } |
| 542 | + return false, errors.WithStack(err) |
| 543 | + } |
| 544 | + // Row was successfully inserted |
| 545 | + return true, nil |
| 546 | +} |
| 547 | +
|
| 548 | +// GetT1ByPkContext select the T1 from the database. |
| 549 | +func GetT1ByPkContext(ctx context.Context, db Queryer, pk0 int64) (*T1, error) { |
| 550 | + var r T1 |
| 551 | + err := db.QueryRowContext(ctx, |
| 552 | + ` + "`SELECT id, i, str, nullable_str, t_with_tz, t_without_tz, tm FROM t1 WHERE id = $1`" + `, |
| 553 | + pk0).Scan(&r.ID, &r.I, &r.Str, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.Tm) |
| 554 | + if err != nil { |
| 555 | + return nil, errors.WithStack(err) |
| 556 | + } |
| 557 | + return &r, nil |
| 558 | +} |
| 559 | +// T2 represents public.t2 |
| 560 | +type T2 struct { |
| 561 | + ID int64 // id |
| 562 | + I int // i |
| 563 | + Str string // str |
| 564 | + TWithTz time.Time // t_with_tz |
| 565 | + TWithoutTz time.Time // t_without_tz |
| 566 | +} |
| 567 | +// Create inserts the T2 to the database. |
| 568 | +func (r *T2) Create(db Queryer) error { |
| 569 | + return r.CreateContext(context.Background(), db) |
| 570 | +} |
| 571 | +
|
| 572 | +// GetT2ByPk select the T2 from the database. |
| 573 | +func GetT2ByPk(db Queryer, pk0 int64) (*T2, error) { |
| 574 | + return GetT2ByPkContext(context.Background(), db, pk0) |
| 575 | +} |
| 576 | +
|
| 577 | +// CreateContext inserts the T2 to the database. |
| 578 | +func (r *T2) CreateContext(ctx context.Context, db Queryer) error { |
| 579 | + err := db.QueryRowContext(ctx, |
| 580 | + ` + "`INSERT INTO t2 (i, str, t_with_tz, t_without_tz) VALUES ($1, $2, $3, $4) RETURNING id`" + `, |
| 581 | + &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID) |
| 582 | + if err != nil { |
| 583 | + return errors.WithStack(err) |
| 584 | + } |
| 585 | + return nil |
| 586 | +} |
| 587 | +
|
| 588 | +// CreateOnConflictDoNothing inserts the T2 to the database. |
| 589 | +// If a conflict occurs (e.g., unique constraint violation), the insert is skipped without error. |
| 590 | +// Returns true if the row was inserted, false if it was skipped due to conflict. |
| 591 | +func (r *T2) CreateOnConflictDoNothing(ctx context.Context, db Queryer) (bool, error) { |
| 592 | + err := db.QueryRowContext(ctx, |
| 593 | + ` + "`INSERT INTO t2 (i, str, t_with_tz, t_without_tz) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING RETURNING id`" + `, |
| 594 | + &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID) |
| 595 | + if err != nil { |
| 596 | + if err == sql.ErrNoRows { |
| 597 | + return false, nil |
| 598 | + } |
| 599 | + return false, errors.WithStack(err) |
| 600 | + } |
| 601 | + // Row was successfully inserted |
| 602 | + return true, nil |
| 603 | +} |
| 604 | +
|
| 605 | +// GetT2ByPkContext select the T2 from the database. |
| 606 | +func GetT2ByPkContext(ctx context.Context, db Queryer, pk0 int64) (*T2, error) { |
| 607 | + var r T2 |
| 608 | + err := db.QueryRowContext(ctx, |
| 609 | + ` + "`SELECT id, i, str, t_with_tz, t_without_tz FROM t2 WHERE id = $1`" + `, |
| 610 | + pk0).Scan(&r.ID, &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz) |
| 611 | + if err != nil { |
| 612 | + return nil, errors.WithStack(err) |
| 613 | + } |
| 614 | + return &r, nil |
| 615 | +} |
| 616 | +// T3 represents public.t3 |
| 617 | +type T3 struct { |
| 618 | + ID int64 // id |
| 619 | + I int // i |
| 620 | + Str string // str |
| 621 | + TWithTz time.Time // t_with_tz |
| 622 | + TWithoutTz time.Time // t_without_tz |
| 623 | +} |
| 624 | +// Create inserts the T3 to the database. |
| 625 | +func (r *T3) Create(db Queryer) error { |
| 626 | + return r.CreateContext(context.Background(), db) |
| 627 | +} |
| 628 | +
|
| 629 | +// GetT3ByPk select the T3 from the database. |
| 630 | +func GetT3ByPk(db Queryer, pk0 int64, pk1 int) (*T3, error) { |
| 631 | + return GetT3ByPkContext(context.Background(), db, pk0, pk1) |
| 632 | +} |
| 633 | +
|
| 634 | +// CreateContext inserts the T3 to the database. |
| 635 | +func (r *T3) CreateContext(ctx context.Context, db Queryer) error { |
| 636 | + err := db.QueryRowContext(ctx, |
| 637 | + ` + "`INSERT INTO t3 (str, t_with_tz, t_without_tz) VALUES ($1, $2, $3) RETURNING id, i`" + `, |
| 638 | + &r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID, &r.I) |
| 639 | + if err != nil { |
| 640 | + return errors.WithStack(err) |
| 641 | + } |
| 642 | + return nil |
| 643 | +} |
| 644 | +
|
| 645 | +// CreateOnConflictDoNothing inserts the T3 to the database. |
| 646 | +// If a conflict occurs (e.g., unique constraint violation), the insert is skipped without error. |
| 647 | +// Returns true if the row was inserted, false if it was skipped due to conflict. |
| 648 | +func (r *T3) CreateOnConflictDoNothing(ctx context.Context, db Queryer) (bool, error) { |
| 649 | + err := db.QueryRowContext(ctx, |
| 650 | + ` + "`INSERT INTO t3 (str, t_with_tz, t_without_tz) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING RETURNING id, i`" + `, |
| 651 | + &r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID, &r.I) |
| 652 | + if err != nil { |
| 653 | + if err == sql.ErrNoRows { |
| 654 | + return false, nil |
| 655 | + } |
| 656 | + return false, errors.WithStack(err) |
| 657 | + } |
| 658 | + // Row was successfully inserted |
| 659 | + return true, nil |
| 660 | +} |
| 661 | +
|
| 662 | +// GetT3ByPkContext select the T3 from the database. |
| 663 | +func GetT3ByPkContext(ctx context.Context, db Queryer, pk0 int64, pk1 int) (*T3, error) { |
| 664 | + var r T3 |
| 665 | + err := db.QueryRowContext(ctx, |
| 666 | + ` + "`SELECT id, i, str, t_with_tz, t_without_tz FROM t3 WHERE id = $1 AND i = $2`" + `, |
| 667 | + pk0, pk1).Scan(&r.ID, &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz) |
| 668 | + if err != nil { |
| 669 | + return nil, errors.WithStack(err) |
| 670 | + } |
| 671 | + return &r, nil |
| 672 | +} |
| 673 | +// T4 represents public.t4 |
| 674 | +type T4 struct { |
| 675 | + ID int // id |
| 676 | + I int // i |
| 677 | +} |
| 678 | +// Create inserts the T4 to the database. |
| 679 | +func (r *T4) Create(db Queryer) error { |
| 680 | + return r.CreateContext(context.Background(), db) |
| 681 | +} |
| 682 | +
|
| 683 | +// GetT4ByPk select the T4 from the database. |
| 684 | +func GetT4ByPk(db Queryer, pk0 int, pk1 int) (*T4, error) { |
| 685 | + return GetT4ByPkContext(context.Background(), db, pk0, pk1) |
| 686 | +} |
| 687 | +
|
| 688 | +// CreateContext inserts the T4 to the database. |
| 689 | +func (r *T4) CreateContext(ctx context.Context, db Queryer) error { |
| 690 | + _, err := db.ExecContext(ctx, |
| 691 | + ` + "`INSERT INTO t4 (id, i) VALUES ($1, $2)`" + `, |
| 692 | + &r.ID, &r.I) |
| 693 | + if err != nil { |
| 694 | + return errors.WithStack(err) |
| 695 | + } |
| 696 | + return nil |
| 697 | +} |
| 698 | +
|
| 699 | +// CreateOnConflictDoNothing inserts the T4 to the database. |
| 700 | +// If a conflict occurs (e.g., unique constraint violation), the insert is skipped without error. |
| 701 | +// Returns true if the row was inserted, false if it was skipped due to conflict. |
| 702 | +func (r *T4) CreateOnConflictDoNothing(ctx context.Context, db Queryer) (bool, error) { |
| 703 | + result, err := db.ExecContext(ctx, |
| 704 | + ` + "`INSERT INTO t4 (id, i) VALUES ($1, $2) ON CONFLICT DO NOTHING`" + `, |
| 705 | + &r.ID, &r.I) |
| 706 | + if err != nil { |
| 707 | + return false, errors.WithStack(err) |
| 708 | + } |
| 709 | + rowsAffected, err := result.RowsAffected() |
| 710 | + if err != nil { |
| 711 | + return false, errors.WithStack(err) |
| 712 | + } |
| 713 | + return rowsAffected > 0, nil |
| 714 | +} |
| 715 | +
|
| 716 | +// GetT4ByPkContext select the T4 from the database. |
| 717 | +func GetT4ByPkContext(ctx context.Context, db Queryer, pk0 int, pk1 int) (*T4, error) { |
| 718 | + var r T4 |
| 719 | + err := db.QueryRowContext(ctx, |
| 720 | + ` + "`SELECT id, i FROM t4 WHERE id = $1 AND i = $2`" + `, |
| 721 | + pk0, pk1).Scan(&r.ID, &r.I) |
| 722 | + if err != nil { |
| 723 | + return nil, errors.WithStack(err) |
| 724 | + } |
| 725 | + return &r, nil |
| 726 | +} |
| 727 | +` |
| 728 | + |
| 729 | + assert.Equal(expected, string(src)) |
| 730 | +} |
0 commit comments