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 ¶
- Variables
- func GenerateProto(path string) error
- type Option
- func WithColumnAliasesFromForeignKeys(ctx context.Context, db dml.Querier) (opt Option)
- func WithLoadColumns(ctx context.Context, db dml.Querier, tables ...string) (opt Option)
- func WithTable(tableName string, columns ddl.Columns) (opt Option)
- func WithTableOption(tableName string, opt *TableOption) (o Option)
- type TableOption
- type Tables
- type TypeDef
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
WithLoadColumns queries the information_schema table and loads the column definition of the provided `tables` slice.
func WithTable ¶
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.
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.