dbmeta

package
v0.0.0-...-7d80f3d Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2024 License: GPL-2.0 Imports: 29 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 CRLFNewlines

func CRLFNewlines(d []byte) []byte

CRLFNewlines transforms \n to \r\n (windows)

func CheckForDupeTable

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

CheckForDupeTable check for duplicate table name, returns available name

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

FormatSource format source code contents

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

GetFunctionName get function name

func GetMappings

func GetMappings() map[string]*SQLMapping

GetMappings get all mappings

func InitColorOutput

func InitColorOutput(_au aurora.Aurora)

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

LoadTableInfo load table info from db connection, and list of tables

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 NormalizeNewlines

func NormalizeNewlines(d []byte) []byte

NormalizeNewlines normalizes \r\n (windows) and \r (mac) into \n (unix)

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 Pwd

func Pwd() string

Pwd template command to return the current working directory

func RegSplit

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

RegSplit split text based on regex

func RenameReservedName

func RenameReservedName(s string) string

RenameReservedName renames a reserved word

func Replace

func Replace(nameFormat, name string) string

Replace takes a template based name format and will render a name using it

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
	Comment() 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
	ServerScheme          string
	ServerListen          string
	Verbose               bool
	OutDir                string
	Overwrite             bool
	LineEndingCRLF        bool
	CmdLine               string
	CmdLineWrapped        string
	CmdLineArgs           []string
	FileNamingTemplate    string
	ModelNamingTemplate   string
	FieldNamingTemplate   string
	ContextMap            map[string]interface{}
	TemplateLoader        TemplateLoader
	TableInfos            map[string]*ModelInfo
	FragmentsDir          string
	// contains filtered or unexported fields
}

Config for generating code

func NewConfig

func NewConfig(templateLoader TemplateLoader) *Config

NewConfig create a new code config

func (*Config) CreateContextForTableFile

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

CreateContextForTableFile create map context for a db table

func (*Config) DisplayConfig

func (c *Config) DisplayConfig() string

DisplayConfig display config info

func (*Config) FileSystemCopy

func (c *Config) FileSystemCopy(src, dst string, options ...string) string

FileSystemCopy template command to copy files, directories and to pass --include XXX and --exclude YYY regular expressions. Files ending in .tmpl will be processed as a template. Files ending in .table.tmpl will be processed as a template iterating through all the tables

func (*Config) GenerateFieldsTypes

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

GenerateFieldsTypes FieldInfo slice from DbTableMeta

func (*Config) GenerateFile

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

GenerateFile generate file from template, non table used within templates

func (*Config) GenerateTableFile

func (c *Config) GenerateTableFile(tableName, templateFilename, outputDirectory, outputFileName string) string

GenerateTableFile generate file from template using specific table used within templates

func (*Config) GetTemplate

func (c *Config) GetTemplate(genTemplate *GenTemplate) (*template.Template, error)

GetTemplate return a Template based on a name and template contents

func (*Config) JSONFieldName

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

JSONFieldName convert name to appropriate case

func (*Config) JSONTag

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

JSONTag converts name to `json:"name"` respecting json-fmt option

func (*Config) JSONTagOmitEmpty

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

JSONTagOmitEmpty converts name to JSON tag with omitempty

func (*Config) LoadFragments

func (c *Config) LoadFragments(dirname string) error

LoadFragments read all filed inside dirname to `fragments`

func (*Config) Mkdir

func (c *Config) Mkdir(dst string) string

Mkdir template command to mkdir under the output directory

func (*Config) ReplaceFieldNamingTemplate

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

ReplaceFieldNamingTemplate use the FieldNamingTemplate to format a table name

func (*Config) ReplaceFileNamingTemplate

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

ReplaceFileNamingTemplate use the FileNamingTemplate to format a table name

func (*Config) ReplaceModelNamingTemplate

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

ReplaceModelNamingTemplate use the ModelNamingTemplate to format a table name

func (*Config) Touch

func (c *Config) Touch(dst string) string

Touch template command to touch a file under the output directory

func (*Config) WriteTemplate

func (c *Config) WriteTemplate(genTemplate *GenTemplate, data map[string]interface{}, outputFile string) error

WriteTemplate write a template out

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 GenTemplate

type GenTemplate struct {
	Name    string
	Content string
}

GenTemplate template info struct

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
}

State struct used for storing state in template parsing

func (*State) Inc

func (s *State) Inc() int

Inc increment state value in template parsing

func (*State) Set

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

Set set state value in template parsing

type SwaggerInfoDetails

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

SwaggerInfoDetails swagger details

type TemplateLoader

type TemplateLoader func(filename string) (tpl *GenTemplate, err error)

TemplateLoader loader function to retrieve a template contents

Jump to

Keyboard shortcuts

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