internal

package
v0.0.0-...-c4aa40a Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2016 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IntRE = regexp.MustCompile(`^int(32|64)?$`)

IntRE matches Go int types.

View Source
var PrecScaleRE = regexp.MustCompile(`\(([0-9]+)(\s*,[0-9]+)?\)$`)

PrecScaleRE is the regexp that matches "(precision[,scale])" definitions in a database.

View Source
var SchemaLoaders = map[string]Loader{}

SchemaLoaders are the available schema loaders.

Functions

func GenRandomID

func GenRandomID() string

GenRandomID generates a 8 character random string.

func SnakeToCamel

func SnakeToCamel(s string) string

SnakeToCamel provides a safer version of snaker.SnakeToCamel

Types

type ArgType

type ArgType struct {
	// Verbose enables verbose output.
	Verbose bool `arg:"-v,help:toggle verbose"`

	// DSN is the database string (ie, pgsql://user@blah:localhost:5432/dbname?args=)
	DSN string `arg:"positional,required,help:data source name"`

	// Schema is the name of the schema to query.
	Schema string `arg:"-s,help:schema name to generate Go types for"`

	// Out is the output path. If Out is a file, then that will be used as the
	// path. If Out is a directory, then the output file will be
	// Out/<$CWD>.xo.go
	Out string `arg:"-o,help:output path or file name"`

	// Append toggles to append to the existing types.
	Append bool `arg:"-a,help:append to existing files"`

	// Suffix is the output suffix for filenames.
	Suffix string `arg:"-f,help:output file suffix"`

	// SingleFile when toggled changes behavior so that output is to one f ile.
	SingleFile bool `arg:"--single-file,help:toggle single file output"`

	// Package is the name used to generate package headers. If not specified,
	// the name of the output directory will be used instead.
	Package string `arg:"-p,help:package name used in generated Go code"`

	// CustomTypePackage is the Go package name to use for unknown types.
	CustomTypePackage string `arg:"--custom-type-package,-C,help:Go package name to use for custom or unknown types"`

	// Int32Type is the type to assign those discovered as int32 (ie, serial, integer, etc).
	Int32Type string `arg:"--int32-type,-i,help:Go type to assign to integers"`

	// Uint32Type is the type to assign those discovered as uint32.
	Uint32Type string `arg:"--uint32-type,-u,help:Go type to assign to unsigned integers"`

	// IncTypes are the types to include.
	InclTypes []string `arg:"--include,help:include type(s)"`

	// ExcTypes are the types to exclude.
	ExclTypes []string `arg:"--exclude,help:exclude type(s)"`

	// QueryMode toggles whether or not to parse a query from stdin.
	QueryMode bool `arg:"--query-mode,-N,help:enable query mode"`

	// Query is the passed query. If not specified, then os.Stdin will be used.
	// cli args take precedence over stdin.
	Query string `arg:"-Q,help:query to generate Go type and func from"`

	// QueryType is the name to give to the Go type generated from the query.
	QueryType string `arg:"--query-type,-T,help:query's generated Go type"`

	// QueryFunc is the name to assign to the generated query func.
	QueryFunc string `arg:"--query-func,-F,help:query's generated Go func name"`

	// QueryOnlyOne toggles the generated query code to expect only one result.
	QueryOnlyOne bool `arg:"--query-only-one,-1,help:toggle query's generated Go func to return only one result"`

	// QueryTrim enables triming whitespace on the supplied query.
	QueryTrim bool `arg:"--query-trim,-M,help:toggle trimming of query whitespace in generated Go code"`

	// QueryStrip enables stripping the '::<type> AS <name>' from supplied query.
	QueryStrip bool `arg:"--query-strip,-B,help:toggle stripping type casts from query in generated Go code"`

	// QueryInterpolate enables interpolation in generated query.
	QueryInterpolate bool `arg:"--query-interpolate,-I,help:toggle query interpolation in generated Go code"`

	// TypeComment is the type comment for a query.
	QueryTypeComment string `arg:"--query-type-comment,help:comment for query's generated Go type"`

	// FuncComment is the func comment to provide the named query.
	QueryFuncComment string `arg:"--query-func-comment,help:comment for query's generated Go func"`

	// QueryParamDelimiter is the delimiter for parameterized values for a query.
	QueryParamDelimiter string `arg:"--query-delimiter,-D,help:delimiter for query's embedded Go parameters"`

	// QueryFields are the fields to scan the result to.
	QueryFields string `arg:"--query-fields,-Z,help:comma separated list of field names to scan query's results to the query's associated Go type"`

	// EnablePostgresOIDs toggles postgres oids
	EnablePostgresOIDs bool `arg:"--enable-postgres-oids,help:enable postgres oids"`

	// TemplatePath is the path to use the user supplied templates instead of
	// the built in versions.
	TemplatePath string `arg:"--template-path,help:user supplied template path"`

	// Path is the output path, as derived from Out.
	Path string `arg:"-"`

	// Filename is the output filename, as derived from Out.
	Filename string `arg:"-"`

	// LoaderType is the loader type.
	LoaderType string `arg:"-"`

	// Loader is the schema loader.
	Loader Loader `arg:"-"`

	// DB is the opened database handle.
	DB *sql.DB `arg:"-"`

	// Generated is the generated templates after a run.
	Generated []TBuf `arg:"-"`

	// KnownTypeMap is the collection of known Go types.
	KnownTypeMap map[string]bool `arg:"-"`

	// ShortNameTypeMap is the collection of Go style short names for types, mainly
	// used for use with declaring a func receiver on a type.
	ShortNameTypeMap map[string]string `arg:"-"`
	// contains filtered or unexported fields
}

