talkback

package module
v0.0.0-...-3d17b1f Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2023 License: Unlicense Imports: 5 Imported by: 0

README

Talkback Lancer

Simplify the way to implement query object pattern to your Go app.

Install:

go get -u github.com/Arsfiqball/talkback-lancer

Use:

func main() {
	urlQS := "field1_eq=value1&field2_ne=value2&field3_isnull=true&group=field1&group=field2&sort=field1&sort=-field2&limit=10&skip=10"

	query, err := FromQueryString(urlQS)
	if err != nil {
		panic(err)
	}

	translations := SqlTranslations{
		"field1": SqlFieldTranslation{
			Alias: "alias1",
		},
		"field2": SqlFieldTranslation{
			Column: "ex.field2",
		},
		"field3": SqlFieldTranslation{},
	}

	statement, args, err := ToSql("somewhere", query, translations)
	if err != nil {
		panic(err)
	}

	fmt.Println(statement)
	fmt.Println(args)
}

See more in API Docs

Documentation

Index

Constants

View Source
const (
	OpIsNull    = "isnull"    // IS NULL
	OpEq        = "eq"        // EQUALS
	OpNe        = "ne"        // NOT EQUALS
	OpGt        = "gt"        // GREATER THAN
	OpLt        = "lt"        // LESS THAN
	OpGte       = "gte"       // GREATER THAN OR EQUALS
	OpLte       = "lte"       // LESS THAN OR EQUALS
	OpContain   = "contain"   // CONTAINS
	OpNcontain  = "ncontain"  // NOT CONTAINS
	OpContains  = "contains"  // CONTAINS CASE SENSITIVE
	OpNcontains = "ncontains" // NOT CONTAINS CASE SENSITIVE
	OpIn        = "in"        // IN
	OpNin       = "nin"       // NOT IN
)

Variables

View Source
var (
	ErrInvalidField   = errors.New("invalid field")
	ErrInvalidOp      = errors.New("invalid op")
	ErrInvalidPreload = errors.New("invalid preload")
)

Functions

func SqlConvertBool

func SqlConvertBool(value string) (interface{}, error)

SqlConvertString is a TypeConverter that converts a string to a string.

func SqlConvertDate

func SqlConvertDate(value string) (interface{}, error)

SqlConvertString is a TypeConverter that converts a string to a string.

func SqlConvertDateTime

func SqlConvertDateTime(value string) (interface{}, error)

SqlConvertString is a TypeConverter that converts a string to a string.

func SqlConvertFloat

func SqlConvertFloat(value string) (interface{}, error)

SqlConvertString is a TypeConverter that converts a string to a string.

func SqlConvertISO8601

func SqlConvertISO8601(value string) (interface{}, error)

SqlConvertString is a TypeConverter that converts a string to a string.

func SqlConvertInt

func SqlConvertInt(value string) (interface{}, error)

SqlConvertString is a TypeConverter that converts a string to a string.

func SqlConvertString

func SqlConvertString(value string) (interface{}, error)

SqlConvertString is a TypeConverter that converts a string to a string.

func SqlConvertTime

func SqlConvertTime(value string) (interface{}, error)

SqlConvertString is a TypeConverter that converts a string to a string.

func ToSql

func ToSql(table string, query Query, translations SqlTranslations) (string, []interface{}, error)

func ToSqlGroup

func ToSqlGroup(query Query, translations SqlTranslations) (string, error)

ToSqlGroup converts a Query to a SQL GROUP BY statement.

func ToSqlGroupSlice

func ToSqlGroupSlice(query Query, translations SqlTranslations) ([]string, error)

ToSqlGroupSlice converts a Query to a slice of SQL GROUP BY statements.

func ToSqlLimit

func ToSqlLimit(query Query) (int, error)

ToSqlLimit converts a Query to a SQL LIMIT statement.

func ToSqlOffset

func ToSqlOffset(query Query) (int, error)

ToSqlOffset converts a Query to a SQL OFFSET statement.

func ToSqlOrderBy

func ToSqlOrderBy(query Query, translations SqlTranslations) (string, error)

ToSqlOrderBy converts a Query to a SQL ORDER BY statement.

func ToSqlOrderBySlice

func ToSqlOrderBySlice(query Query, translations SqlTranslations) ([]string, error)

ToSqlOrderBySlice converts a Query to a slice of SQL ORDER BY statements.

func ToSqlPreload

func ToSqlPreload(query Query, preloadable SqlPreloadable) ([]string, error)

ToSqlPreload converts a Query to a SQL preload statement.

func ToSqlSelect

func ToSqlSelect(query Query, translations SqlTranslations) (string, error)

ToSqlSelect converts a Query to a SQL SELECT statement.

func ToSqlSelectSlice

func ToSqlSelectSlice(query Query, translations SqlTranslations) ([]string, error)

ToSqlSelectSlice converts a Query to a slice of SQL SELECT statements.

func ToSqlWhere

func ToSqlWhere(query Query, translations SqlTranslations) (string, []interface{}, error)

ToSqlWhere converts a Query to a SQL WHERE statement.

Types

type Condition

type Condition struct {
	Field  string   // Field is the name of the field to filter on.
	Op     string   // Op is the operation to perform.
	Values []string // Values is a list of values to filter on.
}

Op is a string representing a valid operation.

func (Condition) Valid

func (c Condition) Valid() bool

Valid returns true if the condition is valid.

type Query

type Query struct {
	Conditions  []Condition
	With        []string
	Group       []string
	Accumulator []string
	Sort        []Sort
	Limit       int
	Skip        int
}

Query is a query to filter on.

func FromQueryString

func FromQueryString(qs string) (Query, error)

FromQueryString returns a Query from a query string.

func FromURLValues

func FromURLValues(params url.Values) (Query, error)

FromQueryString returns a Query from a query string.

type Sort

type Sort struct {
	Field   string
	Reverse bool
}

Query is a query to filter on.

type SqlFieldTranslation

type SqlFieldTranslation struct {
	Column        string
	Alias         string
	TypeConverter func(value string) (interface{}, error)
}

SqlFieldTranslation is a translation from a field name to a SQL field.

type SqlPlan

type SqlPlan struct {
	Select    string
	Where     string
	WhereArgs []interface{}
	Group     string
	Order     string
	Limit     int
	Offset    int
	Preload   []string
}

SqlPlan is a plan for executing a query.

func ToSqlPlan

func ToSqlPlan(query Query, translations SqlTranslations, preloadable SqlPreloadable) (SqlPlan, error)

ToSqlPlan converts a Query to a SqlPlan.

type SqlPreloadable

type SqlPreloadable map[string]string

SqlPreloadable is a map of preloads (key) and their corresponding model (value).

type SqlTranslations

type SqlTranslations map[string]SqlFieldTranslation

SqlTranslations is a map of field names to SQL translations.

Jump to

Keyboard shortcuts

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