dbstruct

package module
v0.0.0-...-a191abc Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

README

dbstruct

Convert database table to golang struct, and produce the sturct instance

package main

import (
	"fmt"
	"strings"

	_ "github.com/go-sql-driver/mysql"
	"github.com/gogap/dbstruct"
)

func nameMapper(name string) (newName string) {
	secs := strings.Split(name, "_")

	for i := 0; i < len(secs); i++ {
		secs[i] = strings.Title(secs[i])
	}

	return strings.Join(secs, "")
}

func main() {
	s, err := dbstruct.New(
		dbstruct.DataSource("mysql", "root:password@tcp(localhost:3306)/test?charset=utf8&parseTime=True&loc=Local"),
		dbstruct.CreateTabelDSN("root:password@tcp(localhost:3306)/test?charset=utf8&parseTime=True&loc=Local"),
		dbstruct.NameMapper(nameMapper),
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	dbTable, err := s.Describe("users")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(dbTable.NewStruct())
	fmt.Println(dbTable.NewStructSlice())

	fmt.Println("Describe Query ....")

	dbTable, err = s.DescribeQuery("select id,name from users")
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(dbTable.NewStruct())
	fmt.Println(dbTable.NewStructSlice())
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Bit       = "BIT"
	TinyInt   = "TINYINT"
	SmallInt  = "SMALLINT"
	MediumInt = "MEDIUMINT"
	Int       = "INT"
	Integer   = "INTEGER"
	BigInt    = "BIGINT"

	Enum = "ENUM"
	Set  = "SET"

	Char             = "CHAR"
	Varchar          = "VARCHAR"
	NVarchar         = "NVARCHAR"
	TinyText         = "TINYTEXT"
	Text             = "TEXT"
	NText            = "NTEXT"
	Clob             = "CLOB"
	MediumText       = "MEDIUMTEXT"
	LongText         = "LONGTEXT"
	Uuid             = "UUID"
	UniqueIdentifier = "UNIQUEIDENTIFIER"
	SysName          = "SYSNAME"

	Date       = "DATE"
	DateTime   = "DATETIME"
	Time       = "TIME"
	TimeStamp  = "TIMESTAMP"
	TimeStampz = "TIMESTAMPZ"

	Decimal = "DECIMAL"
	Numeric = "NUMERIC"

	Real   = "REAL"
	Float  = "FLOAT"
	Double = "DOUBLE"

	Binary     = "BINARY"
	VarBinary  = "VARBINARY"
	TinyBlob   = "TINYBLOB"
	Blob       = "BLOB"
	MediumBlob = "MEDIUMBLOB"
	LongBlob   = "LONGBLOB"
	Bytea      = "BYTEA"

	Bool    = "BOOL"
	Boolean = "BOOLEAN"

	Serial    = "SERIAL"
	BigSerial = "BIGSERIAL"

	Json  = "JSON"
	Jsonb = "JSONB"
)

Functions

func SQLType2Type

func SQLType2Type(dbField DbField) reflect.Type

default sql type change to go types

Types

type DBStruct

type DBStruct struct {
	Options Options
}

func New

func New(opts ...Option) (dbStruct *DBStruct, err error)

func (*DBStruct) Describe

func (p *DBStruct) Describe(tableName string) (tb DbTable, err error)

func (*DBStruct) DescribeQuery

func (p *DBStruct) DescribeQuery(query string) (tb DbTable, err error)

type DbField

type DbField struct {
	Field   string      `db:"Field"`
	Type    string      `db:"Type"`
	Null    string      `db:"Null"`
	Key     string      `db:"Key"`
	Default interface{} `db:"Default"`
	Extra   string      `db:"Extra"`

	Name      string
	GoType    reflect.Type
	StructTag reflect.StructTag
}

type DbTable

type DbTable struct {
	Name   string
	Fields []DbField
	// contains filtered or unexported fields
}

func (*DbTable) FieldByName

func (p *DbTable) FieldByName(name string) (field DbField, exist bool)

func (*DbTable) NewStruct

func (p *DbTable) NewStruct() (v interface{}, err error)

func (*DbTable) NewStructSlice

func (p *DbTable) NewStructSlice() (v interface{}, err error)

func (*DbTable) UpdateField

func (p *DbTable) UpdateField(name string, field DbField) (err error)

type NameMapperFunc

type NameMapperFunc func(string) string

type Option

type Option func(*Options)

func CreateTabelDSN

func CreateTabelDSN(dsn string) Option

func DataSource

func DataSource(dsn, driver string) Option

func NameMapper

func NameMapper(nameMap NameMapperFunc) Option

func Tagger

func Tagger(tagger TaggerFunc) Option

func TypeMapper

func TypeMapper(typeMapper TypeMapperFunc) Option

type Options

type Options struct {
	NameMap        NameMapperFunc
	Tagger         TaggerFunc
	TypeMapper     TypeMapperFunc
	DSN            string
	Driver         string
	CreateTableDSN string
}

type TaggerFunc

type TaggerFunc func(dbName, fieldName string) reflect.StructTag

type TypeMapperFunc

type TypeMapperFunc func(dbName, fieldName, dbFieldType string) reflect.Type

Jump to

Keyboard shortcuts

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