Skip to content

Commit

Permalink
Added Arg() for generating the argument placeholder for use in the query
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdy Pruis committed Feb 25, 2022
1 parent 5279df4 commit a9071f7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# sqeasy
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ferdypruis/sqeasy)](https://pkg.go.dev/github.com/ferdypruis/sqeasy)

Attempting to make usage of Go's `sql`-package easier to maintain, by preventing you to manually have to
count and match columns and variables.

## Features
* Map columns to destination values
* Use named parameters
Small wrappers to make usage of Go's `sql`-package with a CockroachDB database easier to maintain, by for example
mapping variables to positional arguments.

## Examples
### Map columns to destination values
Instead of summing all destination variables in `Scan()`, map the columns onto your variables with `sqeasy.SelectColumns`
and have them scanned into them.
```go
var db *sql.DB


var (
colA string
timestamp time.Time
Expand All @@ -31,15 +28,29 @@ err := columns.Scan(row)
```

### Use named parameters
Instead of the $1, $2, $3 etc positional parameters in your queries, use named ones. Bind values to the names
using `sqeasy.NamedParams`.
```go
var db *sql.DB

params := sqeasy.NamedParams{
{"colA", "notthis"},
{"timestamp", time.Now()},
}

query := "SELECT * FROM table WHERE a_column != :colA AND timestamp < :timestamp"
row := sqeasy.QueryRow(db, query, params)
```

The same `sqeasy.NamedParams` could be used to for example generate INSERT statements.
```go
var db *sql.DB

params := sqeasy.NamedParams{
"colA": "notthis",
"a_column": "this",
"timestamp": time.Now(),
}

query := "SELECT * FROM table WHERE a_column != :colA AND timestamp < :timestamp"
query := "INSERT INTO table (" + params.ExprList() + ") VALUES (" + params.Params() + ")"
row := sqeasy.QueryRow(db, query, params)
```
11 changes: 11 additions & 0 deletions arg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sqeasy

import (
"strconv"
)

// Arg adds a value to args, returning it's position
func Arg(args *[]interface{}, arg interface{}) string {
*args = append(*args, arg)
return `$` + strconv.Itoa(len(*args))
}
15 changes: 15 additions & 0 deletions arg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sqeasy

import (
"database/sql"
"time"
)

func ExampleArg() {
var db *sql.DB

var args []interface{}
query := `SELECT * FROM table WHERE a_column = ` + Arg(&args, `fubar`) + ` AND timestamp < ` + Arg(&args, time.Now())

_ = db.QueryRow(query, args...)
}
2 changes: 1 addition & 1 deletion named.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (np NamedParams) Parse(query string) (string, []interface{}, error) {
return query, args, err
}

// Args returns the value for each parameter on the position as indicated in the param slice
// args returns the value for each parameter on the position as indicated in the param slice
func (np NamedParams) Args(params []string) ([]interface{}, error) {
args := make([]interface{}, len(params))

Expand Down
5 changes: 3 additions & 2 deletions named_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package sqeasy_test

import (
"database/sql"
"github.com/ferdypruis/sqeasy"
"time"

"github.com/ferdypruis/sqeasy"
)

func ExampleNamedParams() {
Expand All @@ -15,5 +16,5 @@ func ExampleNamedParams() {
}

query := "SELECT * FROM table WHERE a_column != :colA AND timestamp < :timestamp"
row := sqeasy.QueryRow(db, query, params)
_ = sqeasy.QueryRow(db, query, params)
}
3 changes: 2 additions & 1 deletion select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sqeasy_test

import (
"database/sql"

"github.com/ferdypruis/sqeasy"
)

Expand All @@ -21,5 +22,5 @@ func ExampleSelectColumns() {
}

row := db.QueryRow("SELECT " + columns.ExprList() + " FROM table")
err := columns.Scan(row)
_ = columns.Scan(row)
}

0 comments on commit a9071f7

Please sign in to comment.