mirror of
https://github.com/ManyakRus/starter.git
synced 2025-11-26 23:10:42 +02:00
сделал ReplaceSchemaName()
This commit is contained in:
@@ -533,3 +533,27 @@ func Ping_err(ctxMain context.Context) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceSchemaName - заменяет имя схемы в тексте SQL
|
||||||
|
func ReplaceSchemaName(TextSQL, SchemaNameFrom string) string {
|
||||||
|
Otvet := TextSQL
|
||||||
|
|
||||||
|
Otvet = strings.ReplaceAll(Otvet, SchemaNameFrom+".", Settings.DB_SCHEMA+".")
|
||||||
|
|
||||||
|
return Otvet
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package postgres_pgxpool
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"golang.org/x/net/context"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -130,8 +131,11 @@ select 1;
|
|||||||
|
|
||||||
SELECT * FROM temp_TestRawMultipleSQL2
|
SELECT * FROM temp_TestRawMultipleSQL2
|
||||||
`
|
`
|
||||||
|
ctx := context.Background()
|
||||||
|
tx, _ := connection.Begin(ctx)
|
||||||
|
defer tx.Commit(ctx)
|
||||||
//TextSQL := "SELECT 1; SELECT 2"
|
//TextSQL := "SELECT 1; SELECT 2"
|
||||||
Rows, err := RawMultipleSQL(connection, TextSQL)
|
Rows, err := RawMultipleSQL(tx, TextSQL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("TestRawMultipleSQL() error: ", err)
|
t.Error("TestRawMultipleSQL() error: ", err)
|
||||||
return
|
return
|
||||||
@@ -184,3 +188,51 @@ SELECT * FROM temp_TestRawMultipleSQL2
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReplaceSchemaName(t *testing.T) {
|
||||||
|
TextSQL := "SELECT * FROM public.users"
|
||||||
|
Settings.DB_SCHEMA = "myschema"
|
||||||
|
ExpectedSQL := "SELECT * FROM myschema.users"
|
||||||
|
ActualSQL := ReplaceSchemaName(TextSQL, "public")
|
||||||
|
if ActualSQL != ExpectedSQL {
|
||||||
|
t.Errorf("Expected %v, but got %v", ExpectedSQL, ActualSQL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user