Add support for prepared statements caching.
This commit is contained in:
parent
4bb9775134
commit
5f220569dd
20 changed files with 591 additions and 134 deletions
|
|
@ -5,8 +5,8 @@ import (
|
|||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/go-jet/jet/v2/internal/utils/throw"
|
||||
"github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/sqlite"
|
||||
"github.com/go-jet/jet/v2/stmtcache"
|
||||
"github.com/go-jet/jet/v2/tests/dbconfig"
|
||||
"github.com/pkg/profile"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
|
@ -17,38 +17,52 @@ import (
|
|||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var db *sqlite.DB
|
||||
var sampleDB *sqlite.DB
|
||||
var testRoot string
|
||||
var db *stmtcache.DB
|
||||
var sampleDB *stmtcache.DB
|
||||
|
||||
var withStatementCaching bool
|
||||
|
||||
func init() {
|
||||
withStatementCaching = os.Getenv("JET_TESTS_WITH_STMT_CACHE") == "true"
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
defer profile.Start().Stop()
|
||||
|
||||
sqlDB, err := sql.Open("sqlite3", "file:"+dbconfig.SakilaDBPath)
|
||||
throw.OnError(err)
|
||||
db = sqlite.NewDB(sqlDB).WithStatementsCaching(true)
|
||||
defer db.Close()
|
||||
func() {
|
||||
fmt.Printf("\nRunning sqlite tests caching enabled: %t \n", withStatementCaching)
|
||||
|
||||
_, err = db.Exec(fmt.Sprintf("ATTACH DATABASE '%s' as 'chinook';", dbconfig.ChinookDBPath))
|
||||
throw.OnError(err)
|
||||
sqlDB, err := sql.Open("sqlite3", "file:"+dbconfig.SakilaDBPath)
|
||||
throw.OnError(err)
|
||||
db = stmtcache.New(sqlDB).SetCaching(withStatementCaching)
|
||||
defer db.Close()
|
||||
|
||||
sqlSampleDB, err := sql.Open("sqlite3", dbconfig.TestSampleDBPath)
|
||||
throw.OnError(err)
|
||||
sampleDB = sqlite.NewDB(sqlSampleDB).WithStatementsCaching(true)
|
||||
defer sampleDB.Close()
|
||||
_, err = db.Exec(fmt.Sprintf("ATTACH DATABASE '%s' as 'chinook';", dbconfig.ChinookDBPath))
|
||||
throw.OnError(err)
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
ret := m.Run()
|
||||
if ret != 0 {
|
||||
os.Exit(ret)
|
||||
sqlSampleDB, err := sql.Open("sqlite3", dbconfig.TestSampleDBPath)
|
||||
throw.OnError(err)
|
||||
sampleDB = stmtcache.New(sqlSampleDB).SetCaching(withStatementCaching)
|
||||
defer sampleDB.Close()
|
||||
|
||||
for i := 0; i < runCount(withStatementCaching); i++ {
|
||||
ret := m.Run()
|
||||
if ret != 0 {
|
||||
fmt.Printf("\nFAIL: Running sqlite tests failed, caching enabled: %t \n", withStatementCaching)
|
||||
os.Exit(ret)
|
||||
}
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
func runCount(stmtCaching bool) int {
|
||||
if stmtCaching {
|
||||
return 4
|
||||
}
|
||||
|
||||
err = sampleDB.Clear()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
var loggedSQL string
|
||||
|
|
@ -72,7 +86,7 @@ func init() {
|
|||
})
|
||||
}
|
||||
|
||||
func requireQueryLogged(t *testing.T, statement postgres.Statement, rowsProcessed int64) {
|
||||
func requireQueryLogged(t *testing.T, statement sqlite.Statement, rowsProcessed int64) {
|
||||
query, args := statement.Sql()
|
||||
queryLogged, argsLogged := queryInfo.Statement.Sql()
|
||||
|
||||
|
|
@ -94,13 +108,13 @@ func requireLogged(t *testing.T, statement sqlite.Statement) {
|
|||
require.Equal(t, loggedDebugSQL, statement.DebugSql())
|
||||
}
|
||||
|
||||
func beginSampleDBTx(t *testing.T) *sqlite.Tx {
|
||||
func beginSampleDBTx(t *testing.T) *stmtcache.Tx {
|
||||
tx, err := sampleDB.BeginTx(context.Background(), nil)
|
||||
require.NoError(t, err)
|
||||
return tx
|
||||
}
|
||||
|
||||
func beginDBTx(t *testing.T) *sqlite.Tx {
|
||||
func beginDBTx(t *testing.T) *stmtcache.Tx {
|
||||
tx, err := db.Begin()
|
||||
require.NoError(t, err)
|
||||
return tx
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue