sqlutil

package module
v0.0.0-...-ee5c689 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2020 License: MIT Imports: 10 Imported by: 0

README

sqlutil

MIT License go.dev reference Discord Chat

Utilities for working with SQL in Go.

  • Append an opinionated UTF-8 string representation of an interface{} to a byte slice, with byte slices being encoded into RFC-4648 Base64.
  • Convert *sql.Rows into JSON with minimal allocations.
  • Convert *sql.Rows into CSV with minimal allocations.
  • Parse/evaluate SQL statements with named parameters.

Example

Given the SQL query:

CREATE TABLE test (id integer primary key autoincrement, name varchar);

INSERT INTO test (name) VALUES ('a');
INSERT INTO test (name) VALUES ('b');
INSERT INTO test (name) VALUES ('c');
INSERT INTO test (name) VALUES ('d');

SELECT * FROM test;

sqlutil.RowsToJSON would yield:

[
  {
    "id": 1,
    "name": "a"
  },
  {
    "id": 2,
    "name": "b"
  },
  {
    "id": 3,
    "name": "c"
  },
  {
    "id": 4,
    "name": "d"
  }
]

sqlutil.RowsToCSV would yield:

id,name
1,"a"
2,"b"
3,"c"
4,"d"

Benchmarks

go test -bench=. -benchmem -benchtime=10s

goos: linux
goarch: amd64
pkg: github.com/lithdew/sqlutil
BenchmarkRowsToJSON-8            1213502              9531 ns/op             584 B/op         27 allocs/op
BenchmarkRowsToCSV-8             1268085              9991 ns/op             584 B/op         27 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNamedQueryMalformed = errors.New("named query is malformed")

Functions

func AppendValue

func AppendValue(dst []byte, src interface{}) ([]byte, error)

AppendValue appends to dst a UTF-8 string representation of src. Byte slices will be formatted via RFC 4648 Base64.

func RowsToCSV

func RowsToCSV(dst []byte, rows *sql.Rows) ([]byte, error)

RowsToCSV appends to dst the CSV representation of a list of resultant rows from a SQL query. It does not support multiple result sets, though may be called again after calling (*sql.Rows).NextResultSet().

func RowsToJSON

func RowsToJSON(dst []byte, rows *sql.Rows) ([]byte, error)

RowsToJSON appends to dst the JSON representation of a list of resultant rows from a SQL query. It does not support multiple result sets, though may be called again after calling (*sql.Rows).NextResultSet().

Types

type NamedQuery

type NamedQuery struct {
	Query  string   `json:"query"`
	Parsed string   `json:"parsed"`
	Names  []string `json:"names"`
}

NamedQuery is the parsed result of a SQL statement with named parameters.

func ParseNamedQuery

func ParseNamedQuery(query string) (res NamedQuery, err error)

ParseNamedQuery parses a SQL statement with named parameters, replaces those named parameters with ?, and returns the parameter names sorted in the order from the start to the end of the SQL statement.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL