vitess.io/vitess@v0.16.2/go/vt/schemamanager/ui_controller.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package schemamanager
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"net/http"
    23  	"strings"
    24  
    25  	"context"
    26  
    27  	"vitess.io/vitess/go/vt/log"
    28  )
    29  
    30  // UIController handles schema events.
    31  type UIController struct {
    32  	sqls     []string
    33  	keyspace string
    34  	writer   http.ResponseWriter
    35  }
    36  
    37  // NewUIController creates a UIController instance
    38  func NewUIController(
    39  	sqlStr string, keyspace string, writer http.ResponseWriter) *UIController {
    40  	controller := &UIController{
    41  		sqls:     make([]string, 0, 32),
    42  		keyspace: keyspace,
    43  		writer:   writer,
    44  	}
    45  	for _, sql := range strings.Split(sqlStr, ";") {
    46  		s := strings.TrimSpace(sql)
    47  		if s != "" {
    48  			controller.sqls = append(controller.sqls, s)
    49  		}
    50  	}
    51  
    52  	return controller
    53  }
    54  
    55  // Open is a no-op.
    56  func (controller *UIController) Open(ctx context.Context) error {
    57  	return nil
    58  }
    59  
    60  // Read reads schema changes
    61  func (controller *UIController) Read(ctx context.Context) ([]string, error) {
    62  	return controller.sqls, nil
    63  }
    64  
    65  // Close is a no-op.
    66  func (controller *UIController) Close() {
    67  }
    68  
    69  // Keyspace returns keyspace to apply schema.
    70  func (controller *UIController) Keyspace() string {
    71  	return controller.keyspace
    72  }
    73  
    74  // OnReadSuccess is no-op
    75  func (controller *UIController) OnReadSuccess(ctx context.Context) error {
    76  	controller.writer.Write(
    77  		[]byte(fmt.Sprintf("OnReadSuccess, sqls: %v\n", controller.sqls)))
    78  	return nil
    79  }
    80  
    81  // OnReadFail is no-op
    82  func (controller *UIController) OnReadFail(ctx context.Context, err error) error {
    83  	controller.writer.Write(
    84  		[]byte(fmt.Sprintf("OnReadFail, error: %v\n", err)))
    85  	return err
    86  }
    87  
    88  // OnValidationSuccess is no-op
    89  func (controller *UIController) OnValidationSuccess(ctx context.Context) error {
    90  	controller.writer.Write(
    91  		[]byte(fmt.Sprintf("OnValidationSuccess, sqls: %v\n", controller.sqls)))
    92  	return nil
    93  }
    94  
    95  // OnValidationFail is no-op
    96  func (controller *UIController) OnValidationFail(ctx context.Context, err error) error {
    97  	controller.writer.Write(
    98  		[]byte(fmt.Sprintf("OnValidationFail, error: %v\n", err)))
    99  	return err
   100  }
   101  
   102  // OnExecutorComplete is no-op
   103  func (controller *UIController) OnExecutorComplete(ctx context.Context, result *ExecuteResult) error {
   104  	data, err := json.Marshal(result)
   105  	if err != nil {
   106  		log.Errorf("Failed to serialize ExecuteResult: %v", err)
   107  		return err
   108  	}
   109  	controller.writer.Write([]byte(fmt.Sprintf("Executor succeeds: %s", string(data))))
   110  	return nil
   111  }
   112  
   113  var _ Controller = (*UIController)(nil)