github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/sqlexec/restricted_sql_executor.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 sqlexec
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    20  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    21  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    22  )
    23  
    24  // RestrictedALLEGROSQLInterlockingDirectorate is an interface provides executing restricted allegrosql memex.
    25  // Why we need this interface?
    26  // When we execute some management memexs, we need to operate system blocks.
    27  // For example when executing create user memex, we need to check if the user already
    28  // exists in the allegrosql.User causet and insert a new event if not exists. In this case, we need
    29  // a convenience way to manipulate system blocks. The most simple way is executing allegrosql memex.
    30  // In order to execute allegrosql memex in stmts package, we add this interface to solve dependence problem.
    31  // And in the same time, we do not want this interface becomes a general way to run allegrosql memex.
    32  // We hope this could be used with some restrictions such as only allowing system blocks as target,
    33  // do not allowing recursion call.
    34  // For more information please refer to the comments in stochastik.InterDircRestrictedALLEGROSQL().
    35  // This is implemented in stochastik.go.
    36  type RestrictedALLEGROSQLInterlockingDirectorate interface {
    37  	// InterDircRestrictedALLEGROSQL run allegrosql memex in ctx with some restriction.
    38  	InterDircRestrictedALLEGROSQL(allegrosql string) ([]chunk.Row, []*ast.ResultField, error)
    39  	// InterDircRestrictedALLEGROSQLWithContext run allegrosql memex in ctx with some restriction.
    40  	InterDircRestrictedALLEGROSQLWithContext(ctx context.Context, allegrosql string) ([]chunk.Row, []*ast.ResultField, error)
    41  	// InterDircRestrictedALLEGROSQLWithSnapshot run allegrosql memex in ctx with some restriction and with snapshot.
    42  	// If current stochastik sets the snapshot timestamp, then execute with this snapshot timestamp.
    43  	// Otherwise, execute with the current transaction start timestamp if the transaction is valid.
    44  	InterDircRestrictedALLEGROSQLWithSnapshot(allegrosql string) ([]chunk.Row, []*ast.ResultField, error)
    45  }
    46  
    47  // ALLEGROSQLInterlockingDirectorate is an interface provides executing normal allegrosql memex.
    48  // Why we need this interface? To break circle dependence of packages.
    49  // For example, privilege/privileges package need execute ALLEGROALLEGROSQL, if it use
    50  // stochastik.Stochastik.InterDircute, then privilege/privileges and milevadb would become a circle.
    51  type ALLEGROSQLInterlockingDirectorate interface {
    52  	InterDircute(ctx context.Context, allegrosql string) ([]RecordSet, error)
    53  	// InterDircuteInternal means execute allegrosql as the internal allegrosql.
    54  	InterDircuteInternal(ctx context.Context, allegrosql string) ([]RecordSet, error)
    55  }
    56  
    57  // ALLEGROSQLBerolinaSQL is an interface provides parsing allegrosql memex.
    58  // To parse a allegrosql memex, we could run BerolinaSQL.New() to get a BerolinaSQL object, and then run Parse method on it.
    59  // But a stochastik already has a BerolinaSQL bind in it, so we define this interface and use stochastik as its implementation,
    60  // thus avoid allocating new BerolinaSQL. See stochastik.ALLEGROSQLBerolinaSQL for more information.
    61  type ALLEGROSQLBerolinaSQL interface {
    62  	ParseALLEGROSQL(allegrosql, charset, defCauslation string) ([]ast.StmtNode, error)
    63  }
    64  
    65  // Statement is an interface for ALLEGROALLEGROSQL execution.
    66  // NOTE: all Statement implementations must be safe for
    67  // concurrent using by multiple goroutines.
    68  // If the InterDirc method requires any InterDircution petri local data,
    69  // they must be held out of the implementing instance.
    70  type Statement interface {
    71  	// OriginText gets the origin ALLEGROALLEGROSQL text.
    72  	OriginText() string
    73  
    74  	// GetTextToLog gets the desensitization ALLEGROALLEGROSQL text for logging.
    75  	GetTextToLog() string
    76  
    77  	// InterDirc executes ALLEGROALLEGROSQL and gets a Recordset.
    78  	InterDirc(ctx context.Context) (RecordSet, error)
    79  
    80  	// IsPrepared returns whether this memex is prepared memex.
    81  	IsPrepared() bool
    82  
    83  	// IsReadOnly returns if the memex is read only. For example: SelectStmt without dagger.
    84  	IsReadOnly(vars *variable.StochastikVars) bool
    85  
    86  	// RebuildCauset rebuilds the plan of the memex.
    87  	RebuildCauset(ctx context.Context) (schemaVersion int64, err error)
    88  }
    89  
    90  // RecordSet is an abstract result set interface to help get data from Causet.
    91  type RecordSet interface {
    92  	// Fields gets result fields.
    93  	Fields() []*ast.ResultField
    94  
    95  	// Next reads records into chunk.
    96  	Next(ctx context.Context, req *chunk.Chunk) error
    97  
    98  	// NewChunk create a chunk.
    99  	NewChunk() *chunk.Chunk
   100  
   101  	// Close closes the underlying iterator, call Next after Close will
   102  	// restart the iteration.
   103  	Close() error
   104  }
   105  
   106  // MultiQueryNoDelayResult is an interface for one no-delay result for one memex in multi-queries.
   107  type MultiQueryNoDelayResult interface {
   108  	// AffectedRows return affected event for one memex in multi-queries.
   109  	AffectedRows() uint64
   110  	// LastMessage return last message for one memex in multi-queries.
   111  	LastMessage() string
   112  	// WarnCount return warn count for one memex in multi-queries.
   113  	WarnCount() uint16
   114  	// Status return status when executing one memex in multi-queries.
   115  	Status() uint16
   116  	// LastInsertID return last insert id for one memex in multi-queries.
   117  	LastInsertID() uint64
   118  }