Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CI and generated files #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ jobs:
# - macOS-latest
# - windows-latest
go:
- "1.19.1"
- "1.19.10"
- "1.20.5"
- "1.21.0-rc.2"
services:
postgres:
image: postgres:12
Expand All @@ -31,16 +33,53 @@ jobs:
run: |
psql -U postgres -h localhost -d postgres -c 'CREATE USER dgw_test;'
psql -U postgres -h localhost -d postgres -c 'CREATE DATABASE dgw_test OWNER dgw_test;'
- name: Checkout code
uses: actions/checkout/@v3
- name: Install Go
uses: actions/setup-go/@v2
uses: actions/setup-go/@v4
with:
go-version: ${{ matrix.go }}

- name: Checkout code
uses: actions/checkout/@v3

- name: Download Go modules
shell: bash
if: ${{ steps.setup-go.outputs.cache-hit != 'true' }}
run: go mod download
- name: Lint
uses: golangci/golangci-lint-action@v3

- name: Run test
run: go test -v

check-test-gen:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:12
env:
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- name: Prepare planter
run: |
psql -U postgres -h localhost -d postgres -c 'CREATE USER dgw_test;'
psql -U postgres -h localhost -d postgres -c 'CREATE DATABASE dgw_test OWNER dgw_test;'
- name: Checkout code
uses: actions/checkout/@v3
- name: Install Go
uses: actions/setup-go/@v4
with:
go-version: "1.20.5"
- name: Download Go modules
shell: bash
if: ${{ steps.setup-go.outputs.cache-hit != 'true' }}
run: go mod download
- name: Install deps
run: |
psql -U postgres -h localhost -d dgw_test -f ./test.sql
go install golang.org/x/tools/cmd/goimports@latest
go install
- name: Generate
run: go generate -v ./example
- name: Check diff
run: git diff --exit-code
139 changes: 25 additions & 114 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,102 +133,12 @@ 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
}

// Create inserts the UserAccountUUID to the database.
func (r *UserAccountUUIDTable) 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)
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
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)
if err != nil {
return nil, err
}
return &r, nil
}

// Queryer database/sql compatible query interface
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
Loading