entsql

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: Apache-2.0 Imports: 1 Imported by: 347

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Annotation

type Annotation struct {
	// The Table option allows overriding the default table
	// name that is generated by ent. For example:
	//
	//	entsql.Annotation{
	//		Table: "Users",
	//	}
	//
	Table string `json:"table,omitempty"`

	// Charset defines the character-set of the table. For example:
	//
	//	entsql.Annotation{
	//		Charset: "utf8mb4",
	//	}
	//
	Charset string `json:"charset,omitempty"`

	// Collation defines the collation of the table (a set of rules for comparing
	// characters in a character set). For example:
	//
	//	entsql.Annotation{
	//		Collation: "utf8mb4_bin",
	//	}
	//
	Collation string `json:"collation,omitempty"`

	// Default specifies the default value of a column. Note that using this option
	// will override the default behavior of the code-generation. For example:
	//
	//	entsql.Annotation{
	//		Default: "CURRENT_TIMESTAMP",
	//	}
	//
	//	entsql.Annotation{
	//		Default: "uuid_generate_v4()",
	//	}
	//
	Default string `json:"default,omitempty"`

	// Options defines the additional table options. For example:
	//
	//	entsql.Annotation{
	//		Options: "ENGINE = INNODB",
	//	}
	//
	Options string `json:"options,omitempty"`

	// Size defines the column size in the generated schema. For example:
	//
	//	entsql.Annotation{
	//		Size: 128,
	//	}
	//
	Size int64 `json:"size,omitempty"`

	// Incremental defines the auto-incremental behavior of a column. For example:
	//
	//  incrementalEnabled := true
	//  entsql.Annotation{
	//      Incremental: &incrementalEnabled,
	//  }
	//
	// By default, this value is nil defaulting to whatever best fits each scenario.
	//
	Incremental *bool `json:"incremental,omitempty"`

	// OnDelete specifies a custom referential action for DELETE operations on parent
	// table that has matching rows in the child table.
	//
	// For example, in order to delete rows from the parent table and automatically delete
	// their matching rows in the child table, pass the following annotation:
	//
	//	entsql.Annotation{
	//		OnDelete: entsql.Cascade,
	//	}
	//
	OnDelete ReferenceOption `json:"on_delete,omitempty"`

	// Check allows injecting custom "DDL" for setting an unnamed "CHECK" clause in "CREATE TABLE".
	//
	//	entsql.Annotation{
	//		Check: "age < 10",
	//	}
	//
	Check string `json:"check,omitempty"`

	// Checks allows injecting custom "DDL" for setting named "CHECK" clauses in "CREATE TABLE".
	//
	//	entsql.Annotation{
	//		Checks: map[string]string{
	//			"valid_discount": "price > discount_price",
	//		},
	//	}
	//
	Checks map[string]string `json:"checks,omitempty"`
}

Annotation is a builtin schema annotation for attaching SQL metadata to schema objects for both codegen and runtime.

func (Annotation) Merge

func (a Annotation) Merge(other schema.Annotation) schema.Annotation

Merge implements the schema.Merger interface.

func (Annotation) Name

func (Annotation) Name() string

Name describes the annotation name.

type IndexAnnotation added in v0.9.0

