dbmeta

package
v0.9.11-0...-06d4ffe Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2020 License: GPL-3.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDefaultTableDDL

func BuildDefaultTableDDL(tableName string, cols []*columnMeta) string

BuildDefaultTableDDL create a ddl mock using the ColumnMeta data

func BytesToString

func BytesToString(bs []uint8) string

BytesToString convert []uint8 to string

func CheckForDupeTable

func CheckForDupeTable(tables map[string]*ModelInfo, name string) string

func Copy

func Copy(dst interface{}, src interface{}) error

Copy a src struct into a destination struct

func Exists

func Exists(name string) bool

Exists reports whether the named file or directory exists.

func FindInSlice

func FindInSlice(slice []string, val string) (int, bool)

FindInSlice takes a slice and looks for an element in it. If found it will return it's key, otherwise it will return -1 and a bool of false.

func FindPrimaryKeyFromInformationSchema

func FindPrimaryKeyFromInformationSchema(db *sql.DB, tableName string) (primaryKey string, err error)

FindPrimaryKeyFromInformationSchema fetch primary key info from information_schema

func FmtFieldName

func FmtFieldName(s string) string

FmtFieldName formats a string as a struct key

Example:

fmtFieldName("foo_id")

Output: FooID

func FormatSource

func FormatSource(s string) string

func GenerateDeleteSql

func GenerateDeleteSql(dbTable DbTableMeta) (string, error)

GenerateDeleteSql generate sql for a delete

func GenerateInsertSql

func GenerateInsertSql(dbTable DbTableMeta) (string, error)

GenerateInsertSql generate sql for a insert

func GenerateSelectMultiSql

func GenerateSelectMultiSql(dbTable DbTableMeta) (string, error)

GenerateSelectMultiSql generate sql for selecting multiple records

func GenerateSelectOneSql

func GenerateSelectOneSql(dbTable DbTableMeta) (string, error)

GenerateSelectOneSql generate sql for selecting one record

func GenerateUpdateSql

func GenerateUpdateSql(dbTable DbTableMeta) (string, error)

GenerateUpdateSql generate sql for a update

func GetFieldLenFromInformationSchema

func GetFieldLenFromInformationSchema(db *sql.DB, tableSchema, tableName, columnName string) (int64, error)

GetFieldLenFromInformationSchema fetch field length from database

func GetFunctionName

func GetFunctionName(i interface{}) string

func GetMappings

func GetMappings() map[string]*SQLMapping

GetMappings get all mappings

func LoadMappings

func LoadMappings(mappingFileName string, verbose bool) error

LoadMappings load sql mappings to load mapping json file

func LoadTableInfo

func LoadTableInfo(db *sql.DB, dbTables []string, excludeDbTables []string, conf *Config) map[string]*ModelInfo

func LoadTableInfoFromMSSqlInformationSchema

func LoadTableInfoFromMSSqlInformationSchema(db *sql.DB, tableName string) (primaryKey map[string]*InformationSchema, err error)

LoadTableInfoFromMSSqlInformationSchema fetch info from information_schema for ms sql database

func LoadTableInfoFromPostgresInformationSchema

func LoadTableInfoFromPostgresInformationSchema(db *sql.DB, tableName string) (primaryKey map[string]*PostgresInformationSchema, err error)

LoadTableInfoFromPostgresInformationSchema fetch info from information_schema for postgres database

func NonPrimaryKeyNames

func NonPrimaryKeyNames(dbTable DbTableMeta) []string

NonPrimaryKeyNames return the list of primary key names

func ParseSQLType

func ParseSQLType(dbType string) (resultType string, dbTypeLen int64)

ParseSQLType parse sql type and return raw type and length

func PrimaryKeyCount

func PrimaryKeyCount(dbTable DbTableMeta) int

PrimaryKeyCount return the number of primary keys in table

func PrimaryKeyNames

func PrimaryKeyNames(dbTable DbTableMeta) []string

PrimaryKeyNames return the list of primary key names

func ProcessMappings

func ProcessMappings(source string, mappingJsonstring []byte, verbose bool) error

ProcessMappings process the json for mappings to load sql mappings

func RegSplit

func RegSplit(text string, delimeter string) []string

func RenameReservedName

func RenameReservedName(s string) string

RenameReservedName renames a reserved word

func Replace

func Replace(nameFormat, name string) string

func SQLTypeToGoType

func SQLTypeToGoType(sqlType string, nullable bool, gureguTypes bool) (string, error)

