Ai
1 Star 0 Fork 0

tomatomeatman/GolangRepository

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ColumnInfo.go 18.95 KB
一键复制 编辑 原始数据 按行查看 历史
tomatomeatman 提交于 2023-07-17 18:13 +08:00 . no commit message
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
package model
import (
"strings"
)
/**
* 表统一字段信息
* @author HuangXinBian
*/
type ColumnInfo struct {
GsTableName string `json:"sTableName" gorm:"column:sTableName; type:varchar; DEFAULT:''"` //字段所在表名
GiIndex int `json:"iIndex" gorm:"column:iIndex; type:int; DEFAULT:'99'"` //序号
GsField string `json:"sField" gorm:"column:sField; type:varchar; DEFAULT:''"` //在数据库中的名字段
GsName string `json:"sName" gorm:"column:sName; type:varchar; DEFAULT:''"` //字段名(驼峰名称头字母小写)
GsType string `json:"sType" gorm:"column:sType; type:varchar; DEFAULT:''"` //字段类型
GiTypeLength int `json:"iTypeLength" gorm:"column:iTypeLength; type:int; DEFAULT:'1'"` //字段长度
GiNull int `json:"iNull" gorm:"column:iNull; type:int; DEFAULT:'1'"` //是否允许为空
GiKey int `json:"iKey" gorm:"column:iKey; type:int; DEFAULT:'2'"` //是否主键
GsDefaultData string `json:"sDefaultData" gorm:"column:sDefaultData; type:varchar; DEFAULT:''"` //默认值
GiExtra int `json:"iExtra" gorm:"column:iExtra; type:int; DEFAULT:'2'"` //是否自增长
GsComment string `json:"sComment" gorm:"column:sComment; type:varchar; DEFAULT:''"` //字段备注
GsTypeByDelphi string `json:"sTypeByDelphi" gorm:"column:sTypeByDelphi; type:varchar; DEFAULT:''"` //字段类型
GsTypeByJava string `json:"sTypeByJava" gorm:"column:sTypeByJava; type:varchar; DEFAULT:''"` //字段类型
GsTableObj string `json:"sTableObj" gorm:"column:sTableObj; type:varchar; DEFAULT:''"` //首字母小写的表名(用于java实体对象)
GsKeyColumnName string `json:"sKeyColumnName" gorm:"column:sKeyColumnName; type:varchar; DEFAULT:''"` //主键列明
GiDecimal int `json:"iDecimal" gorm:"column:iDecimal; type:int; DEFAULT:'2'"` //类型是否有小数
GiIntegralLength int `json:"iIntegralLength" gorm:"column:iIntegralLength; type:int; DEFAULT:''"` //整数位的长度
GiDecimalLength int `json:"iDecimalLength" gorm:"column:iDecimalLength; type:int; DEFAULT:''"` //小数位的长度
GsSimplComment string `json:"sSimplComment" gorm:"column:sSimplComment; type:varchar; DEFAULT:''"` //字段简洁备注(去除括号的内容)
GiDictionary int `json:"iDictionary" gorm:"column:iDictionary; type:int; DEFAULT:''"` //是否字典
GiDictionaryCommon int `json:"iDictionaryCommon" gorm:"column:iDictionaryCommon; type:int; DEFAULT:''"` //通用字典
GiEnum int `json:"iEnum" gorm:"column:iEnum; type:int; DEFAULT:''"` //枚举值
GsEnumArray map[string]string `json:"sEnumArray" gorm:"column:sEnumArray; type:varchaintr; DEFAULT:''"` //枚举值集合
GiBoolean int `json:"iBoolean" gorm:"column:iBoolean; type:int; DEFAULT:''"` //布尔值
GiRelTable int `json:"iRelTable" gorm:"column:iRelTable; type:int; DEFAULT:''"` //关联表
GsRelTableName string `json:"sRelTableName" gorm:"column:sRelTableName; type:varchar; DEFAULT:''"` //关联表名(带库名)
GsRelColumnValue string `json:"sRelColumnValue" gorm:"column:sRelColumnValue; type:varchar; DEFAULT:''"` //关联字段值
GsRelColumnName string `json:"sRelColumnName" gorm:"column:sRelColumnName; type:varchar; DEFAULT:''"` //关联字段名
GsRelColumnAs string `json:"sRelColumnAs" gorm:"column:sRelColumnAs; type:varchar; DEFAULT:''"` //关联后的别名
GsNameObj string `json:"sNameObj"` //驼峰名称(头字母大写)
}
/**
* 初始化对象
*/
func (this *ColumnInfo) SetDefault() {
this.GiIndex = 999 //序号
this.GiTypeLength = -1 //字段长度
this.GiNull = -1 //是否允许为空
this.GiKey = -1 //是否主键
this.GsDefaultData = "" //默认值
this.GiExtra = -1 //是否自增长
this.GiDecimal = -1 //类型是否有小数
this.GiIntegralLength = -1 //整数位的长度
this.GiDecimalLength = -1 //小数位的长度
this.GiDictionary = -1 //是否字典
this.GiDictionaryCommon = -1 //通用字典
this.GiEnum = -1 //枚举值
this.GsEnumArray = nil //枚举值集合
this.GiBoolean = -1 //布尔值
this.GiRelTable = -1 //关联表
}
/**
* 处理附属信息
*/
func (this *ColumnInfo) Supplementary() {
this.GsName = hump(this.GsField, true)
this.GsComment = strings.TrimSpace(this.GsComment)
this.GsTableObj = hump(this.GsTableObj, true)
this.GsSimplComment = this.getsSimplComment()
this.GiDictionary = this.getiDictionary()
this.GiDictionaryCommon = this.getiDictionaryCommon()
this.GiEnum = this.getiEnum()
this.GiBoolean = this.getiBoolean()
this.GiRelTable = this.getiRelTable()
this.GsRelTableName = this.getsRelTableName()
this.GsRelColumnValue = this.getsRelColumnValue()
this.GsRelColumnName = this.getsRelColumnName()
this.GsRelColumnAs = this.getsRelColumnAs()
this.GsEnumArray = this.getsEnumArray()
this.GsNameObj = hump(this.GsField, false) //驼峰名称(头字母大写)
}
/**
* 获得 字典
* @return iDictionary 字典
*/
func (this ColumnInfo) getiDictionary() int {
if "" == this.GsComment {
return 0
}
if strings.Index(this.GsComment, "(字典") > -1 {
return 1
}
return 0
}
/**
* 获得 通用字典
* @return iDictionaryCommon 通用字典
*/
func (this ColumnInfo) getiDictionaryCommon() int {
if "" == this.GsComment {
return 0
}
if strings.Index(this.GsComment, "(通用字典") > -1 {
return 1
}
return 0
}
/**
* 获得 枚举值
* @return iEnum 枚举值
*/
func (this ColumnInfo) getiEnum() int {
if "" == this.GsComment {
return 0
}
if strings.Index(this.GsComment, "(枚举") > -1 {
return 1
}
return 0
}
/**
* 获得 布尔值
* @return iBoolean 布尔值
*/
func (this ColumnInfo) getiBoolean() int {
if "" == this.GsComment {
return 0
}
if strings.Index(this.GsComment, "(布尔值") > -1 {
return 1
}
return 0
}
/**
* 获得 关联表
* @return iRelTable 关联表
*/
func (this ColumnInfo) getiRelTable() int {
if "" == this.GsComment {
return 0
}
if strings.Index(this.GsComment, "(关联表") > -1 {
return 1
}
return 0
}
/**
* 获得 关联表名(带库名)
* @return sRelTableName 关联表名(带库名)
*/
func (this ColumnInfo) getsRelTableName() string {
if "" == this.GsComment {
return ""
}
if "" == this.GsComment { //所属科室(关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|)
return ""
}
iSt := strings.Index(this.GsComment, "(")
iEd := strings.Index(this.GsComment, ")")
if (iSt < 0) || (iEd < 0) || (iSt > iEd) {
return ""
}
temp := this.GsComment[iSt+1 : iEd] //关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|
array := strings.Split(temp, "|")
if len(array) < 3 {
return ""
}
return strings.TrimSpace(array[1])
}
/**
* 获得 关联字段值
* @return sRelColumnValue 关联字段值
*/
func (this ColumnInfo) getsRelColumnValue() string {
if "" == this.GsComment {
return ""
}
if "" == this.GsComment { //所属科室(关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|)
return ""
}
iSt := strings.Index(this.GsComment, "(")
iEd := strings.Index(this.GsComment, ")")
if iSt < 0 || iEd < 0 || iSt > iEd {
return ""
}
temp := this.GsComment[iSt+1 : iEd] //关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|
array := strings.Split(temp, "|")
if len(array) < 4 {
return ""
}
return array[2]
}
/**
* 获得 关联字段名
* @return sRelColumnName 关联字段名
*/
func (this ColumnInfo) getsRelColumnName() string {
if "" == this.GsComment {
return ""
}
if "" == this.GsComment { //所属科室(关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|)
return ""
}
iSt := strings.Index(this.GsComment, "(")
iEd := strings.Index(this.GsComment, ")")
if (iSt < 0) || (iEd < 0) || (iSt > iEd) {
return ""
}
temp := this.GsComment[iSt+1 : iEd] //关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|
array := strings.Split(temp, "|")
if len(array) < 5 {
return ""
}
return array[3]
}
/**
* 获得 关联后的别名
* @return sRelColumnAs 关联后的别名
*/
func (this ColumnInfo) getsRelColumnAs() string {
if "" == this.GsComment {
return ""
}
if "" == this.GsComment { //所属科室(关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|)
return ""
}
iSt := strings.Index(this.GsComment, "(")
iEd := strings.Index(this.GsComment, ")")
if iSt < 0 || iEd < 0 || iSt > iEd {
return ""
}
temp := this.GsComment[iSt+1 : iEd] //关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|
array := strings.Split(temp, "|")
if len(array) < 6 {
return ""
}
return array[4]
}
/**
* 获得 枚举值集合
* @return
*/
func (this ColumnInfo) getsEnumArray() map[string]string {
if "" == this.GsComment { //"固定资产(布尔值,1:是;0否)"
return map[string]string{}
}
iSt := strings.Index(this.GsComment, "(")
iEd := strings.Index(this.GsComment, ")")
if (iSt < 0) || (iEd < 0) || (iSt > iEd) {
return map[string]string{}
}
tempComment := this.GsComment[iSt+1 : iEd] //"布尔值,1:是;0否"
array := strings.Split(tempComment, ",")
if len(array) < 2 {
return map[string]string{}
}
m := map[string]string{}
array = strings.Split(array[1], ";")
for _, val := range array {
temp := strings.Split(val, ":")
if len(temp) < 2 {
m[temp[0]] = ""
continue
}
m[temp[0]] = temp[1]
}
return m
}
/**
* 获得 字段简洁备注(去除括号的内容)
* @return sSimplComment 字段简洁备注(去除括号的内容)
*/
func (this ColumnInfo) getsSimplComment() string {
if "" == this.GsComment {
return ""
}
iEd := strings.Index(this.GsComment, "(")
if iEd < 0 {
return strings.TrimSpace(this.GsComment)
}
if this.GsEnumArray["xxx"] == "" {
}
return strings.TrimSpace(this.GsComment[0:iEd])
}
//-------------------------------------------------------------------------
// FirstUpper 字符串首字母大写
/**
* 字符串首字母大写
* @param str
* @return
*/
func firstUpperStr(s string) string {
if s == "" {
return ""
}
return strings.ToUpper(s[:1]) + s[1:]
}
/**
* 首字母小写
* @param str
* @return
*/
func firstLowerStr(str string) string {
if str == "" {
return ""
}
return strings.ToLower(str[:1]) + str[1:]
}
/**
* 转变驼峰字符串
* @param str 字符串
* @param firstLower 首字母小写
* @return
*/
func hump(str string, firstLower bool) string {
str = strings.TrimSpace(str)
if "" == str {
return ""
}
if !strings.Contains(str, "_") && (str != strings.ToUpper(str)) && (str != strings.ToLower(str)) {
if firstLower {
return firstLowerStr(str) //不含下划线,不是全大写,不是全小写,则首字母小写后符合规则
}
return str
}
var build strings.Builder
doUpper := true
for _, ch := range str {
if ch == 95 { //如果是下划线,则下一个字符转换大写,同时抛弃当前字符
doUpper = true
continue
}
if !doUpper { //转换大写标志未启动
build.WriteString(strings.ToLower(string(ch)))
continue
}
build.WriteString(strings.ToUpper(string(ch)))
doUpper = false //下一个不转
}
if firstLower {
return firstLowerStr(build.String()) //首字母小写
}
return build.String()
}
//-------------------------------------------------------------------------
// /**
// * 初始化 '字段简洁备注(去除括号的内容)'
// */
// func (this ColumnInfo) InitsSimplComment(sComment string) string {
// sComment = strings.TrimSpace(sComment)
// if "" == sComment {
// this.GsSimplComment = ""
// return ""
// }
// iEd := strings.Index(sComment, "(")
// if iEd < 0 {
// this.GsSimplComment = sComment
// return sComment
// }
// this.GsSimplComment = string([]byte(sComment)[:iEd])
// return this.GsSimplComment
// }
// /**
// * 初始化 '是否字典'
// */
// func (this ColumnInfo) InitiDictionary(sComment string) int {
// sComment = strings.TrimSpace(sComment)
// if "" == sComment {
// this.GiDictionary = 2
// return 2
// }
// iEd := strings.Index(sComment, "(字典")
// if iEd < 0 {
// this.GiDictionary = 2
// return 2
// }
// this.GiDictionary = 1
// return 1
// }
// /**
// * 初始化 '是否通用字典'
// */
// func (this ColumnInfo) getiDictionaryCommon(sComment string) int {
// sComment = strings.TrimSpace(sComment)
// if "" == sComment {
// this.GiDictionaryCommon = 2
// return 2
// }
// iEd := strings.Index(sComment, "(通用字典")
// if iEd < 0 {
// this.GiDictionaryCommon = 2
// return 2
// }
// this.GiDictionaryCommon = 1
// return 1
// }
// /**
// * 初始化 '是否枚举值'
// */
// func (this ColumnInfo) InitiEnum(sComment string) int {
// sComment = strings.TrimSpace(sComment)
// if "" == sComment {
// this.GiEnum = 2
// return 2
// }
// iEd := strings.Index(sComment, "(通用字典")
// if iEd < 0 {
// this.GiEnum = 2
// return 2
// }
// this.GiEnum = 1
// return 1
// }
// /**
// * 初始化 '是否布尔值'
// */
// func (this ColumnInfo) InitiBoolean(sComment string) int {
// sComment = strings.TrimSpace(sComment)
// if "" == sComment {
// this.GiBoolean = 2
// return 2
// }
// iEd := strings.Index(sComment, "(布尔值")
// if iEd < 0 {
// this.GiBoolean = 2
// return 2
// }
// this.GiBoolean = 1
// return 1
// }
// /**
// * 初始化 '是否关联表'
// */
// func (this ColumnInfo) InitiRelTable(sComment string) int {
// sComment = strings.TrimSpace(sComment)
// if "" == sComment {
// this.GiRelTable = 2
// return 2
// }
// iEd := strings.Index(sComment, "(关联表")
// if iEd < 0 {
// this.GiRelTable = 2
// return 2
// }
// this.GiRelTable = 1
// return 1
// }
// /**
// * 初始化 '关联表名(带库名)'
// */
// func (this ColumnInfo) InitsRelTableName(sComment string) string {
// sComment = strings.TrimSpace(sComment) //所属科室(关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|)
// if "" == sComment {
// this.GsRelTableName = ""
// return ""
// }
// iSt := strings.Index(sComment, "(")
// iEd := strings.Index(sComment, ")")
// if iSt < 0 || iEd < 0 || iSt > iEd {
// return ""
// }
// temp := string([]byte(sComment)[iSt+1 : iEd]) //关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|
// array := strings.Split(temp, "|")
// if len(array) < 3 {
// return ""
// }
// this.GsRelTableName = array[1]
// return this.GsRelTableName
// }
// /**
// * 初始化 '关联字段值'
// */
// func (this ColumnInfo) InitsRelColumnValue(sComment string) string {
// sComment = strings.TrimSpace(sComment) //所属科室(关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|)
// if "" == sComment {
// this.GsRelColumnValue = ""
// return ""
// }
// iSt := strings.Index(sComment, "(")
// iEd := strings.Index(sComment, ")")
// if iSt < 0 || iEd < 0 || iSt > iEd {
// return ""
// }
// temp := string([]byte(sComment)[iSt+1 : iEd]) //关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|
// array := strings.Split(temp, "|")
// if len(array) < 4 {
// return ""
// }
// this.GsRelColumnValue = array[2]
// return this.GsRelColumnValue
// }
// /**
// * 初始化 '关联字段名'
// */
// func (this ColumnInfo) InitsRelColumnName(sComment string) string {
// sComment = strings.TrimSpace(sComment) //所属科室(关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|)
// if "" == sComment {
// this.GsRelColumnName = ""
// return ""
// }
// iSt := strings.Index(sComment, "(")
// iEd := strings.Index(sComment, ")")
// if iSt < 0 || iEd < 0 || iSt > iEd {
// return ""
// }
// temp := string([]byte(sComment)[iSt+1 : iEd]) //关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|
// array := strings.Split(temp, "|")
// if len(array) < 5 {
// return ""
// }
// this.GsRelColumnName = array[3]
// return this.GsRelColumnName
// }
// /**
// * 初始化 '关联后的别名'
// */
// func (this ColumnInfo) InitsRelColumnAs(sComment string) string {
// sComment = strings.TrimSpace(sComment) //所属科室(关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|)
// if "" == sComment {
// this.GsRelColumnAs = ""
// return ""
// }
// iSt := strings.Index(sComment, "(")
// iEd := strings.Index(sComment, ")")
// if iSt < 0 || iEd < 0 || iSt > iEd {
// return ""
// }
// temp := string([]byte(sComment)[iSt+1 : iEd]) //关联表,|${BricksBaseSystem}Department|sId|sName|sDepartmentText|
// array := strings.Split(temp, "|")
// if len(array) < 5 {
// return ""
// }
// this.GsRelColumnAs = array[4]
// return this.GsRelColumnAs
// }
// /**
// * 初始化 '枚举值集合'
// */
// func (this ColumnInfo) InitsEnumArray(sComment string) map[string]string {
// sComment = strings.TrimSpace(sComment) //"固定资产(布尔值,1:是;0否)"
// if "" == sComment {
// this.GsEnumArray = nil
// return nil
// }
// iSt := strings.Index(sComment, "(")
// iEd := strings.Index(sComment, ")")
// if iSt < 0 || iEd < 0 || iSt > iEd {
// return nil
// }
// temp := string([]byte(sComment)[iSt+1 : iEd]) //"布尔值,1:是;0否"
// array := strings.Split(temp, ",")
// if len(array) < 2 {
// return nil
// }
// tempMap := make(map[string]string)
// array = strings.Split(array[1], ";")
// for _, item := range array {
// item = strings.TrimSpace(item)
// subItem := strings.Split(item, ":")
// if len(subItem) < 2 {
// tempMap[subItem[0]] = ""
// continue
// }
// tempMap[subItem[0]] = subItem[1]
// }
// if len(tempMap) < 1 {
// return nil
// }
// this.GsEnumArray = tempMap
// return this.GsEnumArray
// }
//-----------------------------------------------------
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/tomatomeatman/golang-repository.git
git@gitee.com:tomatomeatman/golang-repository.git
tomatomeatman
golang-repository
GolangRepository
1a0844a34204

搜索帮助