Example #1
0
func foreignKeyConstraintQuery(buf *bytes.Buffer, c *pqt.Constraint) error {
	switch {
	case len(c.Columns) == 0:
		return errors.New("pqt: foreign key constraint require at least one column")
	case len(c.ReferenceColumns) == 0:
		return errors.New("pqt: foreign key constraint require at least one reference column")
	case c.ReferenceTable == nil:
		return errors.New("pqt: foreiqn key constraint missing reference table")
	}

	fmt.Fprintf(buf, `CONSTRAINT "%s" FOREIGN KEY (%s) REFERENCES %s (%s)`,
		c.Name(),
		pqt.JoinColumns(c.Columns, ", "),
		c.ReferenceTable.FullName(),
		pqt.JoinColumns(c.ReferenceColumns, ", "),
	)

	switch c.OnDelete {
	case pqt.Cascade:
		buf.WriteString(" ON DELETE CASCADE")
	case pqt.Restrict:
		buf.WriteString(" ON DELETE RESTRICT")
	case pqt.SetNull:
		buf.WriteString(" ON DELETE SET NULL")
	case pqt.SetDefault:
		buf.WriteString(" ON DELETE SET DEFAULT")
	}

	switch c.OnUpdate {
	case pqt.Cascade:
		buf.WriteString(" ON UPDATE CASCADE")
	case pqt.Restrict:
		buf.WriteString(" ON UPDATE RESTRICT")
	case pqt.SetNull:
		buf.WriteString(" ON UPDATE SET NULL")
	case pqt.SetDefault:
		buf.WriteString(" ON UPDATE SET DEFAULT")
	}

	return nil
}
Example #2
0
func (g *Generator) generateConstantsConstraints(w io.Writer, table *pqt.Table) {
	for _, c := range tableConstraints(table) {
		name := fmt.Sprintf("%s", pqt.JoinColumns(c.Columns, "_"))
		switch c.Type {
		case pqt.ConstraintTypeCheck:
			fmt.Fprintf(w, `%s%sConstraint%sCheck = "%s"`, g.name("table"), g.public(table.Name), g.public(name), c.String())
		case pqt.ConstraintTypePrimaryKey:
			fmt.Fprintf(w, `%s%sConstraintPrimaryKey = "%s"`, g.name("table"), g.public(table.Name), c.String())
		case pqt.ConstraintTypeForeignKey:
			fmt.Fprintf(w, `%s%sConstraint%sForeignKey = "%s"`, g.name("table"), g.public(table.Name), g.public(name), c.String())
		case pqt.ConstraintTypeExclusion:
			fmt.Fprintf(w, `%s%sConstraint%sExclusion = "%s"`, g.name("table"), g.public(table.Name), g.public(name), c.String())
		case pqt.ConstraintTypeUnique:
			fmt.Fprintf(w, `%s%sConstraint%sUnique = "%s"`, g.name("table"), g.public(table.Name), g.public(name), c.String())
		case pqt.ConstraintTypeIndex:
			fmt.Fprintf(w, `%s%sConstraint%sIndex = "%s"`, g.name("table"), g.public(table.Name), g.public(name), c.String())
		}

		io.WriteString(w, "\n")
	}
}
Example #3
0
func primaryKeyConstraintQuery(buf *bytes.Buffer, c *pqt.Constraint) {
	fmt.Fprintf(buf, `CONSTRAINT "%s" PRIMARY KEY (%s)`, c.Name(), pqt.JoinColumns(c.Columns, ", "))
}
Example #4
0
func uniqueConstraintQuery(buf *bytes.Buffer, c *pqt.Constraint) {
	fmt.Fprintf(buf, `CONSTRAINT "%s" UNIQUE (%s)`, c.Name(), pqt.JoinColumns(c.Columns, ", "))
}