From bc6f3d832ce27c9b962b6a27dbd71c63df986a10 Mon Sep 17 00:00:00 2001 From: Nymphium Date: Thu, 6 Jul 2023 15:01:16 +0900 Subject: [PATCH] Rerun `go generate ./example` --- example/customstruct.go | 137 ++++++++----------------- example/defaultstruct.go | 210 ++++++++++++++++++--------------------- example/example_test.go | 4 - 3 files changed, 138 insertions(+), 213 deletions(-) diff --git a/example/customstruct.go b/example/customstruct.go index fdcee98..4131393 100644 --- a/example/customstruct.go +++ b/example/customstruct.go @@ -1,6 +1,7 @@ package dgwexample import ( + "context" "database/sql" "time" ) @@ -10,20 +11,17 @@ type T1Table struct { ID int64 // id I int // i Str string // str - NumFloat float64 // num_float NullableStr sql.NullString // nullable_str TWithTz time.Time // t_with_tz TWithoutTz time.Time // t_without_tz - NullableTz *time.Time // nullable_tz - JSONData []byte // json_data - XMLData []byte // xml_data + Tm *time.Time // tm } // Create inserts the T1 to the database. func (r *T1Table) Create(db Queryer) error { err := db.QueryRow( - `INSERT INTO t1 (i, str, num_float, nullable_str, t_with_tz, t_without_tz, nullable_tz, json_data, xml_data) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING id`, - &r.I, &r.Str, &r.NumFloat, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.NullableTz, &r.JSONData, &r.XMLData).Scan(&r.ID) + `INSERT INTO t1 (i, str, nullable_str, t_with_tz, t_without_tz, tm) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`, + &r.I, &r.Str, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.Tm).Scan(&r.ID) if err != nil { return err } @@ -34,8 +32,8 @@ func (r *T1Table) Create(db Queryer) error { func GetT1TableByPk(db Queryer, pk0 int64) (*T1, error) { var r T1 err := db.QueryRow( - `SELECT id, i, str, num_float, nullable_str, t_with_tz, t_without_tz, nullable_tz, json_data, xml_data FROM t1 WHERE id = $1`, - pk0).Scan(&r.ID, &r.I, &r.Str, &r.NumFloat, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.NullableTz, &r.JSONData, &r.XMLData) + `SELECT id, i, str, nullable_str, t_with_tz, t_without_tz, tm FROM t1 WHERE id = $1`, + pk0).Scan(&r.ID, &r.I, &r.Str, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.Tm) if err != nil { return nil, err } @@ -54,8 +52,8 @@ type T2Table struct { // Create inserts the T2 to the database. func (r *T2Table) Create(db Queryer) error { err := db.QueryRow( - `INSERT INTO t2 (str, t_with_tz, t_without_tz) VALUES ($1, $2, $3) RETURNING id, i`, - &r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID, &r.I) + `INSERT INTO t2 (i, str, t_with_tz, t_without_tz) VALUES ($1, $2, $3, $4) RETURNING id`, + &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID) if err != nil { return err } @@ -63,11 +61,11 @@ func (r *T2Table) Create(db Queryer) error { } // GetT2TableByPk select the T2 from the database. -func GetT2TableByPk(db Queryer, pk0 int64, pk1 int) (*T2, error) { +func GetT2TableByPk(db Queryer, pk0 int64) (*T2, error) { var r T2 err := db.QueryRow( - `SELECT id, i, str, t_with_tz, t_without_tz FROM t2 WHERE id = $1 AND i = $2`, - pk0, pk1).Scan(&r.ID, &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz) + `SELECT id, i, str, t_with_tz, t_without_tz FROM t2 WHERE id = $1`, + pk0).Scan(&r.ID, &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz) if err != nil { return nil, err } @@ -76,15 +74,18 @@ func GetT2TableByPk(db Queryer, pk0 int64, pk1 int) (*T2, error) { // T3Table represents public.t3 type T3Table struct { - ID int // id - I int // i + ID int64 // id + I int // i + Str string // str + TWithTz time.Time // t_with_tz + TWithoutTz time.Time // t_without_tz } // Create inserts the T3 to the database. func (r *T3Table) Create(db Queryer) error { - _, err := db.Exec( - `INSERT INTO t3 (id, i) VALUES ($1, $2)`, - &r.ID, &r.I) + err := db.QueryRow( + `INSERT INTO t3 (str, t_with_tz, t_without_tz) VALUES ($1, $2, $3) RETURNING id, i`, + &r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID, &r.I) if err != nil { return err } @@ -92,11 +93,11 @@ func (r *T3Table) Create(db Queryer) error { } // GetT3TableByPk select the T3 from the database. -func GetT3TableByPk(db Queryer, pk0 int, pk1 int) (*T3, error) { +func GetT3TableByPk(db Queryer, pk0 int64, pk1 int) (*T3, error) { var r T3 err := db.QueryRow( - `SELECT id, i FROM t3 WHERE id = $1 AND i = $2`, - pk0, pk1).Scan(&r.ID, &r.I) + `SELECT id, i, str, t_with_tz, t_without_tz FROM t3 WHERE id = $1 AND i = $2`, + pk0, pk1).Scan(&r.ID, &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz) if err != nil { return nil, err } @@ -132,93 +133,30 @@ func GetT4TableByPk(db Queryer, pk0 int, pk1 int) (*T4, error) { return &r, nil } -// UserAccountTable represents public.user_account -type UserAccountTable struct { - ID int64 // id - Email string // email - LastName string // last_name - FirstName string // first_name -} - -// Create inserts the UserAccount to the database. -func (r *UserAccountTable) Create(db Queryer) error { - err := db.QueryRow( - `INSERT INTO user_account (email, last_name, first_name) VALUES ($1, $2, $3) RETURNING id`, - &r.Email, &r.LastName, &r.FirstName).Scan(&r.ID) - if err != nil { - return err - } - return nil -} - -// GetUserAccountTableByPk select the UserAccount from the database. -func GetUserAccountTableByPk(db Queryer, pk0 int64) (*UserAccount, error) { - var r UserAccount - err := db.QueryRow( - `SELECT id, email, last_name, first_name FROM user_account WHERE id = $1`, - pk0).Scan(&r.ID, &r.Email, &r.LastName, &r.FirstName) - if err != nil { - return nil, err - } - return &r, nil -} - -// UserAccountCompositePkTable represents public.user_account_composite_pk -type UserAccountCompositePkTable struct { - ID int64 // id - Email string // email - LastName string // last_name - FirstName string // first_name -} - -// Create inserts the UserAccountCompositePk to the database. -func (r *UserAccountCompositePkTable) Create(db Queryer) error { - _, err := db.Exec( - `INSERT INTO user_account_composite_pk (id, email, last_name, first_name) VALUES ($1, $2, $3, $4)`, - &r.ID, &r.Email, &r.LastName, &r.FirstName) - if err != nil { - return err - } - return nil -} - -// GetUserAccountCompositePkTableByPk select the UserAccountCompositePk from the database. -func GetUserAccountCompositePkTableByPk(db Queryer, pk0 int64, pk1 string) (*UserAccountCompositePk, error) { - var r UserAccountCompositePk - err := db.QueryRow( - `SELECT id, email, last_name, first_name FROM user_account_composite_pk WHERE id = $1 AND email = $2`, - pk0, pk1).Scan(&r.ID, &r.Email, &r.LastName, &r.FirstName) - if err != nil { - return nil, err - } - return &r, nil -} - -// UserAccountUUIDTable represents public.user_account_uuid -type UserAccountUUIDTable struct { - UUID string // uuid - Email string // email - LastName string // last_name - FirstName string // first_name +// T5Table represents public.t5 +type T5Table struct { + ID int64 // id + Select int // select + From string // from } -// Create inserts the UserAccountUUID to the database. -func (r *UserAccountUUIDTable) Create(db Queryer) error { +// Create inserts the T5 to the database. +func (r *T5Table) Create(db Queryer) error { err := db.QueryRow( - `INSERT INTO user_account_uuid (email, last_name, first_name) VALUES ($1, $2, $3) RETURNING uuid`, - &r.Email, &r.LastName, &r.FirstName).Scan(&r.UUID) + `INSERT INTO t5 (select, from) VALUES ($1, $2) RETURNING id`, + &r.Select, &r.From).Scan(&r.ID) if err != nil { return err } return nil } -// GetUserAccountUUIDTableByPk select the UserAccountUUID from the database. -func GetUserAccountUUIDTableByPk(db Queryer, pk0 string) (*UserAccountUUID, error) { - var r UserAccountUUID +// GetT5TableByPk select the T5 from the database. +func GetT5TableByPk(db Queryer, pk0 int64) (*T5, error) { + var r T5 err := db.QueryRow( - `SELECT uuid, email, last_name, first_name FROM user_account_uuid WHERE uuid = $1`, - pk0).Scan(&r.UUID, &r.Email, &r.LastName, &r.FirstName) + `SELECT id, select, from FROM t5 WHERE id = $1`, + pk0).Scan(&r.ID, &r.Select, &r.From) if err != nil { return nil, err } @@ -230,4 +168,7 @@ type Queryer interface { Exec(string, ...interface{}) (sql.Result, error) Query(string, ...interface{}) (*sql.Rows, error) QueryRow(string, ...interface{}) *sql.Row + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row } diff --git a/example/defaultstruct.go b/example/defaultstruct.go index af70526..3eed177 100644 --- a/example/defaultstruct.go +++ b/example/defaultstruct.go @@ -1,6 +1,7 @@ package dgwexample import ( + "context" "database/sql" "time" @@ -12,32 +13,39 @@ type T1 struct { ID int64 // id I int // i Str string // str - NumFloat float64 // num_float NullableStr sql.NullString // nullable_str TWithTz time.Time // t_with_tz TWithoutTz time.Time // t_without_tz - NullableTz *time.Time // nullable_tz - JSONData []byte // json_data - XMLData []byte // xml_data + Tm *time.Time // tm } // Create inserts the T1 to the database. func (r *T1) Create(db Queryer) error { - err := db.QueryRow( - `INSERT INTO t1 (i, str, num_float, nullable_str, t_with_tz, t_without_tz, nullable_tz, json_data, xml_data) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING id`, - &r.I, &r.Str, &r.NumFloat, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.NullableTz, &r.JSONData, &r.XMLData).Scan(&r.ID) + return r.CreateContext(context.Background(), db) +} + +// GetT1ByPk select the T1 from the database. +func GetT1ByPk(db Queryer, pk0 int64) (*T1, error) { + return GetT1ByPkContext(context.Background(), db, pk0) +} + +// CreateContext inserts the T1 to the database. +func (r *T1) CreateContext(ctx context.Context, db Queryer) error { + err := db.QueryRowContext(ctx, + `INSERT INTO t1 (i, str, nullable_str, t_with_tz, t_without_tz, tm) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`, + &r.I, &r.Str, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.Tm).Scan(&r.ID) if err != nil { return errors.Wrap(err, "failed to insert t1") } return nil } -// GetT1ByPk select the T1 from the database. -func GetT1ByPk(db Queryer, pk0 int64) (*T1, error) { +// GetT1ByPkContext select the T1 from the database. +func GetT1ByPkContext(ctx context.Context, db Queryer, pk0 int64) (*T1, error) { var r T1 - err := db.QueryRow( - `SELECT id, i, str, num_float, nullable_str, t_with_tz, t_without_tz, nullable_tz, json_data, xml_data FROM t1 WHERE id = $1`, - pk0).Scan(&r.ID, &r.I, &r.Str, &r.NumFloat, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.NullableTz, &r.JSONData, &r.XMLData) + err := db.QueryRowContext(ctx, + `SELECT id, i, str, nullable_str, t_with_tz, t_without_tz, tm FROM t1 WHERE id = $1`, + pk0).Scan(&r.ID, &r.I, &r.Str, &r.NullableStr, &r.TWithTz, &r.TWithoutTz, &r.Tm) if err != nil { return nil, errors.Wrap(err, "failed to select t1") } @@ -55,21 +63,31 @@ type T2 struct { // Create inserts the T2 to the database. func (r *T2) Create(db Queryer) error { - err := db.QueryRow( - `INSERT INTO t2 (str, t_with_tz, t_without_tz) VALUES ($1, $2, $3) RETURNING id, i`, - &r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID, &r.I) + return r.CreateContext(context.Background(), db) +} + +// GetT2ByPk select the T2 from the database. +func GetT2ByPk(db Queryer, pk0 int64) (*T2, error) { + return GetT2ByPkContext(context.Background(), db, pk0) +} + +// CreateContext inserts the T2 to the database. +func (r *T2) CreateContext(ctx context.Context, db Queryer) error { + err := db.QueryRowContext(ctx, + `INSERT INTO t2 (i, str, t_with_tz, t_without_tz) VALUES ($1, $2, $3, $4) RETURNING id`, + &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID) if err != nil { return errors.Wrap(err, "failed to insert t2") } return nil } -// GetT2ByPk select the T2 from the database. -func GetT2ByPk(db Queryer, pk0 int64, pk1 int) (*T2, error) { +// GetT2ByPkContext select the T2 from the database. +func GetT2ByPkContext(ctx context.Context, db Queryer, pk0 int64) (*T2, error) { var r T2 - err := db.QueryRow( - `SELECT id, i, str, t_with_tz, t_without_tz FROM t2 WHERE id = $1 AND i = $2`, - pk0, pk1).Scan(&r.ID, &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz) + err := db.QueryRowContext(ctx, + `SELECT id, i, str, t_with_tz, t_without_tz FROM t2 WHERE id = $1`, + pk0).Scan(&r.ID, &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz) if err != nil { return nil, errors.Wrap(err, "failed to select t2") } @@ -78,27 +96,40 @@ func GetT2ByPk(db Queryer, pk0 int64, pk1 int) (*T2, error) { // T3 represents public.t3 type T3 struct { - ID int // id - I int // i + ID int64 // id + I int // i + Str string // str + TWithTz time.Time // t_with_tz + TWithoutTz time.Time // t_without_tz } // Create inserts the T3 to the database. func (r *T3) Create(db Queryer) error { - _, err := db.Exec( - `INSERT INTO t3 (id, i) VALUES ($1, $2)`, - &r.ID, &r.I) + return r.CreateContext(context.Background(), db) +} + +// GetT3ByPk select the T3 from the database. +func GetT3ByPk(db Queryer, pk0 int64, pk1 int) (*T3, error) { + return GetT3ByPkContext(context.Background(), db, pk0, pk1) +} + +// CreateContext inserts the T3 to the database. +func (r *T3) CreateContext(ctx context.Context, db Queryer) error { + err := db.QueryRowContext(ctx, + `INSERT INTO t3 (str, t_with_tz, t_without_tz) VALUES ($1, $2, $3) RETURNING id, i`, + &r.Str, &r.TWithTz, &r.TWithoutTz).Scan(&r.ID, &r.I) if err != nil { return errors.Wrap(err, "failed to insert t3") } return nil } -// GetT3ByPk select the T3 from the database. -func GetT3ByPk(db Queryer, pk0 int, pk1 int) (*T3, error) { +// GetT3ByPkContext select the T3 from the database. +func GetT3ByPkContext(ctx context.Context, db Queryer, pk0 int64, pk1 int) (*T3, error) { var r T3 - err := db.QueryRow( - `SELECT id, i FROM t3 WHERE id = $1 AND i = $2`, - pk0, pk1).Scan(&r.ID, &r.I) + err := db.QueryRowContext(ctx, + `SELECT id, i, str, t_with_tz, t_without_tz FROM t3 WHERE id = $1 AND i = $2`, + pk0, pk1).Scan(&r.ID, &r.I, &r.Str, &r.TWithTz, &r.TWithoutTz) if err != nil { return nil, errors.Wrap(err, "failed to select t3") } @@ -113,7 +144,17 @@ type T4 struct { // Create inserts the T4 to the database. func (r *T4) Create(db Queryer) error { - _, err := db.Exec( + return r.CreateContext(context.Background(), db) +} + +// GetT4ByPk select the T4 from the database. +func GetT4ByPk(db Queryer, pk0 int, pk1 int) (*T4, error) { + return GetT4ByPkContext(context.Background(), db, pk0, pk1) +} + +// CreateContext inserts the T4 to the database. +func (r *T4) CreateContext(ctx context.Context, db Queryer) error { + _, err := db.ExecContext(ctx, `INSERT INTO t4 (id, i) VALUES ($1, $2)`, &r.ID, &r.I) if err != nil { @@ -122,10 +163,10 @@ func (r *T4) Create(db Queryer) error { return nil } -// GetT4ByPk select the T4 from the database. -func GetT4ByPk(db Queryer, pk0 int, pk1 int) (*T4, error) { +// GetT4ByPkContext select the T4 from the database. +func GetT4ByPkContext(ctx context.Context, db Queryer, pk0 int, pk1 int) (*T4, error) { var r T4 - err := db.QueryRow( + err := db.QueryRowContext(ctx, `SELECT id, i FROM t4 WHERE id = $1 AND i = $2`, pk0, pk1).Scan(&r.ID, &r.I) if err != nil { @@ -134,95 +175,42 @@ func GetT4ByPk(db Queryer, pk0 int, pk1 int) (*T4, error) { return &r, nil } -// UserAccount represents public.user_account -type UserAccount struct { - ID int64 // id - Email string // email - LastName string // last_name - FirstName string // first_name -} - -// Create inserts the UserAccount to the database. -func (r *UserAccount) Create(db Queryer) error { - err := db.QueryRow( - `INSERT INTO user_account (email, last_name, first_name) VALUES ($1, $2, $3) RETURNING id`, - &r.Email, &r.LastName, &r.FirstName).Scan(&r.ID) - if err != nil { - return errors.Wrap(err, "failed to insert user_account") - } - return nil -} - -// GetUserAccountByPk select the UserAccount from the database. -func GetUserAccountByPk(db Queryer, pk0 int64) (*UserAccount, error) { - var r UserAccount - err := db.QueryRow( - `SELECT id, email, last_name, first_name FROM user_account WHERE id = $1`, - pk0).Scan(&r.ID, &r.Email, &r.LastName, &r.FirstName) - if err != nil { - return nil, errors.Wrap(err, "failed to select user_account") - } - return &r, nil -} - -// UserAccountCompositePk represents public.user_account_composite_pk -type UserAccountCompositePk struct { - ID int64 // id - Email string // email - LastName string // last_name - FirstName string // first_name +// T5 represents public.t5 +type T5 struct { + ID int64 // id + Select int // select + From string // from } -// Create inserts the UserAccountCompositePk to the database. -func (r *UserAccountCompositePk) Create(db Queryer) error { - _, err := db.Exec( - `INSERT INTO user_account_composite_pk (id, email, last_name, first_name) VALUES ($1, $2, $3, $4)`, - &r.ID, &r.Email, &r.LastName, &r.FirstName) - if err != nil { - return errors.Wrap(err, "failed to insert user_account_composite_pk") - } - return nil -} - -// GetUserAccountCompositePkByPk select the UserAccountCompositePk from the database. -func GetUserAccountCompositePkByPk(db Queryer, pk0 int64, pk1 string) (*UserAccountCompositePk, error) { - var r UserAccountCompositePk - err := db.QueryRow( - `SELECT id, email, last_name, first_name FROM user_account_composite_pk WHERE id = $1 AND email = $2`, - pk0, pk1).Scan(&r.ID, &r.Email, &r.LastName, &r.FirstName) - if err != nil { - return nil, errors.Wrap(err, "failed to select user_account_composite_pk") - } - return &r, nil +// Create inserts the T5 to the database. +func (r *T5) Create(db Queryer) error { + return r.CreateContext(context.Background(), db) } -// UserAccountUUID represents public.user_account_uuid -type UserAccountUUID struct { - UUID string // uuid - Email string // email - LastName string // last_name - FirstName string // first_name +// GetT5ByPk select the T5 from the database. +func GetT5ByPk(db Queryer, pk0 int64) (*T5, error) { + return GetT5ByPkContext(context.Background(), db, pk0) } -// Create inserts the UserAccountUUID to the database. -func (r *UserAccountUUID) Create(db Queryer) error { - err := db.QueryRow( - `INSERT INTO user_account_uuid (email, last_name, first_name) VALUES ($1, $2, $3) RETURNING uuid`, - &r.Email, &r.LastName, &r.FirstName).Scan(&r.UUID) +// CreateContext inserts the T5 to the database. +func (r *T5) CreateContext(ctx context.Context, db Queryer) error { + err := db.QueryRowContext(ctx, + `INSERT INTO t5 (select, from) VALUES ($1, $2) RETURNING id`, + &r.Select, &r.From).Scan(&r.ID) if err != nil { - return errors.Wrap(err, "failed to insert user_account_uuid") + return errors.Wrap(err, "failed to insert t5") } return nil } -// GetUserAccountUUIDByPk select the UserAccountUUID from the database. -func GetUserAccountUUIDByPk(db Queryer, pk0 string) (*UserAccountUUID, error) { - var r UserAccountUUID - err := db.QueryRow( - `SELECT uuid, email, last_name, first_name FROM user_account_uuid WHERE uuid = $1`, - pk0).Scan(&r.UUID, &r.Email, &r.LastName, &r.FirstName) +// GetT5ByPkContext select the T5 from the database. +func GetT5ByPkContext(ctx context.Context, db Queryer, pk0 int64) (*T5, error) { + var r T5 + err := db.QueryRowContext(ctx, + `SELECT id, select, from FROM t5 WHERE id = $1`, + pk0).Scan(&r.ID, &r.Select, &r.From) if err != nil { - return nil, errors.Wrap(err, "failed to select user_account_uuid") + return nil, errors.Wrap(err, "failed to select t5") } return &r, nil } diff --git a/example/example_test.go b/example/example_test.go index aa82fe7..7cec348 100644 --- a/example/example_test.go +++ b/example/example_test.go @@ -33,11 +33,7 @@ func TestT1(t *testing.T) { now := time.Now() t1 := T1{ I: 100, - JSONData: []byte("{\"key\": \"value\"}"), - XMLData: []byte("value"), NullableStr: sql.NullString{String: "test"}, - NullableTz: &now, - NumFloat: 100.10, Str: "test", TWithTz: now.AddDate(0, 0, 7), TWithoutTz: now.AddDate(0, 0, 7),