github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/schema/check_coll.go (about)

     1  // Copyright 2021 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 schema
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  )
    21  
    22  type Check interface {
    23  	Name() string
    24  	Expression() string
    25  	Enforced() bool
    26  }
    27  
    28  // CheckCollection is the set of `check` constraints on a table's schema
    29  type CheckCollection interface {
    30  	// AddCheck adds a check to this collection and returns it
    31  	AddCheck(name, expression string, enforce bool) (Check, error)
    32  	// DropCheck removes the check with the name given
    33  	DropCheck(name string) error
    34  	// AllChecks returns all the checks in the collection
    35  	AllChecks() []Check
    36  	// Count returns the size of the collection
    37  	Count() int
    38  }
    39  
    40  type check struct {
    41  	name       string
    42  	expression string
    43  	enforced   bool
    44  }
    45  
    46  func (c check) Name() string {
    47  	return c.name
    48  }
    49  
    50  func (c check) Expression() string {
    51  	return c.expression
    52  }
    53  
    54  func (c check) Enforced() bool {
    55  	return c.enforced
    56  }
    57  
    58  type checkCollection struct {
    59  	checks []check
    60  }
    61  
    62  func (c *checkCollection) AddCheck(name, expression string, enforce bool) (Check, error) {
    63  	for _, chk := range c.checks {
    64  		if strings.ToLower(name) == strings.ToLower(chk.name) {
    65  			// Engine is supposed to enforce this for us, but just in case
    66  			return nil, fmt.Errorf("name %s in use", name)
    67  		}
    68  	}
    69  
    70  	newCheck := check{
    71  		name:       name,
    72  		expression: expression,
    73  		enforced:   enforce,
    74  	}
    75  	c.checks = append(c.checks, newCheck)
    76  
    77  	return newCheck, nil
    78  }
    79  
    80  func (c *checkCollection) DropCheck(name string) error {
    81  	for i, chk := range c.checks {
    82  		if strings.ToLower(name) == strings.ToLower(chk.name) {
    83  			c.checks = append(c.checks[:i], c.checks[i+1:]...)
    84  			return nil
    85  		}
    86  	}
    87  	return nil
    88  }
    89  
    90  func (c *checkCollection) AllChecks() []Check {
    91  	checks := make([]Check, len(c.checks))
    92  	for i, check := range c.checks {
    93  		checks[i] = check
    94  	}
    95  	return checks
    96  }
    97  
    98  func (c *checkCollection) Count() int {
    99  	return len(c.checks)
   100  }
   101  
   102  func NewCheckCollection() CheckCollection {
   103  	return &checkCollection{
   104  		checks: make([]check, 0),
   105  	}
   106  }