15 changed files with 332 additions and 9 deletions
@ -0,0 +1,29 @@ |
|||||
|
syntax = "v1" |
||||
|
|
||||
|
type PgTestRequest { |
||||
|
Form string `json:"from"` |
||||
|
} |
||||
|
// 通用api返回参数 |
||||
|
type PgTestResponse { |
||||
|
Success bool `json:"success,default=true"` // 是否成功 |
||||
|
Message string `json:"message,optional"` // 消息 |
||||
|
Result interface{} `json:"data,optional"` // 数据 |
||||
|
Total int64 `json:"total,optional"` // 总数 |
||||
|
} |
||||
|
|
||||
|
@server ( |
||||
|
group: PgTest |
||||
|
) |
||||
|
service demo-api { |
||||
|
@doc( |
||||
|
summary: "pg测试接口", |
||||
|
) |
||||
|
@handler PgInsertTestHandler |
||||
|
post /pg_test/insert returns (TestResponse) |
||||
|
@doc( |
||||
|
summary: "pg测试接口", |
||||
|
) |
||||
|
@handler PgSearchTestHandler |
||||
|
post /pg_test/search returns (TestResponse) |
||||
|
|
||||
|
} |
@ -1,17 +1,17 @@ |
|||||
Name: demo-api |
Name: demo-api |
||||
Host: 0.0.0.0 |
Host: 0.0.0.0 |
||||
Port: 8888 |
Port: 8888 |
||||
MaxConns: 1000 |
MaxConns: 10000 |
||||
Pg: |
Pg: |
||||
# postgresql 连接配置 |
# postgresql 连接配置 |
||||
DataSource: postgres://postgres:123456@127.0.0.1:/postgres?sslmode=disable |
DataSource: postgres://postgres:123456@127.0.0.1:/test?sslmode=disable |
||||
CacheRedis: |
CacheRedis: |
||||
- Host: 127.0.0.1:6379 |
- Host: 127.0.0.1:6379 |
||||
# Pass: $pass |
# Pass: $pass |
||||
Type: node |
Type: node |
||||
Log: |
# Log: |
||||
ServiceName: demo |
# ServiceName: demo |
||||
Mode: file |
# Mode: file |
||||
Path: logs |
# Path: logs |
||||
Level: info |
# Level: info |
||||
KeepDays: 7 |
# KeepDays: 7 |
@ -0,0 +1,21 @@ |
|||||
|
package PgTest |
||||
|
|
||||
|
import ( |
||||
|
"net/http" |
||||
|
|
||||
|
"demo/internal/logic/PgTest" |
||||
|
"demo/internal/svc" |
||||
|
"github.com/zeromicro/go-zero/rest/httpx" |
||||
|
) |
||||
|
|
||||
|
func PgInsertTestHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { |
||||
|
return func(w http.ResponseWriter, r *http.Request) { |
||||
|
l := PgTest.NewPgInsertTestLogic(r.Context(), svcCtx) |
||||
|
resp, err := l.PgInsertTest() |
||||
|
if err != nil { |
||||
|
httpx.ErrorCtx(r.Context(), w, err) |
||||
|
} else { |
||||
|
httpx.OkJsonCtx(r.Context(), w, resp) |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
package PgTest |
||||
|
|
||||
|
import ( |
||||
|
"net/http" |
||||
|
|
||||
|
"demo/internal/logic/PgTest" |
||||
|
"demo/internal/svc" |
||||
|
"github.com/zeromicro/go-zero/rest/httpx" |
||||
|
) |
||||
|
|
||||
|
func PgSearchTestHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { |
||||
|
return func(w http.ResponseWriter, r *http.Request) { |
||||
|
l := PgTest.NewPgSearchTestLogic(r.Context(), svcCtx) |
||||
|
resp, err := l.PgSearchTest() |
||||
|
if err != nil { |
||||
|
httpx.ErrorCtx(r.Context(), w, err) |
||||
|
} else { |
||||
|
httpx.OkJsonCtx(r.Context(), w, resp) |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,51 @@ |
|||||
|
package PgTest |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
|
||||
|
"demo/internal/svc" |
||||
|
"demo/internal/types" |
||||
|
Pgmodel "demo/model/pgTest" // 引入model
|
||||
|
|
||||
|
"github.com/google/uuid" |
||||
|
"github.com/zeromicro/go-zero/core/logx" |
||||
|
) |
||||
|
|
||||
|
type PgInsertTestLogic struct { |
||||
|
logx.Logger |
||||
|
ctx context.Context |
||||
|
svcCtx *svc.ServiceContext |
||||
|
} |
||||
|
|
||||
|
func NewPgInsertTestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PgInsertTestLogic { |
||||
|
return &PgInsertTestLogic{ |
||||
|
Logger: logx.WithContext(ctx), |
||||
|
ctx: ctx, |
||||
|
svcCtx: svcCtx, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func (l *PgInsertTestLogic) PgInsertTest() (resp *types.TestResponse, err error) { |
||||
|
// 组装数据
|
||||
|
uid := uuid.New() |
||||
|
server := l.svcCtx.Config.Name |
||||
|
// insertData := new (Pgmodel.Test)
|
||||
|
// insertData.Id = uid.String()
|
||||
|
// insertData.Server = server
|
||||
|
insertData := &Pgmodel.Test{ |
||||
|
Id: uid.String(), |
||||
|
Server: server, |
||||
|
} |
||||
|
|
||||
|
// 插入数据库
|
||||
|
_, err = l.svcCtx.PgModel.Insert(l.ctx, insertData) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
resp = &types.TestResponse{ |
||||
|
Success: true, |
||||
|
Message: "插入成功", |
||||
|
} |
||||
|
|
||||
|
return resp, nil |
||||
|
} |
@ -0,0 +1,40 @@ |
|||||
|
package PgTest |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
|
||||
|
"demo/internal/svc" |
||||
|
"demo/internal/types" |
||||
|
|
||||
|
"github.com/zeromicro/go-zero/core/logx" |
||||
|
) |
||||
|
|
||||
|
type PgSearchTestLogic struct { |
||||
|
logx.Logger |
||||
|
ctx context.Context |
||||
|
svcCtx *svc.ServiceContext |
||||
|
} |
||||
|
|
||||
|
func NewPgSearchTestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PgSearchTestLogic { |
||||
|
return &PgSearchTestLogic{ |
||||
|
Logger: logx.WithContext(ctx), |
||||
|
ctx: ctx, |
||||
|
svcCtx: svcCtx, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func (l *PgSearchTestLogic) PgSearchTest() (resp *types.TestResponse, err error) { |
||||
|
|
||||
|
// 查询数据库
|
||||
|
data, err := l.svcCtx.PgModel.FindOne(l.ctx, "findonetest") |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
resp = &types.TestResponse{ |
||||
|
Success: true, |
||||
|
Message: "查询成功", |
||||
|
Result: *data, |
||||
|
} |
||||
|
|
||||
|
return resp, nil |
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
-- public.test definition |
||||
|
|
||||
|
-- Drop table |
||||
|
|
||||
|
-- DROP TABLE public.test; |
||||
|
|
||||
|
CREATE TABLE public.test ( |
||||
|
id varchar NOT NULL, |
||||
|
"from" varchar NOT NULL, |
||||
|
CONSTRAINT test_pk PRIMARY KEY (id) |
||||
|
); |
@ -0,0 +1,24 @@ |
|||||
|
package pgTest |
||||
|
|
||||
|
import "github.com/zeromicro/go-zero/core/stores/sqlx" |
||||
|
|
||||
|
var _ TestModel = (*customTestModel)(nil) |
||||
|
|
||||
|
type ( |
||||
|
// TestModel is an interface to be customized, add more methods here,
|
||||
|
// and implement the added methods in customTestModel.
|
||||
|
TestModel interface { |
||||
|
testModel |
||||
|
} |
||||
|
|
||||
|
customTestModel struct { |
||||
|
*defaultTestModel |
||||
|
} |
||||
|
) |
||||
|
|
||||
|
// NewTestModel returns a model for the database table.
|
||||
|
func NewTestModel(conn sqlx.SqlConn) TestModel { |
||||
|
return &customTestModel{ |
||||
|
defaultTestModel: newTestModel(conn), |
||||
|
} |
||||
|
} |
@ -0,0 +1,91 @@ |
|||||
|
// 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 |
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
package pgTest |
||||
|
|
||||
|
import "github.com/zeromicro/go-zero/core/stores/sqlx" |
||||
|
|
||||
|
var ErrNotFound = sqlx.ErrNotFound |
Loading…
Reference in new issue