Exemplo n.º 1
0
// AddColumn adds a ConstraintNode from the nodes package to the AST to apply to a column.
func (self *AlterManager) AddConstraint(columns []interface{}, kind sql.Constraint, options ...interface{}) *AlterManager {
	for _, column := range columns {
		if _, ok := column.(string); ok {
			column = nodes.UnqualifiedColumn(column)
		}
	}

	var node interface{}

	switch kind {
	case sql.NotNull:
		node = nodes.NotNull(columns, options...)
	case sql.Unique:
		node = nodes.Unique(columns, options...)
	case sql.PrimaryKey:
		node = nodes.PrimaryKey(columns, options...)
	case sql.ForeignKey:
		node = nodes.ForeignKey(columns, options...)
	case sql.Check:
		node = nodes.Check(columns, options...)
	case sql.Default:
		node = nodes.Default(columns, options...)
	default:
		node = nodes.Constraint(columns, options...)
	}

	self.Tree.Constraints = append(self.Tree.Constraints, node)
	return self
}
Exemplo n.º 2
0
func TestVisitPrimaryKey(t *testing.T) {
	pkey := nodes.PrimaryKey([]interface{}{"column"})
	expected := `ADD PRIMARY KEY('column')`
	if got, _ := sql.Accept(pkey); expected != got {
		t.Errorf("TestVisitPrimaryKey was expected to return %s, got %s", expected, got)
	}
}