From 25a977074d613333a5520ecce8c45e5fccfb548e Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Mon, 24 May 2021 16:37:14 +0200 Subject: [PATCH] Add DB accessor to storage (#8) --- .github/workflows/bench.yml | 4 ++-- .github/workflows/cloc.yml | 36 +++++++++++++++++++++++++++++ .github/workflows/golangci-lint.yml | 1 + .github/workflows/test-unit.yml | 4 ++-- example_test.go | 2 +- go.mod | 6 ++--- go.sum | 14 +++++------ referencer.go | 2 +- storage.go | 23 ++++++++++++++---- storage_test.go | 10 ++++++++ 10 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 .github/workflows/cloc.yml diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 46100ea..bde132f 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -1,3 +1,4 @@ +# This script is provided by github.com/bool64/dev. name: bench on: pull_request: @@ -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: | diff --git a/.github/workflows/cloc.yml b/.github/workflows/cloc.yml new file mode 100644 index 0000000..c6794fe --- /dev/null +++ b/.github/workflows/cloc.yml @@ -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 }} diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index b51abbb..faaf359 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -1,3 +1,4 @@ +# This script is provided by github.com/bool64/dev. name: lint on: push: diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index 0eb5807..7304bfc 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -1,3 +1,4 @@ +# This script is provided by github.com/bool64/dev. name: test-unit on: push: @@ -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: | diff --git a/example_test.go b/example_test.go index 2d47445..ef37634 100644 --- a/example_test.go +++ b/example_test.go @@ -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) diff --git a/go.mod b/go.mod index 2176618..c34b47f 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index b191ec1..6ecf2f5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/referencer.go b/referencer.go index f214afa..614ec4a 100644 --- a/referencer.go +++ b/referencer.go @@ -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) } diff --git a/storage.go b/storage.go index 2275075..c46dc4c 100644 --- a/storage.go +++ b/storage.go @@ -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. @@ -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. @@ -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. @@ -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) } @@ -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 { @@ -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 +} diff --git a/storage_test.go b/storage_test.go index 699823e..19bcd61 100644 --- a/storage_test.go +++ b/storage_test.go @@ -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()) +}