cmd

package
v0.0.0-...-fe6f8ac Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package cmd contains the primary logic of the xo command-line application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DbLoaderSchema

func DbLoaderSchema(ctx context.Context) (models.DB, *loader.Loader, string)

DbLoaderSchema returns the database, loader, and schema name from the context.

func LoadColumns

func LoadColumns(ctx context.Context, args *Args, typ *templates.Type) error

LoadColumns loads table/view columns.

func LoadEnumValues

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

LoadEnumValues loads enum values.

func LoadEnums

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

LoadEnums loads enums.

func LoadForeignKeys

func LoadForeignKeys(ctx context.Context, args *Args, tables map[string]*templates.Type) error

LoadForeignKeys loads foreign keys.

func LoadIndexColumns

func LoadIndexColumns(ctx context.Context, args *Args, index *templates.Index) error

LoadIndexColumns loads the index column information.

func LoadIndexes

func LoadIndexes(ctx context.Context, args *Args, tables map[string]*templates.Type) error

LoadIndexes loads index definitions.

func LoadProcParams

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

LoadProcParams loads stored procedure parameters.

func LoadProcs

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

LoadProcs loads stored procedures definitions.

func LoadTableForeignKeys

func LoadTableForeignKeys(ctx context.Context, args *Args, tables map[string]*templates.Type, typ *templates.Type, fkMap map[string]*templates.ForeignKey) error

LoadTableForeignKeys loads foreign key definitions per table.

func LoadTableIndexes

func LoadTableIndexes(ctx context.Context, args *Args, typ *templates.Type, indexes map[string]*templates.Index) error

LoadTableIndexes loads index definitions per table.

func LoadTypes

func LoadTypes(ctx context.Context, args *Args, kind loader.Kind) (map[string]*templates.Type, error)

LoadTypes loads types for the kind (ie, table/view definitions).

func Open

func Open(ctx context.Context, args *Args) (context.Context, error)

Open opens database and associated loader for the specified dsn.

func ParseQueryParams

func ParseQueryParams(query, mask string, interpol bool, delim string, allowInterpol bool) (string, []*templates.QueryParam, error)

ParseQueryParams takes a SQL query and looks for strings in the form of "<delim><name> <type>[,<option>,...]<delim>", replacing them with the supplied mask.

Mask can contain "%d" to indicate current position. The modified query is returned, along with any extracted parameters.

func Run

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

Run runs the code generation.

Types

type Args

type Args struct {
	// Verbose enables verbose output.
	Verbose bool
	// DbParams are database parameters.
	DbParams DbParams
	// 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(ctx context.Context, name, version string) (context.Context, *Args, string, error)

NewArgs creates the command args.

type ContextKey

type ContextKey string

ContextKey is a context key.

const (
	DbKey     ContextKey = "db"
	LoaderKey ContextKey = "loader"
)

Context key values.

type DbParams

type DbParams struct {
	// Schema is the name of the database schema.
	Schema string
	// DSN is the database string (ie, postgres://user:pass@host:5432/dbname?args=)
	DSN string
	// Flags are additional loader flags.
	Flags map[loader.ContextKey]interface{}
}

DbParams are database parameters.

type Generator

type Generator interface {
	Exec(context.Context, *Args) error
	Process(context.Context, *Args) error
}

Generator is the shared interface for generators.

type NoopGenerator

type NoopGenerator struct{}

NoopGenerator is a no op generator.

func NewNoopGenerator

func NewNoopGenerator() *NoopGenerator

NewNoopGenerator creates a new noop generator.

func (*NoopGenerator) Exec

Exec satisfies the Generator interface.

func (*NoopGenerator) Process

func (*NoopGenerator) Process(ctx context.Context, args *Args) error

Process satisfies the Generator interface.

type OutParams

type OutParams struct {
	// Out is the out path.
	Out string
	// Append toggles to append to the existing types.
	Append bool
	// 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 QueryGenerator

type QueryGenerator struct{}

QueryGenerator generates code for custom SQL queries.

Provides a generator that parses a query and writes templates for generated type(s).

func NewQueryGenerator

func NewQueryGenerator() *QueryGenerator

NewQueryGenerator creates a new query generator.

func (*QueryGenerator) Exec

func (*QueryGenerator) Exec(ctx context.Context, args *Args) error

Satisfies the Generator interface.

func (*QueryGenerator) Process

func (*QueryGenerator) Process(ctx context.Context, args *Args) error

Process satisfies the Generator interface.

type QueryParams

type QueryParams struct {
	// Query is the passed query.
	//
	// If not specified, then os.Stdin will be used.
	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
	// 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 toggles results can contain null types.
	AllowNulls bool
}

QueryParams are query parameters.

type SchemaGenerator

type SchemaGenerator struct{}

SchemaGenerator generates code for a database schema.

func NewSchemaGenerator

func NewSchemaGenerator() *SchemaGenerator

NewSchemaGenerator creates a new schema generator.

func (*SchemaGenerator) Exec

func (*SchemaGenerator) Exec(ctx context.Context, args *Args) error

Exec satisfies the Generator interface.

func (*SchemaGenerator) Process

func (*SchemaGenerator) Process(ctx context.Context, args *Args) error

Process satisfies the Generator interface.

type SchemaParams

type SchemaParams struct {
	// FkMode is the foreign resolution mode.
	FkMode string
	// Ignore allows the user to specify field names which should not be
	// handled by xo in the generated code.
	Ignore []string
	// 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
}

SchemaParams are schema parameters.

type TemplateParams

type TemplateParams struct {
	// Type is the name of the template.
	Type string
	// Suffix is the file extension suffix.
	Suffix string
	// Src is the src directory of the template.
	Src string
	// Esc indicates which types to escape (none, schema, table, column, all).
	Esc []string
	// Flags are additional template flags.
	Flags map[templates.ContextKey]interface{}
}

TemplateParams are template parameters.

Jump to

Keyboard shortcuts

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