mirror of
https://github.com/ManyakRus/crud_generator.git
synced 2025-02-09 11:23:47 +02:00
сделал PrimaryKeyColumnsCount
This commit is contained in:
parent
f069ab8314
commit
038878ad31
@ -280,13 +280,43 @@ func AddSkipNow(Text string, Table1 *types.Table) string {
|
||||
func IsGoodTable(Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
TableName := Table1.Name
|
||||
ColumnName, _ := FindPrimaryKeyNameTypeGo(Table1)
|
||||
if ColumnName == "" {
|
||||
TextError := fmt.Sprint("Wrong table: ", Table1.Name, " error: not found Primary key")
|
||||
//TableName := Table1.Name
|
||||
//ColumnName, _ := FindPrimaryKeyNameTypeGo(Table1)
|
||||
//if ColumnName == "" {
|
||||
// TextError := fmt.Sprint("Wrong table: ", Table1.Name, " error: not found Primary key")
|
||||
// err = errors.New(TextError)
|
||||
//}
|
||||
|
||||
err = IsGoodTablePrefix(Table1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = IsGoodPrimaryKeyColumnsCount(Table1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// IsGoodPrimaryKeyColumnsCount - возвращает ошибку если количество колонок PrimaryKey неправильное
|
||||
func IsGoodPrimaryKeyColumnsCount(Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
if Table1.PrimaryKeyColumnsCount <= 0 || Table1.PrimaryKeyColumnsCount >= 2 {
|
||||
TextError := fmt.Sprint("Wrong table: ", Table1.Name, " error: can not use many Primary key columns count: ", Table1.PrimaryKeyColumnsCount)
|
||||
err = errors.New(TextError)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// IsGoodTablePrefix - возвращает ошибку если префикс таблицы = "DELETED_"
|
||||
func IsGoodTablePrefix(Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
TableName := Table1.Name
|
||||
if strings.HasPrefix(TableName, "DELETED_") == true {
|
||||
TextError := fmt.Sprint("Wrong table: ", Table1.Name, " error: name = DELETED_")
|
||||
err = errors.New(TextError)
|
||||
@ -1233,6 +1263,10 @@ func FindTextProtobufRequest(Table1 *types.Table, TypeGo string) (string, string
|
||||
TextRequest := "Request"
|
||||
|
||||
PrimaryKeyColumn := FindPrimaryKeyColumn(Table1)
|
||||
if PrimaryKeyColumn == nil {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
PrimaryKeyTypeGo := PrimaryKeyColumn.TypeGo
|
||||
switch PrimaryKeyTypeGo {
|
||||
case "string", "uuid.UUID":
|
||||
@ -1362,8 +1396,12 @@ func FindTextProtobufRequest_ID_Type(Table1 *types.Table, Column1 *types.Column,
|
||||
ColumnName := Column1.Name
|
||||
|
||||
//найдём тип колонки PrimaryKey
|
||||
PrimaryKey_Column := FindPrimaryKeyColumn(Table1)
|
||||
PrimaryKey_TypeGo := PrimaryKey_Column.TypeGo
|
||||
PrimaryKeyColumn := FindPrimaryKeyColumn(Table1)
|
||||
if PrimaryKeyColumn == nil {
|
||||
return "", "", "", ""
|
||||
}
|
||||
|
||||
PrimaryKey_TypeGo := PrimaryKeyColumn.TypeGo
|
||||
//Text_Request_ID := "Request_ID"
|
||||
Otvet, _ = FindTextProtobufRequest(Table1, PrimaryKey_TypeGo)
|
||||
//Text_Request_ID = "Request_" + TextID
|
||||
@ -1381,7 +1419,7 @@ func FindTextProtobufRequest_ID_Type(Table1 *types.Table, Column1 *types.Column,
|
||||
|
||||
case "int32":
|
||||
{
|
||||
if Column1.TypeGo == "Int32" && PrimaryKey_Column.TypeGo == "Int32" {
|
||||
if Column1.TypeGo == "Int32" && PrimaryKeyColumn.TypeGo == "Int32" {
|
||||
TextRequestProtoName = "Int32"
|
||||
TextRequestFieldName = "Int32_2"
|
||||
TextRequestFieldGolang = VariableName + "Int32"
|
||||
@ -1393,7 +1431,7 @@ func FindTextProtobufRequest_ID_Type(Table1 *types.Table, Column1 *types.Column,
|
||||
}
|
||||
case "string":
|
||||
{
|
||||
if Column1.TypeGo == "string" && PrimaryKey_Column.TypeGo == "string" {
|
||||
if Column1.TypeGo == "string" && PrimaryKeyColumn.TypeGo == "string" {
|
||||
TextRequestProtoName = "String"
|
||||
TextRequestFieldName = "String_2"
|
||||
TextRequestFieldGolang = VariableName + "String_2"
|
||||
@ -1405,7 +1443,7 @@ func FindTextProtobufRequest_ID_Type(Table1 *types.Table, Column1 *types.Column,
|
||||
}
|
||||
case "uuid.UUID":
|
||||
{
|
||||
if Column1.TypeGo == "string" && PrimaryKey_Column.TypeGo == "string" {
|
||||
if Column1.TypeGo == "string" && PrimaryKeyColumn.TypeGo == "string" {
|
||||
TextRequestProtoName = "String"
|
||||
TextRequestFieldName = "String_2"
|
||||
TextRequestFieldGolang = VariableName + "String_2"
|
||||
@ -1741,6 +1779,10 @@ func Replace_Postgres_ID_Test(Text string, Table1 *types.Table) string {
|
||||
|
||||
TextFind := "const Postgres_ID_Test = 0"
|
||||
ColumnPrimary := FindPrimaryKeyColumn(Table1)
|
||||
if ColumnPrimary == nil {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
IDMinimum := Table1.IDMinimum
|
||||
|
||||
if ColumnPrimary.TypeGo == "uuid.UUID" {
|
||||
@ -1764,10 +1806,14 @@ func Replace_Model_ID_Test(Text string, Table1 *types.Table) string {
|
||||
TEXT_TEMPLATE_MODEL := config.Settings.TEXT_TEMPLATE_MODEL
|
||||
ModelName := Table1.NameGo
|
||||
TextFind := "const " + TEXT_TEMPLATE_MODEL + "_ID_Test = 0"
|
||||
ColumnPrimary := FindPrimaryKeyColumn(Table1)
|
||||
PrimaryKeyColumn := FindPrimaryKeyColumn(Table1)
|
||||
if PrimaryKeyColumn == nil {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
IDMinimum := Table1.IDMinimum
|
||||
|
||||
if ColumnPrimary.TypeGo == "uuid.UUID" {
|
||||
if PrimaryKeyColumn.TypeGo == "uuid.UUID" {
|
||||
if Table1.IDMinimum == "" {
|
||||
Otvet = strings.ReplaceAll(Otvet, TextFind, `var `+ModelName+`_ID_Test = ""`)
|
||||
} else {
|
||||
@ -1811,6 +1857,10 @@ func ReplaceTextRequestID_PrimaryKey1(Text string, Table1 *types.Table, TextRequ
|
||||
Otvet := Text
|
||||
|
||||
PrimaryKeyColumn := FindPrimaryKeyColumn(Table1)
|
||||
if PrimaryKeyColumn == nil {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TypeGo := PrimaryKeyColumn.TypeGo
|
||||
|
||||
TextRequestID, TextID := FindTextProtobufRequest(Table1, TypeGo)
|
||||
@ -1851,6 +1901,10 @@ func ReplaceOtvetIDEqual1(Text string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
PrimaryKeyColumn := FindPrimaryKeyColumn(Table1)
|
||||
if PrimaryKeyColumn == nil {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Value := FindNegativeValue(PrimaryKeyColumn.TypeGo)
|
||||
|
||||
Otvet = strings.ReplaceAll(Otvet, "Otvet.ID = -1", "Otvet.ID = "+Value)
|
||||
@ -1863,6 +1917,10 @@ func ReplaceModelIDEqual1(Text string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
PrimaryKeyColumn := FindPrimaryKeyColumn(Table1)
|
||||
if PrimaryKeyColumn == nil {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Value := FindNegativeValue(PrimaryKeyColumn.TypeGo)
|
||||
|
||||
Otvet = strings.ReplaceAll(Otvet, "m.ID = -1", "m.ID = "+Value)
|
||||
|
@ -268,12 +268,18 @@ func FindTextFindByExtId(TextProto string, ModelName string) string {
|
||||
|
||||
// TextRead - возвращает текст .proto
|
||||
func TextRead(Table1 *types.Table) string {
|
||||
Otvet := ""
|
||||
|
||||
ModelName := Table1.NameGo
|
||||
PrimaryKeyColumn := create_files.FindPrimaryKeyColumn(Table1)
|
||||
if PrimaryKeyColumn == nil {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TypeGo := PrimaryKeyColumn.TypeGo
|
||||
TextRequest := "RequestId"
|
||||
TextRequest, _ = create_files.FindTextProtobufRequest(Table1, TypeGo)
|
||||
Otvet := "rpc " + ModelName + "_Read(" + TextRequest + ") returns (Response) {}"
|
||||
Otvet = "rpc " + ModelName + "_Read(" + TextRequest + ") returns (Response) {}"
|
||||
|
||||
return Otvet
|
||||
}
|
||||
@ -417,10 +423,16 @@ func FindTextReadFromCache(TextProto string, Table1 *types.Table) string {
|
||||
|
||||
// TextReadFromCache - возвращает текст .proto
|
||||
func TextReadFromCache(Table1 *types.Table) string {
|
||||
Column1 := create_files.FindPrimaryKeyColumn(Table1)
|
||||
TextRequestId, _ := create_files.FindTextProtobufRequest(Table1, Column1.TypeGo)
|
||||
Otvet := ""
|
||||
|
||||
PrimaryKeyColumn := create_files.FindPrimaryKeyColumn(Table1)
|
||||
if PrimaryKeyColumn == nil {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TextRequestId, _ := create_files.FindTextProtobufRequest(Table1, PrimaryKeyColumn.TypeGo)
|
||||
ModelName := Table1.NameGo
|
||||
Otvet := "rpc " + ModelName + "_ReadFromCache(" + TextRequestId + ") returns (Response) {}"
|
||||
Otvet = "rpc " + ModelName + "_ReadFromCache(" + TextRequestId + ") returns (Response) {}"
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ CREATE TEMPORARY TABLE temp_primary_keys (table_name text, column_name text);
|
||||
insert into temp_primary_keys
|
||||
select
|
||||
ccu.table_name,
|
||||
max(ccu.column_name) as column_name
|
||||
(ccu.column_name) as column_name
|
||||
|
||||
from pg_constraint pgc
|
||||
join pg_namespace nsp on nsp.oid = pgc.connamespace
|
||||
@ -112,9 +112,9 @@ WHERE 1=1
|
||||
and ccu.table_schema = 'public'
|
||||
and contype = 'p'
|
||||
|
||||
GROUP BY
|
||||
ccu.table_name
|
||||
HAVING sum(1)=1
|
||||
--GROUP BY
|
||||
-- ccu.table_name
|
||||
--HAVING sum(1)=1
|
||||
;
|
||||
------------------------------------------- Все таблицы и колонки ------------------------------
|
||||
|
||||
@ -263,12 +263,14 @@ order by
|
||||
MapColumns := make(map[string]*types.Column, 0)
|
||||
OrderNumberColumn := 0
|
||||
OrderNumberTable := 0
|
||||
PrimaryKeyColumnsCount := 0
|
||||
TableName0 := ""
|
||||
Table1 := CreateTable()
|
||||
for _, v := range MassTableColumn {
|
||||
if v.TableName != TableName0 {
|
||||
OrderNumberColumn = 0
|
||||
Table1.MapColumns = MapColumns
|
||||
Table1.PrimaryKeyColumnsCount = PrimaryKeyColumnsCount
|
||||
MapColumns = make(map[string]*types.Column, 0)
|
||||
if TableName0 != "" {
|
||||
//MassTable = append(MassTable, Table1)
|
||||
@ -291,6 +293,7 @@ order by
|
||||
Table1.Comment = TableComment
|
||||
Table1.NameGo = ModelName
|
||||
|
||||
PrimaryKeyColumnsCount = 0
|
||||
}
|
||||
|
||||
Column1 := types.Column{}
|
||||
@ -319,6 +322,9 @@ order by
|
||||
Column1.TableKey = v.ColumnTableKey
|
||||
Column1.ColumnKey = v.ColumnColumnKey
|
||||
Column1.IsPrimaryKey = v.IsPrimaryKey
|
||||
if v.IsPrimaryKey == true {
|
||||
PrimaryKeyColumnsCount++
|
||||
}
|
||||
|
||||
MapColumns[v.ColumnName] = &Column1
|
||||
//Table1.Columns = append(Table1.Columns, Column1)
|
||||
|
@ -19,11 +19,12 @@ type Table struct {
|
||||
//Element *etree.Element
|
||||
MapColumns map[string]*Column
|
||||
//Columns []Column
|
||||
OrderNumber int
|
||||
NameGo string
|
||||
IDMinimum string
|
||||
Comment string `json:"table_comment" gorm:"column:table_comment;default:''"`
|
||||
RowsCount int64
|
||||
OrderNumber int
|
||||
NameGo string
|
||||
IDMinimum string
|
||||
Comment string `json:"table_comment" gorm:"column:table_comment;default:''"`
|
||||
RowsCount int64
|
||||
PrimaryKeyColumnsCount int
|
||||
}
|
||||
|
||||
type ReplaceStruct struct {
|
||||
|
Loading…
x
Reference in New Issue
Block a user