github.com/matrixorigin/matrixone@v1.2.0/pkg/frontend/self_handle.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  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    19  )
    20  
    21  func execInFrontend(ses *Session, execCtx *ExecCtx) (err error) {
    22  	//check transaction states
    23  	switch st := execCtx.stmt.(type) {
    24  	case *tree.BeginTransaction:
    25  		RecordStatementTxnID(execCtx.reqCtx, ses)
    26  	case *tree.CommitTransaction:
    27  	case *tree.RollbackTransaction:
    28  	case *tree.SetRole:
    29  
    30  		ses.InvalidatePrivilegeCache()
    31  		//switch role
    32  		err = handleSwitchRole(ses, execCtx, st)
    33  		if err != nil {
    34  			return
    35  		}
    36  	case *tree.Use:
    37  
    38  		var v interface{}
    39  		v, err = ses.GetGlobalVar(execCtx.reqCtx, "lower_case_table_names")
    40  		if err != nil {
    41  			return
    42  		}
    43  		st.Name.SetConfig(v.(int64))
    44  		//use database
    45  		err = handleChangeDB(ses, execCtx, st.Name.Compare())
    46  		if err != nil {
    47  			return
    48  		}
    49  		err = changeVersion(execCtx.reqCtx, ses, st.Name.Compare())
    50  		if err != nil {
    51  			return
    52  		}
    53  	case *tree.MoDump:
    54  
    55  		//dump
    56  		err = handleDump(ses, execCtx, st)
    57  		if err != nil {
    58  			return
    59  		}
    60  	case *tree.PrepareStmt:
    61  
    62  		execCtx.prepareStmt, err = handlePrepareStmt(ses, execCtx, st)
    63  		if err != nil {
    64  			return
    65  		}
    66  		err = authenticateUserCanExecutePrepareOrExecute(execCtx.reqCtx, ses, execCtx.prepareStmt.PrepareStmt, execCtx.prepareStmt.PreparePlan.GetDcl().GetPrepare().GetPlan())
    67  		if err != nil {
    68  			ses.RemovePrepareStmt(execCtx.prepareStmt.Name)
    69  			return
    70  		}
    71  	case *tree.PrepareString:
    72  		execCtx.prepareStmt, err = handlePrepareString(ses, execCtx, st)
    73  		if err != nil {
    74  			return
    75  		}
    76  		err = authenticateUserCanExecutePrepareOrExecute(execCtx.reqCtx, ses, execCtx.prepareStmt.PrepareStmt, execCtx.prepareStmt.PreparePlan.GetDcl().GetPrepare().GetPlan())
    77  		if err != nil {
    78  			ses.RemovePrepareStmt(execCtx.prepareStmt.Name)
    79  			return
    80  		}
    81  	case *tree.CreateConnector:
    82  
    83  		err = handleCreateConnector(execCtx.reqCtx, ses, st)
    84  		if err != nil {
    85  			return
    86  		}
    87  	case *tree.PauseDaemonTask:
    88  
    89  		err = handlePauseDaemonTask(execCtx.reqCtx, ses, st)
    90  		if err != nil {
    91  			return
    92  		}
    93  	case *tree.CancelDaemonTask:
    94  
    95  		err = handleCancelDaemonTask(execCtx.reqCtx, ses, st.TaskID)
    96  		if err != nil {
    97  			return
    98  		}
    99  	case *tree.ResumeDaemonTask:
   100  
   101  		err = handleResumeDaemonTask(execCtx.reqCtx, ses, st)
   102  		if err != nil {
   103  			return
   104  		}
   105  	case *tree.DropConnector:
   106  
   107  		err = handleDropConnector(execCtx.reqCtx, ses, st)
   108  		if err != nil {
   109  			return
   110  		}
   111  	case *tree.ShowConnectors:
   112  
   113  		if err = handleShowConnectors(execCtx.reqCtx, ses); err != nil {
   114  			return
   115  		}
   116  	case *tree.Deallocate:
   117  
   118  		err = handleDeallocate(ses, execCtx, st)
   119  		if err != nil {
   120  			return
   121  		}
   122  	case *tree.Reset:
   123  
   124  		err = handleReset(ses, execCtx, st)
   125  		if err != nil {
   126  			return
   127  		}
   128  	case *tree.SetVar:
   129  
   130  		err = handleSetVar(ses, execCtx, st, execCtx.sqlOfStmt)
   131  		if err != nil {
   132  			return
   133  		}
   134  	case *tree.ShowVariables:
   135  
   136  		err = handleShowVariables(ses, execCtx, st)
   137  		if err != nil {
   138  			return
   139  		}
   140  	case *tree.ShowErrors, *tree.ShowWarnings:
   141  
   142  		err = handleShowErrors(ses)
   143  		if err != nil {
   144  			return
   145  		}
   146  	case *tree.AnalyzeStmt:
   147  
   148  		if err = handleAnalyzeStmt(ses, execCtx, st); err != nil {
   149  			return
   150  		}
   151  	case *tree.ExplainStmt:
   152  
   153  		if err = handleExplainStmt(ses, execCtx, st); err != nil {
   154  			return
   155  		}
   156  	case *InternalCmdFieldList:
   157  
   158  		if err = handleCmdFieldList(ses, execCtx, st); err != nil {
   159  			return
   160  		}
   161  	case *tree.CreatePublication:
   162  
   163  		if err = handleCreatePublication(ses, execCtx, st); err != nil {
   164  			return
   165  		}
   166  	case *tree.AlterPublication:
   167  
   168  		if err = handleAlterPublication(ses, execCtx, st); err != nil {
   169  			return
   170  		}
   171  	case *tree.DropPublication:
   172  
   173  		if err = handleDropPublication(ses, execCtx, st); err != nil {
   174  			return
   175  		}
   176  	case *tree.ShowSubscriptions:
   177  
   178  		if err = handleShowSubscriptions(ses, execCtx, st); err != nil {
   179  			return
   180  		}
   181  	case *tree.CreateStage:
   182  
   183  		if err = handleCreateStage(ses, execCtx, st); err != nil {
   184  			return
   185  		}
   186  	case *tree.DropStage:
   187  
   188  		if err = handleDropStage(ses, execCtx, st); err != nil {
   189  			return
   190  		}
   191  	case *tree.AlterStage:
   192  
   193  		if err = handleAlterStage(ses, execCtx, st); err != nil {
   194  			return
   195  		}
   196  	case *tree.CreateAccount:
   197  
   198  		ses.InvalidatePrivilegeCache()
   199  		if err = handleCreateAccount(ses, execCtx, st, execCtx.proc); err != nil {
   200  			return
   201  		}
   202  	case *tree.DropAccount:
   203  
   204  		ses.InvalidatePrivilegeCache()
   205  		if err = handleDropAccount(ses, execCtx, st, execCtx.proc); err != nil {
   206  			return
   207  		}
   208  	case *tree.AlterAccount:
   209  		ses.InvalidatePrivilegeCache()
   210  
   211  		if err = handleAlterAccount(ses, execCtx, st, execCtx.proc); err != nil {
   212  			return
   213  		}
   214  	case *tree.AlterDataBaseConfig:
   215  		ses.InvalidatePrivilegeCache()
   216  
   217  		if st.IsAccountLevel {
   218  			if err = handleAlterAccountConfig(ses, execCtx, st); err != nil {
   219  				return
   220  			}
   221  		} else {
   222  			if err = handleAlterDataBaseConfig(ses, execCtx, st); err != nil {
   223  				return
   224  			}
   225  		}
   226  	case *tree.CreateUser:
   227  
   228  		ses.InvalidatePrivilegeCache()
   229  		if err = handleCreateUser(ses, execCtx, st); err != nil {
   230  			return
   231  		}
   232  	case *tree.DropUser:
   233  
   234  		ses.InvalidatePrivilegeCache()
   235  		if err = handleDropUser(ses, execCtx, st); err != nil {
   236  			return
   237  		}
   238  	case *tree.AlterUser: //TODO
   239  
   240  		ses.InvalidatePrivilegeCache()
   241  		if err = handleAlterUser(ses, execCtx, st); err != nil {
   242  			return
   243  		}
   244  	case *tree.CreateRole:
   245  
   246  		ses.InvalidatePrivilegeCache()
   247  		if err = handleCreateRole(ses, execCtx, st); err != nil {
   248  			return
   249  		}
   250  	case *tree.DropRole:
   251  
   252  		ses.InvalidatePrivilegeCache()
   253  		if err = handleDropRole(ses, execCtx, st); err != nil {
   254  			return
   255  		}
   256  	case *tree.CreateFunction:
   257  
   258  		if err = st.Valid(); err != nil {
   259  			return err
   260  		}
   261  		if err = handleCreateFunction(ses, execCtx, st); err != nil {
   262  			return
   263  		}
   264  	case *tree.DropFunction:
   265  
   266  		if err = handleDropFunction(ses, execCtx, st, execCtx.proc); err != nil {
   267  			return
   268  		}
   269  	case *tree.CreateProcedure:
   270  
   271  		if err = handleCreateProcedure(ses, execCtx, st); err != nil {
   272  			return
   273  		}
   274  	case *tree.DropProcedure:
   275  
   276  		if err = handleDropProcedure(ses, execCtx, st); err != nil {
   277  			return
   278  		}
   279  	case *tree.CallStmt:
   280  
   281  		if err = handleCallProcedure(ses, execCtx, st, execCtx.proc); err != nil {
   282  			return
   283  		}
   284  	case *tree.Grant:
   285  
   286  		ses.InvalidatePrivilegeCache()
   287  		switch st.Typ {
   288  		case tree.GrantTypeRole:
   289  			if err = handleGrantRole(ses, execCtx, &st.GrantRole); err != nil {
   290  				return
   291  			}
   292  		case tree.GrantTypePrivilege:
   293  			if err = handleGrantPrivilege(ses, execCtx, &st.GrantPrivilege); err != nil {
   294  				return
   295  			}
   296  		}
   297  	case *tree.Revoke:
   298  
   299  		ses.InvalidatePrivilegeCache()
   300  		switch st.Typ {
   301  		case tree.RevokeTypeRole:
   302  			if err = handleRevokeRole(ses, execCtx, &st.RevokeRole); err != nil {
   303  				return
   304  			}
   305  		case tree.RevokeTypePrivilege:
   306  			if err = handleRevokePrivilege(ses, execCtx, &st.RevokePrivilege); err != nil {
   307  				return
   308  			}
   309  		}
   310  	case *tree.Kill:
   311  
   312  		ses.InvalidatePrivilegeCache()
   313  		if err = handleKill(ses, execCtx, st); err != nil {
   314  			return
   315  		}
   316  	case *tree.ShowAccounts:
   317  
   318  		if err = handleShowAccounts(ses, execCtx, st); err != nil {
   319  			return
   320  		}
   321  	case *tree.ShowCollation:
   322  
   323  		if err = handleShowCollation(ses, execCtx, st); err != nil {
   324  			return
   325  		}
   326  	case *tree.ShowBackendServers:
   327  		if err = handleShowBackendServers(ses, execCtx); err != nil {
   328  			return
   329  		}
   330  	case *tree.SetTransaction:
   331  
   332  		//TODO: handle set transaction
   333  	case *tree.LockTableStmt:
   334  
   335  	case *tree.UnLockTableStmt:
   336  
   337  	case *tree.BackupStart:
   338  
   339  		if err = handleStartBackup(ses, execCtx, st); err != nil {
   340  			return
   341  		}
   342  	case *tree.EmptyStmt:
   343  
   344  		if err = handleEmptyStmt(ses, execCtx, st); err != nil {
   345  			return
   346  		}
   347  	case *tree.CreateSnapShot:
   348  		//TODO: invalidate privilege cache
   349  		if err = handleCreateSnapshot(ses, execCtx, st); err != nil {
   350  			return
   351  		}
   352  	case *tree.DropSnapShot:
   353  		//TODO: invalidate privilege cache
   354  		if err = handleDropSnapshot(ses, execCtx, st); err != nil {
   355  			return
   356  		}
   357  	case *tree.RestoreSnapShot:
   358  		//TODO: invalidate privilege cache
   359  		if err = handleRestoreSnapshot(ses, execCtx, st); err != nil {
   360  			return
   361  		}
   362  	case *tree.UpgradeStatement:
   363  		//TODO: invalidate privilege cache
   364  		if err = handleExecUpgrade(ses, execCtx, st); err != nil {
   365  			return
   366  		}
   367  	}
   368  	return
   369  }