github.com/dolthub/go-mysql-server@v0.18.0/sql/planbuilder/spatial.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  	"strconv"
    20  	"unicode"
    21  
    22  	ast "github.com/dolthub/vitess/go/vt/sqlparser"
    23  
    24  	"github.com/dolthub/go-mysql-server/sql/plan"
    25  )
    26  
    27  func (b *Builder) buildCreateSpatialRefSys(inScope *scope, n *ast.CreateSpatialRefSys) (outScope *scope) {
    28  	outScope = inScope.push()
    29  	srid, err := strconv.ParseInt(string(n.SRID.Val), 10, 16)
    30  	if err != nil {
    31  		b.handleErr(err)
    32  	}
    33  
    34  	if n.SrsAttr == nil {
    35  		b.handleErr(fmt.Errorf("missing attribute"))
    36  	}
    37  
    38  	if n.SrsAttr.Name == "" {
    39  		b.handleErr(fmt.Errorf("missing mandatory attribute NAME"))
    40  	}
    41  	if unicode.IsSpace(rune(n.SrsAttr.Name[0])) || unicode.IsSpace(rune(n.SrsAttr.Name[len(n.SrsAttr.Name)-1])) {
    42  		b.handleErr(fmt.Errorf("the spatial reference system name can't be an empty string or start or end with whitespace"))
    43  	}
    44  	// TODO: there are additional rules to validate the attribute definition
    45  	if n.SrsAttr.Definition == "" {
    46  		b.handleErr(fmt.Errorf("missing mandatory attribute DEFINITION"))
    47  	}
    48  	if n.SrsAttr.Organization == "" {
    49  		b.handleErr(fmt.Errorf("missing mandatory attribute ORGANIZATION NAME"))
    50  	}
    51  	if unicode.IsSpace(rune(n.SrsAttr.Organization[0])) || unicode.IsSpace(rune(n.SrsAttr.Organization[len(n.SrsAttr.Organization)-1])) {
    52  		b.handleErr(fmt.Errorf("the organization name can't be an empty string or start or end with whitespace"))
    53  	}
    54  	if n.SrsAttr.OrgID == nil {
    55  		b.handleErr(fmt.Errorf("missing mandatory attribute ORGANIZATION ID"))
    56  	}
    57  	orgID, err := strconv.ParseInt(string(n.SrsAttr.OrgID.Val), 10, 16)
    58  	if err != nil {
    59  		b.handleErr(err)
    60  	}
    61  
    62  	srsAttr := plan.SrsAttribute{
    63  		Name:         n.SrsAttr.Name,
    64  		Definition:   n.SrsAttr.Definition,
    65  		Organization: n.SrsAttr.Organization,
    66  		OrgID:        uint32(orgID),
    67  		Description:  n.SrsAttr.Description,
    68  	}
    69  	newN, err := plan.NewCreateSpatialRefSys(uint32(srid), n.OrReplace, n.IfNotExists, srsAttr)
    70  	if err != nil {
    71  		b.handleErr(err)
    72  	}
    73  	outScope.node = newN
    74  	return outScope
    75  }