Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
support mysql dialect for entity generation
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Oct 19, 2020
1 parent 8431376 commit 08ce2a3
Show file tree
Hide file tree
Showing 10 changed files with 491 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package mysqldb
package repository

import (
"context"
"database/sql"
"time"

sq "github.com/Masterminds/squirrel"
"github.com/typical-go/typical-rest-server/internal/app/data_access/mysqldb"
"github.com/typical-go/typical-rest-server/pkg/dbkit"
"github.com/typical-go/typical-rest-server/pkg/dbtxn"
"go.uber.org/dig"
Expand Down Expand Up @@ -34,11 +35,11 @@ type (
// SongRepo to get song data from database
// @mock
SongRepo interface {
Find(context.Context, ...dbkit.SelectOption) ([]*Song, error)
Create(context.Context, *Song) (int64, error)
Find(context.Context, ...dbkit.SelectOption) ([]*mysqldb.Song, error)
Create(context.Context, *mysqldb.Song) (int64, error)
Delete(context.Context, dbkit.DeleteOption) (int64, error)
Update(context.Context, *Song, dbkit.UpdateOption) (int64, error)
Patch(context.Context, *Song, dbkit.UpdateOption) (int64, error)
Update(context.Context, *mysqldb.Song, dbkit.UpdateOption) (int64, error)
Patch(context.Context, *mysqldb.Song, dbkit.UpdateOption) (int64, error)
}
// SongRepoImpl is implementation song repository
SongRepoImpl struct {
Expand All @@ -54,7 +55,7 @@ func NewSongRepo(impl SongRepoImpl) SongRepo {
}

// Find song
func (r *SongRepoImpl) Find(ctx context.Context, opts ...dbkit.SelectOption) (list []*Song, err error) {
func (r *SongRepoImpl) Find(ctx context.Context, opts ...dbkit.SelectOption) (list []*mysqldb.Song, err error) {
builder := sq.
Select(
SongTable.ID,
Expand All @@ -77,9 +78,9 @@ func (r *SongRepoImpl) Find(ctx context.Context, opts ...dbkit.SelectOption) (li
return
}

list = make([]*Song, 0)
list = make([]*mysqldb.Song, 0)
for rows.Next() {
song := new(Song)
song := new(mysqldb.Song)
if err = rows.Scan(
&song.ID,
&song.Title,
Expand All @@ -95,7 +96,7 @@ func (r *SongRepoImpl) Find(ctx context.Context, opts ...dbkit.SelectOption) (li
}

// Create song
func (r *SongRepoImpl) Create(ctx context.Context, song *Song) (int64, error) {
func (r *SongRepoImpl) Create(ctx context.Context, song *mysqldb.Song) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
Expand Down Expand Up @@ -151,7 +152,7 @@ func (r *SongRepoImpl) Delete(ctx context.Context, opt dbkit.DeleteOption) (int6
}

// Update song
func (r *SongRepoImpl) Update(ctx context.Context, song *Song, opt dbkit.UpdateOption) (int64, error) {
func (r *SongRepoImpl) Update(ctx context.Context, song *mysqldb.Song, opt dbkit.UpdateOption) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
Expand All @@ -178,7 +179,7 @@ func (r *SongRepoImpl) Update(ctx context.Context, song *Song, opt dbkit.UpdateO
}

// Patch song to update field of song if available
func (r *SongRepoImpl) Patch(ctx context.Context, song *Song, opt dbkit.UpdateOption) (int64, error) {
func (r *SongRepoImpl) Patch(ctx context.Context, song *mysqldb.Song, opt dbkit.UpdateOption) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mysqldb_test
package repository_test

import (
"context"
Expand All @@ -11,26 +11,27 @@ import (
sqlmock "github.com/DATA-DOG/go-sqlmock"
sq "github.com/Masterminds/squirrel"
"github.com/stretchr/testify/require"
"github.com/typical-go/typical-rest-server/EXPERIMENT/repository"
"github.com/typical-go/typical-rest-server/internal/app/data_access/mysqldb"
"github.com/typical-go/typical-rest-server/pkg/dbkit"
"github.com/typical-go/typical-rest-server/pkg/dbtxn"
)

type bookRepoFn func(sqlmock.Sqlmock)
type songRepoFn func(sqlmock.Sqlmock)

func createSongRepo(fn bookRepoFn) (mysqldb.SongRepo, *sql.DB) {
func createSongRepo(fn songRepoFn) (repository.SongRepo, *sql.DB) {
db, mock, _ := sqlmock.New()
if fn != nil {
fn(mock)
}
return mysqldb.NewSongRepo(mysqldb.SongRepoImpl{DB: db}), db
return repository.NewSongRepo(repository.SongRepoImpl{DB: db}), db
}

func TestSongRepoImpl_Create(t *testing.T) {
testcases := []struct {
TestName string
Song *mysqldb.Song
SongRepoFn bookRepoFn
SongRepoFn songRepoFn
Expected int64
ExpectedErr string
}{
Expand Down Expand Up @@ -92,15 +93,15 @@ func TestSongRepoImpl_Update(t *testing.T) {
testcases := []struct {
TestName string
Song *mysqldb.Song
SongRepoFn bookRepoFn
SongRepoFn songRepoFn
Opt dbkit.UpdateOption
ExpectedErr string
Expected int64
}{
{
TestName: "update error",
Song: &mysqldb.Song{Title: "new-title", Artist: "new-artist"},
Opt: dbkit.Equal(mysqldb.SongTable.ID, 888),
Opt: dbkit.Equal(repository.SongTable.ID, 888),
ExpectedErr: "dbtxn: begin-error",
Expected: -1,
SongRepoFn: func(mock sqlmock.Sqlmock) {
Expand All @@ -110,7 +111,7 @@ func TestSongRepoImpl_Update(t *testing.T) {
{
TestName: "update error",
Song: &mysqldb.Song{Title: "new-title", Artist: "new-artist"},
Opt: dbkit.Equal(mysqldb.SongTable.ID, 888),
Opt: dbkit.Equal(repository.SongTable.ID, 888),
SongRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE songs SET title = ?, artist = ?, updated_at = ? WHERE id = ?`)).
Expand All @@ -137,7 +138,7 @@ func TestSongRepoImpl_Update(t *testing.T) {
{
TestName: "success",
Song: &mysqldb.Song{Title: "new-title", Artist: "new-artist"},
Opt: dbkit.Equal(mysqldb.SongTable.ID, 888),
Opt: dbkit.Equal(repository.SongTable.ID, 888),
SongRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE songs SET title = ?, artist = ?, updated_at = ? WHERE id = ?`)).
Expand All @@ -149,7 +150,7 @@ func TestSongRepoImpl_Update(t *testing.T) {
{
TestName: "success empty artist",
Song: &mysqldb.Song{Title: "new-title"},
Opt: dbkit.Equal(mysqldb.SongTable.ID, 888),
Opt: dbkit.Equal(repository.SongTable.ID, 888),
SongRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE songs SET title = ?, artist = ?, updated_at = ? WHERE id = ?`)).
Expand All @@ -161,7 +162,7 @@ func TestSongRepoImpl_Update(t *testing.T) {
{
TestName: "success empty title",
Song: &mysqldb.Song{Artist: "new-artist"},
Opt: dbkit.Equal(mysqldb.SongTable.ID, 888),
Opt: dbkit.Equal(repository.SongTable.ID, 888),
SongRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE songs SET title = ?, artist = ?, updated_at = ? WHERE id = ?`)).
Expand Down Expand Up @@ -194,15 +195,15 @@ func TestSongRepoImpl_Patch(t *testing.T) {
testcases := []struct {
TestName string
Song *mysqldb.Song
SongRepoFn bookRepoFn
SongRepoFn songRepoFn
Opt dbkit.UpdateOption
ExpectedErr string
Expected int64
}{
{
TestName: "begin error",
Song: &mysqldb.Song{Title: "new-title", Artist: "new-artist"},
Opt: dbkit.Equal(mysqldb.SongTable.ID, 888),
Opt: dbkit.Equal(repository.SongTable.ID, 888),
SongRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin().WillReturnError(errors.New("begin-error"))
},
Expand All @@ -212,7 +213,7 @@ func TestSongRepoImpl_Patch(t *testing.T) {
{
TestName: "update error",
Song: &mysqldb.Song{Title: "new-title", Artist: "new-artist"},
Opt: dbkit.Equal(mysqldb.SongTable.ID, 888),
Opt: dbkit.Equal(repository.SongTable.ID, 888),
SongRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE songs SET title = ?, artist = ?, updated_at = ? WHERE id = ?`)).
Expand Down Expand Up @@ -240,7 +241,7 @@ func TestSongRepoImpl_Patch(t *testing.T) {
{
TestName: "success",
Song: &mysqldb.Song{Title: "new-title", Artist: "new-artist"},
Opt: dbkit.Equal(mysqldb.SongTable.ID, 888),
Opt: dbkit.Equal(repository.SongTable.ID, 888),
SongRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE songs SET title = ?, artist = ?, updated_at = ? WHERE id = ?`)).
Expand All @@ -252,7 +253,7 @@ func TestSongRepoImpl_Patch(t *testing.T) {
{
TestName: "success empty artist",
Song: &mysqldb.Song{Title: "new-title"},
Opt: dbkit.Equal(mysqldb.SongTable.ID, 888),
Opt: dbkit.Equal(repository.SongTable.ID, 888),
SongRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE songs SET title = ?, updated_at = ? WHERE id = ?`)).
Expand All @@ -264,7 +265,7 @@ func TestSongRepoImpl_Patch(t *testing.T) {
{
TestName: "success empty title",
Song: &mysqldb.Song{Artist: "new-artist"},
Opt: dbkit.Equal(mysqldb.SongTable.ID, 888),
Opt: dbkit.Equal(repository.SongTable.ID, 888),
SongRepoFn: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE songs SET artist = ?, updated_at = ? WHERE id = ?`)).
Expand Down Expand Up @@ -302,7 +303,7 @@ func TestSongRepoImpl_Retrieve(t *testing.T) {
Opts []dbkit.SelectOption
Expected []*mysqldb.Song
ExpectedErr string
SongRepoFn bookRepoFn
SongRepoFn songRepoFn
}{
{
TestName: "sql error",
Expand Down Expand Up @@ -368,7 +369,7 @@ func TestSongRepoImpl_Delete(t *testing.T) {
testcases := []struct {
TestName string
Opt dbkit.DeleteOption
SongRepoFn bookRepoFn
SongRepoFn songRepoFn
ExpectedErr string
Expected int64
}{
Expand Down
11 changes: 6 additions & 5 deletions internal/app/data_access/mysqldb/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import "time"

type (
// Song entity
// @entity (table:"songs" dialect:"mysql" ctor_db:"mysql")
Song struct {
ID int64 `json:"id"`
Title string `json:"title" validate:"required"`
Artist string `json:"artist" validate:"required"`
UpdatedAt time.Time `json:"update_at"`
CreatedAt time.Time `json:"created_at"`
ID int64 `column:"id" option:"pk" json:"id"`
Title string `column:"title" json:"title" validate:"required"`
Artist string `column:"artist" json:"artist" validate:"required"`
UpdatedAt time.Time `column:"updated_at" option:"now" json:"update_at"`
CreatedAt time.Time `column:"created_at" option:"now,no_update" json:"created_at"`
}
)
13 changes: 7 additions & 6 deletions internal/app/domain/mymusic/service/song_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

"github.com/typical-go/typical-rest-server/internal/app/data_access/mysqldb"
"github.com/typical-go/typical-rest-server/internal/generated/mysqldb_repo"
"github.com/typical-go/typical-rest-server/pkg/dbkit"
"github.com/typical-go/typical-rest-server/pkg/typrest"
"go.uber.org/dig"
Expand All @@ -26,7 +27,7 @@ type (
// SongSvcImpl is implementation of SongSvc
SongSvcImpl struct {
dig.In
mysqldb.SongRepo
mysqldb_repo.SongRepo
}
)

Expand Down Expand Up @@ -60,7 +61,7 @@ func (b *SongSvcImpl) FindOne(ctx context.Context, paramID string) (*mysqldb.Son
return nil, typrest.NewValidErr("paramID is missing")
}

books, err := b.SongRepo.Find(ctx, dbkit.Equal(mysqldb.SongTable.ID, id))
books, err := b.SongRepo.Find(ctx, dbkit.Equal(mysqldb_repo.SongTable.ID, id))
if err != nil {
return nil, err
} else if len(books) < 1 {
Expand All @@ -70,7 +71,7 @@ func (b *SongSvcImpl) FindOne(ctx context.Context, paramID string) (*mysqldb.Son
}

func (b *SongSvcImpl) findOne(ctx context.Context, id int64) (*mysqldb.Song, error) {
books, err := b.SongRepo.Find(ctx, dbkit.Equal(mysqldb.SongTable.ID, id))
books, err := b.SongRepo.Find(ctx, dbkit.Equal(mysqldb_repo.SongTable.ID, id))
if err != nil {
return nil, err
} else if len(books) < 1 {
Expand All @@ -85,7 +86,7 @@ func (b *SongSvcImpl) Delete(ctx context.Context, paramID string) error {
if id < 1 {
return typrest.NewValidErr("paramID is missing")
}
_, err := b.SongRepo.Delete(ctx, dbkit.Equal(mysqldb.SongTable.ID, id))
_, err := b.SongRepo.Delete(ctx, dbkit.Equal(mysqldb_repo.SongTable.ID, id))
return err
}

Expand All @@ -99,7 +100,7 @@ func (b *SongSvcImpl) Update(ctx context.Context, paramID string, book *mysqldb.
if err != nil {
return nil, typrest.NewValidErr(err.Error())
}
affectedRow, err := b.SongRepo.Update(ctx, book, dbkit.Equal(mysqldb.SongTable.ID, id))
affectedRow, err := b.SongRepo.Update(ctx, book, dbkit.Equal(mysqldb_repo.SongTable.ID, id))
if err != nil {
return nil, err
}
Expand All @@ -115,7 +116,7 @@ func (b *SongSvcImpl) Patch(ctx context.Context, paramID string, book *mysqldb.S
if id < 1 {
return nil, typrest.NewValidErr("paramID is missing")
}
affectedRow, err := b.SongRepo.Patch(ctx, book, dbkit.Equal(mysqldb.SongTable.ID, id))
affectedRow, err := b.SongRepo.Patch(ctx, book, dbkit.Equal(mysqldb_repo.SongTable.ID, id))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 08ce2a3

Please sign in to comment.