-
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
13 changed files
with
633 additions
and
47 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,69 @@ | ||
name: bench | ||
on: | ||
push: | ||
tags: | ||
- v* | ||
branches: | ||
- master | ||
- main | ||
pull_request: | ||
env: | ||
GO111MODULE: "on" | ||
jobs: | ||
bench: | ||
strategy: | ||
matrix: | ||
go-version: [ 1.15.x ] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Install Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: ${{ matrix.go-version }} | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Restore vendor | ||
uses: actions/cache@v2 | ||
with: | ||
path: | | ||
vendor | ||
key: ${{ runner.os }}-go${{ matrix.go-version }}-vendor-${{ hashFiles('**/go.mod') }} | ||
- name: Populate dependencies | ||
run: | | ||
(test -d vendor && echo vendor found) || (go mod vendor && du -sh vendor && du -sh ~/go/pkg/mod) | ||
- name: Restore benchstat | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/go/bin/benchstat | ||
key: ${{ runner.os }}-benchstat | ||
- name: Restore base benchmark result | ||
uses: actions/cache@v2 | ||
with: | ||
path: | | ||
bench-master.txt | ||
bench-main.txt | ||
# Use base sha for PR or new commit hash for master/main push in benchmark result key. | ||
key: ${{ runner.os }}-bench-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }} | ||
- name: Benchmark | ||
id: bench | ||
run: | | ||
export REF_NAME=${GITHUB_REF##*/} | ||
BENCH_COUNT=5 make bench-run bench-stat | ||
OUTPUT=$(make bench-stat) | ||
OUTPUT="${OUTPUT//'%'/'%25'}" | ||
OUTPUT="${OUTPUT//$'\n'/'%0A'}" | ||
OUTPUT="${OUTPUT//$'\r'/'%0D'}" | ||
echo "::set-output name=result::$OUTPUT" | ||
- name: Comment Benchmark Result | ||
uses: marocchino/sticky-pull-request-comment@v2 | ||
with: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
header: bench | ||
message: | | ||
### Benchmark Result | ||
<details><summary>Benchmark diff with base branch</summary> | ||
``` | ||
${{ steps.bench.outputs.result }} | ||
``` | ||
</details> |
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,75 @@ | ||
name: test-unit | ||
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
pull_request: | ||
env: | ||
GO111MODULE: "on" | ||
jobs: | ||
test: | ||
strategy: | ||
matrix: | ||
go-version: [ 1.13.x, 1.14.x, 1.15.x ] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Install Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: ${{ matrix.go-version }} | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Restore base test coverage | ||
uses: actions/cache@v2 | ||
with: | ||
path: | | ||
unit-base.txt | ||
# Use base sha for PR or new commit hash for master/main push in test result key. | ||
key: ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }} | ||
- name: Restore vendor | ||
uses: actions/cache@v2 | ||
with: | ||
path: | | ||
vendor | ||
key: ${{ runner.os }}-go${{ matrix.go-version }}-vendor-${{ hashFiles('**/go.mod') }} | ||
- name: Populate dependencies | ||
run: | | ||
(test -d vendor && echo vendor found) || (go mod vendor && du -sh vendor && du -sh ~/go/pkg/mod) | ||
- name: Test | ||
id: test | ||
run: | | ||
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.txt | ||
OUTPUT=$(test -e unit-base.txt && (diff unit-base.txt unit.txt || exit 0) || cat unit.txt) | ||
OUTPUT="${OUTPUT//'%'/'%25'}" | ||
OUTPUT="${OUTPUT//$'\n'/'%0A'}" | ||
OUTPUT="${OUTPUT//$'\r'/'%0D'}" | ||
TOTAL=$(grep 'total:' unit.txt) | ||
echo "::set-output name=diff::$OUTPUT" | ||
echo "::set-output name=total::$TOTAL" | ||
- name: Store base coverage | ||
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }} | ||
run: cp unit.txt unit-base.txt | ||
- name: Comment Test Coverage | ||
if: matrix.go-version == '1.15.x' | ||
uses: marocchino/sticky-pull-request-comment@v2 | ||
with: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
header: unit-test | ||
message: | | ||
### Unit Test Coverage | ||
${{ steps.test.outputs.total }} | ||
<details><summary>Coverage diff with base branch</summary> | ||
```diff | ||
${{ steps.test.outputs.diff }} | ||
``` | ||
</details> | ||
- name: Upload code coverage | ||
if: matrix.go-version == '1.15.x' | ||
uses: codecov/codecov-action@v1 | ||
with: | ||
file: ./unit.coverprofile | ||
flags: unittests |
This file was deleted.
Oops, something went wrong.
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,55 @@ | ||
package sqluct_test | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/Masterminds/squirrel" | ||
"github.com/bool64/sqluct" | ||
) | ||
|
||
func ExampleReferencer_Fmt() { | ||
type User struct { | ||
ID int `db:"id"` | ||
FirstName string `db:"first_name"` | ||
LastName string `db:"last_name"` | ||
} | ||
|
||
type DirectReport struct { | ||
ManagerID int `db:"manager_id"` | ||
EmployeeID int `db:"employee_id"` | ||
} | ||
|
||
rf := sqluct.Referencer{} | ||
|
||
manager := &User{} | ||
rf.AddTableAlias(manager, "manager") | ||
|
||
employee := &User{} | ||
rf.AddTableAlias(employee, "employee") | ||
|
||
dr := &DirectReport{} | ||
rf.AddTableAlias(dr, "dr") | ||
|
||
// Find direct reports that share same last name and manager is not named John. | ||
qb := squirrel.StatementBuilder.Select(rf.Fmt("%s, %s", &dr.ManagerID, &dr.EmployeeID)). | ||
From(rf.Fmt("%s AS %s", rf.Q("users"), manager)). | ||
InnerJoin(rf.Fmt("%s AS %s ON %s = %s AND %s = %s", | ||
rf.Q("direct_reports"), dr, | ||
&dr.ManagerID, &manager.ID, | ||
&dr.EmployeeID, &employee.ID)). | ||
Where(rf.Fmt("%s = %s", &manager.LastName, &employee.LastName)). | ||
Where(rf.Fmt("%s != ?", &manager.FirstName), "John") | ||
|
||
stmt, args, err := qb.ToSql() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(stmt) | ||
fmt.Println(args) | ||
|
||
// Output: | ||
// SELECT dr.manager_id, dr.employee_id FROM users AS manager INNER JOIN direct_reports AS dr ON dr.manager_id = manager.id AND dr.employee_id = employee.id WHERE manager.last_name = employee.last_name AND manager.first_name != ? | ||
// [John] | ||
} |
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
Oops, something went wrong.