1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-12-22 00:36:41 +02:00

сделал FillRowsCount()

This commit is contained in:
Nikitin Aleksandr 2024-03-21 11:25:12 +03:00
parent edbf66a86b
commit 7b77851e48
8 changed files with 98 additions and 4 deletions

View File

@ -227,4 +227,7 @@ NEED_CREATE_CACHE_TEST_FILES=true
TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME="grpc_client_table_cache.go_" TEMPLATES_GRPC_CLIENT_TABLES_CACHE_FILENAME="grpc_client_table_cache.go_"
#TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME - short filename of "grpc_client_table_cache_test.go_" file #TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME - short filename of "grpc_client_table_cache_test.go_" file
TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME="grpc_client_table_cache_test.go_" TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME="grpc_client_table_cache_test.go_"
#CACHE_ELEMENTS_COUNT_MAX - maximum elements counts in cache, for every table
CACHE_ELEMENTS_COUNT_MAX=1000

View File

@ -12,8 +12,8 @@ import (
// cache - кэш с данными // cache - кэш с данными
var cache *expirable.LRU[int64, lawsuit_status_types.LawsuitStatusType] var cache *expirable.LRU[int64, lawsuit_status_types.LawsuitStatusType]
// CACHE_SIZE - количество элементо в кэше // CACHE_SIZE - количество элементов в кэше
const CACHE_SIZE = 100 const CACHE_SIZE = 1000
// CACHE_EXPIRE_MINUTES - время жизни элемента в кэше // CACHE_EXPIRE_MINUTES - время жизни элемента в кэше
const CACHE_EXPIRE_MINUTES = 86400 const CACHE_EXPIRE_MINUTES = 86400

View File

@ -88,6 +88,7 @@ type SettingsINI struct {
TEXT_MODULE_GENERATED string TEXT_MODULE_GENERATED string
READY_ALIAS_FILENAME string READY_ALIAS_FILENAME string
NEED_CREATE_UPDATE_EVERY_COLUMN bool NEED_CREATE_UPDATE_EVERY_COLUMN bool
CACHE_ELEMENTS_COUNT_MAX int64
} }
func Getenv(Name string, IsRequired bool) string { func Getenv(Name string, IsRequired bool) string {
@ -480,6 +481,15 @@ func FillSettings() {
s = Getenv(Name, true) s = Getenv(Name, true)
Settings.TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME = s Settings.TEMPLATES_GRPC_CLIENT_TABLES_CACHE_TEST_FILENAME = s
//
Name = "CACHE_ELEMENTS_COUNT_MAX"
s = Getenv(Name, true)
i, err := micro.Int64FromString(s)
if err != nil {
log.Error("CACHE_ELEMENTS_COUNT_MAX: ", s, " Int64FromString() error: ", err)
}
Settings.CACHE_ELEMENTS_COUNT_MAX = i
} }
// CurrentDirectory - возвращает текущую директорию ОС // CurrentDirectory - возвращает текущую директорию ОС

View File

@ -62,3 +62,4 @@ const SERVER_GRPC_TABLE_CACHE_FILENAME = "server_grpc_table_cache.go_"
const SERVER_GRPC_TABLE_CACHE_TEST_FILENAME = "server_grpc_table_cache_test.go_" const SERVER_GRPC_TABLE_CACHE_TEST_FILENAME = "server_grpc_table_cache_test.go_"
const TEXT_OTVET_ID_ALIAS = "Otvet.ID = ID" const TEXT_OTVET_ID_ALIAS = "Otvet.ID = ID"
const TEXT_CACHE_SIZE_1000 = "CACHE_SIZE"

View File

@ -1374,3 +1374,24 @@ func FilenameWithoutLastUnderline(Filename string) string {
return Otvet return Otvet
} }
// FillVariable - заменяет переменную в тексте
func FillVariable(Text, VariableName, Value string) string {
Otvet := Text
sFind := VariableName + " = "
pos1 := strings.Index(Otvet, sFind)
if pos1 < 0 {
return Otvet
}
s2 := Text[pos1:]
posEnd := strings.Index(s2, "\n")
if posEnd < 0 {
return Otvet
}
Otvet = Otvet[:pos1] + VariableName + " = " + Value + Otvet[pos1+posEnd:]
return Otvet
}

View File

