https://github.com/indeedeng/libsql
Use libsql to write unit-testable and safer SQL code in Go
https://github.com/indeedeng/libsql
Last synced: about 1 year ago
JSON representation
Use libsql to write unit-testable and safer SQL code in Go
- Host: GitHub
- URL: https://github.com/indeedeng/libsql
- Owner: indeedeng
- License: bsd-3-clause
- Created: 2019-06-29T03:24:52.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-29T07:12:22.000Z (almost 5 years ago)
- Last Synced: 2025-05-07T23:40:07.055Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 114 KB
- Stars: 26
- Watchers: 10
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
libsql
======
[](https://goreportcard.com/report/oss.indeed.com/go/libsql)
[](https://travis-ci.org/indeedeng/libsql)
[](https://godoc.org/oss.indeed.com/go/libsql)
[](OSSMETADATA)
[](LICENSE)
# Project Overview
Package `libsql` provides wrappers for the Go standard library's "sql" package
that enables writing unit-testable and less error-prone SQL database code.
# Getting Started
The `libsql` package can be installed by running:
```
$ go get oss.indeed.com/go/libsql
```
To get started, use the `libsql.Wrap` method and pass a `*sql.DB`:
```go
import (
"context"
"database/sql"
"oss.indeed.com/go/libsql"
)
func usageExample(ctx context.Context, sqldb *sql.DB) error {
db := libsql.Wrap(sqldb)
var column1, column2, column3 int64
err := db.ScanOne(
ctx,
libsql.Into(
&column1,
&column2,
&column3,
),
"SELECT 1, 2, 3 FROM dual WHERE 1 = ? AND 2 = ?",
1,
2,
)
if err != nil {
return err
}
// use the values of column1, column2, column3
}
```
To scan multiple rows, provide a custom implementation of `libsql.RowScanner`:
```go
import (
"context"
"oss.indeed.com/go/libsql"
)
type elephant struct {
name string
}
type elephantRowScanner struct {
rawElephantName string
elephants []elephant
}
var _ libsql.RowScanner = (*elephantRowScanner)(nil)
func (e *elephantRowScanner) Into() []interface{} {
return []interface{}{&e.rawElephantName}
}
func (e *elephantRowScanner) RowScanned() error {
if e.rawElephantName == "" {
return errors.New("empty elephant name in the database")
}
e.elephants = append(e.elephants, elephant{name: e.rawElephantName})
return nil
}
func elephantsFrom(ctx context.Context, q libsql.Queryer) ([]elephant, error) {
elephantRS := elephantRowScanner{}
err := q.Scan(
ctx,
&elephantRS,
`SELECT * FROM (VALUES ROW ('Dumbo'), ROW ('Horton')) AS elephants(name)`,
)
return elephantsRS.elephants, err
}
```
# Asking Questions
For technical questions about `libsql`, just file an issue in the GitHub tracker.
For questions about Open Source in Indeed Engineering, send us an email at
opensource@indeed.com
# Contributing
We welcome contributions! Feel free to help make `libsql` better.
### Process
- Open an issue and describe the desired feature / bug fix before making
changes. It's useful to get a second pair of eyes before investing development
effort.
- Make the change. If adding a new feature, remember to provide tests that
demonstrate the new feature works, including any error paths. If contributing
a bug fix, add tests that demonstrate the erroneous behavior is fixed.
- Open a pull request. Automated CI tests will run. If the tests fail, please
make changes to fix the behavior, and repeat until the tests pass.
- Once everything looks good, one of the indeedeng members will review the
PR and provide feedback.
# Maintainers
The `oss.indeed.com/go/libsql` module is maintained by Indeed Engineering.
While we are always busy helping people get jobs, we will try to respond to
GitHub issues, pull requests, and questions within a couple of business days.
# Code of Conduct
`oss.indeed.com/go/libsql` is governed by the[Contributer Covenant v1.4.1](CODE_OF_CONDUCT.md)
For more information please contact opensource@indeed.com.
# License
The `oss.indeed.com/go/libsql` module is open source under the [BSD-3-Clause](LICENSE)
license.