Skip to content

Commit 00d3fa0

Browse files
authored
Merge pull request #38 from kanmu/add_header
Add header comment
2 parents 22b10ce + 56be53a commit 00d3fa0

File tree

4 files changed

+260
-4
lines changed

4 files changed

+260
-4
lines changed

dgw.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func PgExecuteCustomTmpl(st *StructTmpl, customTmpl string) ([]byte, error) {
364364
// PgCreateStruct creates struct from given schema
365365
func PgCreateStruct(
366366
db Queryer, schema, typeMapPath, pkgName, customTmpl string, exTbls []string) ([]byte, error) {
367-
var src []byte
367+
src := []byte("// Code generated by dgw. DO NOT EDIT.\n\n")
368368
pkgDef := []byte(fmt.Sprintf("package %s\n\n", pkgName))
369369
src = append(src, pkgDef...)
370370

dgw_test.go

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
_ "github.com/lib/pq"
11+
"github.com/stretchr/testify/assert"
1112
)
1213

1314
// before running test, create user and database
@@ -480,3 +481,250 @@ func GetT4ByPkContext(ctx context.Context, db Queryer, pk0 int, pk1 int) (*T4, e
480481
})
481482
}
482483
}
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+
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ require (
77
github.com/achiku/varfmt v0.0.0-20160708124000-f820e1efecee
88
github.com/lib/pq v1.10.9
99
github.com/pkg/errors v0.9.1
10+
github.com/stretchr/testify v1.11.1
1011
gopkg.in/alecthomas/kingpin.v2 v2.2.6
1112
)
1213

1314
require (
1415
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
1516
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 // indirect
17+
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/pmezard/go-difflib v1.0.0 // indirect
19+
gopkg.in/yaml.v3 v3.0.1 // indirect
1620
)

go.sum

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafo
66
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
77
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 h1:AUNCr9CiJuwrRYS3XieqF+Z9B9gNxo/eANAJCF2eiN4=
88
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
9-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
109
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
11+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1112
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
1213
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
1314
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1415
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1516
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1617
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1718
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
1919
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
20+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
21+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
2022
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
2123
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
24+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2225
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
23-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
2426
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
27+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
28+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)