sqlutil

package
v1.75.4 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package sqlutil provides SQL utilities.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*SQLUtil)

Option is a type alias for a function that configures the DB connector.

func WithQuoteIDFunc

func WithQuoteIDFunc(fn SQLQuoteFunc) Option

WithQuoteIDFunc replaces the default QuoteID function.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/sqlutil"
)

func main() {
	// define custom quote function
	fn := func(s string) string { return "TEST-" + s }

	q, err := sqlutil.New(
		sqlutil.WithQuoteIDFunc(fn),
	)
	if err != nil {
		log.Fatal(err)
	}

	o := q.QuoteID("6971")

	fmt.Println(o)

}
Output:

TEST-6971

func WithQuoteValueFunc

func WithQuoteValueFunc(fn SQLQuoteFunc) Option

WithQuoteValueFunc replaces the default QuoteValue function.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/sqlutil"
)

func main() {
	// define custom quote function
	fn := func(s string) string { return "TEST-" + s }

	q, err := sqlutil.New(
		sqlutil.WithQuoteValueFunc(fn),
	)
	if err != nil {
		log.Fatal(err)
	}

	o := q.QuoteValue("4987")

	fmt.Println(o)

}
Output:

TEST-4987

type SQLQuoteFunc

type SQLQuoteFunc func(s string) string

SQLQuoteFunc is the type of function called to quote a string (ID or value).

type SQLUtil

type SQLUtil struct {
	// contains filtered or unexported fields
}

SQLUtil is the structure that helps to manage a SQL DB connection.

func New

func New(opts ...Option) (*SQLUtil, error)

New creates a new instance.

func (*SQLUtil) BuildInClauseInt

func (c *SQLUtil) BuildInClauseInt(field string, values []int) string

BuildInClauseInt prepares a SQL IN clause with the given list of integer values.

func (*SQLUtil) BuildInClauseString

func (c *SQLUtil) BuildInClauseString(field string, values []string) string

BuildInClauseString prepares a SQL IN clause with the given list of string values.

func (*SQLUtil) BuildInClauseUint

func (c *SQLUtil) BuildInClauseUint(field string, values []uint64) string

BuildInClauseUint prepares a SQL IN clause with the given list of integer values.

func (*SQLUtil) BuildNotInClauseInt

func (c *SQLUtil) BuildNotInClauseInt(field string, values []int) string

BuildNotInClauseInt prepares a SQL NOT IN clause with the given list of integer values.

func (*SQLUtil) BuildNotInClauseString

func (c *SQLUtil) BuildNotInClauseString(field string, values []string) string

BuildNotInClauseString prepares a SQL NOT IN clause with the given list of string values.

func (*SQLUtil) BuildNotInClauseUint

func (c *SQLUtil) BuildNotInClauseUint(field string, values []uint64) string

BuildNotInClauseUint prepares a SQL NOT IN clause with the given list of integer values.

func (*SQLUtil) QuoteID

func (c *SQLUtil) QuoteID(s string) string

QuoteID quotes identifiers such as schema, table, or column names.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/sqlutil"
)

func main() {
	q, err := sqlutil.New()
	if err != nil {
		log.Fatal(err)
	}

	o := q.QuoteID("7919")

	fmt.Println(o)

}
Output:

`7919`

func (*SQLUtil) QuoteValue

func (c *SQLUtil) QuoteValue(s string) string

QuoteValue quotes database string values. The returned value will include all surrounding quotes.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/sqlutil"
)

func main() {
	q, err := sqlutil.New()
	if err != nil {
		log.Fatal(err)
	}

	o := q.QuoteValue("5867")

	fmt.Println(o)

}
Output:

'5867'

Jump to

Keyboard shortcuts

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