Exemplo n.º 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
}
Exemplo n.º 2
0
func checkConstraintQuery(buf *bytes.Buffer, c *pqt.Constraint) {
	fmt.Fprintf(buf, `CONSTRAINT "%s" CHECK (%s)`, c.Name(), c.Check)
}
Exemplo n.º 3
0
func primaryKeyConstraintQuery(buf *bytes.Buffer, c *pqt.Constraint) {
	fmt.Fprintf(buf, `CONSTRAINT "%s" PRIMARY KEY (%s)`, c.Name(), pqt.JoinColumns(c.Columns, ", "))
}
Exemplo n.º 4
0
func uniqueConstraintQuery(buf *bytes.Buffer, c *pqt.Constraint) {
	fmt.Fprintf(buf, `CONSTRAINT "%s" UNIQUE (%s)`, c.Name(), pqt.JoinColumns(c.Columns, ", "))
}