feat: Add delete, drop, insert, truncate, update
This commit is contained in:
@@ -1,9 +1,51 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
)
|
||||
|
||||
var (
|
||||
requiredAttributes = [2]string{"name", "type"}
|
||||
onlySupportedTypes = []string{"string", "int", "float", "bool", "date", "time", "datetime", "uuid", "json", "hash"}
|
||||
)
|
||||
|
||||
func (t *Table) Validate() error {
|
||||
func ValidateField(field *Field) error {
|
||||
if err := ValidateHashAlgorithm(field); err != nil {
|
||||
return err
|
||||
}
|
||||
return ValidateFieldType(field)
|
||||
}
|
||||
|
||||
func ValidateFieldType(field *Field) error {
|
||||
if !slices.Contains(onlySupportedTypes, field.Type) {
|
||||
return fmt.Errorf("unsupported type: %s", field.Type)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateHashAlgorithm(field *Field) error {
|
||||
if field.Type != "hash" {
|
||||
return nil
|
||||
}
|
||||
if field.Algorithm == nil {
|
||||
return fmt.Errorf("algorithm is required for hash field")
|
||||
}
|
||||
var hashAlgorithms = []string{"argon2", "bcrypt", "md5", "sha256", "sha512"}
|
||||
for _, algorithm := range hashAlgorithms {
|
||||
if *field.Algorithm == algorithm {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("unsupported hash algorithm: %s", *field.Algorithm)
|
||||
}
|
||||
|
||||
func (t *Table) Validate() error {
|
||||
for _, field := range t.Schema {
|
||||
err := ValidateField(&field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,11 +3,14 @@ package schema
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.secnex.io/secnex/pgson/utils"
|
||||
)
|
||||
|
||||
type Table struct {
|
||||
Name string `json:"table"`
|
||||
Schema []Field `json:"schema"`
|
||||
Name string `json:"table"`
|
||||
PrimaryKey string `json:"primary_key"`
|
||||
Schema []Field `json:"schema"`
|
||||
}
|
||||
|
||||
type Field struct {
|
||||
@@ -25,8 +28,8 @@ type Field struct {
|
||||
type Reference struct {
|
||||
Table string `json:"table"`
|
||||
Column string `json:"column"`
|
||||
OnDelete string `json:"onDelete"`
|
||||
OnUpdate string `json:"onUpdate"`
|
||||
OnDelete string `json:"on_delete"`
|
||||
OnUpdate string `json:"on_update"`
|
||||
}
|
||||
|
||||
// Mapping of field types to SQL types
|
||||
@@ -57,11 +60,13 @@ func (t *Table) JSON() ([]byte, error) {
|
||||
}
|
||||
|
||||
func (f *Field) SQL() string {
|
||||
quotedName := utils.SQLQuoteIdent(f.Name)
|
||||
sqlType := fieldTypeToSQLType[f.Type]
|
||||
if sqlType == "" {
|
||||
return ""
|
||||
}
|
||||
sql := fmt.Sprintf("%s %s", f.Name, sqlType)
|
||||
|
||||
sql := fmt.Sprintf("%s %s", quotedName, sqlType)
|
||||
if f.Nullable != nil && !*f.Nullable {
|
||||
sql += " NOT NULL"
|
||||
}
|
||||
@@ -75,10 +80,10 @@ func (f *Field) SQL() string {
|
||||
sql += " UNIQUE"
|
||||
}
|
||||
if f.Default != nil && f.Primary == nil {
|
||||
sql += fmt.Sprintf(" DEFAULT %s", *f.Default)
|
||||
sql += fmt.Sprintf(" DEFAULT %s", utils.SQLQuoteValue(*f.Default))
|
||||
}
|
||||
if f.References != nil {
|
||||
sql += fmt.Sprintf(" REFERENCES %s(%s)", f.References.Table, f.References.Column)
|
||||
sql += fmt.Sprintf(" REFERENCES %s(%s)", utils.SQLQuoteIdent(f.References.Table), utils.SQLQuoteIdent(f.References.Column))
|
||||
if f.References.OnDelete != "" {
|
||||
sql += fmt.Sprintf(" ON DELETE %s", f.References.OnDelete)
|
||||
}
|
||||
@@ -93,5 +98,5 @@ func (f *Field) SQLReferences() string {
|
||||
if f.References == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(" REFERENCES %s(%s)", f.References.Table, f.References.Column)
|
||||
return fmt.Sprintf(" REFERENCES %s(%s)", utils.SQLQuoteIdent(f.References.Table), utils.SQLQuoteIdent(f.References.Column))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user