Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Release

on:
push:
branches:
- main

jobs:
verify:
name: Test
runs-on: ubuntu-latest

steps:
- name: Set up Go 1.16
uses: actions/setup-go@v2
with:
go-version: 1.16
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Set cache
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-

- name: Get dependencies
run: go mod download

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest

- name: Run Tests
run: go test -covermode=count -coverprofile=count.out -v ./...

release-please:
needs: verify
runs-on: ubuntu-latest

steps:
- uses: GoogleCloudPlatform/release-please-action@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
release-type: simple
package-name: sqlutil
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Verify

on:
push:
branches:
- "**"
- "!main"

jobs:
verify:
name: Test
runs-on: ubuntu-latest

steps:
- name: Set up Go 1.16
uses: actions/setup-go@v2
with:
go-version: 1.16
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Set cache
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-

- name: Get dependencies
run: go mod download

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest

- name: Run Tests
run: go test -covermode=count -coverprofile=count.out -v ./...
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.go-oif
.golangci-lint
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters:
enable:
- gosec
- rowserrcheck
- sqlclosecheck
- ifshort
Empty file added CHANGELOG.md
Empty file.
104 changes: 103 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,103 @@
# sqlutil
# sqlutil
[![Build Status](https://github.com/allisson/sqlutil/workflows/Release/badge.svg)](https://github.com/allisson/sqlutil/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/allisson/sqlutil)](https://goreportcard.com/report/github.com/allisson/sqlutil)
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/allisson/sqlutil)

A collection of helpers to deal with database.

Example:

```golang
package main

import (
"context"
"database/sql"
"fmt"
"log"

"github.com/allisson/sqlutil"
_ "github.com/lib/pq"
)

type Player struct {
ID string `db:"id"`
Name string `db:"name" fieldtag:"update"`
Age int `db:"age" fieldtag:"update"`
}

func main() {
// Connect to database
db, err := sql.Open("postgres", "postgres://user:pass@localhost/sqlutil?sslmode=disable")
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Fatal(err)
}
defer db.Close()

// Create table
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS players(
id VARCHAR PRIMARY KEY,
name VARCHAR NOT NULL,
age INTEGER NOT NULL
)
`)
if err != nil {
log.Fatal(err)
}

// Insert players
r9 := Player{
ID: "R9",
Name: "Ronaldo Fenômeno",
Age: 44,
}
r10 := Player{
ID: "R10",
Name: "Ronaldinho Gaúcho",
Age: 41,
}
flavour := sqlutil.PostgreSQLFlavor
tag := "" // empty tag will use all fields from struct
ctx := context.Background()
if err := sqlutil.Insert(ctx, db, sqlutil.PostgreSQLFlavor, tag, "players", &r9); err != nil {
log.Fatal(err)
}
if err := sqlutil.Insert(ctx, db, sqlutil.PostgreSQLFlavor, tag, "players", &r10); err != nil {
log.Fatal(err)
}

// Get player
findOptions := sqlutil.NewFindOptions(flavour).WithFilter("id", "R10")
if err := sqlutil.Get(ctx, db, "players", findOptions, &r10); err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", r10)

// Select players
players := []*Player{}
findAllOptions := sqlutil.NewFindAllOptions(flavour).WithLimit(10).WithOffset(0).WithOrderBy("name asc")
if err := sqlutil.Select(ctx, db, "players", findAllOptions, &players); err != nil {
log.Fatal(err)
}
for _, p := range players {
fmt.Printf("%#v\n", p)
}

// Update player
tag = "update" // will use fields with fieldtag:"update"
r10.Name = "Ronaldinho Bruxo"
if err := sqlutil.Update(ctx, db, sqlutil.PostgreSQLFlavor, tag, "players", r10.ID, &r10); err != nil {
log.Fatal(err)
}

// Delete player
if err := sqlutil.Delete(ctx, db, sqlutil.PostgreSQLFlavor, "players", r9.ID); err != nil {
log.Fatal(err)
}
}
```
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/allisson/sqlutil

go 1.16

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/georgysavva/scany v0.2.9
github.com/huandu/go-sqlbuilder v1.12.1
github.com/stretchr/testify v1.7.0
)
Loading