moved helper function and created error variable

This commit is contained in:
James Cor
2022-01-31 17:01:39 -08:00
parent 2f4947dbb6
commit 6da740ab04
2 changed files with 20 additions and 26 deletions
@@ -30,6 +30,22 @@ import (
"github.com/dolthub/dolt/go/store/types"
)
var ErrUsingSpatialKey = errors.NewKind("can't use Spatial Types as Primary Key for table %s")
// IsUsingSpatialColAsKey is a utility function that checks for any spatial types being used as a primary key
func IsUsingSpatialColAsKey(sch schema.Schema) bool {
pkCols := sch.GetPKCols()
cols := pkCols.GetColumns()
for _, c := range cols {
if c.TypeInfo.Equals(typeinfo.PointType) ||
c.TypeInfo.Equals(typeinfo.LinestringType) ||
c.TypeInfo.Equals(typeinfo.PolygonType) {
return true
}
}
return false
}
func AddPrimaryKeyToTable(ctx context.Context, table *doltdb.Table, tableName string, nbf *types.NomsBinFormat, columns []sql.IndexColumn, opts editor.Options) (*doltdb.Table, error) {
sch, err := table.GetSchema(ctx)
if err != nil {
@@ -40,15 +56,8 @@ func AddPrimaryKeyToTable(ctx context.Context, table *doltdb.Table, tableName st
return nil, sql.ErrMultiplePrimaryKeysDefined.New() // Also caught in GMS
}
// Prevent adding a spatial type column as primary key column
pkCols := sch.GetPKCols()
cols := pkCols.GetColumns()
for _, c := range cols {
if c.TypeInfo.Equals(typeinfo.PointType) ||
c.TypeInfo.Equals(typeinfo.LinestringType) ||
c.TypeInfo.Equals(typeinfo.PolygonType) {
return nil, errors.NewKind("can't use Spatial Types as Primary Key for table %s").New(tableName)
}
if IsUsingSpatialColAsKey(sch) {
return nil, ErrUsingSpatialKey.New(tableName)
}
pkColOrdering := make(map[string]int, len(columns))
+2 -17
View File
@@ -31,7 +31,6 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/row"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/schema/alterschema"
"github.com/dolthub/dolt/go/libraries/doltcore/schema/typeinfo"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dtables"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/globalstate"
@@ -700,20 +699,6 @@ func (db Database) CreateTable(ctx *sql.Context, tableName string, sch sql.Prima
return db.createSqlTable(ctx, tableName, sch)
}
// isUsingSpatialColAsKey is a utility function that checks for any spatial types being used as a primary key
func isUsingSpatialColAsKey(sch schema.Schema) bool {
pkCols := sch.GetPKCols()
cols := pkCols.GetColumns()
for _, c := range cols {
if c.TypeInfo.Equals(typeinfo.PointType) ||
c.TypeInfo.Equals(typeinfo.LinestringType) ||
c.TypeInfo.Equals(typeinfo.PolygonType) {
return true
}
}
return false
}
// Unlike the exported version CreateTable, createSqlTable doesn't enforce any table name checks.
func (db Database) createSqlTable(ctx *sql.Context, tableName string, sch sql.PrimaryKeySchema) error {
root, err := db.GetRoot(ctx)
@@ -738,8 +723,8 @@ func (db Database) createSqlTable(ctx *sql.Context, tableName string, sch sql.Pr
}
// Prevent any tables that use Spatial Types as Primary Key from being created
if isUsingSpatialColAsKey(doltSch) {
return errors.NewKind("can't use Spatial Types as Primary Key for table %s").New(tableName)
if alterschema.IsUsingSpatialColAsKey(doltSch) {
return alterschema.ErrUsingSpatialKey.New(tableName)
}
return db.createDoltTable(ctx, tableName, root, doltSch)