cmd

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package cmd provides xo command-line application logic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildContext

func BuildContext(ctx context.Context, args *Args) context.Context

BuildContext builds a context for the mode and template.

func DumpCommand

func DumpCommand(ctx context.Context, ts *templates.Set, args *Args) (*cobra.Command, error)

DumpCommand builds the dump command.

func Exec

func Exec(ctx context.Context, mode string, ts *templates.Set, args *Args) func(*cobra.Command, []string) error

Exec handles the execution for query and schema.

func Generate

func Generate(ctx context.Context, mode string, ts *templates.Set, set *xo.Set, args *Args) error

Generate generates the XO files with the provided templates, data, and arguments.

func Introspect

func Introspect(ctx context.Context, query []string, allowNulls, flat bool) ([]xo.Field, error)

Introspect creates a view of a query, introspecting the query's columns and returning as fields.

Creates a temporary view/table, retrieves its column definitions and dropping the temporary view/table.

func LoadColumns

func LoadColumns(ctx context.Context, args *Args, table *xo.Table, enums []xo.Enum) error

LoadColumns loads table/view columns.

func LoadConstraints

func LoadConstraints(ctx context.Context, args *Args) ([]xo.Constraint, error)

LoadConstraints loads constraints for all tables in a schema.

func LoadEnumValues

func LoadEnumValues(ctx context.Context, args *Args, enum *xo.Enum) error

LoadEnumValues loads enum values.

func LoadEnums

func LoadEnums(ctx context.Context, args *Args) ([]xo.Enum, error)

LoadEnums loads enums.

func LoadIndexColumns

func LoadIndexColumns(ctx context.Context, args *Args, table *xo.Table, index *xo.Index) error

LoadIndexColumns loads the index column information.

func LoadProcParams

func LoadProcParams(ctx context.Context, args *Args, proc *xo.Proc) error

LoadProcParams loads stored procedure parameters.

func LoadProcs

func LoadProcs(ctx context.Context, args *Args) ([]xo.Proc, error)

LoadProcs loads stored procedures definitions.

func LoadQuery

func LoadQuery(ctx context.Context, set *xo.Set, args *Args) error

LoadQuery loads a query.

func LoadQueryFields

func LoadQueryFields(ctx context.Context, query []string, fields string, allowNulls, flat bool) ([]xo.Field, error)

LoadQueryFields loads the query type fields.

func LoadSchema

func LoadSchema(ctx context.Context, set *xo.Set, args *Args) error

LoadSchema loads a schema from a database.

func LoadTableForeignKeys

func LoadTableForeignKeys(ctx context.Context, args *Args, tables []xo.Table, table xo.Table) ([]xo.ForeignKey, error)

LoadTableForeignKeys loads foreign key definitions per table.

func LoadTableIndexes

func LoadTableIndexes(ctx context.Context, args *Args, table *xo.Table) error

LoadTableIndexes loads index definitions per table.

func LoadTables

func LoadTables(ctx context.Context, args *Args, typ string, enums []xo.Enum) ([]xo.Table, error)

LoadTables loads types for the type (table, view)

func NewTemplateSet

func NewTemplateSet(ctx context.Context, dir, template string) (*templates.Set, error)

NewTemplateSet creates a new templates set.

func ParseQuery

func ParseQuery(ctx context.Context, sqlstr, delimiter string, interpolate, trim, strip bool) ([]string, []string, []string, []xo.Field, error)

ParseQuery parses a query returning the processed query, a query for introspection, related comments, and extracted params.

func ParseQueryFields

func ParseQueryFields(query, delim string, interpolate, paramInterpolate bool, nth func(int) string) (string, []xo.Field, error)

ParseQueryFields takes a SQL query and looks for strings in the form of "<delim><name> <type>[,<option>,...]<delim>", replacing them with the nth param value.

The modified query is returned, along with any extracted parameters.

func QueryCommand

func QueryCommand(ctx context.Context, ts *templates.Set, args *Args) (*cobra.Command, error)

QueryCommand builds the query command.

func RootCommand

