github.com/dolthub/go-mysql-server@v0.18.0/sql/planbuilder/binlog_replication.go (about)

     1  // Copyright 2023 Dolthub, 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  // 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 planbuilder
    16  
    17  import (
    18  	"fmt"
    19  
    20  	ast "github.com/dolthub/vitess/go/vt/sqlparser"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/binlogreplication"
    24  	"github.com/dolthub/go-mysql-server/sql/plan"
    25  )
    26  
    27  func (b *Builder) buildChangeReplicationSource(inScope *scope, n *ast.ChangeReplicationSource) (outScope *scope) {
    28  	outScope = inScope.push()
    29  	convertedOptions := make([]binlogreplication.ReplicationOption, 0, len(n.Options))
    30  	for _, option := range n.Options {
    31  		convertedOption := b.buildReplicationOption(inScope, option)
    32  		convertedOptions = append(convertedOptions, *convertedOption)
    33  	}
    34  	repSrc := plan.NewChangeReplicationSource(convertedOptions)
    35  	if binCat, ok := b.cat.(binlogreplication.BinlogReplicaCatalog); ok && binCat.IsBinlogReplicaCatalog() {
    36  		repSrc.ReplicaController = binCat.GetBinlogReplicaController()
    37  	}
    38  	outScope.node = repSrc
    39  	return outScope
    40  }
    41  
    42  func (b *Builder) buildReplicationOption(inScope *scope, option *ast.ReplicationOption) *binlogreplication.ReplicationOption {
    43  	if option.Value == nil {
    44  		err := fmt.Errorf("nil replication option specified for option %q", option.Name)
    45  		b.handleErr(err)
    46  	}
    47  	switch vv := option.Value.(type) {
    48  	case string:
    49  		return binlogreplication.NewReplicationOption(option.Name, binlogreplication.StringReplicationOptionValue{Value: vv})
    50  	case int:
    51  		return binlogreplication.NewReplicationOption(option.Name, binlogreplication.IntegerReplicationOptionValue{Value: vv})
    52  	case ast.TableNames:
    53  		urts := make([]sql.UnresolvedTable, len(vv))
    54  		for i, tableName := range vv {
    55  			// downstream logic expects these to specifically be unresolved tables
    56  			urts[i] = plan.NewUnresolvedTable(tableName.Name.String(), tableName.Qualifier.String())
    57  		}
    58  		return binlogreplication.NewReplicationOption(option.Name, binlogreplication.TableNamesReplicationOptionValue{Value: urts})
    59  	default:
    60  		err := fmt.Errorf("unsupported option value type '%T' specified for option %q", option.Value, option.Name)
    61  		b.handleErr(err)
    62  	}
    63  	return nil
    64  }
    65  
    66  func (b *Builder) buildChangeReplicationFilter(inScope *scope, n *ast.ChangeReplicationFilter) (outScope *scope) {
    67  	outScope = inScope.push()
    68  	convertedOptions := make([]binlogreplication.ReplicationOption, 0, len(n.Options))
    69  	for _, option := range n.Options {
    70  		convertedOption := b.buildReplicationOption(inScope, option)
    71  		convertedOptions = append(convertedOptions, *convertedOption)
    72  	}
    73  	changeFilter := plan.NewChangeReplicationFilter(convertedOptions)
    74  	if binCat, ok := b.cat.(binlogreplication.BinlogReplicaCatalog); ok && binCat.IsBinlogReplicaCatalog() {
    75  		changeFilter.ReplicaController = binCat.GetBinlogReplicaController()
    76  	}
    77  	outScope.node = changeFilter
    78  	return outScope
    79  }