sql

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func QueryInterfaceCheck

func QueryInterfaceCheck(ctx context.Context, query string, returnFieldNames []string, args ...any) (map[string]any, error)

QueryInterfaceCheck generates a smulated response for QueryInterface.Query

func ResolverDBCallback

func ResolverDBCallback(ctx context.Context, db QueryInterface, sqlBuilder QueryBuilder) db.ResolverDBCallback

ResolverDBCallback is a db.ResolverDBCallback to generate SQL-based database records. The parameter "fields" and "returnFieldNames" are always sorted to be deterministic.

Types

type DefaultSQLBuilder

type DefaultSQLBuilder struct {
	PlaceholderProviderFactory func() PlaceholderProvider // uses defaultSQLPlaceholderProvider if not set
	QuoteTable                 func(t string) string      // don't quote if not set
	QuoteField                 func(f string) string      // don't quote if not set
}

DefaultSQLBuilder is the default customizable INSERT SQL builder, using placeholders for values. It uses "RETURNING" to get the returnFieldNames.

func (DefaultSQLBuilder) BuildInsertSQL

func (d DefaultSQLBuilder) BuildInsertSQL(tableName string, fieldNames []string, fieldPlaceholders []string, returnFieldNames []string) string

func (DefaultSQLBuilder) CreatePlaceholderProvider

func (d DefaultSQLBuilder) CreatePlaceholderProvider() PlaceholderProvider

type PlaceholderProvider

type PlaceholderProvider interface {
	Next() (placeholder string, argName string)
}

PlaceholderProvider generates database-specific placeholders, like ? for MySQL, $1 for postgres, or :param1 for MSSQL. If the database uses named parameters, its name should be returned in argName, otherwise this should be blank.

type QueryBuilder

type QueryBuilder interface {
	CreatePlaceholderProvider() PlaceholderProvider
	BuildInsertSQL(tableName string, fieldNames []string, fieldPlaceholders []string, returnFieldNames []string) string
}

QueryBuilder is an abstraction for building INSERT queries.

type QueryInterface

type QueryInterface interface {
	Query(ctx context.Context, query string, returnFieldNames []string, args ...any) (map[string]any, error)
}

QueryInterface abstracts executing a query on a database. The return map should contain values for all fields specified in returnFieldNames.

func NewDebugQueryInterface added in v0.13.0

func NewDebugQueryInterface(out io.Writer) QueryInterface

NewDebugQueryInterface returns a QueryInterface that outputs the generated queries. If out is nil, os.Stdout will be used.

func NewDebugResultQueryInterface added in v0.13.0

func NewDebugResultQueryInterface(qi QueryInterface, out io.Writer) QueryInterface

NewDebugResultQueryInterface returns a QueryInterface calls an inner QueryInterface and outputs the generated queries and the returned fields. If out is nil, os.Stdout will be used.

func NewMultiQueryInterface added in v0.13.0

func NewMultiQueryInterface(itfs []QueryInterface) QueryInterface

NewMultiQueryInterface returns a QueryInterface that calls multiple QueryInterface, returning the result of the last one.

func NewSQLQueryInterface

func NewSQLQueryInterface(db *sql.DB) QueryInterface

NewSQLQueryInterface wraps a *sql.DB on the QueryInterface interface.

type QueryInterfaceFunc

type QueryInterfaceFunc func(ctx context.Context, query string, returnFieldNames []string, args ...any) (map[string]any, error)

QueryInterfaceFunc is a func adapter for QueryInterface

func (QueryInterfaceFunc) Query

func (f QueryInterfaceFunc) Query(ctx context.Context, query string, returnFieldNames []string, args ...any) (map[string]any, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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