110 lines
3.0 KiB
Go
110 lines
3.0 KiB
Go
package build
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"git.secnex.io/secnex/pgson/schema"
|
|
"git.secnex.io/secnex/pgson/sql"
|
|
)
|
|
|
|
func CreateTable(s *schema.Table) (*string, error) {
|
|
if err := sql.ValidateIdent(s.Name); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var cols, comments, pks []string
|
|
|
|
for _, f := range s.Schema {
|
|
if err := sql.ValidateIdent(f.Name); err != nil {
|
|
log.Printf("Invalid identifier: %s", f.Name)
|
|
return nil, err
|
|
}
|
|
|
|
pgType, err := sql.ValidateType(f.Type)
|
|
if err != nil {
|
|
log.Printf("Invalid type: %s", f.Type)
|
|
return nil, err
|
|
}
|
|
|
|
if f.Type == "hash" {
|
|
if _, ok := sql.AlgorithmMap[strings.ToLower(*f.Algorithm)]; !ok {
|
|
log.Printf("Invalid algorithm: %s", *f.Algorithm)
|
|
return nil, fmt.Errorf("invalid algorithm: %s", *f.Algorithm)
|
|
}
|
|
}
|
|
|
|
colParts := []string{sql.QuoteIdent(f.Name), pgType}
|
|
if f.Nullable != nil && !*f.Nullable {
|
|
colParts = append(colParts, "NOT NULL")
|
|
}
|
|
|
|
if f.Unique != nil && *f.Unique {
|
|
colParts = append(colParts, "UNIQUE")
|
|
}
|
|
|
|
// Auto-generate UUID for UUID primary keys if no default is specified
|
|
if f.Primary != nil && *f.Primary && f.Type == "uuid" {
|
|
if f.Default == nil || *f.Default == "" {
|
|
colParts = append(colParts, "DEFAULT gen_random_uuid()")
|
|
} else {
|
|
def, err := sql.ValidateDefault(*f.Default)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
colParts = append(colParts, "DEFAULT "+def)
|
|
}
|
|
} else if f.Default != nil && *f.Default != "" {
|
|
def, err := sql.ValidateDefault(*f.Default)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
colParts = append(colParts, "DEFAULT "+def)
|
|
}
|
|
|
|
if f.References != nil {
|
|
if err := sql.ValidateIdent(f.References.Table); err != nil {
|
|
log.Printf("Invalid references table: %s", f.References.Table)
|
|
return nil, err
|
|
}
|
|
if err := sql.ValidateIdent(f.References.Column); err != nil {
|
|
log.Printf("Invalid references column: %s", f.References.Column)
|
|
return nil, err
|
|
}
|
|
onDelete, err := sql.ValidateAction(f.References.OnDelete)
|
|
if err != nil {
|
|
log.Printf("Invalid on delete: %s", f.References.OnDelete)
|
|
return nil, err
|
|
}
|
|
onUpdate, err := sql.ValidateAction(f.References.OnUpdate)
|
|
if err != nil {
|
|
log.Printf("Invalid on update: %s", f.References.OnUpdate)
|
|
return nil, err
|
|
}
|
|
fk := fmt.Sprintf("REFERENCES %s(%s) ON DELETE %s ON UPDATE %s", sql.QuoteIdent(f.References.Table), sql.QuoteIdent(f.References.Column), onDelete, onUpdate)
|
|
colParts = append(colParts, fk)
|
|
}
|
|
|
|
cols = append(cols, strings.Join(colParts, " "))
|
|
if f.Primary != nil && *f.Primary {
|
|
pks = append(pks, sql.QuoteIdent(f.Name))
|
|
}
|
|
|
|
if f.Comment != nil {
|
|
comments = append(comments, fmt.Sprintf("COMMENT ON COLUMN %s.%s IS '%s';", sql.QuoteIdent(s.Name), sql.QuoteIdent(f.Name), *f.Comment))
|
|
}
|
|
}
|
|
|
|
if len(pks) > 0 {
|
|
cols = append(cols, "PRIMARY KEY ("+strings.Join(pks, ", ")+")")
|
|
}
|
|
|
|
ddl := fmt.Sprintf(sql.DDL_CREATE_TABLE, sql.QuoteIdent(s.Name), strings.Join(cols, ",\n "))
|
|
if len(comments) > 0 {
|
|
ddl += "\n" + strings.Join(comments, "\n")
|
|
}
|
|
|
|
return &ddl, nil
|
|
}
|