From 4a1e5f9dd413ab7292f0d74f4a96a0b56f7b9f91 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Tue, 26 Mar 2024 11:23:09 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20pgx5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- postgres_pgx/postgres_pgx.go | 15 +++++++++++---- postgres_pgx/postgres_pgx_test.go | 20 ++++++++++---------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/postgres_pgx/postgres_pgx.go b/postgres_pgx/postgres_pgx.go index 8c1659bb..0ed5ff6a 100644 --- a/postgres_pgx/postgres_pgx.go +++ b/postgres_pgx/postgres_pgx.go @@ -101,6 +101,7 @@ func Connect_WithApplicationName_err(ApplicationName string) error { // config, err := pgx.ParseConfig(databaseUrl) //config.PreferSimpleProtocol = true //для мульти-запросов + Conn = nil Conn, err = pgx.ConnectConfig(ctx, config) if err != nil { @@ -331,7 +332,7 @@ loop: // GetConnection - возвращает соединение к нужной базе данных func GetConnection() *pgx.Conn { - if Conn == nil { + if Conn == nil || Conn.IsClosed() { Connect() } @@ -351,19 +352,25 @@ func GetConnection_WithApplicationName(ApplicationName string) *pgx.Conn { func RawMultipleSQL(db *pgx.Conn, TextSQL string) (pgx.Rows, error) { var Rows pgx.Rows var err error - Conn = db - if Conn == nil { + if db == nil { TextError := "RawMultipleSQL() error: db =nil" log.Error(TextError) err = errors.New(TextError) return Rows, err } + if db.IsClosed() { + TextError := "RawMultipleSQL() error: db is closed" + log.Error(TextError) + err = errors.New(TextError) + return Rows, err + } + ctx := contextmain.GetContext() //запустим транзакцию - tx, err := Conn.Begin(ctx) + tx, err := db.Begin(ctx) if err != nil { log.Error(err) return Rows, err diff --git a/postgres_pgx/postgres_pgx_test.go b/postgres_pgx/postgres_pgx_test.go index dcf87eb7..709f0321 100644 --- a/postgres_pgx/postgres_pgx_test.go +++ b/postgres_pgx/postgres_pgx_test.go @@ -133,9 +133,11 @@ SELECT * FROM temp_TestRawMultipleSQL2 Rows, err := RawMultipleSQL(Conn, TextSQL) if err != nil { t.Error("TestRawMultipleSQL() error: ", err) + return } if Rows == nil { - + t.Error("TestRawMultipleSQL() error: Rows == nil") + return } Otvet := 0 @@ -149,12 +151,14 @@ SELECT * FROM temp_TestRawMultipleSQL2 t.Log("Прошло время: ", time.Since(TimeStart)) } +// TestRawMultipleSQL2 - negative test, with error func TestRawMultipleSQL2(t *testing.T) { config_main.LoadEnv() GetConnection() defer CloseConnection() TimeStart := time.Now() + defer t.Log("Прошло время: ", time.Since(TimeStart)) TextSQL := ` drop table if exists temp_TestRawMultipleSQL2; @@ -170,15 +174,11 @@ SELECT * FROM temp_TestRawMultipleSQL2 Rows, err := Conn.Query(ctx, TextSQL) if err == nil { t.Error("TestRawMultipleSQL2() Query() error: ", err) + return + } + if Rows == nil { + t.Error("TestRawMultipleSQL2() error: Rows == nil") + return } - Otvet := 0 - for Rows.Next() { - err := Rows.Scan(&Otvet) - if err != nil { - t.Error("TestRawMultipleSQL2() Scan() error: ", err) - } - } - - t.Log("Прошло время: ", time.Since(TimeStart)) }