Documentation
¶
Overview ¶
Package ident contains types for safely representing SQL identifiers.
Index ¶
- Variables
- func Comparator[I Identifier]() func(I, I) bool
- func Compare(a, b Identifier) int
- func Equal(a, b Identifier) bool
- func Join(id Identifier, rep Representation, separator rune) string
- func ParseTableRelative(s string, relativeTo Schema) (Table, Qualification, error)
- type Hinted
- type Ident
- func (a Ident) Canonical() Ident
- func (a Ident) Empty() bool
- func (a Ident) Idents(buf []Ident) []Ident
- func (a Ident) MarshalJSON() ([]byte, error)
- func (a Ident) MarshalText() ([]byte, error)
- func (a Ident) Raw() string
- func (a Ident) Split() (Ident, Identifier)
- func (a Ident) String() string
- func (n *Ident) UnmarshalJSON(data []byte) error
- func (n *Ident) UnmarshalText(data []byte) error
- type Identifier
- type IdentifierMap
- func (m *IdentifierMap[I, V]) CopyInto(dest cmap.Map[I, V])
- func (m *IdentifierMap[I, V]) Delete(key I)
- func (m *IdentifierMap[I, V]) Entries() []cmap.Entry[I, V]
- func (m *IdentifierMap[I, V]) Get(key I) (_ V, ok bool)
- func (m *IdentifierMap[I, V]) GetZero(key I) V
- func (m *IdentifierMap[I, V]) Len() int
- func (m *IdentifierMap[I, V]) MarshalJSON() ([]byte, error)
- func (m *IdentifierMap[I, V]) Match(key I) (_ I, _ V, ok bool)
- func (m *IdentifierMap[I, V]) Put(key I, value V)
- func (m *IdentifierMap[I, V]) Range(fn func(k I, v V) error) error
- func (m *IdentifierMap[I, V]) UnmarshalJSON(data []byte) error
- type Idents
- type Map
- type Qualification
- type Representation
- type Schema
- func (s Schema) Canonical() Schema
- func (s Schema) Contains(table Table) bool
- func (a Schema) Empty() bool
- func (a Schema) Idents(buf []Ident) []Ident
- func (a Schema) MarshalJSON() ([]byte, error)
- func (a Schema) MarshalText() ([]byte, error)
- func (a Schema) Raw() string
- func (s Schema) Relative(parts ...Ident) (Schema, Qualification, error)
- func (s Schema) Schema() Schema
- func (a Schema) Split() (Ident, Identifier)
- func (a Schema) String() string
- func (s *Schema) UnmarshalJSON(data []byte) error
- type SchemaFlag
- type SchemaMap
- type Schematic
- type StagingSchema
- type Table
- func (t Table) Canonical() Table
- func (q Table) Empty() bool
- func (q Table) Idents(buf []Ident) []Ident
- func (q Table) MarshalJSON() ([]byte, error)
- func (q Table) MarshalText() ([]byte, error)
- func (q Table) Raw() string
- func (t Table) Schema() Schema
- func (q Table) Split() (Ident, Identifier)
- func (q Table) String() string
- func (t Table) Table() Ident
- func (t *Table) UnmarshalJSON(data []byte) error
- func (t *Table) UnmarshalText(data []byte) error
- type TableMap
- type UDT
- func (q UDT) Empty() bool
- func (q UDT) Idents(buf []Ident) []Ident
- func (t UDT) IsArray() bool
- func (t UDT) MarshalJSON() ([]byte, error)
- func (t *UDT) MarshalText() ([]byte, error)
- func (t UDT) Name() Ident
- func (t UDT) Raw() string
- func (t UDT) Schema() Schema
- func (q UDT) Split() (Ident, Identifier)
- func (t UDT) String() string
- func (t *UDT) UnmarshalJSON(data []byte) error
- type Value
Constants ¶
This section is empty.
Variables ¶
var Public = New("public")
Public is a commonly-used identifier.
Functions ¶
func Comparator ¶
func Comparator[I Identifier]() func(I, I) bool
Comparator returns a comparison function.
func Compare ¶
func Compare(a, b Identifier) int
Compare is similar to strings.Compare. It returns -1, 0, or 1 if a should sort before, equal to, or after b. Nil or empty values sort before any non-nil, non-empty value.
func Equal ¶
func Equal(a, b Identifier) bool
Equal returns true if the identifiers are equal without considering case.
func Join ¶
func Join(id Identifier, rep Representation, separator rune) string
Join returns the identifiers as a string with the given separator.
func ParseTableRelative ¶
func ParseTableRelative(s string, relativeTo Schema) (Table, Qualification, error)
ParseTableRelative parses a table name, relative to a base schema. The string must have no more than the number of name parts in relativeTo, plus one for the table name itself.
Types ¶
type Hinted ¶
type Hinted[I Identifier] struct { Base I Hint string }
Hinted decorates the raw and sql-safe string representations of an Identifier with an extra, target-specific hint string.
func WithHint ¶
func WithHint[I Identifier](id I, hint string) *Hinted[I]
WithHint wraps the identifier with a target-specific hint. An empty hint string is valid.
func (*Hinted[I]) Raw ¶
Raw implements Identifier and concatenates the underlying raw representation with the hint.
func (*Hinted[I]) String ¶
String implements Identifier and concatenates the underlying sql-safe representation with the hint.
type Ident ¶
type Ident struct {
// contains filtered or unexported fields
}
An Ident is a quoted SQL identifier, generally a table, column, or database.
func New ¶
New returns a quoted SQL identifier. Prefer using ParseIdent when operating on user-provided input that may already be quoted.
func ParseIdent ¶
ParseIdent extracts the first, possibly-quoted, Ident from the given string. It returns the parsed Ident and the remainder of the input string. An error is returned if there is an unmatched double-quote character.
func (Ident) Canonical ¶
func (a Ident) Canonical() Ident
Canonical returns a canonical representation of the enclosed ident. That is, it returns a lower-cased form of the SQL identifier.
func (Ident) Empty ¶
func (a Ident) Empty() bool
Empty implements Identifier and returns true if the identifier is empty.
func (Ident) MarshalJSON ¶
MarshalJSON implements Identifier, returning a JSON string.
func (Ident) MarshalText ¶
MarshalText returns the Identifier's raw form, allowing the value to be used as a JSON map-key.
func (Ident) Raw ¶
func (a Ident) Raw() string
Raw implements Identifier and returns the original, raw value.
func (Ident) Split ¶
func (a Ident) Split() (Ident, Identifier)
Split implements Identifier, returning the atom and an empty ident.
func (Ident) String ¶
func (a Ident) String() string
String returns the atom in a manner suitable for constructing a query.
func (*Ident) UnmarshalJSON ¶
UnmarshalJSON converts a raw json string into an Ident.
func (*Ident) UnmarshalText ¶
UnmarshalText converts a raw string into an Ident.
type Identifier ¶
type Identifier interface { encoding.TextMarshaler json.Marshaler // Empty returns true if the Identifier is blank. Empty() bool // Idents appends the components of the Identifier to the buffer and // returns it. It is valid to pass a nil value for the buffer. Idents(buf []Ident) []Ident // Raw returns an unquoted representation of the Identifier. Raw() string // Split returns the first part of a qualified identifier and the // remainder. // // Splitting an unqualified (single-part) identifier returns the // identifier and an empty remainder. // // Splitting an empty Identifier returns two empty Identifiers. Split() (first Ident, remainder Identifier) // String returns a quoted, concatenation-safe representation of the // Identifier that is suitable for use when building queries. String() string }
Identifier represents some, possibly-compound, name in an external system.
type IdentifierMap ¶
type IdentifierMap[I Identifier, V any] struct { // contains filtered or unexported fields }
IdentifierMap implements a case-preserving, but case-insensitive mapping of Identifier instances to values. Prefer using the specialized types instead.
Usage notes:
- The zero value of an IdentifierMap is safe to use.
- An IdentifierMap can be serialized to and from a JSON representation.
- An IdentifierMap is not internally synchronized.
func (*IdentifierMap[I, V]) CopyInto ¶
func (m *IdentifierMap[I, V]) CopyInto(dest cmap.Map[I, V])
CopyInto implements cmap.Map.
func (*IdentifierMap[I, V]) Delete ¶
func (m *IdentifierMap[I, V]) Delete(key I)
Delete implements cmap.Map.
func (*IdentifierMap[I, V]) Entries ¶
func (m *IdentifierMap[I, V]) Entries() []cmap.Entry[I, V]
Entries implements cmap.Map. This will sort the returned slice to ensure stable iteration order.
func (*IdentifierMap[I, V]) Get ¶
func (m *IdentifierMap[I, V]) Get(key I) (_ V, ok bool)
Get implements cmap.Map.
func (*IdentifierMap[I, V]) GetZero ¶
func (m *IdentifierMap[I, V]) GetZero(key I) V
GetZero implements cmap.Map.
func (*IdentifierMap[I, V]) MarshalJSON ¶
func (m *IdentifierMap[I, V]) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (*IdentifierMap[I, V]) Match ¶
func (m *IdentifierMap[I, V]) Match(key I) (_ I, _ V, ok bool)
Match implements cmap.Map.
func (*IdentifierMap[I, V]) Put ¶
func (m *IdentifierMap[I, V]) Put(key I, value V)
Put implements cmap.Map
func (*IdentifierMap[I, V]) Range ¶
func (m *IdentifierMap[I, V]) Range(fn func(k I, v V) error) error
Range implements cmap.Map.
func (*IdentifierMap[I, V]) UnmarshalJSON ¶
func (m *IdentifierMap[I, V]) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler.
type Idents ¶
type Idents []Ident
Idents is a slice of Ident.
type Map ¶
type Map[V any] struct { IdentifierMap[Ident, V] }
Map is a case-insensitive mapping of Ident to values.
type Qualification ¶
type Qualification int
Qualification is a return value from ParseTableRelative, indicating how many name parts were present in the initial input.
const ( TableOnly Qualification = iota + 1 PartialSchema FullyQualified )
Various levels of table-identifier qualification.
func (Qualification) String ¶
func (i Qualification) String() string
type Representation ¶
type Representation bool
Representation selects from a quoted string or a raw string.
const ( // Raw expresses an Identifier as its original text. Raw Representation = true // Quoted expresses an Identifier as a quoted, escaped string. Quoted Representation = false )
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
A Schema identifier is a multipart identifier for a Table container. This type is immutable and suitable for use as a map key.
func MustSchema ¶
MustSchema calls NewSchema and panics if it returns an error. This is intended for use by tests.
func ParseSchema ¶
ParseSchema parses a dot-separated schema name.
func (Schema) Canonical ¶
Canonical returns a canonicalized form of the SQL schema name. That is, it returns a lower-cased form of the enclosed SQL identifiers.
func (Schema) MarshalJSON ¶
MarshalJSON returns the Schema as an array.
func (Schema) MarshalText ¶
MarshalText returns the raw, dotted form of the Schema.
func (Schema) Relative ¶
func (s Schema) Relative(parts ...Ident) (Schema, Qualification, error)
Relative returns a new schema, relative to the receiver. For an input of N parts, the trailing N elements of the receiver will be replaced.
func (Schema) Split ¶
func (a Schema) Split() (Ident, Identifier)
Split returns the first atom as an Ident and an array for the remainder.
func (Schema) String ¶
func (a Schema) String() string
String returns the identifier in a manner suitable for constructing a query.
func (*Schema) UnmarshalJSON ¶
UnmarshalJSON parses an array of strings.
type SchemaFlag ¶
type SchemaFlag Schema
SchemaFlag allows Schema fields to be used with the spf13 flags package.
func NewSchemaFlag ¶
func NewSchemaFlag(id *Schema) *SchemaFlag
NewSchemaFlag wraps the given Schema so that it can be used with the spf13 flags package.
func (*SchemaFlag) String ¶
func (v *SchemaFlag) String() string
String returns the raw value of the underlying Ident.
type SchemaMap ¶
type SchemaMap[V any] struct { IdentifierMap[Schema, V] }
SchemaMap is a case-insensitive mapping of Schema to values.
type Schematic ¶
type Schematic interface { // Schema returns the (enclosing) Schema. Schema() Schema }
Schematic is anything that is or is enclosed by a Schema.
type StagingSchema ¶
type StagingSchema Schema
StagingSchema is a type alias for the name of the "_replicator.public" table schema. It serves as an injection point for uniquely naming the staging database in test cases.
func (StagingSchema) Schema ¶
func (s StagingSchema) Schema() Schema
Schema returns the underying database identifier.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
A Table is an identifier with an enclosing Schema.
func (Table) Canonical ¶
Canonical returns a canonical form of the table identifier. That is, it returns a lower-cased form of the enclosed identifiers.
func (Table) MarshalJSON ¶
MarshalJSON returns the ident as an array.
func (Table) MarshalText ¶
MarshalText returns the raw, dotted form of the Table.
func (Table) Split ¶
func (q Table) Split() (Ident, Identifier)
Split implements Identifier. It returns the first element of the namespace and constructs a new array that contains the terminal atom.
func (Table) String ¶
func (q Table) String() string
String returns the identifier in a manner suitable for constructing a query.
func (*Table) UnmarshalJSON ¶
UnmarshalJSON parses a JSON array.
func (*Table) UnmarshalText ¶
UnmarshalText initializes the Table from its MarshalText representation.
type TableMap ¶
type TableMap[V any] struct { IdentifierMap[Table, V] }
TableMap is a case-insensitive mapping of Table to values.
type UDT ¶
type UDT struct {
// contains filtered or unexported fields
}
A UDT is the name of a user-defined type, such as an enum.
func NewUDTArray ¶
NewUDTArray constructs an identifier for a UDT array.
func (UDT) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*UDT) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (UDT) Split ¶
func (q UDT) Split() (Ident, Identifier)
Split implements Identifier. It returns the first element of the namespace and constructs a new array that contains the terminal atom.
func (*UDT) UnmarshalJSON ¶
UnmarshalJSON parses a JSON array.