Skip to content

Commit 6f570b0

Browse files
author
James Cor
committed
fix parameters
1 parent e7c820f commit 6f570b0

File tree

6 files changed

+157
-64
lines changed

6 files changed

+157
-64
lines changed

sql/base_session.go

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type BaseSession struct {
5353
// privilege set if our counter doesn't equal the database's counter.
5454
privSetCounter uint64
5555
privilegeSet PrivilegeSet
56+
57+
storedProcParams map[string]*StoredProcParam
5658
}
5759

5860
func (s *BaseSession) GetLogger() *logrus.Entry {
@@ -252,6 +254,29 @@ func (s *BaseSession) IncrementStatusVariable(ctx *Context, statVarName string,
252254
return
253255
}
254256

257+
func (s *BaseSession) NewStoredProcParam(name string, param *StoredProcParam) {
258+
if _, ok := s.storedProcParams[name]; ok {
259+
return
260+
}
261+
s.storedProcParams[name] = param
262+
}
263+
264+
func (s *BaseSession) GetStoredProcParam(name string) *StoredProcParam {
265+
if param, ok := s.storedProcParams[name]; ok {
266+
return param
267+
}
268+
return nil
269+
}
270+
271+
func (s *BaseSession) SetStoredProcParam(name string, val any) error {
272+
param := s.GetStoredProcParam(name)
273+
if param == nil {
274+
return fmt.Errorf("variable `%s` could not be found", name)
275+
}
276+
param.SetValue(val)
277+
return nil
278+
}
279+
255280
// GetCharacterSet returns the character set for this session (defined by the system variable `character_set_connection`).
256281
func (s *BaseSession) GetCharacterSet() CharacterSetID {
257282
sysVar, _ := s.systemVars[characterSetConnectionSysVarName]
@@ -504,17 +529,18 @@ func NewBaseSessionWithClientServer(server string, client Client, id uint32) *Ba
504529
statusVars = make(map[string]StatusVarValue)
505530
}
506531
return &BaseSession{
507-
addr: server,
508-
client: client,
509-
id: id,
510-
systemVars: systemVars,
511-
statusVars: statusVars,
512-
userVars: NewUserVars(),
513-
idxReg: NewIndexRegistry(),
514-
viewReg: NewViewRegistry(),
515-
locks: make(map[string]bool),
516-
lastQueryInfo: defaultLastQueryInfo(),
517-
privSetCounter: 0,
532+
addr: server,
533+
client: client,
534+
id: id,
535+
systemVars: systemVars,
536+
statusVars: statusVars,
537+
userVars: NewUserVars(),
538+
storedProcParams: make(map[string]*StoredProcParam),
539+
idxReg: NewIndexRegistry(),
540+
viewReg: NewViewRegistry(),
541+
locks: make(map[string]bool),
542+
lastQueryInfo: defaultLastQueryInfo(),
543+
privSetCounter: 0,
518544
}
519545
}
520546

@@ -534,14 +560,15 @@ func NewBaseSession() *BaseSession {
534560
statusVars = make(map[string]StatusVarValue)
535561
}
536562
return &BaseSession{
537-
id: atomic.AddUint32(&autoSessionIDs, 1),
538-
systemVars: systemVars,
539-
statusVars: statusVars,
540-
userVars: NewUserVars(),
541-
idxReg: NewIndexRegistry(),
542-
viewReg: NewViewRegistry(),
543-
locks: make(map[string]bool),
544-
lastQueryInfo: defaultLastQueryInfo(),
545-
privSetCounter: 0,
563+
id: atomic.AddUint32(&autoSessionIDs, 1),
564+
systemVars: systemVars,
565+
statusVars: statusVars,
566+
userVars: NewUserVars(),
567+
storedProcParams: make(map[string]*StoredProcParam),
568+
idxReg: NewIndexRegistry(),
569+
viewReg: NewViewRegistry(),
570+
locks: make(map[string]bool),
571+
lastQueryInfo: defaultLastQueryInfo(),
572+
privSetCounter: 0,
546573
}
547574
}

sql/core.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,21 @@ func IncrementStatusVariable(ctx *Context, name string, val int) {
880880
ctx.Session.IncrementStatusVariable(ctx, name, val)
881881
}
882882

883+
type StoredProcParam struct {
884+
Type Type
885+
Value any
886+
HasBeenSet bool
887+
Reference *StoredProcParam
888+
}
889+
890+
func (s *StoredProcParam) SetValue(val any) {
891+
s.Value = val
892+
s.HasBeenSet = true
893+
if s.Reference != nil {
894+
s.Reference.SetValue(val)
895+
}
896+
}
897+
883898
// OrderAndLimit stores the context of an ORDER BY ... LIMIT statement, and is used by index lookups and iterators.
884899
type OrderAndLimit struct {
885900
OrderBy Expression

sql/planbuilder/proc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,26 @@ func (b *Builder) buildCall(inScope *scope, c *ast.Call) (outScope *scope) {
345345
b.handleErr(err)
346346
}
347347

348+
// TODO: build references here?
349+
// TODO: here fill in x from session
348350
params := make([]sql.Expression, len(c.Params))
349351
for i, param := range c.Params {
352+
if len(proc.Params) == len(c.Params) {
353+
procParam := proc.Params[i]
354+
rspp := &sql.StoredProcParam{Type: procParam.Type}
355+
b.ctx.Session.NewStoredProcParam(procParam.Name, rspp)
356+
if col, isCol := param.(*ast.ColName); isCol {
357+
colName := col.Name.String() // TODO: to lower?
358+
if spp := b.ctx.Session.GetStoredProcParam(colName); spp != nil {
359+
iv := &procedures.InterpreterVariable{
360+
Type: spp.Type,
361+
Value: spp.Value,
362+
}
363+
param = iv.ToAST()
364+
rspp.Reference = spp
365+
}
366+
}
367+
}
350368
expr := b.buildScalar(inScope, param)
351369
params[i] = expr
352370
}

0 commit comments

Comments
 (0)