github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/bind.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package interlock
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    20  	"github.com/whtcorpsinc/errors"
    21  	"github.com/whtcorpsinc/milevadb/bindinfo"
    22  	causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded"
    23  	"github.com/whtcorpsinc/milevadb/petri"
    24  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    25  )
    26  
    27  // ALLEGROSQLBindInterDirc represents a bind interlock.
    28  type ALLEGROSQLBindInterDirc struct {
    29  	baseInterlockingDirectorate
    30  
    31  	sqlBindOp           causetembedded.ALLEGROSQLBindOpType
    32  	normdOrigALLEGROSQL string
    33  	bindALLEGROSQL      string
    34  	charset             string
    35  	defCauslation       string
    36  	EDB                 string
    37  	isGlobal            bool
    38  	bindAst             ast.StmtNode
    39  }
    40  
    41  // Next implements the InterlockingDirectorate Next interface.
    42  func (e *ALLEGROSQLBindInterDirc) Next(ctx context.Context, req *chunk.Chunk) error {
    43  	req.Reset()
    44  	switch e.sqlBindOp {
    45  	case causetembedded.OpALLEGROSQLBindCreate:
    46  		return e.createALLEGROSQLBind()
    47  	case causetembedded.OpALLEGROSQLBindDrop:
    48  		return e.dropALLEGROSQLBind()
    49  	case causetembedded.OpFlushBindings:
    50  		return e.flushBindings()
    51  	case causetembedded.OpCaptureBindings:
    52  		e.captureBindings()
    53  	case causetembedded.OpEvolveBindings:
    54  		return e.evolveBindings()
    55  	case causetembedded.OpReloadBindings:
    56  		return e.reloadBindings()
    57  	default:
    58  		return errors.Errorf("unsupported ALLEGROALLEGROSQL bind operation: %v", e.sqlBindOp)
    59  	}
    60  	return nil
    61  }
    62  
    63  func (e *ALLEGROSQLBindInterDirc) dropALLEGROSQLBind() error {
    64  	var bindInfo *bindinfo.Binding
    65  	if e.bindALLEGROSQL != "" {
    66  		bindInfo = &bindinfo.Binding{
    67  			BindALLEGROSQL: e.bindALLEGROSQL,
    68  			Charset:        e.charset,
    69  			DefCauslation:  e.defCauslation,
    70  		}
    71  	}
    72  	if !e.isGlobal {
    73  		handle := e.ctx.Value(bindinfo.StochastikBindInfoKeyType).(*bindinfo.StochastikHandle)
    74  		return handle.DropBindRecord(e.normdOrigALLEGROSQL, e.EDB, bindInfo)
    75  	}
    76  	return petri.GetPetri(e.ctx).BindHandle().DropBindRecord(e.normdOrigALLEGROSQL, e.EDB, bindInfo)
    77  }
    78  
    79  func (e *ALLEGROSQLBindInterDirc) createALLEGROSQLBind() error {
    80  	bindInfo := bindinfo.Binding{
    81  		BindALLEGROSQL: e.bindALLEGROSQL,
    82  		Charset:        e.charset,
    83  		DefCauslation:  e.defCauslation,
    84  		Status:         bindinfo.Using,
    85  		Source:         bindinfo.Manual,
    86  	}
    87  	record := &bindinfo.BindRecord{
    88  		OriginalALLEGROSQL: e.normdOrigALLEGROSQL,
    89  		EDB:                e.EDB,
    90  		Bindings:           []bindinfo.Binding{bindInfo},
    91  	}
    92  	if !e.isGlobal {
    93  		handle := e.ctx.Value(bindinfo.StochastikBindInfoKeyType).(*bindinfo.StochastikHandle)
    94  		return handle.CreateBindRecord(e.ctx, record)
    95  	}
    96  	return petri.GetPetri(e.ctx).BindHandle().CreateBindRecord(e.ctx, record)
    97  }
    98  
    99  func (e *ALLEGROSQLBindInterDirc) flushBindings() error {
   100  	return petri.GetPetri(e.ctx).BindHandle().FlushBindings()
   101  }
   102  
   103  func (e *ALLEGROSQLBindInterDirc) captureBindings() {
   104  	petri.GetPetri(e.ctx).BindHandle().CaptureBaselines()
   105  }
   106  
   107  func (e *ALLEGROSQLBindInterDirc) evolveBindings() error {
   108  	return petri.GetPetri(e.ctx).BindHandle().HandleEvolveCausetTask(e.ctx, true)
   109  }
   110  
   111  func (e *ALLEGROSQLBindInterDirc) reloadBindings() error {
   112  	return petri.GetPetri(e.ctx).BindHandle().ReloadBindings()
   113  }