styling

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AliasStyle

type AliasStyle int

AliasStyle defines how SQL aliases are rendered in dialect-specific implementations.

const (
	// AliasNone disables aliasing entirely.
	AliasNone AliasStyle = iota

	// AliasWithoutKeyword renders alias without the AS keyword (e.g., "users u").
	AliasWithoutKeyword

	// AliasWithKeyword renders alias using the AS keyword (e.g., "users AS u").
	AliasWithKeyword
)

func (AliasStyle) Format

func (a AliasStyle) Format(base, alias string) string

Format returns the SQL alias representation for a given base identifier and alias, based on the AliasStyle.

If alias is empty or the style is AliasNone, only the base is returned. Example:

AliasWithKeyword.Format("users", "u") → "users AS u"
AliasWithoutKeyword.Format("users", "u") → "users u"

func (AliasStyle) FormatWith

func (a AliasStyle) FormatWith(q IdentifierQuoter, base, alias string) string

FormatWith renders the alias expression using the provided IdentifierQuoter. It applies the alias style by quoting both the base and alias using q.QuoteIdentifier(), and formatting them according to the AliasStyle.

If alias is empty or AliasStyle is AliasNone, only the quoted base is returned. If q is nil, identifiers are returned unquoted.

Example:

var style AliasStyle = AliasWithKeyword
var q IdentifierQuoter = BaseDialect{
    QuoteStyle: QuoteDouble,
}

style.FormatWith(q, "users", "u")
// Output: `"users" AS "u"`

style = AliasWithoutKeyword
style.FormatWith(q, "users", "u")
// Output: `"users" "u"`

style = AliasNone
style.FormatWith(q, "users", "u")
// Output: `"users"`

Since: v1.5.0

func (AliasStyle) IsValid

func (a AliasStyle) IsValid() bool

IsValid returns true if the AliasStyle is a recognized alias rendering option. Valid values include:

  • AliasNone: no aliasing
  • AliasWithoutKeyword: renders as "table alias"
  • AliasWithKeyword: renders as "table AS alias"

Used in BaseDialect.Validate to ensure table/column aliasing is properly configured.

type IdentifierQuoter

type IdentifierQuoter interface {
	// QuoteIdentifier returns the dialect-specific representation of an identifier,
	// applying the appropriate quoting strategy (e.g., "name", `name`, [name]).
	QuoteIdentifier(name string) string
}

IdentifierQuoter defines the minimal quoting capability required for alias or token formatting.

This interface is used to decouple the styling logic (e.g., FormatWith) from the full Dialect interface, avoiding circular dependencies between styling and dialect packages.

Any type (such as BaseDialect) that implements:

func QuoteIdentifier(name string) string

can satisfy this interface and be passed to FormatWith methods in styling utilities.

Example:

func (a AliasStyle) FormatWith(q IdentifierQuoter, base, alias string) string {
    return fmt.Sprintf("%s AS %s", q.QuoteIdentifier(base), q.QuoteIdentifier(alias))
}

type PlaceholderStyle

type PlaceholderStyle int

PlaceholderStyle defines how SQL parameter placeholders are rendered in parameterized queries. Each style corresponds to a different SQL dialect.

It determines how values are substituted during query preparation, e.g., using "?", "$1", or ":name" depending on the engine.

The zero value is PlaceholderUnset, which indicates that the style is not configured and must be explicitly set.

Example:

switch dialect.Placeholder {
case PlaceholderQuestion:
    return "?"

case PlaceholderDollar:
    return fmt.Sprintf("$%d", index+1)

case PlaceholderNamed:
    return fmt.Sprintf(":%s", name)

case PlaceholderAt:
    return fmt.Sprintf("@%s", name)
}
const (
	// PlaceholderUnset indicates the placeholder style is not configured.
	// This is the default zero value and is invalid during dialect validation.
	PlaceholderUnset PlaceholderStyle = iota

	// PlaceholderQuestion uses "?" — unnumbered positional.
	// Common in MySQL, SQLite, and other lightweight engines.
	PlaceholderQuestion

	// PlaceholderDollar uses "$1", "$2", etc. — numbered positional.
	// Used in PostgreSQL.
	PlaceholderDollar

	// PlaceholderNamed uses ":name" — named parameters.
	// Typical for Oracle, older DB2, and various ORM interfaces.
	PlaceholderNamed

	// PlaceholderAt uses "@name" — alternate named parameter style.
	// Used in SQL Server, Sybase, and some ADO-based engines.
	PlaceholderAt
)

