mirror of
https://github.com/ManyakRus/crud_generator.git
synced 2024-11-21 05:05:52 +02:00
сделал name_replace.json
This commit is contained in:
parent
b8eb2ad0ab
commit
2266b2f578
11
bin/templates/configs/name_replace.json
Normal file
11
bin/templates/configs/name_replace.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"inn": "INN",
|
||||
"kpp": "KPP",
|
||||
"okpo": "OKPO",
|
||||
"ogrn": "OGRN",
|
||||
"okato": "OKATO",
|
||||
"nsi_flat": "NSIFlat",
|
||||
"nsi_flat_id": "NSIFlatID",
|
||||
"nsi_id": "NSIID",
|
||||
"www": "WWW"
|
||||
}
|
@ -24,6 +24,11 @@ func FindSingularName(s string) string {
|
||||
func FormatName(Name string) string {
|
||||
Otvet := Name
|
||||
|
||||
Otvet1, ok := types.MapReplaceName[Name]
|
||||
if ok == true {
|
||||
return Otvet1
|
||||
}
|
||||
|
||||
switch strings.ToLower(Name) {
|
||||
case "id":
|
||||
Otvet = "ID"
|
||||
@ -31,16 +36,12 @@ func FormatName(Name string) string {
|
||||
Otvet = strcase.ToCamel(Otvet)
|
||||
}
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
func IsNumberType(TypeGo string) bool {
|
||||
Otvet := false
|
||||
|
||||
switch TypeGo {
|
||||
case "int", "int8", "int16", "int32", "int64", "float32", "float64", "uint", "uint8", "uint16", "uint32", "uint64", "byte":
|
||||
{
|
||||
Otvet = true
|
||||
//_id в конце заменяем на ID
|
||||
len1 := len(Name)
|
||||
if len1 >= 3 {
|
||||
last3 := strings.ToLower(Name[len1-3:])
|
||||
if last3 == "_id" {
|
||||
Otvet = Otvet[:len1-2-1] + "ID"
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,3 +8,11 @@ func TestFindSingularName(t *testing.T) {
|
||||
t.Error("TestFindSingularName() error: Otvet =''")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatName(t *testing.T) {
|
||||
Name := "contract_id"
|
||||
Otvet := FormatName(Name)
|
||||
if Otvet == "" {
|
||||
t.Error("TestFormatName() error")
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"github.com/ManyakRus/crud_generator/internal/config"
|
||||
"github.com/ManyakRus/crud_generator/internal/constants"
|
||||
"github.com/ManyakRus/crud_generator/internal/create_files"
|
||||
"github.com/ManyakRus/crud_generator/internal/mini_func"
|
||||
"github.com/ManyakRus/crud_generator/internal/types"
|
||||
"github.com/ManyakRus/starter/log"
|
||||
"github.com/ManyakRus/starter/micro"
|
||||
@ -130,6 +131,12 @@ func CreateTestFiles(Table1 *types.Table) error {
|
||||
}
|
||||
TextDB = DeleteFuncTestFind_byExtID(TextDB, Table1)
|
||||
|
||||
//ID Minimum
|
||||
if Table1.IDMinimum != "" {
|
||||
TextFind := "const Postgres_ID_Test = "
|
||||
TextDB = strings.ReplaceAll(TextDB, TextFind+"1", TextFind+Table1.IDMinimum)
|
||||
}
|
||||
|
||||
//запись файла
|
||||
err = os.WriteFile(FilenameReadyDB, []byte(TextDB), constants.FILE_PERMISSIONS)
|
||||
|
||||
@ -300,7 +307,7 @@ func AddTextOmit(TextDB string, Table1 *types.Table) string {
|
||||
}
|
||||
|
||||
`
|
||||
} else if create_files.IsNumberType(TypeGo) == true && Column1.TableKey != "" {
|
||||
} else if mini_func.IsNumberType(TypeGo) == true && Column1.TableKey != "" {
|
||||
TextFind := `if m.` + ColumnNameGo + ` == 0 {`
|
||||
pos1 := strings.Index(TextDB, TextFind)
|
||||
if pos1 >= 0 {
|
||||
|
@ -120,11 +120,12 @@ func FindTextColumn(Column1 *types.Column) string {
|
||||
ColumnName := Column1.Name
|
||||
ColumnModelName := create_files.FormatName(Column1.Name)
|
||||
Column1.NameGo = ColumnModelName
|
||||
SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type]
|
||||
if ok == false {
|
||||
log.Panic("GetMappings() ", Column1.Type, " error: not found")
|
||||
}
|
||||
Type_go := SQLMapping1.GoType
|
||||
//SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type]
|
||||
//if ok == false {
|
||||
// log.Panic("GetMappings() ", Column1.Type, " error: not found")
|
||||
//}
|
||||
//Type_go := SQLMapping1.GoType
|
||||
Type_go := Column1.TypeGo
|
||||
Column1.TypeGo = Type_go
|
||||
TextDefaultValue := FindTextDefaultValue(Type_go)
|
||||
TextPrimaryKey := FindTextPrimaryKey(Column1.IsIdentity)
|
||||
|
@ -1,18 +1,50 @@
|
||||
package load_configs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/ManyakRus/crud_generator/internal/config"
|
||||
"github.com/ManyakRus/crud_generator/internal/types"
|
||||
"github.com/ManyakRus/crud_generator/pkg/dbmeta"
|
||||
"github.com/ManyakRus/starter/log"
|
||||
"github.com/ManyakRus/starter/micro"
|
||||
"os"
|
||||
)
|
||||
|
||||
// LoadMappingsAll - загружает маппинг ТипБД = ТипGolang, из файла .json
|
||||
func LoadMappingsAll() {
|
||||
func LoadConfigsAll() {
|
||||
LoadMappings()
|
||||
LoadNameReplace()
|
||||
}
|
||||
|
||||
// LoadMappings - загружает маппинг ТипБД = ТипGolang, из файла .json
|
||||
func LoadMappings() {
|
||||
dir := micro.ProgramDir_bin()
|
||||
Filename := dir + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + "configs" + micro.SeparatorFile() + "mapping.json"
|
||||
err := dbmeta.LoadMappings(Filename, false)
|
||||
FileName := dir + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + "configs" + micro.SeparatorFile() + "mapping.json"
|
||||
err := dbmeta.LoadMappings(FileName, false)
|
||||
if err != nil {
|
||||
log.Panic("LoadMappings() error: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// LoadNameReplace - загружает маппинг ТипБД = ТипGolang, из файла .json
|
||||
func LoadNameReplace() {
|
||||
dir := micro.ProgramDir_bin()
|
||||
FileName := dir + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + "configs" + micro.SeparatorFile() + "name_replace.json"
|
||||
|
||||
var err error
|
||||
|
||||
//чтение файла
|
||||
bytes, err := os.ReadFile(FileName)
|
||||
if err != nil {
|
||||
TextError := fmt.Sprint("ReadFile() error: ", err)
|
||||
log.Panic(TextError)
|
||||
}
|
||||
|
||||
//json в map
|
||||
//var MapServiceURL2 = make(map[string]string)
|
||||
err = json.Unmarshal(bytes, &types.MapReplaceName)
|
||||
if err != nil {
|
||||
log.Panic("Unmarshal() error: ", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ func StartApp() {
|
||||
config.FillSettings()
|
||||
config.FillFlags()
|
||||
|
||||
load_configs.LoadMappingsAll()
|
||||
load_configs.LoadConfigsAll()
|
||||
|
||||
postgres_gorm.StartDB()
|
||||
postgres_gorm.GetConnection().Logger.LogMode(1)
|
||||
|
14
internal/mini_func/mini_func.go
Normal file
14
internal/mini_func/mini_func.go
Normal file
@ -0,0 +1,14 @@
|
||||
package mini_func
|
||||
|
||||
func IsNumberType(TypeGo string) bool {
|
||||
Otvet := false
|
||||
|
||||
switch TypeGo {
|
||||
case "int", "int8", "int16", "int32", "int64", "float32", "float64", "uint", "uint8", "uint16", "uint32", "uint64", "byte":
|
||||
{
|
||||
Otvet = true
|
||||
}
|
||||
}
|
||||
|
||||
return Otvet
|
||||
}
|
1
internal/mini_func/mini_func_test.go
Normal file
1
internal/mini_func/mini_func_test.go
Normal file
@ -0,0 +1 @@
|
||||
package mini_func
|
@ -2,10 +2,13 @@ package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/ManyakRus/crud_generator/internal/config"
|
||||
"github.com/ManyakRus/crud_generator/internal/mini_func"
|
||||
"github.com/ManyakRus/crud_generator/internal/types"
|
||||
"github.com/ManyakRus/crud_generator/pkg/dbmeta"
|
||||
"github.com/ManyakRus/starter/contextmain"
|
||||
"github.com/ManyakRus/starter/log"
|
||||
"github.com/ManyakRus/starter/postgres_gorm"
|
||||
@ -201,6 +204,16 @@ order by
|
||||
Column1 := types.Column{}
|
||||
Column1.Name = v.ColumnName
|
||||
Column1.Type = v.ColumnType
|
||||
|
||||
//Type_go
|
||||
SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type]
|
||||
if ok == false {
|
||||
log.Panic("GetMappings() ", Column1.Type, " error: not found")
|
||||
}
|
||||
Type_go := SQLMapping1.GoType
|
||||
Column1.TypeGo = Type_go
|
||||
|
||||
//
|
||||
if v.ColumnIsIdentity == "YES" {
|
||||
Column1.IsIdentity = true
|
||||
}
|
||||
@ -218,11 +231,17 @@ order by
|
||||
OrderNumberColumn++
|
||||
TableName0 = v.TableName
|
||||
}
|
||||
|
||||
//
|
||||
if Table1.Name != "" {
|
||||
Table1.MapColumns = MapColumns
|
||||
MapTable[TableName0] = Table1
|
||||
}
|
||||
|
||||
//FillTypeGo(MapTable)
|
||||
|
||||
FillIDMinimum(MapTable)
|
||||
|
||||
return MapTable, err
|
||||
}
|
||||
|
||||
@ -232,3 +251,75 @@ func CreateTable() *types.Table {
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
func FillIDMinimum(MapTable map[string]*types.Table) {
|
||||
var err error
|
||||
|
||||
//соединение
|
||||
db := postgres_gorm.GetConnection()
|
||||
ctxMain := contextmain.GetContext()
|
||||
|
||||
for TableName, Table1 := range MapTable {
|
||||
//текст запроса
|
||||
NameID, TypeGo := FindNameTypeID(Table1)
|
||||
if NameID == "" {
|
||||
continue
|
||||
}
|
||||
TextSQL := "SELECT Min(" + NameID + ") from \"" + postgres_gorm.Settings.DB_SCHEMA + "\".\"" + TableName + "\""
|
||||
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*60)
|
||||
defer ctxCancelFunc()
|
||||
db.WithContext(ctx)
|
||||
|
||||
//запрос
|
||||
tx := db.Raw(TextSQL)
|
||||
err = tx.Error
|
||||
if err != nil {
|
||||
log.Panic("Wrong SQL query: ", TextSQL, " error: ", err)
|
||||
}
|
||||
|
||||
var IDMinimum sql.NullString
|
||||
tx = tx.Scan(&IDMinimum)
|
||||
err = tx.Error
|
||||
if err != nil {
|
||||
log.Panic("Wrong SQL Scan(): ", TextSQL, " error: ", err)
|
||||
}
|
||||
|
||||
//
|
||||
if TypeGo == "string" {
|
||||
Table1.IDMinimum = `"` + IDMinimum.String + `"`
|
||||
} else if mini_func.IsNumberType(TypeGo) == true {
|
||||
Table1.IDMinimum = IDMinimum.String
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func FindNameTypeID(Table1 *types.Table) (string, string) {
|
||||
Otvet := ""
|
||||
Type := ""
|
||||
|
||||
for ColumnName, Column1 := range Table1.MapColumns {
|
||||
if Column1.IsIdentity == true {
|
||||
return ColumnName, Column1.TypeGo
|
||||
}
|
||||
}
|
||||
|
||||
return Otvet, Type
|
||||
}
|
||||
|
||||
//// FillTypeGo - заполняет тип golang из типа postgres
|
||||
//func FillTypeGo(MapTable map[string]*types.Table) {
|
||||
//
|
||||
// for _, Table1 := range MapTable {
|
||||
// for _, Column1 := range Table1.MapColumns {
|
||||
//
|
||||
// SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type]
|
||||
// if ok == false {
|
||||
// log.Panic("GetMappings() ", Column1.Type, " error: not found")
|
||||
// }
|
||||
// Type_go := SQLMapping1.GoType
|
||||
// Column1.TypeGo = Type_go
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
@ -20,4 +20,7 @@ type Table struct {
|
||||
//Columns []Column
|
||||
OrderNumber int
|
||||
NameGo string
|
||||
IDMinimum string
|
||||
}
|
||||
|
||||
var MapReplaceName = make(map[string]string, 0)
|
||||
|
Loading…
Reference in New Issue
Block a user