https://github.com/soreing/ssql
Golang SQL database wrapper with hooks
https://github.com/soreing/ssql
go golang hooks sql
Last synced: about 1 year ago
JSON representation
Golang SQL database wrapper with hooks
- Host: GitHub
- URL: https://github.com/soreing/ssql
- Owner: Soreing
- Created: 2022-11-16T07:20:00.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-11T12:36:30.000Z (over 3 years ago)
- Last Synced: 2025-02-10T09:43:43.903Z (over 1 year ago)
- Topics: go, golang, hooks, sql
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simple SQL
Simple SQL is a light wrapper around the native sql library, using easyscan to scan rows into objects and implementing hooks for additional logic around queries.
## Usage
Get a Database object through connection. The connection includes a ping to ensure that the opened connection is active. The examples use a PostgreSQL database.
```golang
dsn := "host=127.0.0.1 port=5432 user=pg password=admin dbname=library"
db, err := ssql.Connect(context.TODO(), "postgres", dsn)
if err != nil {
panic(err)
}
```
There are only a handful of functions for interacting with the database to keep it simple. All functions require a context, and queries which return rows require a destination object that implement `easyscan.ScanOne` or `easyscan.ScanMany`. Queries require a query string and a list of optional parameters.
```golang
query := `INSERT INTO books(id, title) VALUES($1, $2)`
res, err := db.Exec(context.TODO(), query, 1, "Beards and Beer")
```
```golang
bk := Book{}
err := db.Get(context.TODO(), &bk, "SELECT * FROM books WHERE id=$1", 1)
```
```golang
bkl := BookList{}
err := db.Select(context.TODO(), &bkl, "SELECT * FROM books")
```
For atomic operations with multiple queries, you can start a transaction. Transactions can be committed when finished or rolled back on error.
```golang
tx, err := db.Begin(context.TODO())
```
```golang
query := `UPDATE books SET title=$2 WHERE id=$1`
res, err := db.Exec(context.TODO(), query, 1, "The Ancient Earth")
if err != nil {
tx.Rollback(context.TODO())
}
tx.Commit(context.TODO())
```
## Hooks
You can attach hooks to databases which will be executed after the queries have been completed. Each hook function provides a QueryContext which contains details about the query. Transactions inherit hooks from the database context that started the transaction.
```golang
db.UseHook(
func(ctx context.Context, qctx ssql.QueryContext, err error) {
if err != nil {
fmt.Println(qctx.Function, " query failed")
} else {
dur := time.Since(qctx.StartTime)
fmt.Println(qctx.Function, " query succeeded in ", dur)
}
}
)
```