func RootCommand(ctx context.Context, name, version string, ts *templates.Set, args *Args, cmdargs ...string) (*cobra.Command, error)

RootCommand creates the root command.

func Run

func Run(ctx context.Context, name, version string, cmdArgs ...string) error

Run runs the code generation.

func SchemaCommand

func SchemaCommand(ctx context.Context, ts *templates.Set, args *Args) (*cobra.Command, error)

SchemaCommand builds the schema command.

func SplitFields

func SplitFields(s string) ([]xo.Field, error)

SplitFields splits s (comma separated) into fields.

Types

type Args

type Args struct {
	// Verbose enables verbose output.
	Verbose bool
	// LoaderParams are database loader parameters.
	LoaderParams LoaderParams
	// TemplateParams are template parameters.
	TemplateParams TemplateParams
	// QueryParams are query parameters.
	QueryParams QueryParams
	// SchemaParams are schema parameters.
	SchemaParams SchemaParams
	// OutParams are out parameters.
	OutParams OutParams
}

Args contains command-line arguments.

func NewArgs

func NewArgs(name string, names ...string) *Args

NewArgs creates args for the provided template names.

type LoaderParams

type LoaderParams struct {
	// Schema is the name of the database schema.
	Schema string
	// Flags are additional loader flags.
	Flags map[xo.ContextKey]*xo.Value
}

LoaderParams are loader parameters.

type OutParams

type OutParams struct {
	// Out is the out path.
	Out string
	// Single when true changes behavior so that output is to one file.
	Single string
	// Debug toggles direct writing of files to disk, skipping post processing.
	Debug bool
}

OutParams are out parameters.

type QueryParams

type QueryParams struct {
	// Query is the query to introspect.
	Query string
	// Type is the type name.
	Type string
	// TypeComment is the type comment.
	TypeComment string
	// Func is the func name.
	Func string
	// FuncComment is the func comment.
	FuncComment string
	// Trim enables triming whitespace.
	Trim bool
	// Strip enables stripping the '::<type> AS <name>' in queries.
	Strip bool
	// One toggles the generated code to expect only one result.
	One bool
	// Flat toggles the generated code to return all scanned values directly.
	Flat bool
	// Exec toggles the generated code to do a db exec.
	Exec bool
	// Interpolate enables interpolation.
	Interpolate bool
	// Delimiter is the delimiter for parameterized values.
	Delimiter string
	// Fields are the fields to scan the result to.
	Fields string
	// AllowNulls enables results to have null types.
	AllowNulls bool
}

QueryParams are query parameters.

type SchemaParams

type SchemaParams struct {
	// FkMode is the foreign resolution mode.
	FkMode *xo.Value
	// Include allows the user to specify which types should be included. Can
	// match multiple types via regex patterns.
	//
	// - When unspecified, all types are included.
	// - When specified, only types match will be included.
	// - When a type matches an exclude entry and an include entry,
	//   the exclude entry will take precedence.
	Include *xo.Value
	// Exclude allows the user to specify which types should be skipped. Can
	// match multiple types via regex patterns.
	//
	// When unspecified, all types are included in the schema.
	Exclude *xo.Value
	// UseIndexNames toggles using index names.
	//
	// This is not enabled by default, because index names are often generated
	// using database design software which often gives non-descriptive names
	// to indexes (for example, 'authors__b124214__u_idx' instead of the more
	// descriptive 'authors_title_idx').
	UseIndexNames bool
	// Ignore excludes a pattern of columns from inserts and updates,
	// but can be returned by queries (e.g. *.created_at, *.updated_at)
	Ignore *xo.Value
	// MainSchemaPkg determines the package used for enums, etc. if any.
	MainSchemaPkg *xo.Value
}

SchemaParams are schema parameters.

type TemplateParams

type TemplateParams struct {
	// Type is the name of the template.
	Type *xo.Value
	// Src is the src directory of the template.
	Src string
	// Flags are additional template flags.
	Flags map[xo.ContextKey]*xo.Value
}

TemplateParams are template parameters.

Jump to

Keyboard shortcuts

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