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()
}
//// 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 - выполняет текст запроса, отдельно для каждого запроса
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 tx == nil {
log.Error("RawMultipleSQL() error: db =nil")
return tx
}
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 {
TextError := fmt.Sprint("db.Raw() error: ", err, ", TextSQL: \n", v)
TextError := fmt.Sprint("db.Exec() error: ", err, ", TextSQL: \n", TextSQL1)
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

View File

@@ -3,6 +3,7 @@ package postgres_gorm
import (
"errors"
"testing"
"time"
//log "github.com/sirupsen/logrus"
@@ -125,11 +126,52 @@ func TestRawMultipleSQL(t *testing.T) {
GetConnection()
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)
err := tx.Error
if err != nil {
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))
}