Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions postgres/parser/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -2026,9 +2026,9 @@ alter_oneindex_stmt:
{
$$.val = &tree.AlterIndex{Index: $5.tableIndexName(), IfExists: true, Cmd: $6.alterIndexCmd()}
}
| ALTER INDEX table_index_name ATTACH PARTITION index_name
| ALTER INDEX table_index_name ATTACH PARTITION db_object_name
{
$$.val = &tree.AlterIndex{Index: $3.tableIndexName(), Cmd: &tree.AlterIndexAttachPartition{Index: tree.UnrestrictedName($6)}}
$$.val = &tree.AlterIndex{Index: $3.tableIndexName(), Cmd: &tree.AlterIndexAttachPartition{Index: $6.unresolvedObjectName()}}
}
| ALTER INDEX table_index_name opt_no DEPENDS ON EXTENSION name
{
Expand Down Expand Up @@ -2128,9 +2128,9 @@ alter_table_action:
}
}
// ALTER TABLE <name> ADD CONSTRAINT ... USING INDEX
| ADD CONSTRAINT constraint_name unique_or_primary USING INDEX index_name opt_deferrable_mode opt_initially
| ADD CONSTRAINT constraint_name unique_or_primary USING INDEX db_object_name opt_deferrable_mode opt_initially
{
$$.val = tree.AlterTableConstraintUsingIndex{Constraint: tree.Name($3), IsUnique: $4.bool(), Index: tree.Name($7), Deferrable: $8.deferrableMode(), Initially: $9.initiallyMode()}
$$.val = tree.AlterTableConstraintUsingIndex{Constraint: tree.Name($3), IsUnique: $4.bool(), Index: $7.unresolvedObjectName(), Deferrable: $8.deferrableMode(), Initially: $9.initiallyMode()}
}
// ALTER TABLE <name> ALTER CONSTRAINT ...
| ALTER CONSTRAINT constraint_name opt_deferrable_mode opt_initially
Expand Down
4 changes: 2 additions & 2 deletions postgres/parser/sem/tree/alter_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ var _ AlterIndexCmd = &AlterIndexSetTablespace{}

// AlterIndexAttachPartition represents an ALTER INDEX ... ATTACH PARTITION statement.
type AlterIndexAttachPartition struct {
Index UnrestrictedName
Index *UnresolvedObjectName
}

// Format implements the NodeFormatter interface.
func (node *AlterIndexAttachPartition) Format(ctx *FmtCtx) {
ctx.WriteString(" ATTACH PARTITION ")
ctx.FormatNode(&node.Index)
node.Index.Format(ctx)
}

