sqlcommenter

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2022 License: MIT Imports: 5 Imported by: 0

README

sqlcommenter

GoDoc Build Status Go Report Card

Go implementation of https://google.github.io/sqlcommenter/.

Usage with pgx stdlib driver

package main

import (
    "context"
    "database/sql"

    "github.com/jackc/pgx/v4/stdlib"
    "github.com/jbub/sqlcommenter"
)

type contextKey int

const contextKeyUserID contextKey = 0

func withUserID(ctx context.Context, key string) context.Context {
    return context.WithValue(ctx, contextKeyUserID, key)
}

func userIDFromContext(ctx context.Context) string {
    return ctx.Value(contextKeyUserID).(string)
}

func main() {
    pgxDrv := stdlib.GetDefaultDriver()
    drv := sqlcommenter.WrapDriver(pgxDrv,
        sqlcommenter.WithAttrPairs("application", "hello-app"),
        sqlcommenter.WithAttrFunc(func(ctx context.Context) sqlcommenter.Attrs {
            return sqlcommenter.AttrPairs("user-id", userIDFromContext(ctx))
        }),
    )

    sql.Register("pgx-sqlcommenter", drv)

    db, err := sql.Open("pgx-sqlcommenter", "postgres://user@host:5432/db")
    if err != nil {
        // handle error
    }
    defer db.Close()
    
    ctx := context.Background()

    rows, err := db.QueryContext(withUserID(ctx, "22"), "SELECT 1")
    if err != nil {
        // handle error
    }
    defer rows.Close()
    
    // will produce the following query: SELECT 1 /*application='hello-app',user-id='22'*/
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Comment

func Comment(ctx context.Context, query string, opts ...Option) string

Comment adds comments to query using provided options.

func WrapDriver

func WrapDriver(drv driver.Driver, opts ...Option) driver.Driver

WrapDriver wraps sql driver with sqlcommenter support.

Types

type Attr

type Attr struct {
	Key   string
	Value string
}

Attr represents attribute with key and value.

type AttrProvider

type AttrProvider interface {
	GetAttrs(context.Context) Attrs
}

AttrProvider provides Attrs from context.Context.

type AttrProviderFunc

type AttrProviderFunc func(context.Context) Attrs

AttrProviderFunc adapts func to AttrProvider.

func (AttrProviderFunc) GetAttrs

func (f AttrProviderFunc) GetAttrs(ctx context.Context) Attrs

GetAttrs returns Attrs.

type Attrs

type Attrs map[string]string

Attrs wrap map of attributes.

func AttrPairs

func AttrPairs(pairs ...string) Attrs

AttrPairs builds Attrs from multiple key value pairs.

func (Attrs) Update

func (a Attrs) Update(other Attrs)

Update updates map from other Attrs.

type Option

type Option func(cmt *commenter)

Option configures commenter.

func WithAttrFunc

func WithAttrFunc(fn AttrProviderFunc) Option

WithAttrFunc configures commenter with AttrProviderFunc.

func WithAttrPairs

func WithAttrPairs(pairs ...string) Option

WithAttrPairs configures commenter with attr pairs.

func WithAttrProvider

func WithAttrProvider(prov AttrProvider) Option

WithAttrProvider configures commenter with AttrProvider.

func WithAttrs

func WithAttrs(attrs Attrs) Option

WithAttrs configures commenter with Attrs.

Jump to

Keyboard shortcuts

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