github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/create_schema.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sql
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    19  	"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
    20  )
    21  
    22  type createSchemaNode struct {
    23  	n *tree.CreateSchema
    24  }
    25  
    26  func (n *createSchemaNode) startExec(params runParams) error {
    27  	var schemaExists bool
    28  	if n.n.Schema == tree.PublicSchema {
    29  		schemaExists = true
    30  	} else {
    31  		for _, virtualSchema := range virtualSchemas {
    32  			if n.n.Schema == virtualSchema.name {
    33  				schemaExists = true
    34  				break
    35  			}
    36  		}
    37  	}
    38  	if !schemaExists {
    39  		return unimplemented.NewWithIssuef(26443,
    40  			"new schemas are unsupported")
    41  	}
    42  	if n.n.IfNotExists {
    43  		return nil
    44  	}
    45  	return pgerror.Newf(pgcode.DuplicateSchema, "schema %q already exists", n.n.Schema)
    46  }
    47  
    48  func (*createSchemaNode) Next(runParams) (bool, error) { return false, nil }
    49  func (*createSchemaNode) Values() tree.Datums          { return tree.Datums{} }
    50  func (n *createSchemaNode) Close(ctx context.Context)  {}
    51  
    52  // CreateSchema creates a schema. Currently only works in IF NOT EXISTS mode,
    53  // for schemas that do in fact already exist.
    54  func (p *planner) CreateSchema(ctx context.Context, n *tree.CreateSchema) (planNode, error) {
    55  	return &createSchemaNode{
    56  		n: n,
    57  	}, nil
    58  }