github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/gormgen/internal/model/base.go (about) 1 package model 2 3 import ( 4 "bytes" 5 "strings" 6 7 "github.com/unionj-cloud/go-doudou/v2/toolkit/gormgen/field" 8 ) 9 10 const ( 11 // DefaultModelPkg ... 12 DefaultModelPkg = "model" 13 // DefaultDtoPkg ... 14 DefaultDtoPkg = "dto" 15 ) 16 17 // Status sql status 18 type Status int 19 20 const ( 21 // UNKNOWN ... 22 UNKNOWN Status = iota 23 // SQL ... 24 SQL 25 // DATA ... 26 DATA 27 // VARIABLE ... 28 VARIABLE 29 // IF ... 30 IF 31 // ELSE ... 32 ELSE 33 // WHERE ... 34 WHERE 35 // SET ... 36 SET 37 // FOR ... 38 FOR 39 // END ... 40 END 41 // TRIM ... 42 TRIM 43 ) 44 45 // SourceCode source code 46 type SourceCode int 47 48 const ( 49 // Struct ... 50 Struct SourceCode = iota 51 // Table ... 52 Table 53 // Object ... 54 Object 55 ) 56 57 // GormKeywords ... 58 var GormKeywords = KeyWord{ 59 words: []string{ 60 "UnderlyingDB", "UseDB", "UseModel", "UseTable", "Quote", "Debug", "TableName", "WithContext", 61 "As", "Not", "Or", "Build", "Columns", "Hints", 62 "Distinct", "Omit", 63 "Select", "Where", "Order", "Group", "Having", "Limit", "Offset", 64 "Join", "LeftJoin", "RightJoin", 65 "Save", "Create", "CreateInBatches", 66 "Update", "Updates", "UpdateColumn", "UpdateColumns", 67 "Find", "FindInBatches", "First", "Take", "Last", "Pluck", "Count", 68 "Scan", "ScanRows", "Row", "Rows", 69 "Delete", "Unscoped", 70 "Scopes", 71 }, 72 } 73 74 // DOKeywords ... 75 var DOKeywords = KeyWord{ 76 words: []string{ 77 "Alias", "TableName", "WithContext", 78 }, 79 } 80 81 // GenKeywords ... 82 var GenKeywords = KeyWord{ 83 words: []string{ 84 "generateSQL", "whereClause", "setClause", 85 }, 86 } 87 88 // KeyWord ... 89 type KeyWord struct { 90 words []string 91 } 92 93 // FullMatch full match 94 func (g *KeyWord) FullMatch(word string) bool { 95 for _, item := range g.words { 96 if word == item { 97 return true 98 } 99 } 100 return false 101 } 102 103 // Contain contain 104 func (g *KeyWord) Contain(text string) bool { 105 for _, item := range g.words { 106 if strings.Contains(text, item) { 107 return true 108 } 109 } 110 return false 111 } 112 113 var ( 114 defaultDataType = "string" 115 dataType dataTypeMap = map[string]dataTypeMapping{ 116 "numeric": func(string) string { return "int32" }, 117 "integer": func(string) string { return "int32" }, 118 "int": func(string) string { return "int32" }, 119 "smallint": func(string) string { return "int32" }, 120 "mediumint": func(string) string { return "int32" }, 121 "bigint": func(string) string { return "int64" }, 122 "float": func(string) string { return "float32" }, 123 "real": func(string) string { return "float64" }, 124 "double": func(string) string { return "float64" }, 125 "decimal": func(string) string { return "float64" }, 126 "char": func(string) string { return "string" }, 127 "varchar": func(string) string { return "string" }, 128 "tinytext": func(string) string { return "string" }, 129 "mediumtext": func(string) string { return "string" }, 130 "longtext": func(string) string { return "string" }, 131 "binary": func(string) string { return "[]byte" }, 132 "varbinary": func(string) string { return "[]byte" }, 133 "tinyblob": func(string) string { return "[]byte" }, 134 "blob": func(string) string { return "[]byte" }, 135 "mediumblob": func(string) string { return "[]byte" }, 136 "longblob": func(string) string { return "[]byte" }, 137 "text": func(string) string { return "string" }, 138 "json": func(string) string { return "string" }, 139 "enum": func(string) string { return "string" }, 140 "time": func(string) string { return "time.Time" }, 141 "date": func(string) string { return "time.Time" }, 142 "datetime": func(string) string { return "time.Time" }, 143 "timestamp": func(string) string { return "time.Time" }, 144 "year": func(string) string { return "int32" }, 145 "bit": func(string) string { return "[]uint8" }, 146 "boolean": func(string) string { return "bool" }, 147 "tinyint": func(detailType string) string { 148 if strings.HasPrefix(strings.TrimSpace(detailType), "tinyint(1)") { 149 return "bool" 150 } 151 return "int32" 152 }, 153 } 154 ) 155 156 type dataTypeMapping func(detailType string) (finalType string) 157 158 type dataTypeMap map[string]dataTypeMapping 159 160 func (m dataTypeMap) Get(dataType, detailType string) string { 161 if convert, ok := m[strings.ToLower(dataType)]; ok { 162 return convert(detailType) 163 } 164 return defaultDataType 165 } 166 167 // Field user input structures 168 type Field struct { 169 Name string 170 Type string 171 ColumnName string 172 ColumnComment string 173 MultilineComment bool 174 Tag field.Tag 175 GORMTag field.GormTag 176 CustomGenType string 177 Relation *field.Relation 178 PriKey bool 179 } 180 181 // Tags ... 182 func (m *Field) Tags() string { 183 if _, ok := m.Tag[field.TagKeyGorm]; ok { 184 return m.Tag.Build() 185 } 186 187 if gormTag := strings.TrimSpace(m.GORMTag.Build()); gormTag != "" { 188 m.Tag.Set(field.TagKeyGorm, gormTag) 189 } 190 return m.Tag.Build() 191 } 192 193 // IsRelation ... 194 func (m *Field) IsRelation() bool { return m.Relation != nil } 195 196 // GenType ... 197 func (m *Field) GenType() string { 198 if m.IsRelation() { 199 return m.Type 200 } 201 if m.CustomGenType != "" { 202 return m.CustomGenType 203 } 204 typ := strings.TrimLeft(m.Type, "*") 205 switch typ { 206 case "string", "bytes": 207 return strings.Title(typ) 208 case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64": 209 return strings.Title(typ) 210 case "float64", "float32": 211 return strings.Title(typ) 212 case "bool": 213 return strings.Title(typ) 214 case "time.Time": 215 return "Time" 216 case "json.RawMessage", "[]byte": 217 return "Bytes" 218 case "serializer": 219 return "Serializer" 220 default: 221 return "Field" 222 } 223 } 224 225 // EscapeKeyword escape keyword 226 func (m *Field) EscapeKeyword() *Field { 227 return m.EscapeKeywordFor(GormKeywords) 228 } 229 230 // EscapeKeywordFor escape for specified keyword 231 func (m *Field) EscapeKeywordFor(keywords KeyWord) *Field { 232 if keywords.FullMatch(m.Name) { 233 m.Name += "_" 234 } 235 return m 236 } 237 238 // SQLBuffer sql buffer 239 type SQLBuffer struct{ bytes.Buffer } 240 241 // WriteSQL ... 242 func (s *SQLBuffer) WriteSQL(b byte) { 243 switch b { 244 case '\n', '\t', ' ': 245 if s.Len() == 0 || s.Bytes()[s.Len()-1] != ' ' { 246 _ = s.WriteByte(' ') 247 } 248 default: 249 _ = s.WriteByte(b) 250 } 251 } 252 253 // Dump ... 254 func (s *SQLBuffer) Dump() string { 255 defer s.Reset() 256 return s.String() 257 }