data

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2022 License: MIT Imports: 13 Imported by: 0

README

db.go

数据库维护函数

函数说明
package data

type Column struct {
	Name      string `json:"name"`   // 字段名
	Rename    string `json:"rename"` // 修改前的字段名
	Type      string `json:"type"`
	Length    string `json:"length,omitempty"`
	Default   string `json:"default"`
	Null      bool   `json:"null"`
	Signed    bool   `json:"signed"`
	Increment bool   `json:"increment"`
	Position  string `json:"position"` // 指定新增到什么位置,如 AFTER `updated_at`;
}

type Index struct {
	Name   string   `json:"name"`
	Type   string   `json:"type"`
	Fields []string `json:"fields"`
}

type Schema struct {
	Tablename string             `json:"tablename"`
	Charset   string             `json:"charset"`
	Engine    string             `json:"engine"`
	Increment string             `json:"increment"`
	Fields    map[string]*Column `json:"fields"`
	Indexes   map[string]*Index  `json:"indexes"`
}

type TableFixSqlOpt struct {
	Table1           *Schema // 需要修复的数据表结构
	Table2           *Schema // 基准数据表结构
	Strict           bool    // 使用严格模式, 严格模式将会把表2完全变成表1的结构, 否则将只处理表2种大于表1的内容(多出的字段和索引)
	CompareTableName bool    // 是否比较数据表名称,如果名称不一致,将会根据基准表创建一张新的表
	TablePre         string  // 生成sql语句中的表前缀
	TablePreOld      string  // 原结构的表前缀
}

type CDDiffs struct {
	Charset   bool `json:"charset"`
	Tablename bool `json:"tablename"`
	Engine    bool `json:"engine"`
}

type CDFields struct {
	Less    []string `json:"less"`
	Diff    []string `json:"diff"`
	Greater []string `json:"greater"`
}

type CDIndexes struct {
	Less    []string `json:"less"`
	Diff    []string `json:"diff"`
	Greater []string `json:"greater"`
}

type CompareDiffs struct {
	Diffs   *CDDiffs   `json:"diffs"`
	Fields  *CDFields  `json:"fields"`
	Indexes *CDIndexes `json:"indexes"`
}

type TableCreateSqlOpt struct {
	Table       *Schema
	TablePre    string // 生成sql语句中的表前缀
	TablePreOld string // 原结构的表前缀
}

// TableSchema 获得指定表的结构
func TableSchema(tableName string) (*Schema, error)

// TableFixSql 根据基准表生成修复差异的sql
func TableFixSql(opt TableFixSqlOpt) (sqls []string)

// SchemaCompare 比较两张表的结构差异
// table1 表结构
// table2 表结构 基准表
func SchemaCompare(table1 *Schema, table2 *Schema) *CompareDiffs

// TableCreateSql 根据数据表结构生成建表语句
func TableCreateSql(opt TableCreateSqlOpt) (sql string)

// TableSchemas 生成清空表内数据的sql语句
func TableSchemas(tableName string) (dump string)

// MakeInsertSql 获取某个表的insert语句
func MakeInsertSql(tableName string, start int, size int) (data string, result []map[string]interface{})

// BuildIndexSql 为数据表创建索引
func BuildIndexSql(index *Index) string

// BuildFieldSql 创建一个完整字段
func BuildFieldSql(field *Column) string

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildFieldSql

func BuildFieldSql(field *Column) string

BuildFieldSql 创建一个完整字段

func BuildIndexSql

func BuildIndexSql(index *Index) string

BuildIndexSql 为数据表创建索引

func Check

func Check() (gormDB *gorm.DB)

func InArray

func InArray(val interface{}, array interface{}) (exists bool)

func JsonStr2Map

func JsonStr2Map(str string) map[string]interface{}

func JsonStrSort

func JsonStrSort(jsonStr string) string

JsonStrSort 对json字符串进行排序

func MakeInsertSql

func MakeInsertSql(tableName string, start int, size int) (data string, result []map[string]interface{})

MakeInsertSql 获取某个表的insert语句

func StrReplace

func StrReplace(search interface{}, replace interface{}, subject interface{}, count int) (interface{}, error)

StrReplace 类似于php中的str_replace

func TableCreateSql

func TableCreateSql(opt TableCreateSqlOpt) (sql string)

TableCreateSql 根据数据表结构生成建表语句

func TableFixSql

func TableFixSql(opt TableFixSqlOpt) (sqls []string)

TableFixSql 根据基准表生成修复差异的sql

func TableSchemas

func TableSchemas(tableName string) (dump string)

TableSchemas 生成清空表内数据的sql语句

func ToString

func ToString(value interface{}) (key string)

ToString 将变量转为字符串 浮点型 3.0将会转换成字符串3, "3" 非数值或字符类型的变量将会被转换成JSON格式字符串

Types

type AllTableName

type AllTableName struct {
	TableName string `gorm:"table_name"`
}