// AlterIndexExtension represents an ALTER INDEX ... [NO] DEPENDS ON EXTENSION statement.
Expand Down
8 changes: 4 additions & 4 deletions postgres/parser/sem/tree/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (node *AlterTableComputed) GetColumn() Name {
type AlterTableConstraintUsingIndex struct {
Constraint Name
IsUnique bool
Index Name
Index *UnresolvedObjectName
Deferrable DeferrableMode
Initially InitiallyMode
}
Expand All @@ -421,7 +421,7 @@ func (node *AlterTableConstraintUsingIndex) Format(ctx *FmtCtx) {
ctx.WriteString(" PRIMARY KEY")
}
ctx.WriteString(" USING INDEX")
ctx.FormatNode(&node.Index)
node.Index.Format(ctx)
switch node.Deferrable {
case Deferrable:
ctx.WriteString(" DEFERRABLE")
Expand Down Expand Up @@ -966,7 +966,7 @@ func (node *AlterTablePartition) Format(ctx *FmtCtx) {
node.Name.Format(ctx)
if node.IsDetach {
ctx.WriteString(" DETACH PARTITION ")
node.Name.Format(ctx)
node.Partition.Format(ctx)
switch node.DetachType {
case DetachPartitionNone:
case DetachPartitionConcurrently:
Expand All @@ -976,7 +976,7 @@ func (node *AlterTablePartition) Format(ctx *FmtCtx) {
}
} else {
ctx.WriteString(" ATTACH PARTITION ")
node.Name.Format(ctx)
node.Partition.Format(ctx)
ctx.WriteByte(' ')
ctx.FormatNode(&node.Spec)
}
Expand Down
51 changes: 50 additions & 1 deletion server/ast/alter_default_privileges.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,61 @@
package ast

import (
"github.com/cockroachdb/errors"
vitess "github.com/dolthub/vitess/go/vt/sqlparser"

"github.com/dolthub/doltgresql/postgres/parser/privilege"
"github.com/dolthub/doltgresql/postgres/parser/sem/tree"
"github.com/dolthub/doltgresql/server/auth"
pgnodes "github.com/dolthub/doltgresql/server/node"
)

// nodeAlterDefaultPrivileges handles *tree.AlterDefaultPrivileges nodes.
func nodeAlterDefaultPrivileges(ctx *Context, node *tree.AlterDefaultPrivileges) (vitess.Statement, error) {
return NotYetSupportedError("ALTER DEFAULT PRIVILEGES statement is not yet supported")
if node == nil {
return nil, nil
}

objType, err := convertDefaultPrivilegeObjectType(node.Target.TargetType)
if err != nil {
return nil, err
}

privileges, err := convertPrivilegeKinds(objType, node.Privileges)
if err != nil {
return nil, err
}

return vitess.InjectedStatement{
Statement: &pgnodes.AlterDefaultPrivileges{
OwnerRoles: node.TargetRoles,
Schemas: node.Target.InSchema,
ObjectType: objType,
Privileges: privileges,
Grantees: node.Grantees,
Grant: node.Grant,
GrantOption: node.GrantOption,
Cascade: node.DropBehavior == tree.DropCascade,
},
Children: nil,
}, nil
}

// convertDefaultPrivilegeObjectType converts a privilege.ObjectType to an auth.PrivilegeObject for use in default
// privileges. Only the object types valid for ALTER DEFAULT PRIVILEGES are accepted.
func convertDefaultPrivilegeObjectType(objType privilege.ObjectType) (auth.PrivilegeObject, error) {
switch objType {
case privilege.Table:
return auth.PrivilegeObject_TABLE, nil
case privilege.Sequence:
return auth.PrivilegeObject_SEQUENCE, nil
case privilege.Function, privilege.Procedure, privilege.Routine:
return auth.PrivilegeObject_FUNCTION, nil
case privilege.Schema:
return auth.PrivilegeObject_SCHEMA, nil
case privilege.Type:
return auth.PrivilegeObject_TYPE, nil
default:
return 0, errors.Errorf("object type %q is not supported in ALTER DEFAULT PRIVILEGES", string(objType))
}
}
11 changes: 11 additions & 0 deletions server/auth/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Database struct {
sequencePrivileges *SequencePrivileges
routinePrivileges *RoutinePrivileges
roleMembership *RoleMembership
defaultPrivileges *DefaultPrivileges
}

// ClearDatabase clears the internal database, leaving only the default users. This is primarily for use by tests.
Expand All @@ -57,6 +58,7 @@ func ClearDatabase() {
clear(globalDatabase.sequencePrivileges.Data)
clear(globalDatabase.routinePrivileges.Data)
clear(globalDatabase.roleMembership.Data)
clear(globalDatabase.defaultPrivileges.Data)
dbInitDefault()
}

Expand Down Expand Up @@ -96,6 +98,14 @@ func RoleExists(name string) bool {
return ok
}

// GetRoleName returns the name of the role with the given ID. Returns an empty string if the role does not exist.
func GetRoleName(id RoleID) string {
if role, ok := globalDatabase.rolesByID[id]; ok {
return role.Name
}
return ""
}

// SetRole sets the role matching the given name. This will add a role that does not yet exist, and overwrite an
// existing role.
func SetRole(role Role) {
Expand Down Expand Up @@ -143,6 +153,7 @@ func dbInit(dEnv *env.DoltEnv, cfg Config) {
sequencePrivileges: NewSequencePrivileges(),
routinePrivileges: NewRoutinePrivileges(),
roleMembership: NewRoleMembership(),
defaultPrivileges: NewDefaultPrivileges(),
}
globalLock = &sync.RWMutex{}
if dEnv != nil {
Expand Down
Loading
Loading