pgxsql

package
v0.0.0-...-52f1106 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2023 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	DatabaseURLKey = "DATABASE_URL"
)

Variables

View Source
var (
	Uri = pkgPath
)

Functions

func BuildUri

func BuildUri(path string) string

func ClientShutdown

func ClientShutdown()

func ClientStartup

func ClientStartup(config map[string]string, credentials messaging.Credentials) error

func CollectRows

func CollectRows[T any](rows Rows, fn RowToFunc[T]) ([]T, error)

CollectRows iterates through rows, calling fn for each row, and collecting the results into a slice of T.

func ContextWithExec

func ContextWithExec(ctx context.Context, fn ExecProxy) context.Context

ContextWithExec - creates a new Context with an Exec function

func ContextWithQuery

func ContextWithQuery(ctx context.Context, fn QueryProxy) context.Context

ContextWithQuery - creates a new Context with a Query function

func IsContextExec

func IsContextExec(c context.Context) bool

func IsContextQuery

func IsContextQuery(c context.Context) bool

func IsStarted

func IsStarted() bool

func Ping

func Ping[E template.ErrorHandler](ctx context.Context) (status *template.Status)

func SetActuator

func SetActuator(fn template.ActuatorApply)

func Stat

func Stat[E template.ErrorHandler](ctx context.Context) (stat *pgxpool.Stat, status *template.Status)

Types

type CollectableRow

type CollectableRow interface {
	FieldDescriptions() []FieldDescription
	Scan(dest ...any) error
	Values() ([]any, error)
	RawValues() [][]byte
}

type CommandTag

type CommandTag struct {
	Sql          string
	RowsAffected int64
	Insert       bool
	Update       bool
	Delete       bool
	Select       bool
}

func ContextExec

func ContextExec(ctx context.Context, req Request) (CommandTag, error)

func Exec

func Exec[E template.ErrorHandler](ctx context.Context, req Request, arguments ...any) (CommandTag, *template.Status)
Example
package main

import (
	"context"
	"errors"
	"fmt"
	"github.com/idiomatic-go/middleware/template"
)

func NilEmpty(s string) string {
	if s == "" {
		return "<nil>"
	}
	return s
}

const (
	execUpdateSql  = "update test"
	execInsertSql  = "insert test"
	execUpdatePath = "exec.update"
	execInsertPath = "exec.insert"
)

func execTestProxy(req Request) (CommandTag, error) {
	switch req.Uri {
	case BuildUri(execUpdatePath):
		return emptyCommandTag, errors.New("exec error")
	case BuildUri(execInsertPath):
		return CommandTag{
			Sql:          req.Sql,
			RowsAffected: 1234,
			Insert:       true,
			Update:       false,
			Delete:       false,
			Select:       false,
		}, nil
	}
	return emptyCommandTag, nil
}

func main() {
	ctx := ContextWithExec(context.Background(), execTestProxy)

	cmd, status := Exec[template.DebugError](ctx, NewRequest(execUpdatePath, execUpdateSql))
	fmt.Printf("test: Exec(%v) -> %v [cmd:%v]\n", execUpdateSql, status, cmd)

	cmd, status = Exec[template.DebugError](ctx, NewRequest(execInsertPath, execInsertSql))
	fmt.Printf("test: Exec(%v) -> %v [cmd:%v]\n", execInsertSql, status, cmd)

}
Output:

[[] github.com/idiomatic-go/postgresql-adapter/pgxsql/exec [exec error]]
test: Exec(update test) -> Internal [cmd:{ 0 false false false false}]
test: Exec(insert test) -> OK [cmd:{insert test 1234 true false false false}]

func ExecInsert

func ExecInsert[E template.ErrorHandler](ctx context.Context, tag *CommandTag, req Request, values []any) (CommandTag, *template.Status)

func ExecUpdate

func ExecUpdate[E template.ErrorHandler](ctx context.Context, tag *CommandTag, req Request, attrs []sqldml.Attr, where []sqldml.Attr) (CommandTag, *template.Status)

func ExecWithCommand

func ExecWithCommand[E template.ErrorHandler](ctx context.Context, tag *CommandTag, req Request, arguments ...any) (_ CommandTag, status *template.Status)

type ExecProxy

type ExecProxy func(req Request) (CommandTag, error)

type FieldDescription

type FieldDescription struct {
	Name                 string
	TableOID             uint32
	TableAttributeNumber uint16
	DataTypeOID          uint32
	DataTypeSize         int16
	TypeModifier         int32
	Format               int16
}

type QueryProxy

type QueryProxy func(req Request) (Rows, error)

type Request

type Request struct {
	Uri string
	Sql string
}

func NewRequest

func NewRequest(path string, sql string) Request

func (Request) String

func (r Request) String() string

type RowToFunc

type RowToFunc[T any] func(row CollectableRow) (T, error)

RowToFunc is a function that scans or otherwise converts row to a T.

type Rows

type Rows interface {
	// Close closes the rows, making the connection ready for use again. It is safe
	// to call Close after rows is already closed.
	Close()

	// Err returns any error that occurred while reading.
	Err() error

	// CommandTag returns the command tag from this query. It is only available after Rows is closed.
	CommandTag() CommandTag

	FieldDescriptions() []FieldDescription

	// Next prepares the next row for reading. It returns true if there is another
	// row and false if no more rows are available. It automatically closes rows
	// when all rows are read.
	Next() bool

	// Scan reads the values from the current row into dest values positionally.
	// dest can include pointers to core sqldml, values implementing the Scanner
	// interface, and nil. nil will skip the value entirely. It is an error to
	// call Scan without first calling Next() and checking that it returned true.
	Scan(dest ...any) error

	// Values returns the decoded row values. As with Scan(), it is an error to
	// call Values without first calling Next() and checking that it returned
	// true.
	Values() ([]any, error)

	// RawValues returns the unparsed bytes of the row values. The returned data is only valid until the next Next
	// call or the Rows is closed.
	RawValues() [][]byte
}

func ContextQuery

func ContextQuery(ctx context.Context, req Request) (Rows, error)

func Query

func Query[E template.ErrorHandler](ctx context.Context, req Request, arguments ...any) (result Rows, status *template.Status)

Jump to

Keyboard shortcuts

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