Skip to content

Commit

Permalink
Rerun go generate ./example
Browse files Browse the repository at this point in the history
  • Loading branch information
Nymphium committed Jul 6, 2023
1 parent 7d2e598 commit bc6f3d8
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 213 deletions.
137 changes: 39 additions & 98 deletions example/customstruct.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dgwexample

import (
"context"
"database/sql"
"time"
)
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -54,20 +52,20 @@ 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
}
return nil
}

// 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
}
Expand All @@ -76,27 +74,30 @@ 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
}
return nil
}

// 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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Loading

0 comments on commit bc6f3d8

Please sign in to comment.