feat: Add delete, drop, insert, truncate, update
This commit is contained in:
@@ -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