Skip to content

Commit

Permalink
Add DB accessor to storage (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored May 24, 2021
1 parent 93d033a commit 25a9770
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: bench
on:
pull_request:
Expand Down Expand Up @@ -68,8 +69,7 @@ jobs:
run: |
export REF_NAME=master
cd __base
BENCH_COUNT=5 make bench-run bench-stat
cp bench-master.txt ../bench-master.txt
make | grep bench-run && (BENCH_COUNT=5 make bench-run bench-stat && cp bench-master.txt ../bench-master.txt) || echo "No benchmarks in base"
- name: Benchmark
id: bench
run: |
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/cloc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This script is provided by github.com/bool64/dev.
name: cloc
on:
pull_request:
jobs:
cloc:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: pr
- name: Checkout base code
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base
- name: Count Lines Of Code
id: loc
run: |
curl -OL https://github.com/vearutop/builds/releases/download/sccdiff-v0/sccdiff && chmod +x sccdiff
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
echo "::set-output name=diff::$OUTPUT"
- name: Comment Code Lines
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: LOC
message: |
### Lines Of Code
${{ steps.loc.outputs.diff }}
1 change: 1 addition & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: lint
on:
push:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This script is provided by github.com/bool64/dev.
name: test-unit
on:
push:
Expand Down Expand Up @@ -51,8 +52,7 @@ jobs:
if: matrix.go-version == '1.16.x' && env.RUN_BASE_COVERAGE == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
run: |
cd __base
make test-unit
go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt
make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt) || echo "No test-unit in base"
- name: Test
id: test
run: |
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func ExampleStorage_UpdateStmt() {
}

qb := s.UpdateStmt("my_table", row).
Where(s.Mapper.WhereEq(MyIdentity{ID: 123}))
Where(s.WhereEq(MyIdentity{ID: 123}))

if _, err := s.Exec(ctx, qb); err != nil {
log.Fatal(err)
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.13
require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/Masterminds/squirrel v1.5.0
github.com/bool64/ctxd v0.1.4
github.com/bool64/dev v0.1.27
github.com/jmoiron/sqlx v1.3.3
github.com/bool64/ctxd v0.1.5
github.com/bool64/dev v0.1.32
github.com/jmoiron/sqlx v1.3.4
github.com/stretchr/testify v1.6.1
)
14 changes: 7 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20O
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/Masterminds/squirrel v1.5.0 h1:JukIZisrUXadA9pl3rMkjhiamxiB0cXiu+HGp/Y8cY8=
github.com/Masterminds/squirrel v1.5.0/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
github.com/bool64/ctxd v0.1.4 h1:UxL3FCpnLP/h6cZFKBGxAL9TXMd45l8QZQdG35D7BlU=
github.com/bool64/ctxd v0.1.4/go.mod h1:vbCBsEfD4TGXGTPEEQwjB4M5Ny2MC8r+9N2JSP3yJPY=
github.com/bool64/ctxd v0.1.5 h1:ilZo2AgQ+NG9NyusoYEdcMcGgleAFDnrju+5KMxwUbk=
github.com/bool64/ctxd v0.1.5/go.mod h1:+rjDVFNOJeO+xlvMqQfG0p53CzuRB7FhPSo5nWSkpQ0=
github.com/bool64/dev v0.1.25/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.26/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.27 h1:Cx4g/QLtVVmmfKZfyxAfOGKqgT86tqUbFB1vRN6JbfM=
github.com/bool64/dev v0.1.27/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.28/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.32 h1:/9dpNGF/73diJzQxZS2hOxOPrsbTyLhr0VrQztylSKY=
github.com/bool64/dev v0.1.32/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmoiron/sqlx v1.3.3 h1:j82X0bf7oQ27XeqxicSZsTU5suPwKElg3oyxNn43iTk=
github.com/jmoiron/sqlx v1.3.3/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w=
github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
Expand Down
2 changes: 1 addition & 1 deletion referencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (r *Referencer) ColumnsOf(rowStructPtr interface{}) func(o *Options) {
//
// Empty alias is not added to column reference.
func (r *Referencer) AddTableAlias(rowStructPtr interface{}, alias string) {
f, err := r.Mapper.FindColumnNames(rowStructPtr)
f, err := mapper(r.Mapper).FindColumnNames(rowStructPtr)
if err != nil {
panic(err)
}
Expand Down
23 changes: 18 additions & 5 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (s *Storage) SelectStmt(tableName string, columns interface{}, options ...f

qb := s.QueryBuilder().Select().From(tableName)

return s.Mapper.Select(qb, columns, s.options(options)...)
return mapper(s.Mapper).Select(qb, columns, s.options(options)...)
}

// InsertStmt makes an insert query builder.
Expand All @@ -246,7 +246,7 @@ func (s *Storage) InsertStmt(tableName string, val interface{}, options ...func(

qb := s.QueryBuilder().Insert(tableName)

return s.Mapper.Insert(qb, val, s.options(options)...)
return mapper(s.Mapper).Insert(qb, val, s.options(options)...)
}

// UpdateStmt makes an update query builder.
Expand All @@ -257,7 +257,7 @@ func (s *Storage) UpdateStmt(tableName string, val interface{}, options ...func(

qb := s.QueryBuilder().Update(tableName)

return s.Mapper.Update(qb, val, s.options(options)...)
return mapper(s.Mapper).Update(qb, val, s.options(options)...)
}

// DeleteStmt makes a delete query builder.
Expand All @@ -271,7 +271,7 @@ func (s *Storage) DeleteStmt(tableName string) squirrel.DeleteBuilder {

// Col will try to find column name and will panic on error.
func (s *Storage) Col(structPtr, fieldPtr interface{}) string {
col := s.Mapper.Col(structPtr, fieldPtr)
col := mapper(s.Mapper).Col(structPtr, fieldPtr)
if s.IdentifierQuoter != nil {
col = s.IdentifierQuoter(col)
}
Expand All @@ -289,7 +289,7 @@ func (s *Storage) Ref() *Referencer {

// WhereEq maps struct values as conditions to squirrel.Eq.
func (s *Storage) WhereEq(conditions interface{}, options ...func(*Options)) squirrel.Eq {
return s.Mapper.WhereEq(conditions, s.options(options)...)
return mapper(s.Mapper).WhereEq(conditions, s.options(options)...)
}

func (s *Storage) error(ctx context.Context, err error) error {
Expand All @@ -299,3 +299,16 @@ func (s *Storage) error(ctx context.Context, err error) error {

return err
}

func mapper(m *Mapper) *Mapper {
if m == nil {
return defaultMapper
}

return m
}

// DB returns database instance.
func (s *Storage) DB() *sqlx.DB {
return s.db
}
10 changes: 10 additions & 0 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,13 @@ func TestStorage_SelectStmt_backticks(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, query, "SELECT `order_id`, `amount` FROM `table`")
}

func TestStorage_DB(t *testing.T) {
db, _, err := sqlmock.New()
assert.NoError(t, err)

dbx := sqlx.NewDb(db, "test")
st := sqluct.NewStorage(dbx)

assert.Equal(t, dbx, st.DB())
}

0 comments on commit 25a9770

Please sign in to comment.