You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.4 KiB
91 lines
2.4 KiB
// Code generated by goctl. DO NOT EDIT.
|
|
|
|
package pgTest
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/builder"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
)
|
|
|
|
var (
|
|
testFieldNames = builder.RawFieldNames(&Test{}, true)
|
|
testRows = strings.Join(testFieldNames, ",")
|
|
testRowsExpectAutoSet = strings.Join(stringx.Remove(testFieldNames), ",")
|
|
testRowsWithPlaceHolder = builder.PostgreSqlJoin(stringx.Remove(testFieldNames, "id"))
|
|
)
|
|
|
|
type (
|
|
testModel interface {
|
|
Insert(ctx context.Context, data *Test) (sql.Result, error)
|
|
FindOne(ctx context.Context, id string) (*Test, error)
|
|
Update(ctx context.Context, data *Test) error
|
|
Delete(ctx context.Context, id string) error
|
|
}
|
|
|
|
defaultTestModel struct {
|
|
conn sqlx.SqlConn
|
|
table string
|
|
}
|
|
|
|
Test struct {
|
|
Id string `db:"id"`
|
|
Server string `db:"server"`
|
|
}
|
|
)
|
|
|
|
func newTestModel(conn sqlx.SqlConn) *defaultTestModel {
|
|
return &defaultTestModel{
|
|
conn: conn,
|
|
table: `"public"."test"`,
|
|
}
|
|
}
|
|
|
|
func (m *defaultTestModel) withSession(session sqlx.Session) *defaultTestModel {
|
|
return &defaultTestModel{
|
|
conn: sqlx.NewSqlConnFromSession(session),
|
|
table: `"public"."test"`,
|
|
}
|
|
}
|
|
|
|
func (m *defaultTestModel) Delete(ctx context.Context, id string) error {
|
|
query := fmt.Sprintf("delete from %s where id = $1", m.table)
|
|
_, err := m.conn.ExecCtx(ctx, query, id)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultTestModel) FindOne(ctx context.Context, id string) (*Test, error) {
|
|
query := fmt.Sprintf("select %s from %s where id = $1 limit 1", testRows, m.table)
|
|
var resp Test
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlc.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultTestModel) Insert(ctx context.Context, data *Test) (sql.Result, error) {
|
|
query := fmt.Sprintf("insert into %s (%s) values ($1, $2)", m.table, testRowsExpectAutoSet)
|
|
ret, err := m.conn.ExecCtx(ctx, query, data.Id, data.Server)
|
|
return ret, err
|
|
}
|
|
|
|
func (m *defaultTestModel) Update(ctx context.Context, data *Test) error {
|
|
query := fmt.Sprintf("update %s set %s where id = $1", m.table, testRowsWithPlaceHolder)
|
|
_, err := m.conn.ExecCtx(ctx, query, data.Id, data.Server)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultTestModel) tableName() string {
|
|
return m.table
|
|
}
|
|
|