1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-11-21 05:05:52 +02:00

сделал NEED_USE_DB_VIEWS=false

This commit is contained in:
Nikitin Aleksandr 2024-10-10 16:00:43 +03:00
parent 4d575a4e72
commit cbc3ff089f
6 changed files with 139 additions and 96 deletions

View File

@ -566,6 +566,15 @@
"guregu_type": "null.Int",
"go_nullable_type": "sql.NullInt64",
"swagger_type": "int"
},
{
"sql_type": "oid",
"go_type": "int64",
"json_type": "Integer",
"protobuf_type": "int64",
"guregu_type": "null.Int",
"go_nullable_type": "sql.NullInt64",
"swagger_type": "int64"
}
]
}

View File

@ -26,6 +26,10 @@ EXCLUDE_TABLES="ext_"
#SINGULAR_TABLE_NAMES: true = table names will be not renamed to plural name
SINGULAR_TABLE_NAMES=true
#NEED_USE_DB_VIEWS - need create crud for database views, same as usual tables
NEED_USE_DB_VIEWS=false
#---------------------Template settings---------------------
#SERVICE_REPOSITORY_URL - URL of your new service repository (Github, gitlab, etc.)

View File

@ -198,6 +198,8 @@ type SettingsINI struct {
SERVER_GRPC_TABLE_CACHE_TEST_FILENAME string
TEXT_READALL string
NEED_USE_DB_VIEWS bool
}
func Getenv(Name string, IsRequired bool) string {
@ -1034,6 +1036,11 @@ func FillSettings() {
s = Getenv(Name, true)
Settings.TEXT_READALL = s
//
Name = "NEED_USE_DB_VIEWS"
s = Getenv(Name, true)
Settings.NEED_USE_DB_VIEWS = BoolFromString(s)
}
// CurrentDirectory - возвращает текущую директорию ОС

View File

@ -29,33 +29,23 @@ import (
"github.com/ManyakRus/crud_generator/internal/load_configs"
"github.com/ManyakRus/crud_generator/internal/postgres"
"github.com/ManyakRus/crud_generator/internal/types"
"github.com/ManyakRus/crud_generator/pkg/dbmeta"
"github.com/ManyakRus/starter/log"
"github.com/ManyakRus/starter/micro"
)
//var MassTable []types.Table
// FillGlobalVariables - заполняет глобальные переменные
func FillGlobalVariables(MapAll map[string]*types.Table) {
// MassFindBy
Mass1 := create_files.FindMass_TableColumns(MapAll, types.MassFindBy_String)
types.MassFindBy = Mass1
// MassFindMassBy
Mass1 = create_files.FindMass_TableColumns(MapAll, types.MassFindMassBy_String)
types.MassFindMassBy = Mass1
// ReadAll
types.MapReadAll = load_configs.LoadReadAll(MapAll)
}
func StartFillAll() error {
var err error
//заполним MapAll
MapAll, err := postgres.FillMapTable()
SettingsFillFromDatabase := types.SettingsFillFromDatabase{}
SettingsFillFromDatabase.MapDBTypes = dbmeta.GetMappings()
SettingsFillFromDatabase.INCLUDE_TABLES = config.Settings.INCLUDE_TABLES
SettingsFillFromDatabase.EXCLUDE_TABLES = config.Settings.EXCLUDE_TABLES
SettingsFillFromDatabase.NEED_USE_DB_VIEWS = config.Settings.NEED_USE_DB_VIEWS
MapAll, err := postgres.FillMapTable(SettingsFillFromDatabase)
if err != nil {
log.Error("FillMapTable() error: ", err)
return err
@ -240,3 +230,19 @@ func StartFillAll() error {
return err
}
// FillGlobalVariables - заполняет глобальные переменные
func FillGlobalVariables(MapAll map[string]*types.Table) {
// MassFindBy
Mass1 := create_files.FindMass_TableColumns(MapAll, types.MassFindBy_String)
types.MassFindBy = Mass1
// MassFindMassBy
Mass1 = create_files.FindMass_TableColumns(MapAll, types.MassFindMassBy_String)
types.MassFindMassBy = Mass1
// ReadAll
types.MapReadAll = load_configs.LoadReadAll(MapAll)
}

View File

@ -5,10 +5,8 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/ManyakRus/crud_generator/internal/config"
"github.com/ManyakRus/crud_generator/internal/create_files"
"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"
@ -36,11 +34,11 @@ type TableRowsStruct struct {
}
// FillMapTable - возвращает массив MassTable данными из БД
func FillMapTable() (map[string]*types.Table, error) {
func FillMapTable(SettingsFill types.SettingsFillFromDatabase) (map[string]*types.Table, error) {
MapTable := make(map[string]*types.Table, 0)
var err error
MapTable, err = FillMapTable1()
MapTable, err = FillMapTable1(SettingsFill)
if err != nil {
log.Error("FillMapTable1() error: ", err)
return MapTable, err
@ -63,7 +61,7 @@ func FillMapTable() (map[string]*types.Table, error) {
}
// FillMapTable1 - возвращает массив MassTable данными из БД
func FillMapTable1() (map[string]*types.Table, error) {
func FillMapTable1(SettingsFill types.SettingsFillFromDatabase) (map[string]*types.Table, error) {
var err error
//MassTable := make([]types.Table, 0)
MapTable := make(map[string]*types.Table, 0)
@ -182,9 +180,7 @@ order by
table_name,
is_identity desc,
column_name
`
SCHEMA := strings.Trim(postgres_gorm.Settings.DB_SCHEMA, " ")
@ -192,12 +188,16 @@ order by
TextSQL = strings.ReplaceAll(TextSQL, "public", SCHEMA)
}
if config.Settings.INCLUDE_TABLES != "" {
TextSQL = strings.ReplaceAll(TextSQL, "--INCLUDE_TABLES", "and c.table_name ~* '"+config.Settings.INCLUDE_TABLES+"'")
if SettingsFill.INCLUDE_TABLES != "" {
TextSQL = strings.ReplaceAll(TextSQL, "--INCLUDE_TABLES", "and c.table_name ~* '"+SettingsFill.INCLUDE_TABLES+"'")
}
if config.Settings.EXCLUDE_TABLES != "" {
TextSQL = strings.ReplaceAll(TextSQL, "--EXCLUDE_TABLES", "and c.table_name !~* '"+config.Settings.EXCLUDE_TABLES+"'")
if SettingsFill.EXCLUDE_TABLES != "" {
TextSQL = strings.ReplaceAll(TextSQL, "--EXCLUDE_TABLES", "and c.table_name !~* '"+SettingsFill.EXCLUDE_TABLES+"'")
}
if SettingsFill.NEED_USE_DB_VIEWS == true {
TextSQL = strings.ReplaceAll(TextSQL, "\tand v.table_name is null", "")
}
//соединение
@ -304,7 +304,7 @@ order by
FillNameGo(&Column1)
//Type_go
FillTypeGo(&Column1)
FillTypeGo(SettingsFill, &Column1)
//SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type]
//if ok == false {
// log.Panic("GetMappings() ", Column1.Type, " error: not found")
@ -354,64 +354,64 @@ func CreateTable() *types.Table {
return Otvet
}
// FillIDMinimum - находим минимальный ID, для тестов с этим ID
func FillIDMinimum(MapTable map[string]*types.Table) error {
var err error
//соединение
db := postgres_gorm.GetConnection()
ctxMain := contextmain.GetContext()
Schema := strings.Trim(postgres_gorm.Settings.DB_SCHEMA, " ")
for TableName, Table1 := range MapTable {
//текст запроса
NameID, TypeGo := FindNameType_from_PrimaryKey(Table1)
if NameID == "" {
continue
}
TextSQL := ""
Is_UUID_Type := create_files.Is_UUID_Type(TypeGo)
if Is_UUID_Type == false {
DefaultValueSQL := create_files.FindText_DefaultValueSQL(TypeGo)
TextSQL = `SELECT
Min("` + NameID + `") as id_minimum
FROM
"` + Schema + `"."` + TableName + `"
WHERE
"` + NameID + `" <> ` + DefaultValueSQL
} else {
TextSQL = `SELECT "` + NameID + `" as id_minimum
FROM "` + Schema + `"."` + TableName + `"
WHERE "` + NameID + `" is not null
ORDER BY ` + NameID + `
LIMIT 1`
}
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)
}
//
ColumnPK := create_files.Find_PrimaryKeyColumn(Table1)
ColumnPK.IDMinimum = IDMinimum.String
}
return err
}
//// FillIDMinimum - находим минимальный ID, для тестов с этим ID
//func FillIDMinimum(MapTable map[string]*types.Table) error {
// var err error
//
// //соединение
// db := postgres_gorm.GetConnection()
// ctxMain := contextmain.GetContext()
//
// Schema := strings.Trim(postgres_gorm.Settings.DB_SCHEMA, " ")
//
// for TableName, Table1 := range MapTable {
// //текст запроса
// NameID, TypeGo := FindNameType_from_PrimaryKey(Table1)
// if NameID == "" {
// continue
// }
// TextSQL := ""
// Is_UUID_Type := create_files.Is_UUID_Type(TypeGo)
// if Is_UUID_Type == false {
// DefaultValueSQL := create_files.FindText_DefaultValueSQL(TypeGo)
// TextSQL = `SELECT
// Min("` + NameID + `") as id_minimum
// FROM
// "` + Schema + `"."` + TableName + `"
// WHERE
// "` + NameID + `" <> ` + DefaultValueSQL
// } else {
// TextSQL = `SELECT "` + NameID + `" as id_minimum
// FROM "` + Schema + `"."` + TableName + `"
// WHERE "` + NameID + `" is not null
// ORDER BY ` + NameID + `
// LIMIT 1`
// }
// 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)
// }
//
// //
// ColumnPK := create_files.Find_PrimaryKeyColumn(Table1)
// ColumnPK.IDMinimum = IDMinimum.String
// }
//
// return err
//}
// FillIDMinimum_ManyPK - находим минимальный ID, для тестов с этим ID, для многих Primary Key
func FillIDMinimum_ManyPK(MapTable map[string]*types.Table) error {
@ -625,34 +625,41 @@ func FindNameType_from_PrimaryKey(Table1 *types.Table) (string, string) {
//}
// FillNameGo - заполняет NameGo во все колонки
func FillNameGo(Column1 *types.Column) error {
func FillNameGo(Column1 *types.Column) {
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= \"\"")
err = errors.New("FillNameGo() error: Column: " + ColumnName + " Type: " + Column1.Type + " NameGo= \"\"")
log.Panic(err)
}
return err
return
}
// FillTypeGo - заполняет тип golang из типа postgres
func FillTypeGo(Column1 *types.Column) error {
func FillTypeGo(SettingsFill types.SettingsFillFromDatabase, Column1 *types.Column) {
var err error
SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type]
//SQLMapping1, ok := dbmeta.GetMappings()[Column1.Type]
//if ok == false {
// log.Panic("GetMappings() ", Column1.Type, " error: not found")
//}
SQLMapping1, ok := SettingsFill.MapDBTypes[Column1.Type]
if ok == false {
log.Panic("GetMappings() ", Column1.Type, " error: not found")
log.Panic("FillTypeGo() Column1.Type: ", 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= \"\"")
err = errors.New("FillTypeGo() error: Column: " + ColumnName + " Type: " + Column1.Type + " TypeGo= \"\"")
log.Panic(err)
}
return err
return
}

View File

@ -1,5 +1,7 @@
package types
import "github.com/ManyakRus/crud_generator/pkg/dbmeta"
type Column struct {
Name string `json:"name" gorm:"column:name;default:''"`
Type string `json:"type_name" gorm:"column:type_name;default:''"`
@ -78,3 +80,11 @@ var MassFindMassBy = make([]TableColumns, 0)
// MapReadAll - таблицы, для которых нужна функция ReadAll()
var MapReadAll = make(map[*Table]bool, 0)
// SettingsFillFromDatabase - настройки для заполнения данных из базы данных
type SettingsFillFromDatabase struct {
INCLUDE_TABLES string
EXCLUDE_TABLES string
NEED_USE_DB_VIEWS bool
MapDBTypes map[string]*dbmeta.SQLMapping //карта соответсвий типов в базе данных и типов в golang
}