ArgType is the type that specifies the command line arguments.

var Args *ArgType

Args are the application arguments.

func NewDefaultArgs

func NewDefaultArgs() *ArgType

NewDefaultArgs returns the default arguments.

func (*ArgType) ExecuteTemplate

func (a *ArgType) ExecuteTemplate(tt TemplateType, name string, sub string, obj interface{}) error

ExecuteTemplate loads and parses the supplied template with name and executes it with obj as the context.

func (*ArgType) NewTemplateFuncs

func (a *ArgType) NewTemplateFuncs() template.FuncMap

NewTemplateFuncs returns a set of template funcs bound to the supplied args.

func (*ArgType) ParsePrecision

func (a *ArgType) ParsePrecision(dt string) (string, int, int)

ParsePrecision extracts (precision[,scale]) strings from a data type and returns the data type without the string.

func (*ArgType) ParseQuery

func (a *ArgType) ParseQuery(mask string, interpol bool) (string, []*QueryParam)

ParseQuery takes the query in args and looks for strings in the form of "%%<name> <type>[,<option>,...]%%", replacing them with the supplied mask. mask can contain "%d" to indicate current position. The modified query is returned, and the slice of extracted QueryParam's.

func (*ArgType) TemplateLoader

func (a *ArgType) TemplateLoader(name string) ([]byte, error)

TemplateLoader loads templates from the specified name.

func (*ArgType) TemplateSet

func (a *ArgType) TemplateSet() *TemplateSet

TemplateSet retrieves the created template set.

type Enum

type Enum struct {
	Name    string
	Schema  string
	Values  []*EnumValue
	Enum    *models.Enum
	Comment string
}

Enum is a template item for a enum.

type EnumValue

type EnumValue struct {
	Name    string
	Val     *models.EnumValue
	Comment string
}

EnumValue holds data for a single enum value.

type Field

type Field struct {
	Name    string
	Type    string
	NilType string
	Len     int
	Col     *models.Column
	Comment string
}

Field contains field information.

type ForeignKey

type ForeignKey struct {
	/*Name       string*/
	Schema     string
	Type       *Type
	Field      *Field
	RefType    *Type
	RefField   *Field
	ForeignKey *models.ForeignKey
	Comment    string
}

ForeignKey is a template item for a foreign relationship on a table.

type Index

type Index struct {
	Name     string
	TypeName string
	Schema   string
	Type     *Type
	Fields   []*Field
	Index    *models.Index
	Comment  string
}

