sqlg

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

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

Go to latest
Published: Sep 1, 2023 License: MIT Imports: 6 Imported by: 3

README

Sql-Glue

SQL Glue is library for GO which speeding up constructing SQL queries. It generate input for sqlx or other libraries.

Features

  • Escaping variables and identifiers
  • Joining AND and OR conditions
  • Generate SET part
  • Splitting Structs and Maps

Basic example

There is just basic example how it is work. For more examples look to CookBook(in progress). Try in go playground

package main

import (
    "strings"
    "fmt"

    "github.com/lib/pq"
    sqlg "github.com/mzahradnicek/sql-glue"
)

type filter map[string]interface{}

var builder *sqlg.Builder

func generateQuery(f filter) (string, []interface{}, error) {
    where := sqlg.Qg{"deleted = 0"}

    if v, ok := f["id"]; ok {
        where.Append("id = %v", v)  // automatically escaped value if string
    }

    if v, ok := f["name"]; ok {
        where.Append("name = %v", v)
    }

    q := &sqlg.Qg{"SELECT * FROM users WHERE %and", where}
    return builder.Glue(q)
}

func main() {
    builder = sqlg.NewBuilder(sqlg.Config{
        KeyModifier:      strings.ToLower,
        IdentifierEscape: pq.QuoteIdentifier,
        PlaceholderInit:  sqlg.PqPlaceholder,
        Tag:              "sqlg",
    })

    query, data, err := generateQuery(filter{"id": 5, "name": "John Smith"})
    fmt.Printf("%#v\n\n%#v\n\n%#v", query, data, err)
	// then use with sqlx.Select(dest, query, data...)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PqPlaceholder

func PqPlaceholder() func(buf *bytes.Buffer)

PostgreSQL placeholder generator

func QmPlaceholder

func QmPlaceholder() func(buf *bytes.Buffer)

Question mark placehodler generator

func Split

func Split(i interface{}, exclude []string) (resKeys []string, resVals []interface{}, err error)

func ToCamel

func ToCamel(snake string) string

func ToSnake

func ToSnake(camel string) string

Types

type Builder

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

func NewBuilder

func NewBuilder(cfg Config) *Builder

func (*Builder) GetSplitter

func (b *Builder) GetSplitter() *Splitter

func (*Builder) Glue

func (b *Builder) Glue(q *Qg) (string, []interface{}, error)

func (*Builder) Split

func (b *Builder) Split(i interface{}, exclude []string) ([]string, []interface{}, error)

type Config

type Config struct {
	IdentifierEscape func(string) string
	KeyModifier      func(string) string
	PlaceholderInit  func() func(buf *bytes.Buffer)
	Tag              string
}

type Qg

type Qg []interface{}

func (*Qg) Append

func (qg *Qg) Append(chunks ...interface{})

func (*Qg) Compile

func (qg *Qg) Compile(cfg *qgConfig) ([]string, []interface{}, error)

func (*Qg) ToSql

func (qg *Qg) ToSql(cfg *Config) (string, []interface{}, error)

type Splitter

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

func GetSplitter

func GetSplitter() *Splitter

func NewSplitter

func NewSplitter() *Splitter

func (*Splitter) KeyModifier

func (ss *Splitter) KeyModifier(km func(string) string) *Splitter

func (*Splitter) Split

func (ss *Splitter) Split(i interface{}, exclude []string) (resKeys []string, resVals []interface{}, err error)

func (*Splitter) Tag

func (ss *Splitter) Tag(t string) *Splitter

type StructSplitter

type StructSplitter struct {
	Exclude     []string
	KeyModifier func(string) string
	Tag         string
}

func (*StructSplitter) Split

func (ss *StructSplitter) Split(i interface{}) (resKeys []string, resVals []interface{}, err error)

Jump to

Keyboard shortcuts

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