SQLTypeToGoType map a sql type to a go type

func SQLTypeToProtobufType

func SQLTypeToProtobufType(sqlType string) (string, error)

SQLTypeToProtobufType map a sql type to a protobuf type

func Spew

func Spew(val interface{}) string

Spew func to return spewed string representation of struct

func ToJSON

func ToJSON(val interface{}, indent int) string

ToJSON func to return json string representation of struct

func TrimSpaceNewlineInString

func TrimSpaceNewlineInString(s string) string

TrimSpaceNewlineInString replace spaces in string

Types

type ColumnMeta

type ColumnMeta interface {
	Name() string
	String() string
	Nullable() bool
	DatabaseTypeName() string
	DatabaseTypePretty() string
	Index() int
	IsPrimaryKey() bool
	IsAutoIncrement() bool
	IsArray() bool
	ColumnType() string
	Notes() string
	ColumnLength() int64
	DefaultValue() string
}

ColumnMeta meta data for a column

type Config

type Config struct {
	SqlType               string
	SqlConnStr            string
	SqlDatabase           string
	Module                string
	ModelPackageName      string
	ModelFQPN             string
	AddJSONAnnotation     bool
	AddGormAnnotation     bool
	AddProtobufAnnotation bool
	AddXMLAnnotation      bool
	AddDBAnnotation       bool
	UseGureguTypes        bool
	JsonNameFormat        string
	XMLNameFormat         string
	ProtobufNameFormat    string
	DaoPackageName        string
	DaoFQPN               string
	ApiPackageName        string
	ApiFQPN               string
	GrpcPackageName       string
	GrpcFQPN              string
	Swagger               *SwaggerInfoDetails
	ServerPort            int
	ServerHost            string
	Verbose               bool
	OutDir                string
	Overwrite             bool
	CmdLine               string
	CmdLineWrapped        string
	CmdLineArgs           []string
	FileNamingTemplate    string
	ModelNamingTemplate   string
	FieldNamingTemplate   string

	ContextMap     map[string]interface{}
	TemplateLoader TemplateLoader
	// contains filtered or unexported fields
}

func NewConfig

func NewConfig(templateLoader TemplateLoader) *Config

func (*Config) CreateContextForTableFile

func (c *Config) CreateContextForTableFile(tableInfo *ModelInfo) map[string]interface{}

func (*Config) GenerateFieldsTypes

func (c *Config) GenerateFieldsTypes(dbMeta DbTableMeta) ([]*FieldInfo, error)

Generate fields string

func (*Config) GenerateFile

func (c *Config) GenerateFile(templateFilename, outDir, outputDirectory, outputFileName string, formatOutput bool, overwrite bool) string

GenerateFile generate file from template, non table used within templates

func (*Config) GenerateTableFile

func (c *Config) GenerateTableFile(tableInfos map[string]*ModelInfo, tableName, templateFilename, outputDirectory, outputFileName string, formatOutput bool) string

GenerateTableFile generate file from template using specific table used within templates

func (*Config) GetTemplate

func (c *Config) GetTemplate(name, t string) (*template.Template, error)

func (*Config) ReplaceFieldNamingTemplate

func (c *Config) ReplaceFieldNamingTemplate(name string) string

func (*Config) ReplaceFileNamingTemplate

func (c *Config) ReplaceFileNamingTemplate(name string) string

func (*Config) ReplaceModelNamingTemplate

func (c *Config) ReplaceModelNamingTemplate(name string) string

func (*Config) WriteTemplate

func (c *Config) WriteTemplate(name, templateStr string, data map[string]interface{}, outputFile string, formatOutput bool)

type DbTableMeta

type DbTableMeta interface {
	Columns() []ColumnMeta
	SQLType() string
	SQLDatabase() string
	TableName() string
	DDL() string
}

DbTableMeta table meta data

func LoadMeta

func LoadMeta(sqlType string, db *sql.DB, sqlDatabase, tableName string) (DbTableMeta, error)

LoadMeta loads the DbTableMeta data from the db connection for the table

func LoadMsSQLMeta

func LoadMsSQLMeta(db *sql.DB, sqlType, sqlDatabase, tableName string) (DbTableMeta, error)

LoadMsSQLMeta fetch db meta data for MS SQL database

func LoadMysqlMeta

func LoadMysqlMeta(db *sql.DB, sqlType, sqlDatabase, tableName string) (DbTableMeta, error)

