dmlgen

package
v0.0.0-...-0a8238e Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2018 License: Apache-2.0 Imports: 23 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dmlgen provides code generation templates and library code for sql/dml.

To generated the protocol buffer file $ protoc --gogo_out=Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types:. --proto_path=/Users/kiri/GoPro/src/:/Users/kiri/GoPro/src/github.com/gogo/protobuf/protobuf/:. *.proto

TODO: Generate also protobuf code for https://github.com/twitchtv/twirp/wiki

Index

Constants

This section is empty.

Variables

View Source
var MysqlTypeToGo = map[string]*TypeDef{
	"int":        goTypeInt,
	"bigint":     goTypeInt64,
	"smallint":   goTypeInt,
	"tinyint":    goTypeInt,
	"mediumint":  goTypeInt,
	"double":     goTypeFloat64,
	"float":      goTypeFloat64,
	"decimal":    goTypeDecimal,
	"date":       goTypeTime,
	"datetime":   goTypeTime,
	"timestamp":  goTypeTime,
	"char":       goTypeString,
	"varchar":    goTypeString,
	"enum":       goTypeString,
	"set":        goTypeString,
	"text":       goTypeString,
	"longtext":   goTypeString,
	"mediumtext": goTypeString,
	"tinytext":   goTypeString,
	"blob":       goTypeString,
	"longblob":   goTypeString,
	"mediumblob": goTypeString,
	"tinyblob":   goTypeString,
	"binary":     goTypeByte,
	"varbinary":  goTypeByte,
	"bit":        goTypeBool,
}

MysqlTypeToGo maps the MySql/MariaDB field type to the correct Go/protobuf type. See the type TypeDef for more details. This exported variable allows to set custom types before code generation.

Functions

func GenerateProto

func GenerateProto(path string) error

GenerateProto searches all *.proto files in the given path and calls protoc to generate the Go source code.

Types

type Option

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

Option represents a sortable option for the NewTables function. Each option function can be applied in a mixed order.

func WithColumnAliasesFromForeignKeys

func WithColumnAliasesFromForeignKeys(ctx context.Context, db dml.Querier) (opt Option)

WithColumnAliasesFromForeignKeys extracts similar column names from foreign key definitions. For the list of tables and their primary/unique keys, this function searches the foreign keys to other tables and uses the column name as the alias. For example the table `customer_entity` and its PK column `entity_id` has a foreign key in table `sales_order` whose name is `customer_id`. When generation code for customer_entity, the column entity_id can be used additionally with the name customer_id, hence customer_id is the alias.

func WithLoadColumns

func WithLoadColumns(ctx context.Context, db dml.Querier, tables ...string) (opt Option)

WithLoadColumns queries the information_schema table and loads the column definition of the provided `tables` slice.

func WithTable

func WithTable(tableName string, columns ddl.Columns) (opt Option)

WithTable sets a table and its columns. Allows to overwrite a table fetched with function WithLoadColumns.

func WithTableOption

func WithTableOption(tableName string, opt *TableOption) (o Option)

WithTableOption applies options to a table, identified by the table name used as map key.

type TableOption

type TableOption struct {
	// Encoders add method receivers for, each struct, compatible with the
	// interface declarations in the various encoding packages. Supported
	// encoder names are: text, binary, and protobuf. Text includes JSON. Binary
	// includes Gob.
	Encoders []string
	// StructTags enables struct tags proactively for the whole struct. Allowed
	// values are: bson, db, env, json, protobuf, toml, yaml and xml. For bson,
	// json, yaml and xml the omitempty attribute has been set. If you need a
	// different struct tag for a specifiv column you must set the option
	// CustomStructTags.
	StructTags []string
	// CustomStructTags allows to specify custom struct tags for a specific
	// column. The slice must be balanced, means index i sets to the column name
	// and index i+1 to the desired struct tag.
	//		[]string{"column_a",`json: ",omitempty"`,"column_b","`xml:,omitempty`"}
	CustomStructTags []string // balanced slice
	// Comment adds custom comments to each struct type. Useful when relying on
	// 3rd party JSON marshaler code generators like easyjson or ffjson. If
	// comment spans over multiple lines each line will be checked if it starts
	// with the comment identifier (//). If not, the identifier will be
	// prepended.
	Comment string
	// ColumnAliases specifies different names used for a column. For example
	// customer_entity.entity_id can also be sales_order.customer_id, hence a
	// Foreign Key. The alias would be just: entity_id:[]string{"customer_id"}.
	ColumnAliases map[string][]string // key=column name value a list of aliases
	// UniquifiedColumns specifies columns which are non primary/unique key one
	// but should have a dedicated function to extract their unique primitive
	// values as a slice.
	UniquifiedColumns []string
	// contains filtered or unexported fields
}

TableOption used in conjunction with WithTableOption to apply different configurations for a generated struct and its struct collection.

type Tables

type Tables struct {
	Package     string // Name of the package
	ImportPaths []string
	// Tables uses the table name as map key and the table description as value.
	Tables map[string]*table
	template.FuncMap
	DisableFileHeader   bool
	DisableTableSchemas bool
	GogoProtoOptions    []string
	// contains filtered or unexported fields
}

Tables can generated Go source for for database tables once correctly configured.

func NewTables

func NewTables(packageName string, opts ...Option) (*Tables, error)

NewTables creates a new instance of the SQL table code generator.

func (*Tables) WriteGo

func (ts *Tables) WriteGo(w io.Writer) error

WriteGo writes the Go source code into `w`.

func (*Tables) WriteProto

func (ts *Tables) WriteProto(w io.Writer) error

WriteProto writes the protocol buffer specifications into `w`.

type TypeDef

type TypeDef struct {
	MysqlUnsignedNull    string
	MysqlUnsignedNotNull string
	MysqlSignedNull      string
	MysqlSignedNotNull   string

	ProtobufUnsignedNull    string
	ProtobufUnsignedNotNull string
	ProtobufSignedNull      string
	ProtobufSignedNotNull   string
}

TypeDef used in variable `MysqlTypeToGo` to map a MySQL/MariaDB type to its appropriate Go or Protocol Buffer type. Those types are getting printed in the generated files.

Jump to

Keyboard shortcuts

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