mirror of
https://github.com/ManyakRus/starter.git
synced 2025-02-21 20:47:37 +02:00
сделал RawMultipleSQL()
This commit is contained in:
parent
efa1ece363
commit
74736b68fb
@ -5,6 +5,7 @@ package postgres_pgx
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/ManyakRus/starter/logger"
|
||||
"github.com/ManyakRus/starter/port_checker"
|
||||
"github.com/jackc/pgx/v4"
|
||||
@ -345,3 +346,55 @@ func GetConnection_WithApplicationName(ApplicationName string) *pgx.Conn {
|
||||
|
||||
return Conn
|
||||
}
|
||||
|
||||
// RawMultipleSQL - выполняет текст запроса, отдельно для каждого запроса
|
||||
func RawMultipleSQL(db *pgx.Conn, TextSQL string) (pgx.Rows, error) {
|
||||
var Rows pgx.Rows
|
||||
var err error
|
||||
Conn = db
|
||||
|
||||
if Conn == nil {
|
||||
TextError := "RawMultipleSQL() error: db =nil"
|
||||
log.Error(TextError)
|
||||
err = errors.New(TextError)
|
||||
return Rows, err
|
||||
}
|
||||
|
||||
ctx := contextmain.GetContext()
|
||||
|
||||
//запустим транзакцию
|
||||
tx, err := Conn.Begin(ctx)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return Rows, err
|
||||
}
|
||||
//defer tx.Commit()
|
||||
|
||||
//
|
||||
TextSQL1 := ""
|
||||
TextSQL2 := TextSQL
|
||||
|
||||
//запустим все запросы, кроме последнего
|
||||
pos1 := strings.LastIndex(TextSQL, ";")
|
||||
if pos1 > 0 {
|
||||
TextSQL1 = TextSQL[0:pos1]
|
||||
TextSQL2 = TextSQL[pos1:]
|
||||
_, err := tx.Exec(ctx, TextSQL1)
|
||||
if err != nil {
|
||||
TextError := fmt.Sprint("db.Exec() error: ", err, ", TextSQL: \n", TextSQL1)
|
||||
err = errors.New(TextError)
|
||||
log.Error(err)
|
||||
return Rows, err
|
||||
}
|
||||
}
|
||||
|
||||
//запустим последний запрос, с возвратом результата
|
||||
Rows, err = tx.Query(ctx, TextSQL2)
|
||||
if err != nil {
|
||||
TextError := fmt.Sprint("db.Raw() error: ", err, ", TextSQL: \n", TextSQL2)
|
||||
err = errors.New(TextError)
|
||||
return Rows, err
|
||||
}
|
||||
|
||||
return Rows, err
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package postgres_pgx
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
//log "github.com/sirupsen/logrus"
|
||||
|
||||
@ -111,3 +112,39 @@ func TestConnect_WithApplicationName_err(t *testing.T) {
|
||||
CloseConnection()
|
||||
|
||||
}
|
||||
|
||||
func TestRawMultipleSQL(t *testing.T) {
|
||||
config_main.LoadEnv()
|
||||
GetConnection()
|
||||
defer CloseConnection()
|
||||
|
||||
TimeStart := time.Now()
|
||||
|
||||
TextSQL := `
|
||||
drop table if exists temp_TestRawMultipleSQL2;
|
||||
CREATE TEMPORARY TABLE temp_TestRawMultipleSQL2 (id int);
|
||||
|
||||
insert into temp_TestRawMultipleSQL2
|
||||
select 1;
|
||||
|
||||
SELECT * FROM temp_TestRawMultipleSQL2
|
||||
`
|
||||
//TextSQL := "SELECT 1; SELECT 2"
|
||||
Rows, err := RawMultipleSQL(Conn, TextSQL)
|
||||
if err != nil {
|
||||
t.Error("TestRawMultipleSQL() error: ", err)
|
||||
}
|
||||
if Rows == nil {
|
||||
|
||||
}
|
||||
|
||||
Otvet := 0
|
||||
for Rows.Next() {
|
||||
err := Rows.Scan(&Otvet)
|
||||
if err != nil {
|
||||
t.Error("TestRawMultipleSQL() Scan() error: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
t.Log("Прошло время: ", time.Since(TimeStart))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user