Categorygithub.com/reddec/gsql
modulepackage
1.2.0
Repository: https://github.com/reddec/gsql.git
Documentation: pkg.go.dev

# README

Generic SQL

Go Reference

Tiny wrapper built around standard Go SQL library and SQLx with generic support.

Installation

go get -v github.com/reddec/gsql

Examples

Schema for the example

CREATE TABLE book
(
    id     INTEGER NOT NULL PRIMARY KEY,
    title  TEXT    NOT NULL,
    author TEXT,
    year   INTEGER NOT NULL
)

Type in Go

type Book struct {
    ID     int64
    Title  string
    Author string
    Year   int
}

Initialization

// ...
conn, err := sqlx.Open("sqlite", "database.sqlite")
// ...

Get

Get single book by ID

book, err := gsql.Get[Book](ctx, conn, "SELECT * FROM book WHERE id = ?", 1234)

List

List books by author

books, err := gsql.List[Book](ctx, conn, "SELECT * FROM book WHERE author = ?", "O'Really")

Iterate

Iterate each book one by one

iter := gsql.Iterate[Book](ctx, conn, "SELECT * FROM book LIMIT ?", 100)
defer iter.Close()
for iter.Next() {
    book, err := iter.Get()
    // ...
}
if iter.Err() != nil {
    panic(iter.Err()) // use normal handling in real application
}

LazyGet

Save query which returns one object and execute it later

getBooks := gsql.LazyGet[Book](conn, "SELECT * FROM book WHERE id = ?", 123)
// ...
book, err := getBooks(ctx) // can be called many times

LazyList

Save query which returns multiple objects and execute it later

listBooks := gsql.LazyList[Book](conn, "SELECT * FROM book")
// ...
books, err := listBooks(ctx) // can be called many times

CachedGet

Save query which returns one object and execute it later. Once query executed, result will be cached until Invalidate or Refresh called.

cache := gsql.CachedGet[Book](conn, "SELECT * FROM book WHERE id = ?", 123)
book, err := cache.Get(ctx) // first time it will execute the query
//...
book2, err := cache.Get(ctx) // second time it will return cached information
//...
cache.Invalidate() // reset cache, the following Get will again execute the query

CachedList

Save query which returns multiple objects and execute it later. Once query executed, result will be cached until Invalidate or Refresh called.

cache := gsql.CachedList[Book](conn, "SELECT * FROM book WHERE id = ?", 123)
books, err := cache.Get(ctx) // first time it will execute the query
//...
books2, err := cache.Get(ctx) // second time it will return cached information
//...
cache.Invalidate() // reset cache, the following Get will again execute the query

JSON

Simple generic wrapper around any JSON-serializable value: JSON[T].

Support both reading (scanner) and writing (Value).

Example

Assuming schema

CREATE TABLE book
(
    id     INTEGER NOT NULL PRIMARY KEY,
    title  TEXT    NOT NULL,
    author TEXT,
    year   INTEGER NOT NULL,
    meta   JSONB NOT NULL
)

Go code

type Metadata struct {
    Zip int
}

type Book struct {
    ID     int64
    Title  string
    Author string
    Year   int
    Meta   JSON[Metadata]
}

# Packages

No description provided by the author

# Functions

AsJSON is a handy wrapper to use Go type automatic detection.
CachedGet is alias of [NewCache]([LazyGet]) and provides cached information from database.
CachedList is alias of [NewCache]([LazyList]) and provides cached information from database.
Get single result or return an error.
Iterate over multiple results and automatically scans each row to the provided type.
LazyGet generates closure which wraps prepared query with arguments and executes query with single result on-demand.
LazyList generates closure which wraps prepared query with arguments and executes query on-demand.
List creates slice of results based on SQL query.
NewCache creates new concurrent-safe cache for internal data.
Rows is a simple wrapper around [sqlx.Rows] which is automatically scans row to the provided type.

# Structs

Cache stores data and factory to get data.
Iterator is typed wrapper around [sql.Rows] which is automatically scans row to the provided type.
JSON wrapper which converts/parses data from/to JSON (as bytes).

# Interfaces

No description provided by the author