You've already forked crud_generator
mirror of
https://github.com/ManyakRus/crud_generator.git
synced 2025-07-16 02:54:19 +02:00
сделал pgx, not working
This commit is contained in:
@ -2,6 +2,7 @@ package crud_lawsuit_status_types
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/ManyakRus/starter/postgres_pgxpool"
|
||||
"github.com/ManyakRus/starter/stopapp"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/db_constants"
|
||||
|
@ -25,13 +25,36 @@ func FindMassBy_FieldNamesWithUnderline_ctx(ctx context.Context, db *pgxpool.Poo
|
||||
defer atomic.AddInt32(&stopapp.TotalMessagesSendingNow, -1)
|
||||
|
||||
//
|
||||
//tx := db.WithContext(ctx)
|
||||
tx := db
|
||||
if ctx.Err() != nil {
|
||||
err = ctx.Err()
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
tx = tx.Where(`"ColumnName" = ?`, m.FieldName)
|
||||
tx = tx.Order(`ColumnNamesWithCommaQuotes`)
|
||||
tx = tx.Find(&Otvet)
|
||||
err = tx.Error
|
||||
//
|
||||
TextSQL := TextSQL_Read + `
|
||||
WHERE 1=1
|
||||
ReplaceWhere
|
||||
`
|
||||
|
||||
//
|
||||
rows, err := db.Query(ctx, TextSQL)
|
||||
defer rows.Close()
|
||||
if rows == nil {
|
||||
err = fmt.Errorf("RawMultipleSQL() error: rows =nil")
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
//результат в массив
|
||||
for rows.Next() {
|
||||
m := lawsuit_status_types.LawsuitStatusType{}
|
||||
err := rows.Scan(ReplaceAllFieldsWithComma)
|
||||
if err != nil {
|
||||
Values, _ := rows.Values()
|
||||
err = fmt.Errorf("rows.Scan() values: %v, error: %w", Values, err)
|
||||
return Otvet, err
|
||||
}
|
||||
Otvet = append(Otvet, m)
|
||||
}
|
||||
|
||||
return Otvet, err
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"github.com/ManyakRus/starter/micro"
|
||||
"github.com/ManyakRus/starter/postgres_pgxpool"
|
||||
"github.com/ManyakRus/starter/postgres_func"
|
||||
"github.com/ManyakRus/starter/stopapp"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/db_constants"
|
||||
|
@ -2,6 +2,7 @@ package crud_lawsuit_status_types
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/ManyakRus/starter/postgres_pgxpool"
|
||||
"github.com/ManyakRus/starter/stopapp"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/db_constants"
|
||||
|
@ -25,13 +25,36 @@ func ReadAll_ctx(ctx context.Context, db *pgxpool.Pool) ([]lawsuit_status_types.
|
||||
defer atomic.AddInt32(&stopapp.TotalMessagesSendingNow, -1)
|
||||
|
||||
//
|
||||
tx := db
|
||||
|
||||
tx = tx.Where("is_deleted = ?", false)
|
||||
tx = tx.Order("PrimaryKeyNamesWithComma")
|
||||
tx = tx.Find(&Otvet)
|
||||
err = tx.Error
|
||||
if ctx.Err() != nil {
|
||||
err = ctx.Err()
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
//
|
||||
TextSQL := TextSQL_Read + `
|
||||
WHERE 1=1
|
||||
ReplaceWhere
|
||||
`
|
||||
|
||||
//
|
||||
rows, err := db.Query(ctx, TextSQL)
|
||||
defer rows.Close()
|
||||
if rows == nil {
|
||||
err = fmt.Errorf("RawMultipleSQL() error: rows =nil")
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
//результат в массив
|
||||
for rows.Next() {
|
||||
m := lawsuit_status_types.LawsuitStatusType{}
|
||||
err := rows.Scan(ReplaceAllFieldsWithComma)
|
||||
if err != nil {
|
||||
Values, _ := rows.Values()
|
||||
err = fmt.Errorf("rows.Scan() values: %v, error: %w", Values, err)
|
||||
return Otvet, err
|
||||
}
|
||||
Otvet = append(Otvet, m)
|
||||
}
|
||||
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
|
@ -121,10 +121,19 @@ func CreateFiles_Create1(Text string, Table1 *types.Table) string {
|
||||
Comma = ""
|
||||
CommaNewline := ""
|
||||
CommaNewline2 := ""
|
||||
Number := 0
|
||||
MassColumns := micro.MassFrom_Map(Table1.MapColumns)
|
||||
for i, Column1 := range MassColumns {
|
||||
for _, Column1 := range MassColumns {
|
||||
//кроме ненужных колонок
|
||||
if create_files.Is_NotNeedUpdate_Сolumn(Column1) == true {
|
||||
continue
|
||||
}
|
||||
|
||||
Number = Number + 1
|
||||
sNumber := strconv.Itoa(Number)
|
||||
|
||||
ReplaceAllColumnNamesWithComma = ReplaceAllColumnNamesWithComma + CommaNewline + Column1.Name
|
||||
ReplaceDollarsWithComma = ReplaceDollarsWithComma + Comma + "$" + strconv.Itoa(i+1)
|
||||
ReplaceDollarsWithComma = ReplaceDollarsWithComma + Comma + "$" + sNumber
|
||||
if Column1.Name == "created_at" {
|
||||
ReplaceAllFieldsWithComma = ReplaceAllFieldsWithComma + CommaNewline2 + "time.Now()"
|
||||
} else {
|
||||
|
@ -136,8 +136,15 @@ func CreateFiles_FindBy_Table1(Table1 *types.Table, TextTemplateFunction string,
|
||||
//CommaNewline := ""
|
||||
CommaNewline2 := ""
|
||||
MassColumns := micro.MassFrom_Map(Table1.MapColumns)
|
||||
for i, Column1 := range MassColumns {
|
||||
sNumber := strconv.Itoa(i + 1)
|
||||
Number := 0
|
||||
for _, Column1 := range MassColumns {
|
||||
//кроме ненужных колонок
|
||||
if create_files.Is_NotNeedUpdate_Сolumn(Column1) == true {
|
||||
continue
|
||||
}
|
||||
|
||||
Number = Number + 1
|
||||
sNumber := strconv.Itoa(Number)
|
||||
//if Column1.IsNullable == true {
|
||||
// DefaultValueSQL := create_files.FindText_DefaultValueSQL_NotNull(Column1.TypeGo)
|
||||
// ReplaceTextSQL = ReplaceTextSQL + CommaNewline + "COALESCE(" + TableAlias + "." + Column1.Name + ", " + DefaultValueSQL + ") as " + Column1.Name
|
||||
|
@ -119,6 +119,10 @@ func CreateFiles_FindBy_ExtID1(Text string, Table1 *types.Table) string {
|
||||
CommaNewline2 := ""
|
||||
MassColumns := micro.MassFrom_Map(Table1.MapColumns)
|
||||
for _, Column1 := range MassColumns {
|
||||
if create_files.Is_NotNeedUpdate_Сolumn(Column1) == true {
|
||||
continue
|
||||
}
|
||||
|
||||
ReplaceAllFieldsWithComma = ReplaceAllFieldsWithComma + CommaNewline2 + "&m." + Column1.NameGo
|
||||
CommaNewline2 = ",\n\t\t"
|
||||
}
|
||||
|
@ -6,7 +6,9 @@ import (
|
||||
"github.com/ManyakRus/crud_generator/internal/types"
|
||||
"github.com/ManyakRus/starter/log"
|
||||
"github.com/ManyakRus/starter/micro"
|
||||
"github.com/bxcodec/faker/v3/support/slice"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -120,56 +122,39 @@ func CreateFiles_FindMassBy_Table(Table1 *types.Table, TextTemplateFunction stri
|
||||
func CreateFiles_FindMassBy_Table1(Table1 *types.Table, TextTemplateFunction string, MassColumns1 []string) string {
|
||||
Otvet := TextTemplateFunction
|
||||
|
||||
//
|
||||
//все колонки
|
||||
ReplaceAllFieldsWithComma := ""
|
||||
FieldNamesWithUnderline := ""
|
||||
FieldNamesWithComma := ""
|
||||
ColumnNamesWithComma := ""
|
||||
ColumnNamesWithCommaQuotes := ""
|
||||
|
||||
//
|
||||
TextFind := "\t" + `tx = tx.Where(` + "`" + `"ColumnName" = ?` + "`" + `, m.FieldName)` + "\n"
|
||||
TextWhere := ""
|
||||
Underline := ""
|
||||
Plus := ""
|
||||
Comma := ""
|
||||
for _, ColumnName1 := range MassColumns1 {
|
||||
Column1, ok := Table1.MapColumns[ColumnName1]
|
||||
if ok == false {
|
||||
log.Panic(Table1.Name + " .MapColumns[" + ColumnName1 + "] = false")
|
||||
ReplaceWhere := ""
|
||||
CommaNewline2 := ""
|
||||
Underline := "_"
|
||||
MassColumns := micro.MassFrom_Map(Table1.MapColumns)
|
||||
Number := 0
|
||||
for _, Column1 := range MassColumns {
|
||||
if create_files.Is_NotNeedUpdate_Сolumn(Column1) == true {
|
||||
continue
|
||||
}
|
||||
TextWhere = TextWhere + "\t" + `tx = tx.Where(` + "`" + `"` + ColumnName1 + `"` + ` = ?` + "`" + `, m.` + Column1.NameGo + `)` + "\n"
|
||||
FieldNamesWithUnderline = FieldNamesWithUnderline + Underline + Column1.NameGo
|
||||
FieldNamesWithComma = FieldNamesWithComma + Plus + Column1.NameGo
|
||||
ColumnNamesWithComma = ColumnNamesWithComma + Comma + Column1.Name
|
||||
ColumnNamesWithCommaQuotes = ColumnNamesWithCommaQuotes + Comma + `"` + Column1.Name + `"`
|
||||
Number = Number + 1
|
||||
sNumber := strconv.Itoa(Number)
|
||||
|
||||
//
|
||||
if slice.Contains(MassColumns1, Column1.Name) == true {
|
||||
ReplaceWhere = ReplaceWhere + "\tand " + Column1.Name + " = $" + sNumber + "\n"
|
||||
FieldNamesWithUnderline = FieldNamesWithUnderline + Underline + Column1.NameGo
|
||||
}
|
||||
|
||||
ReplaceAllFieldsWithComma = ReplaceAllFieldsWithComma + CommaNewline2 + "&m." + Column1.NameGo
|
||||
CommaNewline2 = ",\n\t\t\t"
|
||||
Underline = "_"
|
||||
Plus = "+"
|
||||
Comma = ", "
|
||||
}
|
||||
Otvet = strings.ReplaceAll(Otvet, "ReplaceAllFieldsWithComma", ReplaceAllFieldsWithComma)
|
||||
Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithUnderline", FieldNamesWithUnderline)
|
||||
|
||||
//кроме помеченных на удаление
|
||||
if create_files.Has_Column_IsDeleted_Bool(Table1) == true {
|
||||
TextWhere = TextWhere + "\t" + `tx = tx.Where("is_deleted = ?", false)` + "\n"
|
||||
ReplaceWhere = ReplaceWhere + "\tand is_deleted = false\n"
|
||||
}
|
||||
|
||||
//
|
||||
//if len(MassColumns1) == 0 {
|
||||
// FuncName := constants.TEXT_READALL
|
||||
// Otvet = strings.ReplaceAll(Otvet, "FindMassBy_FieldNamesWithUnderline", FuncName)
|
||||
// ColumnsPK := create_files.Find_PrimaryKeyColumns(Table1)
|
||||
// ColumnNamesWithComma = create_files.Find_ColumnNamesWithComma(ColumnsPK)
|
||||
// Otvet = strings.ReplaceAll(Otvet, ", m *lawsuit_status_types.LawsuitStatusType", "")
|
||||
// Otvet = strings.ReplaceAll(Otvet, "m *lawsuit_status_types.LawsuitStatusType", "")
|
||||
// Otvet = strings.ReplaceAll(Otvet, "(ctx, db, m)", "(ctx, db)")
|
||||
//}
|
||||
|
||||
//
|
||||
Otvet = strings.ReplaceAll(Otvet, TextFind, TextWhere)
|
||||
Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithUnderline", FieldNamesWithUnderline)
|
||||
Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithPlus", FieldNamesWithComma)
|
||||
Otvet = strings.ReplaceAll(Otvet, "ColumnNamesWithCommaQuotes", ColumnNamesWithCommaQuotes)
|
||||
Otvet = strings.ReplaceAll(Otvet, "ColumnNamesWithComma", ColumnNamesWithComma)
|
||||
Otvet = strings.ReplaceAll(Otvet, "ReplaceWhere", ReplaceWhere)
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
@ -123,6 +123,10 @@ SELECT
|
||||
CommaNewline2 := ""
|
||||
MassColumns := micro.MassFrom_Map(Table1.MapColumns)
|
||||
for _, Column1 := range MassColumns {
|
||||
if create_files.Is_NotNeedUpdate_Сolumn(Column1) == true {
|
||||
continue
|
||||
}
|
||||
|
||||
if Column1.IsNullable == true {
|
||||
DefaultValueSQL := create_files.FindText_DefaultValueSQL_NotNull(Column1.TypeGo)
|
||||
ReplaceTextSQL = ReplaceTextSQL + CommaNewline + "COALESCE(" + TableAlias + "." + Column1.Name + ", " + DefaultValueSQL + ") as " + Column1.Name
|
||||
|
@ -120,15 +120,26 @@ func CreateFiles_ReadAllTable(Table1 *types.Table, TextTemplateFunction string)
|
||||
func CreateFiles_ReadAll_Table1(Table1 *types.Table, TextTemplateFunction string) string {
|
||||
Otvet := TextTemplateFunction
|
||||
|
||||
//кроме помеченных на удаление
|
||||
if create_files.Has_Column_IsDeleted_Bool(Table1) == false {
|
||||
TextWhere := "\t" + `tx = tx.Where("is_deleted = ?", false)` + "\n"
|
||||
Otvet = strings.ReplaceAll(Otvet, TextWhere, "")
|
||||
}
|
||||
//все колонки
|
||||
ReplaceAllFieldsWithComma := ""
|
||||
CommaNewline2 := ""
|
||||
MassColumns := micro.MassFrom_Map(Table1.MapColumns)
|
||||
for _, Column1 := range MassColumns {
|
||||
if create_files.Is_NotNeedUpdate_Сolumn(Column1) == true {
|
||||
continue
|
||||
}
|
||||
|
||||
//
|
||||
PrimaryKeyNamesWithComma := create_files.Find_PrimaryKeyNamesWithComma(Table1)
|
||||
Otvet = strings.ReplaceAll(Otvet, "PrimaryKeyNamesWithComma", PrimaryKeyNamesWithComma)
|
||||
ReplaceAllFieldsWithComma = ReplaceAllFieldsWithComma + CommaNewline2 + "&m." + Column1.NameGo
|
||||
CommaNewline2 = ",\n\t\t\t"
|
||||
}
|
||||
Otvet = strings.ReplaceAll(Otvet, "ReplaceAllFieldsWithComma", ReplaceAllFieldsWithComma)
|
||||
|
||||
//кроме помеченных на удаление
|
||||
ReplaceWhere := ""
|
||||
if create_files.Has_Column_IsDeleted_Bool(Table1) == true {
|
||||
ReplaceWhere = "\tand is_deleted = false"
|
||||
}
|
||||
Otvet = strings.ReplaceAll(Otvet, "ReplaceWhere", ReplaceWhere)
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
@ -138,8 +138,14 @@ func CreateFiles_Update1(Text string, Table1 *types.Table) string {
|
||||
CommaNewline := "\t"
|
||||
CommaNewline2 := ""
|
||||
MassColumns := micro.MassFrom_Map(Table1.MapColumns)
|
||||
for i, Column1 := range MassColumns {
|
||||
sNumber := strconv.Itoa(i + 1)
|
||||
Number := 0
|
||||
for _, Column1 := range MassColumns {
|
||||
if create_files.Is_NotNeedUpdate_Сolumn(Column1) == true {
|
||||
continue
|
||||
}
|
||||
|
||||
Number = Number + 1
|
||||
sNumber := strconv.Itoa(Number)
|
||||
ReplaceColumnNameEqualDollarComma = ReplaceColumnNameEqualDollarComma + CommaNewline + Column1.NameGo + " = $" + sNumber
|
||||
if Column1.Name == "modified_at" {
|
||||
ReplaceAllFieldsWithComma = ReplaceAllFieldsWithComma + CommaNewline2 + "time.Now()"
|
||||
|
@ -64,6 +64,7 @@ func CreateFiles_UpdateEveryColumn(Table1 *types.Table) error {
|
||||
//}
|
||||
|
||||
TextCrud = TextCrud + TextUpdateEveryColumn
|
||||
|
||||
TextCrud = create_files.Replace_TemplateModel_to_Model(TextCrud, Table1.NameGo)
|
||||
TextCrud = create_files.Replace_TemplateTableName_to_TableName(TextCrud, Table1.Name)
|
||||
TextCrud = config.Settings.TEXT_MODULE_GENERATED + TextCrud
|
||||
@ -78,9 +79,13 @@ func CreateFiles_UpdateEveryColumn(Table1 *types.Table) error {
|
||||
ModelTableURL := create_files.Find_ModelTableURL(TableName)
|
||||
TextCrud = create_files.AddImport(TextCrud, ModelTableURL)
|
||||
|
||||
TextCrud = create_files.CheckAndAdd_ImportGorm_FromText(TextCrud)
|
||||
//TextCrud = create_files.CheckAndAdd_ImportGorm_FromText(TextCrud)
|
||||
//TextCrud = create_files.Convert_RequestIdToAlias(TextCrud, Table1)
|
||||
//добавим импорт uuid
|
||||
|
||||
//postgres_func
|
||||
TextCrud = create_files.CheckAndAdd_Import(TextCrud, "github.com/ManyakRus/starter/postgres_func")
|
||||
|
||||
}
|
||||
|
||||
//кэш
|
||||
@ -143,19 +148,10 @@ func CreateFiles_UpdateEveryColumn1(Text string, Table1 *types.Table) string {
|
||||
func FindTextUpdateEveryColumn(TextCrudUpdateFunc string, Table1 *types.Table) string {
|
||||
Otvet := ""
|
||||
|
||||
//сортировка по названию колонок
|
||||
keys := make([]string, 0, len(Table1.MapColumns))
|
||||
for k := range Table1.MapColumns {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
//найдём новый текст для каждой таблицы
|
||||
for _, key1 := range keys {
|
||||
Column1, ok := Table1.MapColumns[key1]
|
||||
if ok == false {
|
||||
log.Panic("FindTextUpdateEveryColumn() Table1.MapColumns[key1] = false")
|
||||
}
|
||||
MassColumns := micro.MassFrom_Map(Table1.MapColumns)
|
||||
for _, Column1 := range MassColumns {
|
||||
//кроме ненужных колонок
|
||||
if create_files.Is_NotNeedUpdate_Сolumn(Column1) == true {
|
||||
continue
|
||||
}
|
||||
|
@ -87,7 +87,13 @@ on
|
||||
|
||||
WHERE 1=1
|
||||
--and c.confrelid = (select oid from pg_class where relname = 'lawsuit_invoices')
|
||||
--AND c.confrelid!=c.conrelid
|
||||
AND c.confrelid!=c.conrelid
|
||||
|
||||
GROUP BY
|
||||
(select r.relname from pg_class r where r.oid = c.conrelid),
|
||||
UNNEST((select array_agg(attname) from pg_attribute where attrelid = c.conrelid and array[attnum] <@ c.conkey)),
|
||||
(select r.relname from pg_class r where r.oid = c.confrelid),
|
||||
a.attname
|
||||
;
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user