dbmeta

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: May 12, 2020 License: GPL-3.0 Imports: 17 Imported by: 11

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 Copy

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

Copy a src struct into a destination struct

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 GetFieldLenFromInformationSchema

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

GetFieldLenFromInformationSchema fetch field length from database

func GetMappings

func GetMappings() map[string]*SQLMapping

GetMappings get all mappings

func LoadMappings

func LoadMappings(mappingFileName string) error

LoadMappings load sql mappings to load mapping json file

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 ParseSQLType added in v0.9.1

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

ParseSQLType parse sql type and return raw type and length

func ProcessMappings

func ProcessMappings(mappingJsonstring []byte) error

ProcessMappings process the json for mappings to load sql mappings

func SQLTypeToGoType added in v0.9.1

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

SQLTypeToGoType map a sql type to a go type

func SQLTypeToProtobufType added in v0.9.1

func SQLTypeToProtobufType(sqlType string) (string, error)

SQLTypeToProtobufType map a sql type to a protobuf type

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
	ColumnType() string
	ColumnLength() int64
	DefaultValue() string
}

ColumnMeta meta data for a column

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 NewMsSQLMeta added in v0.9.1

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

NewMsSQLMeta fetch db meta data for MS SQL database

func NewMysqlMeta

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

NewMysqlMeta fetch db meta data for MySQL database

func NewPostgresMeta

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

NewPostgresMeta fetch db meta data for Postgres database

func NewSqliteMeta

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

NewSqliteMeta fetch db meta data for Sqlite3 database

func NewUnknownMeta

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

NewUnknownMeta 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
	Code              string
	FakeData          interface{}
	ColumnMeta        ColumnMeta
}

FieldInfo info for each field in sql table

type InformationSchema added in v0.9.1

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
	PrimaryKeyField       int
	PrimaryKeyGoType      string
	PrimaryKeyFieldParser string
}

ModelInfo info for a sql table

func GenerateStruct

func GenerateStruct(sqlType string,
	db *sql.DB,
	sqlDatabase,
	tableName string,
	structName string,
	pkgName string,
	addJSONAnnotation bool,
	addGormAnnotation bool,
	addDBAnnotation bool,
	addProtobufAnnotation bool,
	gureguTypes bool,
	jsonNameFormat string,
	protobufNameFormat string,
	verbose bool) (*ModelInfo, error)

GenerateStruct generates a struct for the given table.

type PostgresInformationSchema added in v0.9.1

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

PostgresInformationSchema results from a query of the postgres InformationSchema db table

type SQLMapping added in v0.9.1

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"`
}

SQLMapping mapping

func SQLTypeToMapping added in v0.9.1

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

SQLTypeToMapping retrieve a SqlMapping based on a sql type

type SQLMappings added in v0.9.1

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

SQLMappings mappings for sql types to json, go etc

Jump to

Keyboard shortcuts

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