Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vasayxtx committed Oct 8, 2024
0 parents commit 5b8b441
Show file tree
Hide file tree
Showing 58 changed files with 6,719 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
.vscode/
vendor/
83 changes: 83 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
linters-settings:
gocyclo:
min-complexity: 15
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
lll:
line-length: 140
gocritic:
enabled-tags:
- diagnostic
- performance
- style
- experimental
disabled-checks:
- whyNoLint
- unnecessaryDefer
- paramTypeCombine
settings:
hugeParam:
sizeThreshold: 256
rangeValCopy:
sizeThreshold: 256
funlen:
lines: 100
statements: 50
revive:
rules:
- name: "unused-parameter"
disabled: true

linters:
disable-all: true
enable:
- bodyclose
- dogsled
- dupl
- errcheck
- exportloopref
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace

issues:
# Don't use default excluding to be sure all exported things (method, functions, consts and so on) have comments.
exclude-use-default: false
exclude-rules:
- path: _test\.go
linters:
- dogsled
- dupl
- funlen
- goconst
- gocritic
- gocyclo
- gosec
- govet
- lll
- ineffassign
- noctx
- unused
9 changes: 9 additions & 0 deletions .trufflehog3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exclude:
- message: Exclude tests
paths:
- dsn_test.go
- pgx/postgres_test.go

- message: Skip commit hashes
id: high-entropy
pattern: 9cf57526250f6cd3e6cbf4fd7269c882e66898ce
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright © 2024 Acronis International GmbH.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
159 changes: 159 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Toolkit for working with SQL databases in Go

## Structure

### `/`
Package `db` provides helpers for working with different SQL databases (MySQL, PostgreSQL, SQLite and MSSQL).

### `/distrlock`
Package distrlock contains DML (distributed lock manager) implementation (now DMLs based on MySQL and PostgreSQL are supported).
Now only manager that uses SQL database (PostgreSQL and MySQL are currently supported) is available.
Other implementations (for example, based on Redis) will probably be implemented in the future.

### `/migrate`
Package migrate provides functionality for applying database migrations.

### `/mssql`
Package mssql provides helpers for working with MSSQL.
Should be imported explicitly.
To register mssql as retryable func use side effect import like so:

```go
import _ "github.com/acronis/go-dbkit/mssql"
```

### `/mysql`
Package mysql provides helpers for working with MySQL.
Should be imported explicitly.
To register mysql as retryable func use side effect import like so:

```go
import _ "github.com/acronis/go-dbkit/mysql"
```

### `/pgx`
Package pgx provides helpers for working with Postgres via `jackc/pgx` driver.
Should be imported explicitly.
To register postgres as retryable func use side effect import like so:

```go
import _ "github.com/acronis/go-dbkit/pgx"
```

### `/postgres`
Package postgres provides helpers for working with Postgres via `lib/pq` driver.
Should be imported explicitly.
To register postgres as retryable func use side effect import like so:

```go
import _ "github.com/acronis/go-dbkit/postgres"
```

### `/sqlite`
Package sqlite provides helpers for working with SQLite.
Should be imported explicitly.
To register sqlite as retryable func use side effect import like so:

```go
import _ "github.com/acronis/go-dbkit/sqlite"
```

### `/dbrutil`
Package dbrutil provides utilities and helpers for [dbr](https://github.com/gocraft/dbr) query builder.

### `/goquutil`
Package goquutil provides auxiliary routines for working with [goqu](https://github.com/doug-martin/goqu) query builder.

## Examples

### Open database connection using the `dbrutil` package

```go
func main() {
// Create a new database configuration
cfg := &db.Config{
Driver: db.DialectMySQL,
Host: "localhost",
Port: 3306,
Username: "your-username",
Password: "your-password",
Database: "your-database",
}

// Open a connection to the database
conn, err := dbrutil.Open(cfg, true, nil)
if err != nil {
fmt.Println("Failed to open database connection:", err)
return
}
defer conn.Close()

// Create a new transaction runner
runner := dbrutil.NewTxRunner(conn, &sql.TxOptions{}, nil)

// Execute code inside a transaction
err = runner.DoInTx(context.Background(), func(runner dbr.SessionRunner) error {
// Perform database operations using the runner
_, err := runner.InsertInto("users").
Columns("name", "email").
Values("Bob", "bob@example.com").
Exec()
if err != nil {
return err
}

// Return nil to commit the transaction
return nil
})
if err != nil {
fmt.Println("Failed to execute transaction:", err)
return
}
}
```

### Usage of `distrlock` package

```go
// Create a new DBManager with the MySQL dialect
dbManager, err := distrlock.NewDBManager(db.DialectMySQL)
if err != nil {
log.Fatal(err)
}

// Open a connection to the MySQL database
dbConn, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/database")
if err != nil {
log.Fatal(err)
}
defer dbConn.Close()

// Create a new lock
lock, err := dbManager.NewLock(context.Background(), dbConn, "my_lock")
if err != nil {
log.Fatal(err)
}

// Acquire the lock
err = lock.Acquire(context.Background(), dbConn, 5*time.Second)
if err != nil {
log.Fatal(err)
}

// Do some work while holding the lock
fmt.Println("Lock acquired, doing some work...")

// Release the lock
err = lock.Release(context.Background(), dbConn)
if err != nil {
log.Fatal(err)
}

fmt.Println("Lock released")
```

## License

Copyright © 2024 Acronis International GmbH.

Licensed under [MIT License](./LICENSE).
Loading

0 comments on commit 5b8b441

Please sign in to comment.