-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package clock | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Clocker interface { | ||
Now() time.Time | ||
} | ||
|
||
type RealClocker struct{} | ||
|
||
func (r RealClocker) Now() time.Time { | ||
return time.Now() | ||
} | ||
|
||
type FixedClocker struct{} | ||
|
||
func (fc FixedClocker) Now() time.Time { | ||
return time.Date(2022, 5, 10, 12, 34, 56, 0, time.UTC) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package store | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"time" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/myeunee/GolangStudy/chapter18/section72/clock" | ||
"github.com/myeunee/GolangStudy/chapter18/section72/config" | ||
) | ||
|
||
func New(ctx context.Context, cfg *config.Config) (*sqlx.DB, func(), error) { | ||
// sqlx.Connect를 사용하면 내부에서 ping함 | ||
db, err := sql.Open("mysql", | ||
fmt.Sprintf( | ||
"%s:%s@tcp(%s:%d)/%s?parseTime=true", | ||
cfg.DBUser, cfg.DBPassword, | ||
cfg.DBHost, cfg.DBPort, | ||
cfg.DBName, | ||
), | ||
) | ||
if err != nil { | ||
return nil, func() {}, err | ||
} | ||
// Open은 실제로 접속 테스트를 하지X | ||
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) | ||
defer cancel() | ||
if err := db.PingContext(ctx); err != nil { | ||
return nil, func() { _ = db.Close() }, err | ||
} | ||
xdb := sqlx.NewDb(db, "mysql") | ||
return xdb, func() { _ = db.Close() }, nil | ||
} | ||
|
||
type Beginner interface { | ||
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) | ||
} | ||
|
||
type Preparer interface { | ||
PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error) | ||
} | ||
|
||
type Execer interface { | ||
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) | ||
NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) | ||
} | ||
|
||
type Queryer interface { | ||
Preparer | ||
QueryxContext(ctx context.Context, query string, args ...any) (*sqlx.Rows, error) | ||
QueryRowxContext(ctx context.Context, query string, args ...any) *sqlx.Row | ||
GetContext(ctx context.Context, dest interface{}, query string, args ...any) error | ||
SelectContext(ctx context.Context, dest interface{}, query string, args ...any) error | ||
} | ||
|
||
var ( | ||
// 인터페이스가 기대한 대로 선언돼 있는지 확인 | ||
_ Beginner = (*sqlx.DB)(nil) | ||
_ Preparer = (*sqlx.DB)(nil) | ||
_ Queryer = (*sqlx.DB)(nil) | ||
_ Execer = (*sqlx.DB)(nil) | ||
_ Execer = (*sqlx.Tx)(nil) | ||
) | ||
|
||
type Repository struct { | ||
Clocker clock.Clocker | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package store | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/myeunee/GolangStudy/chapter18/section72/entity" | ||
) | ||
|
||
func (r *Repository) AddTask( | ||
ctx context.Context, db Execer, t *entity.Task, | ||
) error { | ||
t.Created = r.Clocker.Now() | ||
t.Modified = r.Clocker.Now() | ||
sql := `INSERT INTO task | ||
(title, status, created, modified) | ||
VALUES (?, ?, ?, ?)` | ||
result, err := db.ExecContext( | ||
ctx, sql, t.Title, t.Status, | ||
t.Created, t.Modified, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
id, err := result.LastInsertId() | ||
if err != nil { | ||
return err | ||
} | ||
t.ID = entity.TaskID(id) | ||
return nil | ||
} | ||
|
||
func (r *Repository) ListTasks( | ||
ctx context.Context, db Queryer, | ||
) (entity.Tasks, error) { | ||
tasks := entity.Tasks{} | ||
sql := `SELECT | ||
id, title, | ||
status, created, modified | ||
FROM task;` | ||
if err := db.SelectContext(ctx, &tasks, sql); err != nil { | ||
return nil, err | ||
} | ||
return tasks, nil | ||
} |