@ -828,6 +828,13 @@ func CreateFilesCache(Table1 *types.Table) error {
//alias //alias
TextCache = create_files.ConvertIDToAlias_OtvetID(TextCache, Table1) TextCache = create_files.ConvertIDToAlias_OtvetID(TextCache, Table1)
//const CACHE_SIZE = 1000
CACHE_ELEMENTS_COUNT_MAX := config.Settings.CACHE_ELEMENTS_COUNT_MAX
Count_Now := Table1.RowsCount
CACHE_ELEMENTS_COUNT := micro.MinInt64(Count_Now, CACHE_ELEMENTS_COUNT_MAX)
sCACHE_ELEMENTS_COUNT := micro.StringFromInt64(CACHE_ELEMENTS_COUNT)
TextCache = create_files.FillVariable(TextCache, constants.TEXT_CACHE_SIZE_1000, sCACHE_ELEMENTS_COUNT)
//удаление пустого импорта //удаление пустого импорта
TextCache = create_files.DeleteEmptyImport(TextCache) TextCache = create_files.DeleteEmptyImport(TextCache)

View File

@ -30,6 +30,11 @@ type TableColumn struct {
TableComment string `json:"table_comment" gorm:"column:table_comment;default:''"` TableComment string `json:"table_comment" gorm:"column:table_comment;default:''"`
} }
type TableRowsStruct struct {
IDMinimum sql.NullString `json:"id_min" gorm:"column:id_min;default:0"`
RowsCount sql.NullInt64 `json:"rows_count" gorm:"column:rows_count;default:0"`
}
// FillMapTable - возвращает массив MassTable данными из БД // FillMapTable - возвращает массив MassTable данными из БД
func FillMapTable() (map[string]*types.Table, error) { func FillMapTable() (map[string]*types.Table, error) {
var err error var err error
@ -261,6 +266,7 @@ order by
//FillTypeGo(MapTable) //FillTypeGo(MapTable)
FillIDMinimum(MapTable) FillIDMinimum(MapTable)
FillRowsCount(MapTable)
return MapTable, err return MapTable, err
} }
@ -272,6 +278,7 @@ func CreateTable() *types.Table {
return Otvet return Otvet
} }
// FillIDMinimum - находим минимальный ID, для тестов с этим ID
func FillIDMinimum(MapTable map[string]*types.Table) { func FillIDMinimum(MapTable map[string]*types.Table) {
var err error var err error
@ -286,7 +293,7 @@ func FillIDMinimum(MapTable map[string]*types.Table) {
continue continue
} }
DefaultValueSQL := create_files.FindTextDefaultValueSQL(TypeGo) DefaultValueSQL := create_files.FindTextDefaultValueSQL(TypeGo)
TextSQL := "SELECT Min(" + NameID + ") from \"" + postgres_gorm.Settings.DB_SCHEMA + "\".\"" + TableName + "\" WHERE " + NameID + " <> " + DefaultValueSQL TextSQL := "SELECT Min(" + NameID + ") as id_minimum FROM \"" + postgres_gorm.Settings.DB_SCHEMA + "\".\"" + TableName + "\" WHERE " + NameID + " <> " + DefaultValueSQL
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*60) ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*60)
defer ctxCancelFunc() defer ctxCancelFunc()
db.WithContext(ctx) db.WithContext(ctx)
@ -299,6 +306,7 @@ func FillIDMinimum(MapTable map[string]*types.Table) {
} }
var IDMinimum sql.NullString var IDMinimum sql.NullString
//TableRows := TableRowsStruct{}
tx = tx.Scan(&IDMinimum) tx = tx.Scan(&IDMinimum)
err = tx.Error err = tx.Error
if err != nil { if err != nil {
@ -315,6 +323,49 @@ func FillIDMinimum(MapTable map[string]*types.Table) {
} }
// FillRowsCount - находим количество строк в таблице, для кэша
func FillRowsCount(MapTable map[string]*types.Table) {
var err error
//соединение
db := postgres_gorm.GetConnection()
ctxMain := contextmain.GetContext()
for TableName, Table1 := range MapTable {
//текст запроса
//только Postgres SQL
TextSQL := `
SELECT
reltuples::bigint AS rows_count
FROM
pg_class
WHERE
oid = 'public."` + TableName + `"'::regclass
`
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 RowsCount sql.NullInt64
//TableRows := TableRowsStruct{}
tx = tx.Scan(&RowsCount)
err = tx.Error
if err != nil {
log.Panic("Wrong SQL Scan(): ", TextSQL, " error: ", err)
}
Table1.RowsCount = RowsCount.Int64
}
}
func FindNameTypeID(Table1 *types.Table) (string, string) { func FindNameTypeID(Table1 *types.Table) (string, string) {
Otvet := "" Otvet := ""
Type := "" Type := ""

View File

@ -22,6 +22,7 @@ type Table struct {
NameGo string NameGo string
IDMinimum string IDMinimum string
Comment string `json:"table_comment" gorm:"column:table_comment;default:''"` Comment string `json:"table_comment" gorm:"column:table_comment;default:''"`
RowsCount int64
} }
type ReplaceStruct struct { type ReplaceStruct struct {