Ejemplo 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
}
Ejemplo n.º 2
0
// Set appends to the trees Values slice a list of UnqualifiedColumnNodes
// which are to be modified in the query.
func (self *UpdateManager) Set(columns ...interface{}) *UpdateManager {
	for _, column := range columns {
		self.Tree.Values = append(self.Tree.Values, nodes.UnqualifiedColumn(column))
	}

	return self
}
Ejemplo n.º 3
0
func TestUnqualifiedColumn(t *testing.T) {
	column := nodes.UnqualifiedColumn("column")
	expected := `"column"`
	if got, _ := sql.Accept(column); expected != got {
		t.Errorf("TestUnqualifiedColumn was expected to return %s, got %s", expected, got)
	}
}
Ejemplo n.º 4
0
// AddColumn adds a UnexistingColumn from the nodes package to the AST for creation.
func (self *AlterManager) AlterColumn(name interface{}, typ sql.Type) *AlterManager {
	if _, ok := name.(string); ok {
		name = nodes.UnqualifiedColumn(name)
	}

	self.Tree.ModifiedColumns = append(self.Tree.ModifiedColumns, nodes.ExistingColumn(name, typ))
	return self
}
Ejemplo n.º 5
0
func TestVisitForeignKey(t *testing.T) {
	fkey := nodes.ForeignKey([]interface{}{nodes.UnqualifiedColumn("column")})
	fkey.Options = append(fkey.Options, nodes.Relation("table"))
	expected := `ADD FOREIGN KEY("column") REFERENCES "table"`
	if got, _ := sql.Accept(fkey); expected != got {
		t.Errorf("TestVisitForeignKey was expected to return %s, got %s", expected, got)
	}
}
Ejemplo n.º 6
0
func (self *AlterManager) RemoveColumn(column interface{}) *AlterManager {
	if _, ok := column.(string); ok {
		column = nodes.UnqualifiedColumn(column)
	}

	self.Tree.RemovedColumns = append(self.Tree.RemovedColumns, column)

	return self
}
Ejemplo n.º 7
0
// Appends a projection to the current Context's Projections slice,
// typically an AttributeNode or string.  If a string is provided, it is
// inserted as a LiteralNode.
func (self *SelectManager) Project(projections ...interface{}) *SelectManager {
	for _, projection := range projections {
		if _, ok := projection.(string); ok {
			projection = nodes.UnqualifiedColumn(projection)
		}

		self.Context.Projections = append(self.Context.Projections, projection)
	}

	return self
}