github.com/matrixorigin/matrixone@v0.7.0/pkg/frontend/resultset_stmt.go (about)

     1  // Copyright 2021 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 frontend
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/logutil"
    21  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    22  )
    23  
    24  // resultSetStmtExecutor represents the execution outputting result set to the client
    25  type resultSetStmtExecutor struct {
    26  	*baseStmtExecutor
    27  }
    28  
    29  func (rsse *resultSetStmtExecutor) ResponseBeforeExec(ctx context.Context, ses *Session) error {
    30  	var err error
    31  	var columns []interface{}
    32  	proto := ses.GetMysqlProtocol()
    33  	err = rsse.baseStmtExecutor.ResponseBeforeExec(ctx, ses)
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	columns, err = rsse.GetColumns()
    39  	if err != nil {
    40  		logutil.Errorf("GetColumns from Computation handler failed. error: %v", err)
    41  		return err
    42  	}
    43  	/*
    44  		Step 1 : send column count and column definition.
    45  	*/
    46  	//send column count
    47  	colCnt := uint64(len(columns))
    48  	err = proto.SendColumnCountPacket(colCnt)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	//send columns
    53  	//column_count * Protocol::ColumnDefinition packets
    54  	cmd := ses.GetCmd()
    55  	mrs := ses.GetMysqlResultSet()
    56  	for _, c := range columns {
    57  		mysqlc := c.(Column)
    58  		mrs.AddColumn(mysqlc)
    59  
    60  		/*
    61  			mysql COM_QUERY response: send the column definition per column
    62  		*/
    63  		err = proto.SendColumnDefinitionPacket(ctx, mysqlc, int(cmd))
    64  		if err != nil {
    65  			return err
    66  		}
    67  	}
    68  
    69  	/*
    70  		mysql COM_QUERY response: End after the column has been sent.
    71  		send EOF packet
    72  	*/
    73  	err = proto.SendEOFPacketIf(0, 0)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	return nil
    78  }
    79  
    80  func (rsse *resultSetStmtExecutor) ResponseAfterExec(ctx context.Context, ses *Session) error {
    81  	/*
    82  		Step 3: Say goodbye
    83  		mysql COM_QUERY response: End after the data row has been sent.
    84  		After all row data has been sent, it sends the EOF or OK packet.
    85  	*/
    86  	proto := ses.GetMysqlProtocol()
    87  	return proto.sendEOFOrOkPacket(0, 0)
    88  }
    89  
    90  // TODO: special handle for export
    91  type SelectExecutor struct {
    92  	*resultSetStmtExecutor
    93  	sel *tree.Select
    94  }
    95  
    96  type ValuesStmtExecutor struct {
    97  	*resultSetStmtExecutor
    98  	sel *tree.ValuesStatement
    99  }
   100  
   101  type ShowCreateTableExecutor struct {
   102  	*resultSetStmtExecutor
   103  	sct *tree.ShowCreateTable
   104  }
   105  
   106  type ShowCreateDatabaseExecutor struct {
   107  	*resultSetStmtExecutor
   108  	scd *tree.ShowCreateDatabase
   109  }
   110  
   111  type ShowTablesExecutor struct {
   112  	*resultSetStmtExecutor
   113  	st *tree.ShowTables
   114  }
   115  
   116  type ShowDatabasesExecutor struct {
   117  	*resultSetStmtExecutor
   118  	sd *tree.ShowDatabases
   119  }
   120  
   121  type ShowColumnsExecutor struct {
   122  	*resultSetStmtExecutor
   123  	sc *tree.ShowColumns
   124  }
   125  
   126  func (sec *ShowColumnsExecutor) Setup(ctx context.Context, ses *Session) error {
   127  	err := sec.baseStmtExecutor.Setup(ctx, ses)
   128  	if err != nil {
   129  		return err
   130  	}
   131  	ses.SetShowStmtType(ShowColumns)
   132  	ses.SetData(nil)
   133  	return err
   134  }
   135  
   136  type ShowProcessListExecutor struct {
   137  	*resultSetStmtExecutor
   138  	spl *tree.ShowProcessList
   139  }
   140  
   141  type ShowStatusExecutor struct {
   142  	*resultSetStmtExecutor
   143  	ss *tree.ShowStatus
   144  }
   145  
   146  type ShowTableStatusExecutor struct {
   147  	*resultSetStmtExecutor
   148  	sts *tree.ShowTableStatus
   149  }
   150  
   151  func (sec *ShowTableStatusExecutor) Setup(ctx context.Context, ses *Session) error {
   152  	err := sec.baseStmtExecutor.Setup(ctx, ses)
   153  	if err != nil {
   154  		return err
   155  	}
   156  	ses.showStmtType = ShowTableStatus
   157  	ses.SetData(nil)
   158  	return nil
   159  }
   160  
   161  type ShowGrantsExecutor struct {
   162  	*resultSetStmtExecutor
   163  	sg *tree.ShowGrants
   164  }
   165  
   166  type ShowIndexExecutor struct {
   167  	*resultSetStmtExecutor
   168  	si *tree.ShowIndex
   169  }
   170  
   171  type ShowCreateViewExecutor struct {
   172  	*resultSetStmtExecutor
   173  	scv *tree.ShowCreateView
   174  }
   175  
   176  type ShowTargetExecutor struct {
   177  	*resultSetStmtExecutor
   178  	st *tree.ShowTarget
   179  }
   180  
   181  type ExplainForExecutor struct {
   182  	*resultSetStmtExecutor
   183  	ef *tree.ExplainFor
   184  }
   185  
   186  type ExplainStmtExecutor struct {
   187  	*resultSetStmtExecutor
   188  	es *tree.ExplainStmt
   189  }
   190  
   191  func (ese *ExplainStmtExecutor) ExecuteImpl(ctx context.Context, ses *Session) error {
   192  	//TODO
   193  	return nil
   194  }
   195  
   196  type ShowVariablesExecutor struct {
   197  	*resultSetStmtExecutor
   198  	sv *tree.ShowVariables
   199  }
   200  
   201  func (sve *ShowVariablesExecutor) ExecuteImpl(ctx context.Context, ses *Session) error {
   202  	return doShowVariables(ses, sve.GetProcess(), sve.sv)
   203  }
   204  
   205  type ShowErrorsExecutor struct {
   206  	*resultSetStmtExecutor
   207  	se *tree.ShowErrors
   208  }
   209  
   210  func (see *ShowErrorsExecutor) ExecuteImpl(ctx context.Context, ses *Session) error {
   211  	return doShowErrors(ses)
   212  }
   213  
   214  type ShowWarningsExecutor struct {
   215  	*resultSetStmtExecutor
   216  	sw *tree.ShowWarnings
   217  }
   218  
   219  func (swe *ShowWarningsExecutor) ExecuteImpl(ctx context.Context, ses *Session) error {
   220  	return doShowErrors(ses)
   221  }
   222  
   223  type AnalyzeStmtExecutor struct {
   224  	*resultSetStmtExecutor
   225  	as *tree.AnalyzeStmt
   226  }
   227  
   228  func (ase *AnalyzeStmtExecutor) ExecuteImpl(ctx context.Context, ses *Session) error {
   229  	//TODO:
   230  	return nil
   231  }
   232  
   233  type ExplainAnalyzeExecutor struct {
   234  	*resultSetStmtExecutor
   235  	ea *tree.ExplainAnalyze
   236  }
   237  
   238  func (eae *ExplainAnalyzeExecutor) Setup(ctx context.Context, ses *Session) error {
   239  	err := eae.baseStmtExecutor.Setup(ctx, ses)
   240  	if err != nil {
   241  		return err
   242  	}
   243  	ses.SetData(nil)
   244  	switch eae.ea.Statement.(type) {
   245  	case *tree.Delete:
   246  		ses.GetTxnCompileCtx().SetQueryType(TXN_DELETE)
   247  	case *tree.Update:
   248  		ses.GetTxnCompileCtx().SetQueryType(TXN_UPDATE)
   249  	default:
   250  		ses.GetTxnCompileCtx().SetQueryType(TXN_DEFAULT)
   251  	}
   252  	return err
   253  }
   254  
   255  func (eae *ExplainAnalyzeExecutor) ExecuteImpl(ctx context.Context, ses *Session) error {
   256  	//TODO:
   257  	return nil
   258  }
   259  
   260  type InternalCmdFieldListExecutor struct {
   261  	*resultSetStmtExecutor
   262  	icfl *InternalCmdFieldList
   263  }
   264  
   265  func (icfle *InternalCmdFieldListExecutor) ResponseBeforeExec(ctx context.Context, ses *Session) error {
   266  	return nil
   267  }
   268  
   269  func (icfle *InternalCmdFieldListExecutor) ExecuteImpl(ctx context.Context, ses *Session) error {
   270  	return doCmdFieldList(ctx, ses, icfle.icfl)
   271  }
   272  
   273  func (icfle *InternalCmdFieldListExecutor) ResponseAfterExec(ctx context.Context, ses *Session) error {
   274  	var err error
   275  	if icfle.GetStatus() == stmtExecSuccess {
   276  		proto := ses.GetMysqlProtocol()
   277  		/*
   278  			mysql CMD_FIELD_LIST response: End after the column has been sent.
   279  			send EOF packet
   280  		*/
   281  		err = proto.sendEOFOrOkPacket(0, 0)
   282  		if err != nil {
   283  			return err
   284  		}
   285  	}
   286  	return nil
   287  }