Documentation ¶
Index ¶
- Constants
- Variables
- func BuildDeleteUri(resource string) string
- func BuildInsertUri(resource string) string
- func BuildQueryUri(resource string) string
- func BuildUpdateUri(resource string) string
- func ClientShutdown()
- func ClientStartup(db messaging.DatabaseUrl, credentials messaging.Credentials) error
- func ContextWithExec(ctx context.Context, exec func(*Request) (CommandTag, error)) context.Context
- func ContextWithQuery(ctx context.Context, query func(req *Request) (Rows, error)) context.Context
- func ContextWithValue(ctx context.Context, key any, val any) context.Context
- func IsStarted() bool
- func Ping[E runtime.ErrorHandler](ctx context.Context) (status *runtime.Status)
- func Scan[T Scanner[T]](rows Rows) ([]T, error)
- type CommandTag
- type ExecExchange
- type FieldDescription
- type QueryExchange
- type Request
- func NewDeleteRequest(resource, template string, where []pgxdml.Attr) *Request
- func NewInsertRequest(resource, template string, values [][]any) *Request
- func NewQueryRequest(resource, template string, where []pgxdml.Attr) *Request
- func NewQueryRequestFromValues(resource, template string, values map[string][]string) *Request
- func NewUpdateRequest(resource, template string, attrs []pgxdml.Attr, where []pgxdml.Attr) *Request
- type Rows
- type Scanner
- type Stats
Examples ¶
Constants ¶
const ( PostgresNID = "postgres" QueryNSS = "query" InsertNSS = "insert" UpdateNSS = "update" DeleteNSS = "delete" PingNSS = "ping" StatNSS = "stat" PingUri = "urn:" + PostgresNID + ":" + PingNSS StatUri = "urn:" + PostgresNID + ":" + StatNSS )
const (
NullCount = int64(-1)
)
Variables ¶
var (
Uri = pkgPath
)
Functions ¶
func BuildDeleteUri ¶
BuildDeleteUri - build an uri with the Delete NSS
func BuildInsertUri ¶
BuildInsertUri - build an uri with the Insert NSS
func BuildQueryUri ¶
BuildQueryUri - build an uri with the Query NSS
func BuildUpdateUri ¶
BuildUpdateUri - build an uri with the Update NSS
func ClientShutdown ¶
func ClientShutdown()
func ClientStartup ¶
func ClientStartup(db messaging.DatabaseUrl, credentials messaging.Credentials) error
ClientStartup - entry point for creating the pooling client and verifying a connection can be acquired
Example ¶
db := messaging.DatabaseUrl{Url: ""} err := ClientStartup(db, nil) if err == nil { defer ClientShutdown() } fmt.Printf("test: ClientStartup() -> %v\n", err)
Output: test: ClientStartup() -> database URL is empty
func ContextWithExec ¶
func ContextWithQuery ¶
func ContextWithValue ¶
ContextWithValue - create a new context with a value, updating the context if it is an ExecExchange or QueryExchange context
Example ¶
k1 := "1" v1 := "value 1" k2 := "2" v2 := "value 2" ctx := ContextWithValue(context.Background(), k1, v1) fmt.Printf("test: ContextWithValue(ctx,k1,v1) -> [v1:%v] [v2:%v] [query:%v] [exec:%v]\n", ctx.Value(k1), ctx.Value(k2), isQueryContext(ctx), isExecContext(ctx)) ctx = ContextWithValue(ctx, k2, v2) fmt.Printf("test: ContextWithValue(ctx,k1,v1) -> [v1:%v] [v2:%v] [query:%v] [exec:%v]\n", ctx.Value(k1), ctx.Value(k2), isQueryContext(ctx), isExecContext(ctx))
Output: test: ContextWithValue(ctx,k1,v1) -> [v1:value 1] [v2:<nil>] [query:false] [exec:false] test: ContextWithValue(ctx,k1,v1) -> [v1:value 1] [v2:value 2] [query:false] [exec:false]
func Ping ¶
Ping - templated function for pinging 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", IsStarted()) status := Ping[runtime.DebugError](nil) fmt.Printf("test: Ping(nil) -> %v\n", status) }
Output: test: clientStartup() -> [started:true] test: Ping(nil) -> OK
Types ¶
type CommandTag ¶
type CommandTag struct { Sql string RowsAffected int64 Insert bool Update bool Delete bool Select bool }
CommandTag - results from an Exec command
type ExecExchange ¶
type ExecExchange interface {
Exec(*Request) (CommandTag, error)
}
ExecExchange - interface for exec
Example ¶
ctx := ContextWithExec(context.Background(), execTestProxy) cmd, status := Exec[runtime.DebugError](ctx, NullCount, NewUpdateRequest(execUpdateRsc, execUpdateSql, nil, nil)) fmt.Printf("test: Exec[DebugError](%v) -> %v [cmd:%v]\n", execUpdateSql, status, cmd) cmd, status = Exec[runtime.DebugError](ctx, NullCount, NewInsertRequest(execInsertRsc, execInsertSql, nil)) fmt.Printf("test: Exec[DebugError](%v) -> %v [cmd:%v]\n", execInsertSql, status, cmd)
Output: [[] github.com/idiomatic-go/postgresql/pgxsql/exec [exec error]] test: Exec[DebugError](update test) -> Internal [cmd:{ 0 false false false false}] test: Exec[DebugError](insert test) -> OK [cmd:{INSERT 1 1234 true false false false}]
type FieldDescription ¶
type FieldDescription struct { Name string TableOID uint32 TableAttributeNumber uint16 DataTypeOID uint32 DataTypeSize int16 TypeModifier int32 Format int16 }
FieldDescription - data for defining the returned Query fields/columns
type QueryExchange ¶
QueryExchange - interface for query
type Request ¶
type Request struct { Uri string Template string Values [][]any Attrs []pgxdml.Attr Where []pgxdml.Attr Error error // contains filtered or unexported fields }
Request - contains data needed to build the SQL statement related to the uri
func NewDeleteRequest ¶
func NewInsertRequest ¶
func NewQueryRequest ¶
func NewUpdateRequest ¶
func (*Request) Validate ¶
Example ¶
uri := "urn:postgres:query.resource" sql := "select * from table" req := Request{} err := req.Validate() fmt.Printf("test: Validate(empty) -> %v\n", err) req.Uri = uri err = req.Validate() fmt.Printf("test: Validate(%v) -> %v\n", uri, err) req.Uri = "" req.Template = sql err = req.Validate() fmt.Printf("test: Validate(%v) -> %v\n", sql, err) req.Uri = uri req.Template = sql err = req.Validate() fmt.Printf("test: Validate(all) -> %v\n", err) //rsc := "access-log" //t := "delete from access_log" //req1 := NewDeleteRequest(rsc, t, nil) //err = req1.Validate() //fmt.Printf("test: Validate(%v) -> %v\n", t, err) //t = "update access_log" //req1 = NewUpdateRequest(rsc, t, nil, nil) //err = req1.Validate() //fmt.Printf("test: Validate(%v) -> %v\n", t, err) //t = "update access_log" //req1 = NewUpdateRequest(rsc, t, []pgxdml.Attr{{Name: "test", Val: "test"}}, nil) //err = req1.Validate() //fmt.Printf("test: Validate(%v) -> %v\n", t, err)
Output: test: Validate(empty) -> invalid argument: request Uri is empty test: Validate(urn:postgres:query.resource) -> invalid argument: request template is empty test: Validate(select * from table) -> invalid argument: request Uri is empty test: Validate(all) -> <nil>
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 pgxsql, 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 }
Rows - interface that proxies the postgresql Rows interface
type Stats ¶
Stats - pool statistics
func Stat ¶
Stat - templated function for retrieving runtime 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", IsStarted()) stat, status := Stat[runtime.DebugError](nil) fmt.Printf("test: Stat(nil) -> [status:%v] [stat:%v]\n", status, stat != nil) }
Output: test: clientStartup() -> [started:true] test: Stat(nil) -> [status:OK] [stat:true]