func (PlaceholderStyle) Format

func (p PlaceholderStyle) Format(index int) string

Format returns a placeholder string based on the given positional index. This applies only to positional styles (Question, Dollar).

Example:

PlaceholderQuestion.Format(1) → "?"
PlaceholderDollar.Format(3)   → "$3"

func (PlaceholderStyle) FormatNamed

func (p PlaceholderStyle) FormatNamed(name string) string

FormatNamed returns a placeholder for named styles like ":name" or "@name". Positional styles will default to "?".

Example:

PlaceholderNamed.FormatNamed("id") → ":id"
PlaceholderAt.FormatNamed("uid")   → "@uid"

func (PlaceholderStyle) IsValid

func (p PlaceholderStyle) IsValid() bool

IsValid returns true if the PlaceholderStyle is a defined value. Valid values include:

  • PlaceholderQuestion: "?"
  • PlaceholderDollar: "$1"
  • PlaceholderNamed: ":name"
  • PlaceholderAt: "@name"

Used in BaseDialect.Validate to ensure placeholder formatting is configured.

type QuoteStyle

type QuoteStyle uint8

QuoteStyle defines how SQL identifiers (e.g., table or column names) are quoted by a dialect to prevent conflicts with reserved keywords or support case-sensitivity.

Dialects use this enum to determine how to wrap identifiers during query generation. For example, a column named "user" may be quoted as `"user"` or `[user]` depending on the database engine.

Validation Behavior

The zero value of QuoteStyle is `QuoteUnset`, which is considered invalid during dialect validation. All dialects must explicitly set a valid quote style.

Example

func (d *MyDialect) QuoteIdentifier(s string) string {
    switch d.Quote {
    case QuoteDouble:
        return `"` + s + `"`
    case QuoteBacktick:
        return "`" + s + "`"
    case QuoteBracket:
        return "[" + s + "]"
    case QuoteNone:
        return s
    default:
        panic("unsupported quote style")
    }
}
const (
	// QuoteUnset represents the unset state.
	// This is the default zero value and is considered invalid during dialect validation.
	QuoteUnset QuoteStyle = iota // 0

	// QuoteDouble uses double quotes (e.g., "column") — common in PostgreSQL, SQLite, etc.
	QuoteDouble // 1

	// QuoteBacktick uses backticks (e.g., `column`) — used in MySQL and MariaDB.
	QuoteBacktick // 2

	// QuoteBracket uses square brackets (e.g., [column]) — used in SQL Server.
	QuoteBracket // 3

	// QuoteNone applies no quoting to identifiers — useful for simple dialects like generic SQL.
	QuoteNone // 4
)

func (QuoteStyle) IsValid

func (q QuoteStyle) IsValid() bool

IsValid returns true if the QuoteStyle is one of the known quoting options. Valid values include:

  • QuoteNone: no quoting
  • QuoteDouble: "identifier"
  • QuoteBacktick: `identifier`
  • QuoteBracket: [identifier]

Ensures that identifier quoting is correctly set by the dialect.

func (QuoteStyle) Quote

func (q QuoteStyle) Quote(identifier string) string

Quote returns the quoted version of the given identifier, based on the configured QuoteStyle.

Examples:

QuoteDouble.Quote("id")   → `"id"`
QuoteBacktick.Quote("id") → "`id"`
QuoteBracket.Quote("id")  → `[id]`
QuoteNone.Quote("id")     → `id`

Jump to

Keyboard shortcuts

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