New Prepare useage #2061
|
@ -915,3 +915,44 @@ func TestGetVars(t *testing.T) {
|
||||||
assert.EqualValues(t, "xlw", name)
|
assert.EqualValues(t, "xlw", name)
|
||||||
assert.EqualValues(t, 42, age)
|
assert.EqualValues(t, 42, age)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetWithPrepare(t *testing.T) {
|
||||||
|
type GetVarsWithPrepare struct {
|
||||||
|
Id int64
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
assertSync(t, new(GetVarsWithPrepare))
|
||||||
|
|
||||||
|
_, err := testEngine.Insert(&GetVarsWithPrepare{
|
||||||
|
Name: "xlw",
|
||||||
|
Age: 42,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var v1 GetVarsWithPrepare
|
||||||
|
has, err := testEngine.Prepare().Get(&v1)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, has)
|
||||||
|
assert.EqualValues(t, "xlw", v1.Name)
|
||||||
|
assert.EqualValues(t, 42, v1.Age)
|
||||||
|
|
||||||
|
sess := testEngine.NewSession()
|
||||||
|
defer sess.Close()
|
||||||
|
|
||||||
|
var v2 GetVarsWithPrepare
|
||||||
|
has, err = sess.Prepare().Get(&v2)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, has)
|
||||||
|
assert.EqualValues(t, "xlw", v2.Name)
|
||||||
|
assert.EqualValues(t, 42, v2.Age)
|
||||||
|
|
||||||
|
var v3 GetVarsWithPrepare
|
||||||
|
has, err = sess.Prepare().Get(&v3)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, has)
|
||||||
|
assert.EqualValues(t, "xlw", v3.Name)
|
||||||
|
assert.EqualValues(t, 42, v3.Age)
|
||||||
|
}
|
||||||
|
|
|
@ -99,6 +99,7 @@ type EngineInterface interface {
|
||||||
MapCacher(interface{}, caches.Cacher) error
|
MapCacher(interface{}, caches.Cacher) error
|
||||||
NewSession() *Session
|
NewSession() *Session
|
||||||
NoAutoTime() *Session
|
NoAutoTime() *Session
|
||||||
|
Prepare() *Session
|
||||||
Quote(string) string
|
Quote(string) string
|
||||||
SetCacher(string, caches.Cacher)
|
SetCacher(string, caches.Cacher)
|
||||||
SetConnMaxLifetime(time.Duration)
|
SetConnMaxLifetime(time.Duration)
|
||||||
|
|
19
session.go
19
session.go
|
@ -79,7 +79,8 @@ type Session struct {
|
||||||
afterClosures []func(interface{})
|
afterClosures []func(interface{})
|
||||||
afterProcessors []executedProcessor
|
afterProcessors []executedProcessor
|
||||||
|
|
||||||
stmtCache map[uint32]*core.Stmt //key: hash.Hash32 of (queryStr, len(queryStr))
|
stmtCache map[uint32]*core.Stmt //key: hash.Hash32 of (queryStr, len(queryStr))
|
||||||
|
txStmtCache map[uint32]*core.Stmt // for tx statement
|
||||||
|
|
||||||
lastSQL string
|
lastSQL string
|
||||||
lastSQLArgs []interface{}
|
lastSQLArgs []interface{}
|
||||||
|
@ -200,6 +201,7 @@ func (session *Session) IsClosed() bool {
|
||||||
func (session *Session) resetStatement() {
|
func (session *Session) resetStatement() {
|
||||||
if session.autoResetStatement {
|
if session.autoResetStatement {
|
||||||
session.statement.Reset()
|
session.statement.Reset()
|
||||||
|
session.prepareStmt = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -370,6 +372,21 @@ func (session *Session) doPrepare(db *core.DB, sqlStr string) (stmt *core.Stmt,
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (session *Session) doPrepareTx(sqlStr string) (stmt *core.Stmt, err error) {
|
||||||
|
crc := crc32.ChecksumIEEE([]byte(sqlStr))
|
||||||
|
// TODO try hash(sqlStr+len(sqlStr))
|
||||||
|
var has bool
|
||||||
|
stmt, has = session.txStmtCache[crc]
|
||||||
|
if !has {
|
||||||
|
stmt, err = session.tx.PrepareContext(session.ctx, sqlStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
session.txStmtCache[crc] = stmt
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func getField(dataStruct *reflect.Value, table *schemas.Table, colName string, idx int) (*schemas.Column, *reflect.Value, error) {
|
func getField(dataStruct *reflect.Value, table *schemas.Table, colName string, idx int) (*schemas.Column, *reflect.Value, error) {
|
||||||
var col = table.GetColumnIdx(colName, idx)
|
var col = table.GetColumnIdx(colName, idx)
|
||||||
if col == nil {
|
if col == nil {
|
||||||
|
|
|
@ -46,25 +46,22 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := stmt.QueryContext(session.ctx, args...)
|
return stmt.QueryContext(session.ctx, args...)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return rows, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.QueryContext(session.ctx, sqlStr, args...)
|
return db.QueryContext(session.ctx, sqlStr, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if session.prepareStmt {
|
||||||
|
stmt, err := session.doPrepareTx(sqlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return rows, nil
|
|
||||||
|
return stmt.QueryContext(session.ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := session.tx.QueryContext(session.ctx, sqlStr, args...)
|
return session.tx.QueryContext(session.ctx, sqlStr, args...)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return rows, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) queryRow(sqlStr string, args ...interface{}) *core.Row {
|
func (session *Session) queryRow(sqlStr string, args ...interface{}) *core.Row {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user