Package squirrel provides a fluent SQL generator. See https://github.com/Masterminds/squirrel for examples.
package squirrel

import (
	
	
	
	

	
)
Sqlizer is the interface that wraps the ToSql method. ToSql returns a SQL representation of the Sqlizer, along with a slice of args as passed to e.g. database/sql.Exec. It can also return an error.
type Sqlizer interface {
	ToSql() (string, []interface{}, error)
}
rawSqlizer is expected to do what Sqlizer does, but without finalizing placeholders. This is useful for nested queries.
type rawSqlizer interface {
	toSqlRaw() (string, []interface{}, error)
}
Execer is the interface that wraps the Exec method. Exec executes the given query as implemented by database/sql.Exec.
type Execer interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
}
Queryer is the interface that wraps the Query method. Query executes the given query as implemented by database/sql.Query.
type Queryer interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
}
QueryRower is the interface that wraps the QueryRow method. QueryRow executes the given query as implemented by database/sql.QueryRow.
type QueryRower interface {
	QueryRow(query string, args ...interface{}) RowScanner
}
BaseRunner groups the Execer and Queryer interfaces.
type BaseRunner interface {
	Execer
	Queryer
}
Runner groups the Execer, Queryer, and QueryRower interfaces.
type Runner interface {
	Execer
	Queryer
	QueryRower
}
WrapStdSql wraps a type implementing the standard SQL interface with methods that squirrel expects.
func ( StdSql) Runner {
	return &stdsqlRunner{}
}
StdSql encompasses the standard methods of the *sql.DB type, and other types that wrap these methods.
type StdSql interface {
	Query(string, ...interface{}) (*sql.Rows, error)
	QueryRow(string, ...interface{}) *sql.Row
	Exec(string, ...interface{}) (sql.Result, error)
}

type stdsqlRunner struct {
	StdSql
}

func ( *stdsqlRunner) ( string,  ...interface{}) RowScanner {
	return .StdSql.QueryRow(, ...)
}

func ( interface{},  BaseRunner) interface{} {
	switch r := .(type) {
	case StdSqlCtx:
		 = WrapStdSqlCtx()
	case StdSql:
		 = WrapStdSql()
	}
	return builder.Set(, "RunWith", )
}
RunnerNotSet is returned by methods that need a Runner if it isn't set.
var RunnerNotSet = fmt.Errorf("cannot run; no Runner set (RunWith)")
RunnerNotQueryRunner is returned by QueryRow if the RunWith value doesn't implement QueryRower.
var RunnerNotQueryRunner = fmt.Errorf("cannot QueryRow; Runner is not a QueryRower")
ExecWith Execs the SQL returned by s with db.
func ( Execer,  Sqlizer) ( sql.Result,  error) {
	, ,  := .ToSql()
	if  != nil {
		return
	}
	return .Exec(, ...)
}
QueryWith Querys the SQL returned by s with db.
func ( Queryer,  Sqlizer) ( *sql.Rows,  error) {
	, ,  := .ToSql()
	if  != nil {
		return
	}
	return .Query(, ...)
}
QueryRowWith QueryRows the SQL returned by s with db.
func ( QueryRower,  Sqlizer) RowScanner {
	, ,  := .ToSql()
	return &Row{RowScanner: .QueryRow(, ...), err: }
}
DebugSqlizer calls ToSql on s and shows the approximate SQL to be executed If ToSql returns an error, the result of this method will look like: "[ToSql error: %s]" or "[DebugSqlizer error: %s]" IMPORTANT: As its name suggests, this function should only be used for debugging. While the string result *might* be valid SQL, this function does not try very hard to ensure it. Additionally, executing the output of this function with any untrusted user input is certainly insecure.
func ( Sqlizer) string {
	, ,  := .ToSql()
	if  != nil {
		return fmt.Sprintf("[ToSql error: %s]", )
	}

	var  string
	,  := .(placeholderDebugger)
	if ! {
		 = "?"
	} else {
		 = .debugPlaceholder()
TODO: dedupe this with placeholder.go
	 := &bytes.Buffer{}
	 := 0
	for {
		 := strings.Index(, )
		if  == -1 {
			break
		}
		if len([:]) > 1 && [:+2] == "??" { // escape ?? => ?
			.WriteString([:])
			.WriteString("?")
			if len([:]) == 1 {
				break
			}
			 = [+2:]
		} else {
			if +1 > len() {
				return fmt.Sprintf(
					"[DebugSqlizer error: too many placeholders in %#v for %d args]",
					, len())
			}
			.WriteString([:])
advance our sql string "cursor" beyond the arg we placed
			 = [+1:]
			++
		}
	}
	if  < len() {
		return fmt.Sprintf(
			"[DebugSqlizer error: not enough placeholders in %#v for %d args]",
			, len())
"append" any remaning sql that won't need interpolating
	.WriteString()
	return .String()