コード例 #1
0
ファイル: alter_manager.go プロジェクト: phanle/codex
func (self *AlterManager) RemoveIndex(name interface{}) *AlterManager {
	if _, ok := name.(string); ok {
		name = nodes.IndexName(name)
	}

	self.Tree.RemovedIndicies = append(self.Tree.RemovedIndicies, name)

	return self
}
コード例 #2
0
ファイル: to_sql_visitor.go プロジェクト: phanle/codex
func (self *ToSqlVisitor) VisitPrimaryKey(o *nodes.PrimaryKeyNode, visitor VisitorInterface) string {
	str := "ADD "
	// Optional index name.
	if 0 < len(o.Options) {
		expr := o.Options[0]
		if _, ok := expr.(string); ok {
			expr = nodes.IndexName(expr)
		}

		str = fmt.Sprintf("%vCONSTRAINT %v ", str, visitor.Visit(expr, visitor))
	}

	return fmt.Sprintf("%vPRIMARY KEY(%v)", str, visitor.FormatConstraintColumns(o.Columns, visitor))
}
コード例 #3
0
ファイル: to_sql_visitor.go プロジェクト: phanle/codex
func (self *ToSqlVisitor) VisitForeignKey(o *nodes.ForeignKeyNode, visitor VisitorInterface) string {
	if 0 >= len(o.Options) {
		panic("Missing column REFERENCE name for FOREIGN KEY constraint.")
	}

	str := "ADD "
	// For FOREIGN KEY, index name is optional, REFERENCES is not.
	//
	// No index name ex.
	//
	//  CreateTable("orders").
	//    AddColumn("user_id").
	//    AddConstraint("user_id", FOREIGN_KEY, "users")
	//
	// With option index name ex.
	//
	//  CreateTable("orders").
	//    AddColumn("user_id").
	//    AddConstraint("user_id", FOREIGN_KEY, "users_fkey", "users")
	if 1 < len(o.Options) {
		expr := o.Options[0]
		if _, ok := expr.(string); ok {
			expr = nodes.IndexName(expr)
		}

		// Remove this item from the array, avoiding any potential memory leak.
		// https://code.google.com/p/go-wiki/wiki/SliceTricks
		length := len(o.Options) - 1
		copy(o.Options[0:], o.Options[1:])
		o.Options[length] = nil
		o.Options = o.Options[:length]

		str = fmt.Sprintf("%vCONSTRAINT %v%v", str, visitor.Visit(expr, visitor), SPACE)
	}

	str = fmt.Sprintf("%vFOREIGN KEY(%v)", str, visitor.FormatConstraintColumns(o.Columns, visitor))

	// If option is not here, user didn't do it right, but don't dereference and panic.
	if 0 < len(o.Options) {
		relation := o.Options[0]
		if _, ok := relation.(string); ok {
			relation = nodes.Relation(relation.(string))
		}

		str = fmt.Sprintf("%v%vREFERENCES %v", str, SPACE, visitor.Visit(relation, visitor))
	}

	return str
}