Index is a template item for a index into a table.

type Loader

type Loader interface {
	// IsSupported processes the passed url.URL, returning the sql.Open
	// driverName and dataSourceName and whether or not the Loader supports the
	// scheme in the url.
	IsSupported(*url.URL) (string, bool)

	// NthParam returns the 0-based Nth param for the Loader.
	NthParam(i int) string

	// Mask returns the mask.
	Mask() string

	// Relkind returns the schema's relkind identifier (ie, TABLE, VIEW, BASE TABLE, etc).
	Relkind(RelType) string

	// SchemaName loads the active schema name from the database if not provided on the cli.
	SchemaName(*ArgType) (string, error)

	// ParseQuery parses the ArgType.Query and writes any necessary type(s) to
	// the ArgType from the opened database handle.
	ParseQuery(*ArgType) error

	// LoadSchema loads the ArgType.Schema from the opened database handle,
	// writing any generated types to ArgType.
	LoadSchema(*ArgType) error
}

Loader is the common interface for database drivers that can generate code from a database schema.

type Proc

type Proc struct {
	Name       string
	Schema     string
	ProcParams string
	Params     []*Field
	Return     *Field
	Proc       *models.Proc
	Comment    string
}

Proc is a template item for a stored procedure.

type Query

type Query struct {
	Schema        string
	Name          string
	Query         []string
	QueryComments []string
	QueryParams   []*QueryParam
	OnlyOne       bool
	Interpolate   bool
	Type          *Type
	Comment       string
}

Query is a template item for a custom query.

type QueryParam

type QueryParam struct {
	Name        string
	Type        string
	Interpolate bool
}

QueryParam is a query parameter for a custom query.

type RelType

type RelType uint

RelType represents the different types of relational storage (table/view).

const (
	// Table reltype
	Table RelType = iota

	// View reltype
	View
)

func (RelType) String

func (rt RelType) String() string

String provides the string representation of RelType.

type TBuf

type TBuf struct {
	TemplateType TemplateType
	Name         string
	Subname      string
	Buf          *bytes.Buffer
}

TBuf is to hold the executed templates.

type TBufSlice

type TBufSlice []TBuf

TBufSlice is a slice of TBuf compatible with sort.Interface.

func (TBufSlice) Len

func (t TBufSlice) Len() int

func (TBufSlice) Less

func (t TBufSlice) Less(i, j int) bool

func (TBufSlice) Swap

func (t TBufSlice) Swap(i, j int)

type TemplateSet

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

TemplateSet is a set of templates.

func (*TemplateSet) Execute

func (ts *TemplateSet) Execute(w io.Writer, name string, obj interface{}) error

Execute executes a specified template in the template set using the supplied obj as its parameters and writing the output to w.

type TemplateType

type TemplateType uint

TemplateType represents a template type

const (
	EnumTemplate TemplateType = iota
	ProcTemplate
	TypeTemplate
	ForeignKeyTemplate
	IndexTemplate
	QueryTypeTemplate
	QueryTemplate

	// always last
	XOTemplate
)

the order here will be the alter the output order per file.

func (TemplateType) String

func (tt TemplateType) String() string

String returns the name for the associated template type.

type Type

type Type struct {
	Name       string
	Schema     string
	RelType    RelType
	PrimaryKey *Field
	Fields     []*Field
	Table      *models.Table
	Comment    string
}

Type is a template item for a type (ie, table/view/custom query).

type TypeLoader

type TypeLoader struct {
	Schemes         []string
	ProcessDSN      func(*url.URL, string) string
	ParamN          func(int) string
	MaskFunc        func() string
	ProcessRelkind  func(RelType) string
	Schema          func(*ArgType) (string, error)
	ParseType       func(*ArgType, string, bool) (int, string, string)
	EnumList        func(models.XODB, string) ([]*models.Enum, error)
	EnumValueList   func(models.XODB, string, string) ([]*models.EnumValue, error)
	ProcList        func(models.XODB, string) ([]*models.Proc, error)
	ProcParamList   func(models.XODB, string, string) ([]*models.ProcParam, error)
	TableList       func(models.XODB, string, string) ([]*models.Table, error)
	ColumnList      func(models.XODB, string, string) ([]*models.Column, error)
	ForeignKeyList  func(models.XODB, string, string) ([]*models.ForeignKey, error)
	IndexList       func(models.XODB, string, string) ([]*models.Index, error)
	IndexColumnList func(models.XODB, string, string, string) ([]*models.IndexColumn, error)
	QueryStrip      func([]string, []string)
	QueryColumnList func(*ArgType, []string) ([]*models.Column, error)
}

