diff --git a/internal/create_files/create_files.go b/internal/create_files/create_files.go index 4b1176a..548d474 100644 --- a/internal/create_files/create_files.go +++ b/internal/create_files/create_files.go @@ -6,7 +6,6 @@ import ( "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/log" "github.com/ManyakRus/starter/micro" "github.com/iancoleman/strcase" @@ -101,40 +100,6 @@ func DeleteFuncFromComment(Text, Comment string) string { } -// Has_Column_ExtID_ConnectionID - возвращает true если есть поля ExtId и ConnectionID -func Has_Column_ExtID_ConnectionID(Table1 *types.Table) bool { - Otvet := false - - // - _, ok := Table1.MapColumns["ext_id"] - if ok == false { - return Otvet - } - - // - _, ok = Table1.MapColumns["connection_id"] - if ok == false { - return Otvet - } - - Otvet = true - return Otvet -} - -// Has_Column_IsDeleted - возвращает true если есть поле is_deleted -func Has_Column_IsDeleted(Table1 *types.Table) bool { - Otvet := false - - // - _, ok := Table1.MapColumns["is_deleted"] - if ok == false { - return Otvet - } - - Otvet = true - return Otvet -} - // DeleteCommentFromFuncName - удаляет комментарий с названием функции func DeleteCommentFromFuncName(Text, FuncName string) string { Otvet := Text @@ -336,24 +301,6 @@ func AddImport(Text, URL string) string { return Otvet } -// FindHasTimeColumn - возвращает true если есть колонка с типом время -func FindHasTimeColumn(Table1 *types.Table) bool { - Otvet := false - - for _, Column1 := range Table1.MapColumns { - SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type] - if ok == false { - log.Panic("GetMappings() ", Column1.Type, " error: not found") - } - if SQLMapping1.GoType == "time.Time" { - Otvet = true - break - } - } - - return Otvet -} - // AddImportTime - добавляет покет в секцию Import, если его там нет func AddImportTime(TextModel string) string { Otvet := TextModel @@ -380,7 +327,7 @@ func AddImportTime(TextModel string) string { func CheckAndAddImportTime_FromTable(TextModel string, Table1 *types.Table) string { Otvet := TextModel - HasTimeColumn := FindHasTimeColumn(Table1) + HasTimeColumn := Has_ColumnType_Time(Table1) if HasTimeColumn == false { return Otvet } diff --git a/internal/create_files/create_files_columns.go b/internal/create_files/create_files_columns.go new file mode 100644 index 0000000..4f3f0d5 --- /dev/null +++ b/internal/create_files/create_files_columns.go @@ -0,0 +1,308 @@ +package create_files + +import ( + "github.com/ManyakRus/crud_generator/internal/types" + "github.com/ManyakRus/crud_generator/pkg/dbmeta" + "github.com/ManyakRus/starter/log" +) + +// Has_Column_ExtID_ConnectionID - возвращает true если есть поля ExtId и ConnectionID +func Has_Column_ExtID_ConnectionID(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["ext_id"] + if ok == false { + return Otvet + } + + // + _, ok = Table1.MapColumns["connection_id"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_IsDeleted - возвращает true если есть поле is_deleted +func Has_Column_IsDeleted(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["is_deleted"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_ColumnType_Time - возвращает true если есть колонка с типом время +func Has_ColumnType_Time(Table1 *types.Table) bool { + Otvet := false + + for _, Column1 := range Table1.MapColumns { + SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type] + if ok == false { + log.Panic("GetMappings() ", Column1.Type, " error: not found") + } + if SQLMapping1.GoType == "time.Time" { + Otvet = true + break + } + } + + return Otvet +} + +func Has_Column_ID(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["id"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_ExtID - возвращает true если есть поле ext_id +func Has_Column_ExtID(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["ext_id"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_CreatedAt - возвращает true если есть поле created_at +func Has_Column_CreatedAt(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["created_at"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_ModifiedAt - возвращает true если есть поле modified_at +func Has_Column_ModifiedAt(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["modified_at"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_DeletedAt - возвращает true если есть поле deleted_at +func Has_Column_DeletedAt(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["deleted_at"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_TableNameID - возвращает true если есть поле table_name_id +func Has_Column_TableNameID(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["table_name_id"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_TableRowID - возвращает true если есть поле table_row_id +func Has_Column_TableRowID(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["table_row_id"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_IsGroup - возвращает true если есть поле is_group +func Has_Column_IsGroup(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["is_group"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_ParentID - возвращает true если есть поле parent_id +func Has_Column_ParentID(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["parent_id"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_Name - возвращает true если есть поле name +func Has_Column_Name(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["name"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// Has_Column_Description - возвращает true если есть поле description +func Has_Column_Description(Table1 *types.Table) bool { + Otvet := false + + // + _, ok := Table1.MapColumns["description"] + if ok == false { + return Otvet + } + + Otvet = true + return Otvet +} + +// ---- + +// Has_Columns_CommonStruct - возвращает true если есть все общие структуры +func Has_Columns_CommonStruct(Table1 *types.Table) bool { + Otvet := false + + Otvet = Has_Column_ExtID(Table1) && Has_Column_CreatedAt(Table1) && Has_Column_ModifiedAt(Table1) && Has_Column_DeletedAt(Table1) && Has_Column_IsDeleted(Table1) && Has_Column_ID(Table1) + + return Otvet +} + +// Has_Columns_NameStruct - возвращает true если есть колонки name + description +func Has_Columns_NameStruct(Table1 *types.Table) bool { + Otvet := false + + Otvet = Has_Column_Name(Table1) && Has_Column_Description(Table1) + + return Otvet +} + +// Has_Columns_Groups - возвращает true если есть колонки is_group + parent_id +func Has_Columns_Groups(Table1 *types.Table) bool { + Otvet := false + + Otvet = Has_Column_IsGroup(Table1) && Has_Column_ParentID(Table1) + + return Otvet +} + +// Has_Columns_ExtLink - возвращает true если есть колонки table_name_id + table_row_id +func Has_Columns_ExtLink(Table1 *types.Table) bool { + Otvet := false + + Otvet = Has_Column_TableNameID(Table1) && Has_Column_TableRowID(Table1) + + return Otvet +} + +// ---- + +// Is_Column_CommonStruct - возвращает true если это колонка ext_id, created_at, modified_at, deleted_at, id +func Is_Column_CommonStruct(Column1 *types.Column) bool { + Otvet := false + + ColumnName := Column1.Name + + switch ColumnName { + case "ext_id", "created_at", "modified_at", "deleted_at", "is_deleted", "id": + Otvet = true + } + + return Otvet +} + +// Is_Column_NameStruct - возвращает true если это колонка name или description +func Is_Column_NameStruct(Column1 *types.Column) bool { + Otvet := false + + ColumnName := Column1.Name + + switch ColumnName { + case "name", "description": + Otvet = true + } + + return Otvet +} + +// Is_Column_GroupsStruct - возвращает true если это колонка is_group, parent_id +func Is_Column_GroupsStruct(Column1 *types.Column) bool { + Otvet := false + + ColumnName := Column1.Name + + switch ColumnName { + case "is_group", "parent_id": + Otvet = true + } + + return Otvet +} + +// Is_Column_ExtLinksStruct - возвращает true если это колонка table_name_id, table_row_id +func Is_Column_ExtLinksStruct(Column1 *types.Column) bool { + Otvet := false + + ColumnName := Column1.Name + + switch ColumnName { + case "table_name_id", "table_row_id": + Otvet = true + } + + return Otvet +} diff --git a/internal/create_files/model/model.go b/internal/create_files/model/model.go index c4fae49..a8e4652 100644 --- a/internal/create_files/model/model.go +++ b/internal/create_files/model/model.go @@ -1,6 +1,7 @@ package model import ( + "errors" "github.com/ManyakRus/crud_generator/internal/config" "github.com/ManyakRus/crud_generator/internal/constants" "github.com/ManyakRus/crud_generator/internal/create_files" @@ -100,7 +101,7 @@ func CreateFilesModel_struct(Table1 *types.Table, DirTemplatesModel, DirReadyMod } TextModel = DeleteFuncFind_byExtID(TextModel, ModelName, Table1) - TextModel = create_files.CheckAndAddImportTime_FromTable(TextModel, Table1) + TextModel = create_files.CheckAndAddImportTime_FromText(TextModel) TextModel = create_files.DeleteImportModel(TextModel) //запись файла @@ -171,11 +172,46 @@ type ` + ModelName + ` struct { } sort.Strings(keys) + has_Columns_CommonStruct := create_files.Has_Columns_CommonStruct(Table1) + has_Columns_NameStruct := create_files.Has_Columns_NameStruct(Table1) + has_Columns_Groups := create_files.Has_Columns_Groups(Table1) + has_Columns_ExtLinks := create_files.Has_Columns_ExtLink(Table1) + + ImportModelsName := micro.LastWord(config.Settings.TEMPLATE_FOLDERNAME_MODEL) + + if has_Columns_CommonStruct == true { + Otvet = Otvet + "\t" + ImportModelsName + ".CommonStruct\n" + } + + if has_Columns_NameStruct == true { + Otvet = Otvet + "\t" + ImportModelsName + ".NameStruct\n" + } + + if has_Columns_Groups == true { + Otvet = Otvet + "\t" + ImportModelsName + ".GroupStruct\n" + } + + if has_Columns_ExtLinks == true { + Otvet = Otvet + "\t" + ImportModelsName + ".ExtLinkStruct\n" + } + //цикл по всем колонкам for _, key1 := range keys { Column1, _ := Table1.MapColumns[key1] + + //пропускаем колонки если они уже есть в CommonStruct + if has_Columns_CommonStruct == true && create_files.Is_Column_CommonStruct(Column1) == true { + continue + } else if has_Columns_NameStruct == true && create_files.Is_Column_NameStruct(Column1) == true { + continue + } else if has_Columns_Groups == true && create_files.Is_Column_GroupsStruct(Column1) == true { + continue + } else if has_Columns_ExtLinks == true && create_files.Is_Column_ExtLinksStruct(Column1) == true { + continue + } + var TextColumn string - TextModel, TextColumn = FindTextColumn(TextModel, Table1, &Column1) + TextModel, TextColumn = FindTextColumn(TextModel, Table1, Column1) Otvet = Otvet + TextColumn + "\n" Table1.MapColumns[key1] = Column1 } @@ -192,15 +228,15 @@ func FindTextColumn(TextModel string, Table1 *types.Table, Column1 *types.Column ColumnName := Column1.Name ColumnNameLowerCase := strings.ToLower(ColumnName) ColumnModelName := create_files.FormatName(Column1.Name) - Column1.NameGo = ColumnModelName + //Column1.NameGo = ColumnModelName //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 - TextModel, Type_go = FindColumnTypeGo(TextModel, Table1, Column1) - Column1.TypeGo = Type_go + TextModel, Type_go = FindColumnTypeGoImport(TextModel, Table1, Column1) + //Column1.TypeGo = Type_go TextDefaultValue := create_files.FindTextDefaultValue(Type_go) TextPrimaryKey := FindTextPrimaryKey(Column1.IsIdentity) Description := Column1.Description @@ -364,8 +400,8 @@ func DeleteFuncFind_byExtID(TextModel, Modelname string, Table1 *types.Table) st return Otvet } -// FindColumnTypeGo - заменяет ID на Alias -func FindColumnTypeGo(TextModel string, Table1 *types.Table, Column1 *types.Column) (string, string) { +// FindColumnTypeGoImport - заменяет ID на Alias +func FindColumnTypeGoImport(TextModel string, Table1 *types.Table, Column1 *types.Column) (string, string) { Otvet := Column1.TypeGo //тип колонки из БД или из convert_id.json @@ -388,3 +424,22 @@ func FindColumnTypeGo(TextModel string, Table1 *types.Table, Column1 *types.Colu return TextModel, Otvet } + +// FillColumnsNameGo - заполняет NameGo во все колонки +func FillColumnsNameGo(MapAll *map[string]*types.Table) error { + var err error + + for _, Table1 := range *MapAll { + for _, Column1 := range Table1.MapColumns { + ColumnName := Column1.Name + ColumnModelName := create_files.FormatName(ColumnName) + Column1.NameGo = ColumnModelName + if ColumnModelName == "" { + err = errors.New("Table: " + Table1.Name + " Column: " + ColumnName + " = \"\"") + } + } + + } + + return err +} diff --git a/internal/logic/logic.go b/internal/logic/logic.go index 7e918e5..0733ebe 100644 --- a/internal/logic/logic.go +++ b/internal/logic/logic.go @@ -28,6 +28,12 @@ func StartFillAll() error { return err } + ////заполним типы TypeGo + //err = model.FillColumnsNameGo(&MapAll) + //if err != nil { + // return err + //} + //модель err = model.CreateAllFiles(MapAll) if err != nil { diff --git a/internal/postgres/postgres.go b/internal/postgres/postgres.go index 779e2c8..1f44f8d 100644 --- a/internal/postgres/postgres.go +++ b/internal/postgres/postgres.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "github.com/ManyakRus/crud_generator/internal/config" + "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/crud_generator/pkg/dbmeta" @@ -188,7 +189,7 @@ order by } //заполним MapTable - MapColumns := make(map[string]types.Column, 0) + MapColumns := make(map[string]*types.Column, 0) OrderNumberColumn := 0 OrderNumberTable := 0 TableName0 := "" @@ -197,7 +198,7 @@ order by if v.TableName != TableName0 { OrderNumberColumn = 0 Table1.MapColumns = MapColumns - MapColumns = make(map[string]types.Column, 0) + MapColumns = make(map[string]*types.Column, 0) if TableName0 != "" { //MassTable = append(MassTable, Table1) MapTable[TableName0] = Table1 @@ -212,14 +213,16 @@ order by Column1 := types.Column{} Column1.Name = v.ColumnName Column1.Type = v.ColumnType + FillNameGo(&Column1) //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 + FillTypeGo(&Column1) + //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" { @@ -233,7 +236,7 @@ order by Column1.TableKey = v.ColumnTableKey Column1.ColumnKey = v.ColumnColumnKey - MapColumns[v.ColumnName] = Column1 + MapColumns[v.ColumnName] = &Column1 //Table1.Columns = append(Table1.Columns, Column1) OrderNumberColumn++ @@ -255,7 +258,7 @@ order by func CreateTable() *types.Table { Otvet := &types.Table{} - Otvet.MapColumns = make(map[string]types.Column, 0) + Otvet.MapColumns = make(map[string]*types.Column, 0) return Otvet } @@ -331,3 +334,36 @@ func FindNameTypeID(Table1 *types.Table) (string, string) { // } // //} + +// FillNameGo - заполняет NameGo во все колонки +func FillNameGo(Column1 *types.Column) error { + var err error + + ColumnName := Column1.Name + ColumnNameGo := create_files.FormatName(ColumnName) + Column1.NameGo = ColumnNameGo + if ColumnNameGo == "" { + err = errors.New("Column: " + ColumnName + " Type: " + Column1.Type + " NameGo= \"\"") + } + + return err +} + +// FillTypeGo - заполняет тип golang из типа postgres +func FillTypeGo(Column1 *types.Column) error { + var err error + + SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type] + if ok == false { + log.Panic("GetMappings() ", Column1.Type, " error: not found") + } + + ColumnName := Column1.Name + Type_go := SQLMapping1.GoType + Column1.TypeGo = Type_go + if Type_go == "" { + err = errors.New("Column: " + ColumnName + " Type: " + Column1.Type + " TypeGo= \"\"") + } + + return err +} diff --git a/internal/types/types.go b/internal/types/types.go index 409926a..4a555a9 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -16,7 +16,7 @@ type Column struct { type Table struct { Name string `json:"name" gorm:"column:name;default:''"` //Element *etree.Element - MapColumns map[string]Column + MapColumns map[string]*Column //Columns []Column OrderNumber int NameGo string