type IndexAnnotation struct {
	// Prefix defines a column prefix for a single string column index.
	// In MySQL, the following annotation maps to:
	//
	//	index.Fields("column").
	//		Annotation(entsql.Prefix(100))
	//
	//	CREATE INDEX `table_column` ON `table`(`column`(100))
	//
	Prefix uint

	// PrefixColumns defines column prefixes for a multi-column index.
	// In MySQL, the following annotation maps to:
	//
	//	index.Fields("c1", "c2", "c3").
	//		Annotation(
	//			entsql.PrefixColumn("c1", 100),
	//			entsql.PrefixColumn("c2", 200),
	//		)
	//
	//	CREATE INDEX `table_c1_c2_c3` ON `table`(`c1`(100), `c2`(200), `c3`)
	//
	PrefixColumns map[string]uint

	// Desc defines the DESC clause for a single column index.
	// In MySQL, the following annotation maps to:
	//
	//	index.Fields("column").
	//		Annotation(entsql.Desc())
	//
	//	CREATE INDEX `table_column` ON `table`(`column` DESC)
	//
	Desc bool

	// DescColumns defines the DESC clause for columns in a multi column index.
	// In MySQL, the following annotation maps to:
	//
	//	index.Fields("c1", "c2", "c3").
	//		Annotation(
	//			entsql.DescColumns("c1", "c2"),
	//		)
	//
	//	CREATE INDEX `table_c1_c2_c3` ON `table`(`c1` DESC, `c2` DESC, `c3`)
	//
	DescColumns map[string]bool

	// Type defines the type of the index.
	// In MySQL, the following annotation maps to:
	//
	//	index.Fields("c1").
	//		Annotation(
	//			entsql.IndexType("FULLTEXT"),
	//		)
	//
	//	CREATE FULLTEXT INDEX `table_c1` ON `table`(`c1`)
	//
	Type string

	// Types is like the Type option but allows mapping an index-type per dialect.
	//
	//	index.Fields("c1").
	//		Annotation(
	//			entsql.IndexTypes(map[string]string{
	//				dialect.MySQL:		"FULLTEXT",
	//				dialect.Postgres:	"GIN",
	//			}),
	//		)
	//
	Types map[string]string
}

IndexAnnotation is a builtin schema annotation for attaching SQL metadata to schema indexes for both codegen and runtime.

func Desc added in v0.10.1

func Desc() *IndexAnnotation

Desc returns a new index annotation with the DESC clause for a single column index. In MySQL, the following annotation maps to:

index.Fields("column").
	Annotation(entsql.Desc())

CREATE INDEX `table_column` ON `table`(`column` DESC)

func DescColumns added in v0.10.1

func DescColumns(names ...string) *IndexAnnotation

DescColumns returns a new index annotation with the DESC clause attached to the columns in the index. In MySQL, the following annotation maps to:

index.Fields("c1", "c2", "c3").
	Annotation(
		entsql.DescColumns("c1", "c2"),
	)

CREATE INDEX `table_c1_c2_c3` ON `table`(`c1` DESC, `c2` DESC, `c3`)

func IndexType added in v0.10.1

func IndexType(t string) *IndexAnnotation

Type defines the type of the index. In MySQL, the following annotation maps to:

index.Fields("c1").
	Annotation(
		entsql.IndexType("FULLTEXT"),
	)

CREATE FULLTEXT INDEX `table_c1` ON `table`(`c1`)

func IndexTypes added in v0.10.1

func IndexTypes(types map[string]string) *IndexAnnotation

Types is like the Type option but allows mapping an index-type per dialect.

index.Fields("c1").
	Annotations(
		entsql.IndexTypes(map[string]string{
			dialect.MySQL:    "FULLTEXT",
			dialect.Postgres: "GIN",
		}),
	)

func Prefix added in v0.9.0

func Prefix(prefix uint) *IndexAnnotation

Prefix returns a new index annotation with a single string column index. In MySQL, the following annotation maps to:

index.Fields("column").
	Annotation(entsql.Prefix(100))

CREATE INDEX `table_column` ON `table`(`column`(100))

func PrefixColumn added in v0.9.0

func PrefixColumn(name string, prefix uint) *IndexAnnotation

PrefixColumn returns a new index annotation with column prefix for multi-column indexes. In MySQL, the following annotation maps to:

index.Fields("c1", "c2", "c3").
	Annotation(
		entsql.PrefixColumn("c1", 100),
		entsql.PrefixColumn("c2", 200),
	)

CREATE INDEX `table_c1_c2_c3` ON `table`(`c1`(100), `c2`(200), `c3`)

func (IndexAnnotation) Merge added in v0.9.0

Merge implements the schema.Merger interface.

func (IndexAnnotation) Name added in v0.9.0

func (IndexAnnotation) Name() string

Name describes the annotation name.

type ReferenceOption added in v0.8.0

type ReferenceOption string

ReferenceOption for constraint actions.

const (
	NoAction   ReferenceOption = "NO ACTION"
	Restrict   ReferenceOption = "RESTRICT"
	Cascade    ReferenceOption = "CASCADE"
	SetNull    ReferenceOption = "SET NULL"
	SetDefault ReferenceOption = "SET DEFAULT"
)

Reference options (actions) specified by ON UPDATE and ON DELETE subclauses of the FOREIGN KEY clause.

Jump to

Keyboard shortcuts

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