https://github.com/cycloidio/sqlr
Set of useful interfaces to work with the GO 'database/sql'
https://github.com/cycloidio/sqlr
go golang mysql sql
Last synced: 5 months ago
JSON representation
Set of useful interfaces to work with the GO 'database/sql'
- Host: GitHub
- URL: https://github.com/cycloidio/sqlr
- Owner: cycloidio
- License: mit
- Created: 2020-10-13T10:10:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-20T12:43:19.000Z (over 5 years ago)
- Last Synced: 2025-02-28T17:54:37.514Z (about 1 year ago)
- Topics: go, golang, mysql, sql
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SQLR
[](https://pkg.go.dev/github.com/cycloidio/sqlr)
Defines a default set of Interfaces to work with the default [database/sql](https://golang.org/pkg/database/sql/).
## Querier
The [sql.DB](https://golang.org/pkg/database/sql/#DB) and the [sql.Tx](https://golang.org/pkg/database/sql/#Tx) have the same signature when quering
so for that we have created the `sqlr.Querier` which is fulfilled for either `sql.DB` and `sql.Tx` so in some contexts you don't need to have the
specific implementation this interface can be used
## Scanner
Used to abstract the logic of Scanning an `sql.Row` and `sql.Rows` reusable:
```go
type Scanner interface {
Scan(dest ...interface{}) error
}
```
Example usage:
```go
func scanUsers(rows *sql.Rows) ([]*user.User, error) {
users := make([]*user.User, 0, 0)
defer rows.Close()
for rows.Next() {
u, err := scanUser(rows)
if err != nil {
return nil, err
}
users = append(users, u)
}
if err := rows.Err(); err != nil {
return nil, err
}
return users, nil
}
func scanUser(s sqlr.Scanner) (*user.User, error) {
var u user.User
err := s.Scan(
&u.Name,
)
if err != nil {
return nil, err
}
return u, nil
}
```