1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-23 22:45:11 +02:00

сделал ReplaceTableNamesToUnique1()

This commit is contained in:
Nikitin Aleksandr
2024-04-26 10:24:37 +03:00
parent b1fa9d3993
commit 1d033238b2
4 changed files with 265 additions and 0 deletions

View File

@@ -473,3 +473,47 @@ func RawMultipleSQL(db *gorm.DB, TextSQL string) *gorm.DB {
return tx
}
// ReplaceSchema - заменяет "public." на Settings.DB_SCHEMA
func ReplaceSchema(TextSQL string) string {
Otvet := TextSQL
if Settings.DB_SCHEMA == "" {
return Otvet
}
Otvet = strings.ReplaceAll(Otvet, "\tpublic.", "\t"+Settings.DB_SCHEMA+".")
Otvet = strings.ReplaceAll(Otvet, "\npublic.", "\n"+Settings.DB_SCHEMA+".")
Otvet = strings.ReplaceAll(Otvet, " public.", " "+Settings.DB_SCHEMA+".")
return Otvet
}
//func ReplaceTableNamesToUnique(TextSQL string) string {
// Otvet := TextSQL
//
// sUUID := micro.StringIdentifierFromUUID()
//
// return Otvet
//}
// ReplaceTableNamesToUnique1 - заменяет "public.TableName" на "public.TableName_UUID"
func ReplaceTableNamesToUnique1(TextSQL, sUUID string) string {
Otvet := TextSQL
sFind := "public."
pos1 := strings.Index(Otvet, sFind)
if pos1 == -1 {
return Otvet
}
s2 := Otvet[pos1+len(sFind):]
pos2 := micro.IndexSubstringMin2(s2, " ", "\n")
if pos2 == -1 {
return Otvet
}
Otvet = Otvet[0:pos1+len(sFind)+pos2] + "_" + sUUID + Otvet[pos1+len(sFind)+pos2:]
return Otvet
}

View File

@@ -216,3 +216,84 @@ SELECT * FROM temp_TestRawMultipleSQL2
t.Log("Прошло время: ", time.Since(TimeStart))
}
func TestReplaceSchema(t *testing.T) {
tests := []struct {
name string
input string
schema string
expected string
}{
{
name: "No schema",
input: "SELECT * FROM public.users",
schema: "",
expected: "SELECT * FROM public.users",
},
{
name: "Schema with tabs and newlines",
input: "\tSELECT * FROM public.users\n",
schema: "myschema",
expected: "\tSELECT * FROM myschema.users\n",
},
{
name: "Schema with spaces",
input: "SELECT * FROM public.users ",
schema: "myschema",
expected: "SELECT * FROM myschema.users ",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Settings.DB_SCHEMA = tt.schema
got := ReplaceSchema(tt.input)
if got != tt.expected {
t.Errorf("ReplaceSchema() = %v, expected %v", got, tt.expected)
}
})
}
}
func TestReplaceTableNamesToUnique1(t *testing.T) {
tests := []struct {
name string
input string
uuid string
expected string
}{
{
name: "No public schema",
input: "SELECT * FROM TableName",
uuid: "1234567890",
expected: "SELECT * FROM TableName",
},
{
name: "Public schema with no spaces",
input: "SELECT * FROM public.TableName",
uuid: "1234567890",
expected: "SELECT * FROM public.TableName",
},
{
name: "Public schema with spaces",
input: "SELECT * FROM public.Table Name",
uuid: "1234567890",
expected: "SELECT * FROM public.Table_1234567890 Name",
},
{
name: "Public schema with tabs",
input: "SELECT * FROM public.\tTableName\t",
uuid: "1234567890",
expected: "SELECT * FROM public.\tTableName_1234567890\t",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ReplaceTableNamesToUnique1(tt.input, tt.uuid)
if got != tt.expected {
t.Errorf("ReplaceTableNamesToUnique1() = %v, expected %v", got, tt.expected)
}
})
}
}