mirror of
https://github.com/ManyakRus/starter.git
synced 2025-07-14 06:24:19 +02:00
сделал ReplaceTableNamesToUnique1()
This commit is contained in:
@ -8,6 +8,7 @@ import (
|
|||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/google/uuid"
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -848,3 +849,50 @@ func IsEmptyValue(v any) bool {
|
|||||||
Otvet := !rv.IsValid() || reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface())
|
Otvet := !rv.IsValid() || reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface())
|
||||||
return Otvet
|
return Otvet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringIdentifierFromUUID - возвращает строку из UUID
|
||||||
|
func StringIdentifierFromUUID() string {
|
||||||
|
Otvet := uuid.New().String()
|
||||||
|
Otvet = strings.ReplaceAll(Otvet, "-", "")
|
||||||
|
|
||||||
|
return Otvet
|
||||||
|
}
|
||||||
|
|
||||||
|
// IndexMassSubstringMin - возвращает индекс первого вхождения в строке
|
||||||
|
func IndexMassSubstringMin(s string, MassSubstr ...string) int {
|
||||||
|
Otvet := -1
|
||||||
|
|
||||||
|
for _, v := range MassSubstr {
|
||||||
|
Otvet1 := -1
|
||||||
|
if v != "" {
|
||||||
|
Otvet1 = strings.Index(s, v)
|
||||||
|
}
|
||||||
|
if Otvet1 != -1 && (Otvet1 < Otvet || Otvet == -1) {
|
||||||
|
Otvet = Otvet1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Otvet
|
||||||
|
}
|
||||||
|
|
||||||
|
// IndexSubstringMin2 - возвращает индекс первого вхождения в строке
|
||||||
|
func IndexSubstringMin2(s string, substr1, substr2 string) int {
|
||||||
|
Otvet := -1
|
||||||
|
|
||||||
|
Otvet1 := -1
|
||||||
|
Otvet2 := -1
|
||||||
|
if substr1 != "" {
|
||||||
|
Otvet1 = strings.Index(s, substr1)
|
||||||
|
}
|
||||||
|
if substr2 != "" {
|
||||||
|
Otvet2 = strings.Index(s, substr2)
|
||||||
|
}
|
||||||
|
|
||||||
|
if Otvet1 != -1 && (Otvet1 < Otvet2 || Otvet2 == -1) {
|
||||||
|
Otvet = Otvet1
|
||||||
|
} else {
|
||||||
|
Otvet = Otvet2
|
||||||
|
}
|
||||||
|
|
||||||
|
return Otvet
|
||||||
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -653,3 +654,94 @@ func TestIsEmptyValue(t *testing.T) {
|
|||||||
t.Error("Expected true for empty uuid value")
|
t.Error("Expected true for empty uuid value")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStringIdentifierFromUUID(t *testing.T) {
|
||||||
|
// Test that the function returns a non-empty string
|
||||||
|
result := StringIdentifierFromUUID()
|
||||||
|
if result == "" {
|
||||||
|
t.Error("Expected non-empty string, but got empty string")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that the function returns a string of the correct length
|
||||||
|
expectedLength := 32
|
||||||
|
if len(result) != expectedLength {
|
||||||
|
t.Errorf("Expected string of length %d, but got %d", expectedLength, len(result))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that the function returns a string with no hyphens
|
||||||
|
if strings.Contains(result, "-") {
|
||||||
|
t.Error("Expected string with no hyphens, but got hyphen")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndexSubstringMin(t *testing.T) {
|
||||||
|
// Test case 1: empty input string and no substrings provided
|
||||||
|
s1 := ""
|
||||||
|
Otvet1 := -1
|
||||||
|
if IndexMassSubstringMin(s1) != Otvet1 {
|
||||||
|
t.Errorf("IndexMassSubstringMin(%q) = %d; want %d", s1, IndexMassSubstringMin(s1), Otvet1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case 2: non-empty input string and no substrings provided
|
||||||
|
s2 := "Hello, world!"
|
||||||
|
Otvet2 := -1
|
||||||
|
if IndexMassSubstringMin(s2) != Otvet2 {
|
||||||
|
t.Errorf("IndexMassSubstringMin(%q) = %d; want %d", s2, IndexMassSubstringMin(s2), Otvet2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case 3: input string contains one of the substrings
|
||||||
|
s3 := "Hello, world!"
|
||||||
|
substrings3 := []string{"world"}
|
||||||
|
Otvet3 := 7
|
||||||
|
if IndexMassSubstringMin(s3, substrings3...) != Otvet3 {
|
||||||
|
t.Errorf("IndexMassSubstringMin(%q, %v...) = %d; want %d", s3, substrings3, IndexMassSubstringMin(s3, substrings3...), Otvet3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case 4: input string contains multiple occurrences of the same substring
|
||||||
|
s4 := "Hello, world! Hello, world!"
|
||||||
|
substrings4 := []string{"world"}
|
||||||
|
Otvet4 := 7
|
||||||
|
if IndexMassSubstringMin(s4, substrings4...) != Otvet4 {
|
||||||
|
t.Errorf("IndexMassSubstringMin(%q, %v...) = %d; want %d", s4, substrings4, IndexMassSubstringMin(s4, substrings4...), Otvet4)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case 5: input string contains multiple different substrings
|
||||||
|
s5 := "Hello, world! How are you?"
|
||||||
|
substrings5 := []string{"world", "are"}
|
||||||
|
Otvet5 := 7
|
||||||
|
if IndexMassSubstringMin(s5, substrings5...) != Otvet5 {
|
||||||
|
t.Errorf("IndexMassSubstringMin(%q, %v...) = %d; want %d", s5, substrings5, IndexMassSubstringMin(s5, substrings5...), Otvet5)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case 6: input string contains a mix of substrings that overlap and don't overlap
|
||||||
|
s6 := "Hello, world! How are you?"
|
||||||
|
substrings6 := []string{"world", "orl"}
|
||||||
|
Otvet6 := 7
|
||||||
|
if IndexMassSubstringMin(s6, substrings6...) != Otvet6 {
|
||||||
|
t.Errorf("IndexMassSubstringMin(%q, %v...) = %d; want %d", s6, substrings6, IndexMassSubstringMin(s6, substrings6...), Otvet6)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndexSubstringMin2(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
s string
|
||||||
|
substr1 string
|
||||||
|
substr2 string
|
||||||
|
expected int
|
||||||
|
}{
|
||||||
|
{s: "hello world", substr1: "world", substr2: "test", expected: 6},
|
||||||
|
{s: "hello world", substr1: "", substr2: "test", expected: -1},
|
||||||
|
{s: "hello world", substr1: "test", substr2: "", expected: -1},
|
||||||
|
{s: "hello world", substr1: "", substr2: "", expected: -1},
|
||||||
|
{s: "hello world", substr1: "world", substr2: "hello", expected: 0},
|
||||||
|
{s: "hello world", substr1: "test", substr2: "world", expected: 6},
|
||||||
|
{s: "hello world", substr1: "test", substr2: "test", expected: -1},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
result := IndexSubstringMin2(test.s, test.substr1, test.substr2)
|
||||||
|
if result != test.expected {
|
||||||
|
t.Errorf("IndexSubstringMin2(%q, %q, %q) = %d, expected %d", test.s, test.substr1, test.substr2, result, test.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -473,3 +473,47 @@ func RawMultipleSQL(db *gorm.DB, TextSQL string) *gorm.DB {
|
|||||||
|
|
||||||
return tx
|
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
|
||||||
|
}
|
||||||
|
@ -216,3 +216,84 @@ SELECT * FROM temp_TestRawMultipleSQL2
|
|||||||
|
|
||||||
t.Log("Прошло время: ", time.Since(TimeStart))
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user