pgxsql

package
v0.0.0-...-cd787a4 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: BSD-3-Clause Imports: 16 Imported by: 19

Documentation

Index

Examples

Constants

View Source
const (
	StartTimeName = "start_time"
	DurationName  = "duration_ms"
	TrafficName   = "traffic"
	CreatedTSName = "created_ts"

	RegionName     = "region"
	ZoneName       = "zone"
	SubZoneName    = "sub_zone"
	HostName       = "host"
	InstanceIdName = "instance_id"

	RequestIdName = "request_id"
	RelatesToName = "relates_to"
	ProtocolName  = "protocol"
	MethodName    = "method"
	FromName      = "from"
	ToName        = "to"
	UrlName       = "url"
	PathName      = "path"

	StatusCodeName = "status_code"
	EncodingName   = "encoding"
	BytesName      = "bytes"

	RouteName      = "route"
	RouteToName    = "route_to"
	TimeoutName    = "timeout"
	RateLimitName  = "rate_limit"
	RateBurstName  = "rate_burst"
	ReasonCodeName = "rc"
)
View Source
const (
	PkgPath = "github/advanced-go/postgresql/pgxsql"

	QueryRouteName  = "postgresql-query"
	InsertRouteName = "postgresql-insert"
	UpdateRouteName = "postgresql-update"
	DeleteRouteName = "postgresql-delete"
	PingRouteName   = "postgresql-ping"
)

Variables

This section is empty.

Functions

func Ping

func Ping(ctx context.Context, h http.Header) *core.Status

Ping - ping the database cluster

Example
err := testStartup()
if err != nil {
	fmt.Printf("test: testStartup() -> [error:%v]\n", err)
} else {
	defer clientShutdown()
	fmt.Printf("test: clientStartup() -> [started:%v]\n", isReady())

	status := ping(nil, newPingRequest())
	fmt.Printf("test: Ping(nil) -> %v\n", status)
}
Output:

test: clientStartup() -> [started:true]
test: Ping(nil) -> OK

func Query

func Query(ctx context.Context, h http.Header, resource, template string, values map[string][]string, args ...any) (rows pgx.Rows, status *core.Status)

Query - process a SQL select statement

func QueryT

func QueryT[T Scanner[T]](ctx context.Context, h http.Header, resource, template string, values map[string][]string, args ...any) (rows []T, status *core.Status)

QueryT - process a SQL select statement, returning a type

func Readiness

func Readiness() *core.Status

Readiness - package readiness

func Rows

func Rows[T Scanner[T]](entries []T) ([][]any, *core.Status)

Rows - templated function for creating rows

func Scan

func Scan[T Scanner[T]](rows pgx.Rows) ([]T, *core.Status)

Scan - templated function for scanning rows

func Stat

func Stat() (*pgxpool.Stat, *core.Status)

Stat - retrieve Pgx pool stats

Example
err := testStartup()
if err != nil {
	fmt.Printf("test: testStartup() -> [error:%v]\n", err)
} else {
	defer clientShutdown()
	fmt.Printf("test: clientStartup() -> [started:%v]\n", isReady())

	stat1, status := stat()
	fmt.Printf("test: Stat() -> [status:%v] [stat:%v]\n", status, stat1 != nil)
}
Output:

test: clientStartup() -> [started:true]
test: Stat(nil) -> [status:OK] [stat:true]

func Unmarshal

func Unmarshal[T Scanner[T]](t any) ([]T, *core.Status)

Unmarshal - templated function for JSON unmarshalling

Types

type Attr

type Attr struct {
	Key string
	Val any
}

Attr - key value pair

type CommandTag

type CommandTag struct {
	Sql          string `json:"sql"`
	RowsAffected int64  `json:"rows-affected"`
	Insert       bool   `json:"insert"`
	Update       bool   `json:"update"`
	Delete       bool   `json:"delete"`
	Select       bool   `json:"select"`
}

CommandTag - results from an Exec command

func Delete

func Delete(ctx context.Context, h http.Header, resource, template string, where []Attr, args ...any) (tag CommandTag, status *core.Status)

Delete - execute a SQL delete statement

func Insert

func Insert(ctx context.Context, h http.Header, resource, template string, values [][]any, args ...any) (tag CommandTag, status *core.Status)

Insert - execute a SQL insert statement

func InsertT

func InsertT[T Scanner[T]](ctx context.Context, h http.Header, resource, template string, entries []T, args ...any) (tag CommandTag, status *core.Status)

InsertT - execute a SQL insert statement

func NewCommandTag

func NewCommandTag(url string) CommandTag
Example
package main

import (
	"fmt"
)

var (
	//var cmdJson = "{\"sql\":\"select * from table\",\"rows-affected\":123,\"insert\":false,\"update\":true,\"delete\":false,\"select\":false}"
	updateCmdTag2 = "file://[cwd]/test/update-cmd-tag.json"
)

func main() {
	/*
		cmd := CommandTag{
			Sql:          "select * from table",
			RowsAffected: 123,
			Insert:       false,
			Update:       true,
			Delete:       false,
			Select:       false,
		}
		buf, status := json.Marshal(cmd)
		fmt.Printf("test: NewCommandTag() -> %v [status:%v]\n", string(buf), status)

	*/

	cmd2 := NewCommandTag(updateCmdTag2)
	fmt.Printf("test: NewCommandTag() -> %v\n", cmd2)

}
Output:

test: NewCommandTag() -> {update test 123 false true false false}

func Update

func Update(ctx context.Context, h http.Header, resource, template string, where []Attr, args []Attr) (tag CommandTag, status *core.Status)

Update - execute a SQL update statement

type DeleteFunc

type DeleteFunc func(context.Context, http.Header, string, string, []Attr, ...any) (CommandTag, *core.Status)

DeleteFunc - type

type InsertFunc

type InsertFunc func(context.Context, http.Header, string, string, [][]any, ...any) (CommandTag, *core.Status)

InsertFunc - type

type InsertFuncT

type InsertFuncT[T Scanner[T]] func(context.Context, http.Header, string, string, []T, ...any) (CommandTag, *core.Status)

InsertFuncT - type

type QueryFunc

type QueryFunc func(context.Context, http.Header, string, string, map[string][]string, ...any) (pgx.Rows, *core.Status)

QueryFunc - type declaration

type QueryFuncT

type QueryFuncT[T Scanner[T]] func(context.Context, http.Header, string, string, map[string][]string, ...any) ([]T, *core.Status)

QueryFuncT - type declaration

type Scanner

type Scanner[T any] interface {
	Scan(columnNames []string, values []any) (T, error)
	Rows([]T) [][]any
}

Scanner - templated interface for scanning rows

type UpdateFunc

type UpdateFunc func(context.Context, http.Header, string, string, []Attr, []Attr) (CommandTag, *core.Status)

UpdateFunc - type

Jump to

Keyboard shortcuts

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