package specutil
import "ariga.io/atlas/sql/internal/specutil"
Index ¶
- Variables
- func Check(spec *sqlspec.Check) (*schema.Check, error)
- func Column(spec *sqlspec.Column, conv ConvertTypeFunc) (*schema.Column, error)
- func ColumnByRef(tv interface { Column(string) (*schema.Column, bool) }, ref *schemahcl.Ref) (*schema.Column, error)
- func ColumnDefault(c *schema.Column) (cty.Value, error)
- func ColumnRef(cName string) *schemahcl.Ref
- func ConvertGenExpr(r *schemahcl.Resource, c *schema.Column, t func(string) string) error
- func Default(d cty.Value) (schema.Expr, error)
- func ExternalColumnRef(cName, tName string) *schemahcl.Ref
- func FromCheck(s *schema.Check) *sqlspec.Check
- func FromColumn(c *schema.Column, columnTypeSpec ColumnTypeSpecFunc) (*sqlspec.Column, error)
- func FromForeignKey(s *schema.ForeignKey) (*sqlspec.ForeignKey, error)
- func FromGenExpr(x schema.GeneratedExpr, t func(string) string) *schemahcl.Resource
- func FromIndex(idx *schema.Index, partFns ...func(*schema.Index, *schema.IndexPart, *sqlspec.IndexPart) error) (*sqlspec.Index, error)
- func FromPrimaryKey(s *schema.Index) (*sqlspec.PrimaryKey, error)
- func FromTable(t *schema.Table, colFn TableColumnSpecFunc, pkFn PrimaryKeySpecFunc, idxFn IndexSpecFunc, fkFn ForeignKeySpecFunc, ckFn CheckSpecFunc) (*sqlspec.Table, error)
- func FromVar(s string) string
- func FromView(v *schema.View, colFn ViewColumnSpecFunc, idxFn IndexSpecFunc) (*sqlspec.View, error)
- func HCLBytesFunc(ev schemahcl.Evaluator) func(b []byte, v any, inp map[string]cty.Value) error
- func Index(spec *sqlspec.Index, parent *schema.Table, partFns ...func(*sqlspec.IndexPart, *schema.IndexPart) error) (*schema.Index, error)
- func IndexByRef(tv interface { Index(string) (*schema.Index, bool) }, ref *schemahcl.Ref) (*schema.Index, error)
- func IndexRef(name string) *schemahcl.Ref
- func Marshal(v any, marshaler schemahcl.Marshaler, funcs RealmFuncs) ([]byte, error)
- func ObjectRef(s *schema.Schema, o SpecTypeNamer) *schemahcl.Ref
- func PrimaryKey(spec *sqlspec.PrimaryKey, parent *schema.Table) (*schema.Index, error)
- func QualifiedExternalColRef(cName, tName, sName string) *schemahcl.Ref
- func QualifyObjects[T SchemaObject](specs []T) error
- func QualifyReferences(tableSpecs []*sqlspec.Table, realm *schema.Realm) error
- func RefName(ref *schemahcl.Ref, typeName string) (qualifier, name string, err error)
- func Scan(r *schema.Realm, doc *ScanDoc, funcs *ScanFuncs) error
- func SchemaName(ref *schemahcl.Ref) (string, error)
- func SchemaRef(name string) *schemahcl.Ref
- func Table(spec *sqlspec.Table, parent *schema.Schema, convertColumn ConvertTableColumnFunc, convertPK ConvertPrimaryKeyFunc, convertIndex ConvertIndexFunc, convertCheck ConvertCheckFunc) (*schema.Table, error)
- func TableName(ref *schemahcl.Ref) (string, string, error)
- func TableSpecRef(t *schema.Table) *schemahcl.Ref
- func TypeAttr(k string, t *schemahcl.Type) *schemahcl.Attr
- func Var(s string) string
- func VarAttr(k, v string) *schemahcl.Attr
- func View(spec *sqlspec.View, parent *schema.Schema, convertC ConvertViewColumnFunc, convertI ConvertViewIndexFunc) (*schema.View, error)
- type Attrer
- type CheckSpecFunc
- type ColumnTypeSpecFunc
- type ConvertCheckFunc
- type ConvertFuncFunc
- type ConvertIndexFunc
- type ConvertPrimaryKeyFunc
- type ConvertProcFunc
- type ConvertTableColumnFunc
- type ConvertTableFunc
- type ConvertTypeFunc
- type ConvertViewColumnFunc
- type ConvertViewFunc
- type ConvertViewIndexFunc
- type Doc
- type ForeignKeySpecFunc
- type IndexSpecFunc
- type PrimaryKeySpecFunc
- type RealmFuncs
- type RefNamer
- type ScanDoc
- type ScanFuncs
- type SchemaFuncs
- type SchemaObject
- type SchemaSpec
- type SpecTypeNamer
- type TableColumnSpecFunc
- type TableSpecFunc
- type ViewColumnSpecFunc
- type ViewSpecFunc
Variables ¶
var ReferenceVars = []string{ Var(string(schema.NoAction)), Var(string(schema.Restrict)), Var(string(schema.Cascade)), Var(string(schema.SetNull)), Var(string(schema.SetDefault)), }
ReferenceVars holds the HCL variables for foreign keys' referential-actions.
Functions ¶
func Check ¶
Check converts a sqlspec.Check to a schema.Check.
func Column ¶
Column converts a sqlspec.Column into a schema.Column.
func ColumnByRef ¶
func ColumnByRef(tv interface { Column(string) (*schema.Column, bool) }, ref *schemahcl.Ref) (*schema.Column, error)
ColumnByRef returns a column from the table by its reference.
func ColumnDefault ¶
ColumnDefault converts the column default into cty.Value.
func ColumnRef ¶
ColumnRef returns the reference of a column by its name.
func ConvertGenExpr ¶
ConvertGenExpr converts the "as" attribute or the block under the given resource.
func Default ¶
Default converts a cty.Value (as defined in the spec) into a schema.Expr.
func ExternalColumnRef ¶
ExternalColumnRef returns the reference of a column by its name and table name.
func FromCheck ¶
FromCheck converts schema.Check to sqlspec.Check.
func FromColumn ¶
FromColumn converts a *schema.Column into a *sqlspec.Column using the ColumnTypeSpecFunc.
func FromForeignKey ¶
func FromForeignKey(s *schema.ForeignKey) (*sqlspec.ForeignKey, error)
FromForeignKey converts schema.ForeignKey to sqlspec.ForeignKey.
func FromGenExpr ¶
FromGenExpr returns the spec for a generated expression.
func FromIndex ¶
func FromIndex(idx *schema.Index, partFns ...func(*schema.Index, *schema.IndexPart, *sqlspec.IndexPart) error) (*sqlspec.Index, error)
FromIndex converts schema.Index to sqlspec.Index.
func FromPrimaryKey ¶
func FromPrimaryKey(s *schema.Index) (*sqlspec.PrimaryKey, error)
FromPrimaryKey converts schema.Index to a sqlspec.PrimaryKey.
func FromTable ¶
func FromTable(t *schema.Table, colFn TableColumnSpecFunc, pkFn PrimaryKeySpecFunc, idxFn IndexSpecFunc, fkFn ForeignKeySpecFunc, ckFn CheckSpecFunc) (*sqlspec.Table, error)
FromTable converts a schema.Table to a sqlspec.Table.
func FromVar ¶
FromVar is the inverse function of Var.
func FromView ¶
func FromView(v *schema.View, colFn ViewColumnSpecFunc, idxFn IndexSpecFunc) (*sqlspec.View, error)
FromView converts a schema.View to a sqlspec.View.
func HCLBytesFunc ¶
HCLBytesFunc returns a helper that evaluates an HCL document from a byte slice instead of from an hclparse.Parser instance.
func Index ¶
func Index(spec *sqlspec.Index, parent *schema.Table, partFns ...func(*sqlspec.IndexPart, *schema.IndexPart) error) (*schema.Index, error)
Index converts a sqlspec.Index to a schema.Index. The optional arguments allow passing functions for mutating the created index-part (e.g. add attributes).
func IndexByRef ¶
func IndexByRef(tv interface { Index(string) (*schema.Index, bool) }, ref *schemahcl.Ref) (*schema.Index, error)
IndexByRef returns a index from the table/view by its reference.
func IndexRef ¶
IndexRef returns the reference of a index by its name.
func Marshal ¶
Marshal marshals v into an Atlas DDL document using a schemahcl.Marshaler. Marshal uses the given schemaSpec function to convert a *schema.Schema into *sqlspec.Schema, []*sqlspec.Table and []*sqlspec.View.
func ObjectRef ¶
func ObjectRef(s *schema.Schema, o SpecTypeNamer) *schemahcl.Ref
ObjectRef returns a reference to the object. In case there is more than one object of this type with the same name, the reference will be qualified with the schema name.
func PrimaryKey ¶
PrimaryKey converts a sqlspec.PrimaryKey to a schema.Index.
func QualifiedExternalColRef ¶
QualifiedExternalColRef returns the reference of a column by its name and qualified table name.
func QualifyObjects ¶
func QualifyObjects[T SchemaObject](specs []T) error
QualifyObjects sets the Qualifier field equal to the schema name in any objects with duplicate names in the provided specs.
func QualifyReferences ¶
QualifyReferences qualifies any reference with qualifier.
func RefName ¶
RefName returns the qualifier and name from a reference.
func Scan ¶
Scan populates the Realm from the schemas and table specs.
func SchemaName ¶
SchemaName returns the name from a ref to a schema.
func SchemaRef ¶
SchemaRef returns the schemahcl.Ref to the schema with the given name.
func Table ¶
func Table(spec *sqlspec.Table, parent *schema.Schema, convertColumn ConvertTableColumnFunc, convertPK ConvertPrimaryKeyFunc, convertIndex ConvertIndexFunc, convertCheck ConvertCheckFunc) (*schema.Table, error)
Table converts a sqlspec.Table to a schema.Table. Table conversion is done without converting ForeignKeySpecs into ForeignKeys, as the target tables do not necessarily exist in the schema at this point. Instead, the linking is done by the Schema function.
func TableName ¶
TableName returns the qualifier and name from a reference to a table.
func TableSpecRef ¶
TableSpecRef returns a reference to the table in the spec. In case there is more than one table with the same name, the reference will be qualified with the schema name.
func TypeAttr ¶
TypeAttr is a helper method for constructing *schemahcl.Attr instances that contain a type reference.
func Var ¶
Var formats a string as variable to make it HCL compatible. The result is simple, replace each space with underscore.
func VarAttr ¶
VarAttr is a helper method for constructing *schemahcl.Attr instances that contain a variable reference.
func View ¶
func View(spec *sqlspec.View, parent *schema.Schema, convertC ConvertViewColumnFunc, convertI ConvertViewIndexFunc) (*schema.View, error)
View converts a sqlspec.View to a schema.View.
Types ¶
type Attrer ¶
Attrer is the interface that wraps the Attr method.
type CheckSpecFunc ¶
List of convert function types.
type ColumnTypeSpecFunc ¶
List of convert function types.
type ConvertCheckFunc ¶
List of convert function types.
type ConvertFuncFunc ¶
List of convert function types.
type ConvertIndexFunc ¶
List of convert function types.
type ConvertPrimaryKeyFunc ¶
List of convert function types.
type ConvertProcFunc ¶
List of convert function types.
type ConvertTableColumnFunc ¶
List of convert function types.
type ConvertTableFunc ¶
List of convert function types.
type ConvertTypeFunc ¶
List of convert function types.
type ConvertViewColumnFunc ¶
List of convert function types.
type ConvertViewFunc ¶
List of convert function types.
type ConvertViewIndexFunc ¶
List of convert function types.
type Doc ¶
type Doc struct { Tables []*sqlspec.Table `spec:"table"` Views []*sqlspec.View `spec:"view"` Materialized []*sqlspec.View `spec:"materialized"` Funcs []*sqlspec.Func `spec:"function"` Procs []*sqlspec.Func `spec:"procedure"` Triggers []*sqlspec.Trigger `spec:"trigger"` Schemas []*sqlspec.Schema `spec:"schema"` }
Doc represents the common HCL spec document.
type ForeignKeySpecFunc ¶
type ForeignKeySpecFunc func(*schema.ForeignKey) (*sqlspec.ForeignKey, error)
List of convert function types.
type IndexSpecFunc ¶
List of convert function types.
type PrimaryKeySpecFunc ¶
type PrimaryKeySpecFunc func(*schema.Index) (*sqlspec.PrimaryKey, error)
List of convert function types.
type RealmFuncs ¶
type RealmFuncs struct { Schema func(*schema.Schema) (*SchemaSpec, error) Triggers func([]*schema.Trigger, *Doc) ([]*sqlspec.Trigger, error) }
RealmFuncs represents the functions that used to convert the schema.Realm into HCL spec document.
type RefNamer ¶
RefNamer is an interface for objects that can return their reference.
type ScanDoc ¶
type ScanDoc struct { Schemas []*sqlspec.Schema Tables []*sqlspec.Table Views []*sqlspec.View Materialized []*sqlspec.View Funcs []*sqlspec.Func Procs []*sqlspec.Func Triggers []*sqlspec.Trigger }
ScanDoc represents a scanned HCL document.
type ScanFuncs ¶
type ScanFuncs struct { Table ConvertTableFunc View ConvertViewFunc Func ConvertFuncFunc Proc ConvertProcFunc // Triggers add themselves to the relevant tables/views. Triggers func(*schema.Realm, []*sqlspec.Trigger) error // Objects add themselves to the realm. Objects func(*schema.Realm) error // Optional function to extend the foreign keys. ForeignKey func(*sqlspec.ForeignKey, *schema.ForeignKey) error }
ScanFuncs represents a set of scan functions used to convert the HCL document to the Realm.
type SchemaFuncs ¶
type SchemaFuncs struct { Table TableSpecFunc View ViewSpecFunc Func func(*schema.Func) (*sqlspec.Func, error) Proc func(*schema.Proc) (*sqlspec.Func, error) }
SchemaFuncs represents a set of spec functions used to convert the Schema object to an HCL document.
type SchemaObject ¶
type SchemaObject interface { Label() string QualifierLabel() string SetQualifier(string) SchemaRef() *schemahcl.Ref }
SchemaObject describes a top-level schema object that might be qualified, e.g. a table or a view.
type SchemaSpec ¶
type SchemaSpec struct { Schema *sqlspec.Schema Tables []*sqlspec.Table Views []*sqlspec.View Funcs []*sqlspec.Func Procs []*sqlspec.Func Materialized []*sqlspec.View // Collected triggers to convert into spec. Triggers []*schema.Trigger }
SchemaSpec is returned by driver convert functions to marshal a *schema.Schema into top-level spec objects.
func FromSchema ¶
func FromSchema(s *schema.Schema, funcs *SchemaFuncs) (*SchemaSpec, error)
FromSchema converts a schema.Schema into sqlspec.Schema and []sqlspec.Table.
type SpecTypeNamer ¶
type SpecTypeNamer interface { // SpecType returns the spec type of the object. SpecType() string // SpecName returns the spec name of the object. SpecName() string }
SpecTypeNamer is an interface for objects that can return their spec type and name.
type TableColumnSpecFunc ¶
List of convert function types.
type TableSpecFunc ¶
List of convert function types.
type ViewColumnSpecFunc ¶
List of convert function types.
type ViewSpecFunc ¶
List of convert function types.
Source Files ¶
convert.go spec.go
- Version
- v0.32.0 (latest)
- Published
- Mar 10, 2025
- Platform
- linux/amd64
- Imports
- 11 packages
- Last checked
- 1 month ago –
Tools for package owners.