sql

package
v0.0.0-...-7625c6f Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package sql implements the logic required to analyze the links between SQL tables (represented as structs in the Go source code).

Index

Constants

View Source
const ExhaustiveSQLTypeSwitch = "ExhaustiveSQLTypeSwitch"

Variables

This section is empty.

Functions

func IsInt64

func IsInt64(ty types.Type) bool

IsInt64 returns true if the underlying type of `ty` is int64

func IsNullXXX

func IsNullXXX(typ *types.Named) *types.Var

IsNullXXX matches types whose underlying type is a struct similar to sql.NullInt64, that is, a struct of the form

	struct {
		Valid bool
 	<Field> <Type>
	}

If so, it returns the data field declaration.

Types

type Array

type Array struct {
	A *an.Array // Elem is *Basic
}

func (Array) Name

func (ar Array) Name() string

func (Array) Type

func (ty Array) Type() an.Type

type Builtin

type Builtin struct {
	// contains filtered or unexported fields
}

Builtin is a SQL type which does not require additional definitions

func (Builtin) IsNullable

func (b Builtin) IsNullable() bool

IsNullable returns `true` if the type comes from a sql.NullXXX type.

func (Builtin) Name

func (b Builtin) Name() string

func (Builtin) Type

func (ty Builtin) Type() an.Type

type Column

type Column struct {
	// SQLType is the resolved SQL type
	SQLType Type

	// Field is the Go struct field yielding this column
	Field an.StructField
}

type Enum

type Enum struct {
	E *an.Enum
}

func (Enum) Name

func (e Enum) Name() string

func (Enum) Type

func (ty Enum) Type() an.Type

type ForeignKey

type ForeignKey struct {
	// F has underlying type int64, sql.NullInt64 or similar
	F an.StructField

	// Target is the foreign table being referenced
	Target TableName

	// IsUnique is true if an SQL UNIQUE constraint
	// was added as special comment
	IsUnique bool
}

ForeignKey is a struct field used as (single) SQL foreign key.

func (ForeignKey) IsNullable

func (fk ForeignKey) IsNullable() bool

IsNullable returns true if the key is optional.

func (ForeignKey) OnDelete

func (fk ForeignKey) OnDelete() string

OnDelete returns the action defined by the tag `gomacro-sql-on-delete:"<action>"`, or an empty string.

func (ForeignKey) TargetIDType

func (fk ForeignKey) TargetIDType() types.Type

TargetIDType returns the type used for the IDs of the target table. For nullable fields, it is the wrapped type, not the wrapper struct.

type JSON

type JSON struct {
	// contains filtered or unexported fields
}

func (JSON) Name

func (JSON) Name() string

func (JSON) Type

func (ty JSON) Type() an.Type

type Table

type Table struct {
	Name *types.Named

	// Columns only exposes exported struct fields.
	Columns []Column

	// CustomComments are the user provided constraints
	// defined with `// gomacro:SQL <constraint>` comments
	CustomConstraints []string
	// contains filtered or unexported fields
}

Table is a Struct used as SQL table.

func NewTable

func NewTable(s *an.Struct) Table

func SelectTables

func SelectTables(ana *an.Analysis) (out []Table)

SelectTables returns the SQL tables found in the given analysis.

func (Table) AdditionalUniqueCols

func (ta Table) AdditionalUniqueCols() [][]Column

`AdditionalUniqueCols` returns the columns which have a UNIQUE constraint. The columns returned by `ForeignKeys` are not included, since they usually require additional handling.

func (Table) ForeignKeys

func (ta Table) ForeignKeys() (out []ForeignKey)

ForeignKeys returns the columns which are references into other tables (sorted by name). They are identified by table ID types or with the gomacro-sql-foreign:"<table>" tag.

func (Table) Primary

func (ta Table) Primary() int

Primary returns the index of the slice element which is the ID field, or -1 if not found

func (Table) SelectKeys

func (ta Table) SelectKeys() [][]Column

SelectKeys returns a list of keys for which 'SelectByXXX' and 'DeleteByXXX' functions should be generated.

func (*Table) TableName

func (ta *Table) TableName() TableName

TableName return the Go table name.

type TableName

type TableName string

TableName is a singular name of a table entity, as found in Go source.

type Type

type Type interface {
	Type() an.Type

	// Name returns the SQL description of the type
	Name() string
}

Directories

Path Synopsis
pq
Package pq is used as replacement for "github.com/lib/pq" in tests
Package pq is used as replacement for "github.com/lib/pq" in tests

Jump to

Keyboard shortcuts

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