Files
simpleorm/schema/constraint.go
T
gdulai ffd9cd004e
Go Tests / test (push) Failing after 6s
Introduce Exec interface and implementers (#1)
Reviewed-on: #1
2026-05-11 06:03:40 +00:00

79 lines
1.5 KiB
Go

package schema
import (
"errors"
"strings"
)
type Constraint struct {
Name string
Type string
Columns []Column
RefTable *Table
RefColumns []Column
}
func (c *Constraint) GetDDL() (string, error) {
switch c.Type {
case "pk":
return c.getPkDDL(), nil
case "fk":
return c.getFkDDL(), nil
}
return "", errors.New("Unimplemented constraing: " + c.Type)
}
func (c *Constraint) getPkDDL() string {
var ddl strings.Builder
ddl.WriteString("CONSTRAINT " + c.Name + " PRIMARY KEY(")
for i, col := range c.Columns {
if i != 0 {
ddl.WriteString(", ")
}
ddl.WriteString(col.Name)
}
ddl.WriteString(")")
return ddl.String()
}
func (c *Constraint) getFkDDL() string {
var ddl strings.Builder
ddl.WriteString("CONSTRAINT " + c.Name + " FOREIGN KEY(")
for i, col := range c.Columns {
if i != 0 {
ddl.WriteString(", ")
}
ddl.WriteString(col.Name)
}
ddl.WriteString(") REFERENCES " + c.RefTable.Name + "(")
for i, col := range c.RefColumns {
if i != 0 {
ddl.WriteString(", ")
}
ddl.WriteString(col.Name)
}
ddl.WriteString(")")
return ddl.String()
}
func (Constraint) GetSelectDML() (string, error) {
return "", errors.New("No DML for constraints")
}
func (Constraint) GetInsertDML() (string, error) {
return "", errors.New("No DML for constraints")
}
func (Constraint) GetUpdateDML() (string, error) {
return "", errors.New("No DML for constraints")
}
func (Constraint) GetDeleteDML() (string, error) {
return "", errors.New("No DML for constraints")
}