github.com/matrixorigin/matrixone@v1.2.0/pkg/util/internalExecutor/internal_executor.go (about)

     1  // Copyright 2022 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package internalExecutor
    16  
    17  import "context"
    18  
    19  /*
    20  Define InternalExecutor interface here to avoid cycle dependency
    21  
    22  The Impl of InternalExecutor is in frontend package
    23  */
    24  
    25  type SessionOverrideOptions struct {
    26  	Database      *string
    27  	Username      *string
    28  	IsInternal    *bool
    29  	AccountId     *uint32
    30  	UserId        *uint32
    31  	DefaultRoleId *uint32
    32  }
    33  
    34  type OptsBuilder struct {
    35  	opts *SessionOverrideOptions
    36  }
    37  
    38  func NewOptsBuilder() *OptsBuilder {
    39  	return &OptsBuilder{
    40  		opts: new(SessionOverrideOptions),
    41  	}
    42  }
    43  
    44  func (s *OptsBuilder) Database(db string) *OptsBuilder {
    45  	s.opts.Database = &db
    46  	return s
    47  }
    48  
    49  func (s *OptsBuilder) Username(name string) *OptsBuilder {
    50  	s.opts.Username = &name
    51  	return s
    52  }
    53  
    54  func (s *OptsBuilder) Internal(b bool) *OptsBuilder {
    55  	s.opts.IsInternal = &b
    56  	return s
    57  }
    58  
    59  func (s *OptsBuilder) AccountId(id uint32) *OptsBuilder {
    60  	s.opts.AccountId = &id
    61  	return s
    62  }
    63  
    64  func (s *OptsBuilder) UserId(id uint32) *OptsBuilder {
    65  	s.opts.UserId = &id
    66  	return s
    67  }
    68  
    69  func (s *OptsBuilder) DefaultRoleId(id uint32) *OptsBuilder {
    70  	s.opts.DefaultRoleId = &id
    71  	return s
    72  }
    73  
    74  func (s *OptsBuilder) Finish() SessionOverrideOptions {
    75  	return *s.opts
    76  }
    77  
    78  type InternalExecResult interface {
    79  	Error() error
    80  	ColumnCount() uint64
    81  	Column(context.Context, uint64) (string, uint8, bool, error) // type refer: pkg/defines/type.go & func convertEngineTypeToMysqlType
    82  	RowCount() uint64
    83  	Row(context.Context, uint64) ([]interface{}, error)
    84  	Value(context.Context, uint64, uint64) (interface{}, error)
    85  	ValueByName(context.Context, uint64, string) (interface{}, error)
    86  	StringValueByName(context.Context, uint64, string) (string, error)
    87  	Float64ValueByName(context.Context, uint64, string) (float64, error)
    88  }
    89  
    90  type InternalExecutor interface {
    91  	// exec sql without returning results set
    92  	Exec(context.Context, string, SessionOverrideOptions) error
    93  	// exec sql and return results set
    94  	Query(context.Context, string, SessionOverrideOptions) InternalExecResult
    95  	// override session for the executor scope
    96  	ApplySessionOverride(SessionOverrideOptions)
    97  }