LoadMysqlMeta fetch db meta data for MySQL database

func LoadPostgresMeta

func LoadPostgresMeta(db *sql.DB, sqlType, sqlDatabase, tableName string) (DbTableMeta, error)

LoadPostgresMeta fetch db meta data for Postgres database

func LoadSqliteMeta

func LoadSqliteMeta(db *sql.DB, sqlType, sqlDatabase, tableName string) (DbTableMeta, error)

LoadSqliteMeta fetch db meta data for Sqlite3 database

func LoadUnknownMeta

func LoadUnknownMeta(db *sql.DB, sqlType, sqlDatabase, tableName string) (DbTableMeta, error)

LoadUnknownMeta fetch db meta data for unknown database type

type FieldInfo

type FieldInfo struct {
	Index                 int
	GoFieldName           string
	GoFieldType           string
	GoAnnotations         []string
	JSONFieldName         string
	ProtobufFieldName     string
	ProtobufType          string
	ProtobufPos           int
	Comment               string
	Notes                 string
	Code                  string
	FakeData              interface{}
	ColumnMeta            ColumnMeta
	PrimaryKeyFieldParser string
	PrimaryKeyArgName     string
	SqlMapping            *SQLMapping
	GormAnnotation        string
	JSONAnnotation        string
	XMLAnnotation         string
	DBAnnotation          string
	GoGoMoreTags          string
}

FieldInfo codegen info for each column in sql table

type InformationSchema

type InformationSchema struct {
	TableCatalog           string
	TableSchema            string
	TableName              string
	OrdinalPosition        int
	ColumnName             string
	DataType               string
	CharacterMaximumLength interface{}
	ColumnDefault          interface{}
	IsNullable             string
}

InformationSchema results from a query of the InformationSchema db table

type ModelInfo

type ModelInfo struct {
	Index           int
	IndexPlus1      int
	PackageName     string
	StructName      string
	ShortStructName string
	TableName       string
	Fields          []string
	DBMeta          DbTableMeta
	Instance        interface{}
	CodeFields      []*FieldInfo
}

ModelInfo info for a sql table

func GenerateModelInfo

func GenerateModelInfo(tables map[string]*ModelInfo, dbMeta DbTableMeta,
	tableName string,
	conf *Config) (*ModelInfo, error)

GenerateModelInfo generates a struct for the given table.

func (*ModelInfo) Notes

func (m *ModelInfo) Notes() string

Notes notes on table generation

type PostgresInformationSchema

type PostgresInformationSchema struct {
	TableCatalog           string
	TableSchema            string
	TableName              string
	OrdinalPosition        int
	ColumnName             string
	DataType               string
	CharacterMaximumLength interface{}
	ColumnDefault          interface{}
	IsNullable             string
	IsIdentity             string
	PrimaryKey             bool
}

PostgresInformationSchema results from a query of the postgres InformationSchema db table

type SQLMapping

type SQLMapping struct {

	// SqlType sql type reported from db
	SQLType string `json:"sql_type"`

	// GoType mapped go type
	GoType string `json:"go_type"`

	// JSONType mapped json type
	JSONType string `json:"json_type"`

	// ProtobufType mapped protobuf type
	ProtobufType string `json:"protobuf_type"`

	// GureguType mapped go type using Guregu
	GureguType string `json:"guregu_type"`

	// GoNullableType mapped go type using nullable
	GoNullableType string `json:"go_nullable_type"`

	// SwaggerType mapped type
	SwaggerType string `json:"swagger_type"`
}

SQLMapping mapping

func SQLTypeToMapping

func SQLTypeToMapping(sqlType string) (*SQLMapping, error)

SQLTypeToMapping retrieve a SqlMapping based on a sql type

func (*SQLMapping) String

func (m *SQLMapping) String() interface{}

type SQLMappings

type SQLMappings struct {
	SQLMappings []*SQLMapping `json:"mappings"`
}

SQLMappings mappings for sql types to json, go etc

type State

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

func (*State) Inc

func (s *State) Inc() int

func (*State) Set

func (s *State) Set(n int) int

type SwaggerInfoDetails

type SwaggerInfoDetails struct {
	Version      string
	Host         string
	BasePath     string
	Title        string
	Description  string
	TOS          string
	ContactName  string
	ContactURL   string
	ContactEmail string
}

type TemplateLoader

type TemplateLoader func(filename string) (content string, err error)

Jump to

Keyboard shortcuts

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