https://github.com/whitecypher/stmts
Create a collection of sqlx named statements
https://github.com/whitecypher/stmts
collection golang named sql sqlx statements
Last synced: 5 months ago
JSON representation
Create a collection of sqlx named statements
- Host: GitHub
- URL: https://github.com/whitecypher/stmts
- Owner: whitecypher
- License: mit
- Created: 2017-08-25T10:43:06.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-25T11:46:01.000Z (almost 9 years ago)
- Last Synced: 2024-06-20T15:01:56.470Z (almost 2 years ago)
- Topics: collection, golang, named, sql, sqlx, statements
- Language: Go
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stmts
This is a utility package to create a concurrency safe collection of sqlx named statements that can be consumed in 3 stages. Registration, preparation, and execution.
[](https://goreportcard.com/report/github.com/whitecypher/stmts)
[](https://godoc.org/github.com/whitecypher/stmts)
[]()
## how to
```go
package main
import (
"fmt"
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/whitecypher/stmts"
)
// storing queries in constants will make it easier to retrieve the queries from the collection later. The query is the key.
const (
qGetSomething = `SELECT * FROM mytable WHERE id = :id`
qUpdateSomething = `UPDATE mytable SET my_name_is = :name WHERE id = :id`
qDeleteSomething = `DELETE FROM mytable WHERE id = :id`
)
func main() {
// using sqlx for database operations
var db *sqlx.DB
// TODO: resolve the connection to the database
// collection has no dependencies so a simple declaration is sufficient.
var named stmts.Named
// add queries
named.Add(
qGetSomething,
qUpdateSomething,
qDeleteSomething,
)
// prepare the statements
err := named.Prepare(db)
if err != nil {
// TODO: handle the error however you like
}
// or use
named.MustPrepare(db, func(format string, v ...interface{}) {
// TODO: handle the error however you like
})
// retrieve and execute a query
var row struct {
ID uint64 `db:"id"`
Name string `db:"name"`
}
err := named.Stmt(qGetSomething).Get(&row, struct {
ID uint64 `db:"id"`
}{
ID: 1,
})
if err != nil && err != sql.ErrNoRows {
// TODO: handle the error however you like
}
}
```