1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-26 23:10:42 +02:00

сделал RawMultipleSQL()

This commit is contained in:
Nikitin Aleksandr
2024-03-04 18:00:03 +03:00
parent f49fd0b4d2
commit e052577f73
2 changed files with 98 additions and 16 deletions

View File

@@ -398,32 +398,72 @@ loop:
stopapp.GetWaitGroup_Main().Done() stopapp.GetWaitGroup_Main().Done()
} }
//// RawMultipleSQL - выполняет текст запроса, отдельно для каждого запроса
//func RawMultipleSQL(db *gorm.DB, TextSQL string) *gorm.DB {
// var tx *gorm.DB
// var err error
// tx = db
//
// // запустим все запросы отдельно
// sqlSlice := strings.Split(TextSQL, ";")
// len1 := len(sqlSlice)
// for i, v := range sqlSlice {
// if i == len1-1 {
// tx = tx.Raw(v)
// err = tx.Error
// } else {
// tx = tx.Exec(v)
// err = tx.Error
// }
// if err != nil {
// TextError := fmt.Sprint("db.Raw() error: ", err, ", TextSQL: \n", v)
// err = errors.New(TextError)
// break
// }
// }
//
// if tx == nil {
// log.Panic("db.Raw() error: rows =nil")
// }
//
// return tx
//}
// RawMultipleSQL - выполняет текст запроса, отдельно для каждого запроса // RawMultipleSQL - выполняет текст запроса, отдельно для каждого запроса
func RawMultipleSQL(db *gorm.DB, TextSQL string) *gorm.DB { func RawMultipleSQL(db *gorm.DB, TextSQL string) *gorm.DB {
var tx *gorm.DB var tx *gorm.DB
var err error var err error
tx = db tx = db
// запустим все запросы отдельно if tx == nil {
sqlSlice := strings.Split(TextSQL, ";") log.Error("RawMultipleSQL() error: db =nil")
len1 := len(sqlSlice) return tx
for i, v := range sqlSlice {
if i == len1-1 {
tx = tx.Raw(v)
err = tx.Error
} else {
tx = tx.Exec(v)
err = tx.Error
} }
TextSQL1 := ""
TextSQL2 := TextSQL
//запустим все запросы, кроме последнего
pos1 := strings.LastIndex(TextSQL, ";")
if pos1 > 0 {
TextSQL1 = TextSQL[0:pos1]
TextSQL2 = TextSQL[pos1:]
tx = tx.Exec(TextSQL1)
err = tx.Error
if err != nil { if err != nil {
TextError := fmt.Sprint("db.Raw() error: ", err, ", TextSQL: \n", v) TextError := fmt.Sprint("db.Exec() error: ", err, ", TextSQL: \n", TextSQL1)
err = errors.New(TextError) err = errors.New(TextError)
break return tx
} }
} }
if tx == nil { //запустим последний запрос, с возвратом результата
log.Panic("db.Raw() error: rows =nil") tx = tx.Raw(TextSQL2)
err = tx.Error
if err != nil {
TextError := fmt.Sprint("db.Raw() error: ", err, ", TextSQL: \n", TextSQL2)
err = errors.New(TextError)
return tx
} }
return tx return tx

View File

@@ -3,6 +3,7 @@ package postgres_gorm
import ( import (
"errors" "errors"
"testing" "testing"
"time"
//log "github.com/sirupsen/logrus" //log "github.com/sirupsen/logrus"
@@ -125,11 +126,52 @@ func TestRawMultipleSQL(t *testing.T) {
GetConnection() GetConnection()
defer CloseConnection() defer CloseConnection()
TextSQL := "SELECT 1; SELECT 2" 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"
tx := RawMultipleSQL(Conn, TextSQL) tx := RawMultipleSQL(Conn, TextSQL)
err := tx.Error err := tx.Error
if err != nil { if err != nil {
t.Error("TestRawMultipleSQL() error: ", err) t.Error("TestRawMultipleSQL() error: ", err)
} }
t.Log("Прошло время: ", time.Since(TimeStart))
}
func TestRawMultipleSQL2(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
`
tx := RawMultipleSQL(Conn, TextSQL)
err := tx.Error
if err != nil {
t.Error("TestRawMultipleSQL() error: ", err)
}
if tx.RowsAffected != 1 {
t.Error("TestRawMultipleSQL() RowsAffected = ", tx.RowsAffected)
}
t.Log("Прошло время: ", time.Since(TimeStart))
} }