TypeLoader provides a common Loader implementation used by the built in schema/query loaders.

func (TypeLoader) IsSupported

func (tl TypeLoader) IsSupported(u *url.URL) (string, bool)

IsSupported processes the passed url.URL, returning the sql.Open driverName and dataSourceName and whether or not the Loader supports the scheme in the url.

func (TypeLoader) LoadColumns

func (tl TypeLoader) LoadColumns(args *ArgType, typeTpl *Type) error

LoadColumns loads schema table/view columns.

func (TypeLoader) LoadEnumValues

func (tl TypeLoader) LoadEnumValues(args *ArgType, enumTpl *Enum) error

LoadEnumValues loads schema enum values.

func (TypeLoader) LoadEnums

func (tl TypeLoader) LoadEnums(args *ArgType) (map[string]*Enum, error)

LoadEnums loads schema enums.

func (TypeLoader) LoadForeignKeys

func (tl TypeLoader) LoadForeignKeys(args *ArgType, tableMap map[string]*Type) (map[string]*ForeignKey, error)

LoadForeignKeys loads foreign keys.

func (TypeLoader) LoadIndexColumns

func (tl TypeLoader) LoadIndexColumns(args *ArgType, ixTpl *Index) error

LoadIndexColumns loads the index column information.

func (TypeLoader) LoadIndexes

func (tl TypeLoader) LoadIndexes(args *ArgType, tableMap map[string]*Type) (map[string]*Index, error)

LoadIndexes loads schema index definitions.

func (TypeLoader) LoadProcParams

func (tl TypeLoader) LoadProcParams(args *ArgType, procTpl *Proc) error

LoadProcParams loads schema stored procedure parameters.

func (TypeLoader) LoadProcs

func (tl TypeLoader) LoadProcs(args *ArgType) (map[string]*Proc, error)

LoadProcs loads schema stored procedures definitions.

func (TypeLoader) LoadRelkind

func (tl TypeLoader) LoadRelkind(args *ArgType, relType RelType) (map[string]*Type, error)

LoadRelkind loads a schema table/view definition.

func (TypeLoader) LoadSchema

func (tl TypeLoader) LoadSchema(args *ArgType) error

LoadSchema loads schema definitions.

func (TypeLoader) LoadTableForeignKeys

func (tl TypeLoader) LoadTableForeignKeys(args *ArgType, tableMap map[string]*Type, typeTpl *Type, fkMap map[string]*ForeignKey) error

LoadTableForeignKeys loads schema foreign key definitions per table.

func (TypeLoader) LoadTableIndexes

func (tl TypeLoader) LoadTableIndexes(args *ArgType, typeTpl *Type, ixMap map[string]*Index) error

LoadTableIndexes loads schema index definitions per table.

func (TypeLoader) Mask

func (tl TypeLoader) Mask() string

Mask returns the parameter mask

func (TypeLoader) NthParam

func (tl TypeLoader) NthParam(i int) string

NthParam satisifies Loader's NthParam.

func (TypeLoader) ParseQuery

func (tl TypeLoader) ParseQuery(args *ArgType) error

ParseQuery satisfies Loader's ParseQuery.

func (TypeLoader) Relkind

func (tl TypeLoader) Relkind(rt RelType) string

Relkind satisfies Loader's Relkind.

func (TypeLoader) SchemaName

func (tl TypeLoader) SchemaName(args *ArgType) (string, error)

SchemaName returns the active schema name.

Jump to

Keyboard shortcuts

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