From 27726ad3d395d069ab8d0f0937df563dae9c624c Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Wed, 13 Aug 2025 13:42:42 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20ReplaceS?= =?UTF-8?q?chema()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- postgres_pgx/postgres_pgx.go | 52 ++++++++++++++++++++++--------- postgres_pgx/postgres_pgx_test.go | 48 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 14 deletions(-) diff --git a/postgres_pgx/postgres_pgx.go b/postgres_pgx/postgres_pgx.go index 3dc00544..54c2c202 100644 --- a/postgres_pgx/postgres_pgx.go +++ b/postgres_pgx/postgres_pgx.go @@ -61,6 +61,25 @@ const TextConnBusy = "conn busy" // timeOutSeconds - время ожидания для Ping() const timeOutSeconds = 60 +// IConnectionTransaction - интерфейс для работы с базой данных +// объединяет в себе функции pgx.Conn, pgxpool.Pool и pgx.Tx +// чтобы передавать в функцию любой их них +type IConnectionTransaction interface { + // Transaction management + Begin(ctx context.Context) (pgx.Tx, error) + + // Query execution + Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error) + Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) + QueryRow(ctx context.Context, sql string, args ...any) pgx.Row + + // Batch operations + SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults + + // Bulk copy operations + CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) +} + // Connect_err - подключается к базе данных func Connect() { @@ -542,21 +561,26 @@ func Ping_err(ctxMain context.Context) error { return err } -// IConnectionTransaction - интерфейс для работы с базой данных -// объединяет в себе функции pgx.Conn, pgxpool.Pool и pgx.Tx -// чтобы передавать в функцию любой их них -type IConnectionTransaction interface { - // Transaction management - Begin(ctx context.Context) (pgx.Tx, error) +// ReplaceSchema - заменяет "public." на Settings.DB_SCHEMA +func ReplaceSchema(TextSQL string) string { + Otvet := TextSQL - // Query execution - Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error) - Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) - QueryRow(ctx context.Context, sql string, args ...any) pgx.Row + if Settings.DB_SCHEMA == "" { + return Otvet + } - // Batch operations - SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults + 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+".") - // Bulk copy operations - CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) + return Otvet +} + +// ReplaceSchemaName - заменяет имя схемы в тексте SQL +func ReplaceSchemaName(TextSQL, SchemaNameFrom string) string { + Otvet := TextSQL + + Otvet = strings.ReplaceAll(Otvet, SchemaNameFrom+".", Settings.DB_SCHEMA+".") + + return Otvet } diff --git a/postgres_pgx/postgres_pgx_test.go b/postgres_pgx/postgres_pgx_test.go index 05c690e7..0422c946 100644 --- a/postgres_pgx/postgres_pgx_test.go +++ b/postgres_pgx/postgres_pgx_test.go @@ -187,3 +187,51 @@ SELECT * FROM temp_TestRawMultipleSQL2 } } + +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 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) + } +}