superbasic

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2022 License: MIT Imports: 3 Imported by: 1

README

The superbasic SQL-Builder

go.dev reference Go Report Card golangci-lint GitHub tag (latest SemVer)

go get github.com/wroge/superbasic and use a super-simple SQL builder consisting of the 4 basic functions SQL(sql, expr...), Append(expr...), Join(sep, expr...) and If(condition, then, else).

insert := superbasic.SQL(
    "INSERT INTO presidents (first, last) VALUES ?",
    superbasic.Join(", ",
        superbasic.SQL("(?)", superbasic.Join(", ", "Joe", "Biden")),
        superbasic.SQL("(?)", superbasic.Join(", ", "Donald", "Trump")),
        superbasic.SQL("(?)", superbasic.Join(", ", "Barack", "Obama")),
        superbasic.SQL("(?)", superbasic.Join(", ", "George W.", "Bush")),
        superbasic.SQL("(?)", superbasic.Join(", ", "Bill", "Clinton")),
        superbasic.SQL("(?)", superbasic.Join(", ", "George H. W.", "Bush")),
    ),
)

fmt.Println(insert.ToSQL())
// INSERT INTO presidents (first, last) VALUES (?, ?), (?, ?), (?, ?), (?, ?), (?, ?), (?, ?) 
// [Joe Biden Donald Trump Barack Obama George W. Bush Bill Clinton George H. W. Bush]

Dynamic Queries

This package is written to be able to build dynamic queries in the simplest way possible. Each Expression is automatically compiled to the correct place. There is a ToPostgres function to translate expressions for Postgres databases.

type Select struct {
 Columns []superbasic.Sqlizer
 From    superbasic.Sqlizer
 Where   []superbasic.Sqlizer
}

func (s Select) Expression() superbasic.Expression {
 return superbasic.Append(
  superbasic.SQL("SELECT "),
  superbasic.If(len(s.Columns) > 0, s.Columns, superbasic.SQL("*")),
  superbasic.If(s.From != nil, superbasic.SQL(" FROM ?", s.From), superbasic.Skip()),
  superbasic.If(len(s.Where) > 0,
   superbasic.SQL(" WHERE ?",
    superbasic.If(len(s.Where) > 1,
     superbasic.Join(" AND ", s.Where),
     s.Where,
    ),
   ),
   superbasic.Skip(),
  ),
 )
}

func main() {
 query := Select{
  Columns: []superbasic.Sqlizer{
   superbasic.SQL("id"),
   superbasic.SQL("first"),
   superbasic.SQL("last"),
  },
  From: superbasic.SQL("presidents"),
  Where: []superbasic.Sqlizer{
   superbasic.SQL("last = ?", "Bush"),
  },
 }

 fmt.Println(superbasic.ToPostgres(query))
 // SELECT id, first, last FROM presidents WHERE last = $1 [Bush]

 query = Select{
  Columns: []superbasic.Sqlizer{},
  From:    superbasic.SQL("presidents"),
  Where: []superbasic.Sqlizer{
   superbasic.SQL("last = ?", "Bush"),
   superbasic.SQL("first = ?", "Joe"),
  },
 }

 fmt.Println(superbasic.ToPostgres(query))
 // SELECT * FROM presidents WHERE last = $1 AND first = $2 [Bush Joe]

 query = Select{
  From: superbasic.SQL("presidents"),
 }

 fmt.Println(superbasic.ToPostgres(query))
 // SELECT * FROM presidents []
}

Types and Interfaces

superbasic.Expression, superbasic.Sqlizer, superbasic.Expr, []superbasic.Expression, []superbasic.Sqlizer and []superbasic.Expr can be passed to any function and are handled accordingly. The slice expressions are converted to Join(sep, expr...) within Join expressions and to Join(", ", expr...) in all other cases.

type Expr interface {
 Expression() Expression
}

type Sqlizer interface {
 ToSQL() (string, []any, error)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidNumberOfArguments = errors.New("invalid number of arguments")

Functions

func ToPostgres

func ToPostgres(expr any) (string, []any, error)

Types

type Expr

type Expr interface {
	Expression() Expression
}

type Expression

type Expression struct {
	SQL  string
	Args []any
	Err  error
}

func Append

func Append(expr ...any) Expression

func Error added in v0.0.2

func Error(err error) Expression

func If

func If(condition bool, then any, els any) Expression

func Join

func Join(sep string, expr ...any) Expression

func SQL

func SQL(sql string, args ...any) Expression

func Skip

func Skip() Expression

func (Expression) ToSQL

func (e Expression) ToSQL() (string, []any, error)

type Sqlizer

type Sqlizer interface {
	ToSQL() (string, []any, error)
}

Jump to

Keyboard shortcuts

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