func GetAllTableName

func GetAllTableName() (result []AllTableName)

GetAllTableName 获取数据库中所有的表名

type ArrStr

type ArrStr struct {
	Arr  []string // 数组
	Sort bool     // 执行ArrayValue方法时是否排序
}

func SetArrStr

func SetArrStr(str []string) *ArrStr

func (*ArrStr) ArrayDiff

func (a *ArrStr) ArrayDiff(oArr ...[]string) (diff []string)

func (*ArrStr) ArrayIntersect

func (a *ArrStr) ArrayIntersect(oArr ...[]string) (intersects []string)

func (*ArrStr) ArrayValue

func (a *ArrStr) ArrayValue() (value []string)

func (*ArrStr) DoSort

func (a *ArrStr) DoSort(sort bool) *ArrStr

DoSort 设置ArrayValue方法¬是否排序

type CDDiffs

type CDDiffs struct {
	Charset   bool `json:"charset"`
	Tablename bool `json:"tablename"`
	Engine    bool `json:"engine"`
}

type CDFields

type CDFields struct {
	Less    []string `json:"less"`
	Diff    []string `json:"diff"`
	Greater []string `json:"greater"`
}

type CDIndexes

type CDIndexes struct {
	Less    []string `json:"less"`
	Diff    []string `json:"diff"`
	Greater []string `json:"greater"`
}

type Column

type Column struct {
	Name      string `json:"name"`   // 字段名
	Rename    string `json:"rename"` // 修改前的字段名
	Type      string `json:"type"`
	Length    string `json:"length,omitempty"`
	Default   string `json:"default"`
	Null      bool   `json:"null"`
	Signed    bool   `json:"signed"`
	Increment bool   `json:"increment"`
	Position  string `json:"position"` // 指定新增到什么位置,如 AFTER `updated_at`;
}

type CompareDiffs

type CompareDiffs struct {
	Diffs   *CDDiffs   `json:"diffs"`
	Fields  *CDFields  `json:"fields"`
	Indexes *CDIndexes `json:"indexes"`
}

func SchemaCompare

func SchemaCompare(table1 *Schema, table2 *Schema) *CompareDiffs

SchemaCompare 比较两张表的结构差异

type Index

type Index struct {
	Name   string   `json:"name"`
	Type   string   `json:"type"`
	Fields []string `json:"fields"`
}

type MapStrInterface

type MapStrInterface struct {
	Data map[string]interface{}
	Keys []string
	Sort bool
}

func SetMapStrInterface

func SetMapStrInterface(data map[string]interface{}) *MapStrInterface

func (*MapStrInterface) ArrayKeys

func (d *MapStrInterface) ArrayKeys() []string

func (*MapStrInterface) ArrayValues

func (d *MapStrInterface) ArrayValues() []interface{}

func (*MapStrInterface) DoSort

func (d *MapStrInterface) DoSort(sort bool) *MapStrInterface

func (*MapStrInterface) GetData

func (d *MapStrInterface) GetData() map[string]interface{}

type Schema

type Schema struct {
	Tablename string             `json:"tablename"`
	Charset   string             `json:"charset"`
	Engine    string             `json:"engine"`
	Increment string             `json:"increment"`
	Fields    map[string]*Column `json:"fields"`
	Indexes   map[string]*Index  `json:"indexes"`
}

func TableSchema

func TableSchema(tableName string) (*Schema, error)

TableSchema 获得指定数据表的结构

type Struct2JsonOpt

type Struct2JsonOpt struct {
	Struct   interface{} // 结构体
	Sort     bool        // 是否需要排序
	NeedFile bool        // 是否需要输出json文件
	FilePath string      // 输出json文件路径
}

func SetStruct

func SetStruct(_struct interface{}) *Struct2JsonOpt

func (*Struct2JsonOpt) DoSort

func (opt *Struct2JsonOpt) DoSort() *Struct2JsonOpt

func (*Struct2JsonOpt) File

func (opt *Struct2JsonOpt) File(filepath string) *Struct2JsonOpt

func (*Struct2JsonOpt) ToJson

func (opt *Struct2JsonOpt) ToJson() (string, error)

type TableCreateSqlOpt

type TableCreateSqlOpt struct {
	Table       *Schema
	TablePre    string // 生成sql语句中的表前缀
	TablePreOld string // 原结构的表前缀
}

type TableFixSqlOpt

type TableFixSqlOpt struct {
	Table1           *Schema // 需要修复的数据表结构
	Table2           *Schema // 基准数据表结构
	Strict           bool    // 使用严格模式, 严格模式将会把表2完全变成表1的结构, 否则将只处理表2种大于表1的内容(多出的字段和索引)
	CompareTableName bool    // 是否比较数据表名称,如果名称不一致,将会根据基准表创建一张新的表
	TablePre         string  // 生成sql语句中的表前缀
	TablePreOld      string  // 原结构的表前缀
}

Jump to

Keyboard shortcuts

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