diff --git a/build/create.go b/build/create.go new file mode 100644 index 0000000..6a2cbbd --- /dev/null +++ b/build/create.go @@ -0,0 +1,17 @@ +package build + +import ( + "fmt" + "strings" + + "git.secnex.io/secnex/pgson/schema" +) + +func Create(s *schema.Table) (string, error) { + schemaParts := make([]string, len(s.Schema)) + for i, field := range s.Schema { + schemaParts[i] = field.SQL() + } + query := fmt.Sprintf("CREATE TABLE %s (%s)", s.Name, strings.Join(schemaParts, ",\n")) + return query, nil +} diff --git a/pgson.go b/pgson.go index a32a54a..f27dec9 100644 --- a/pgson.go +++ b/pgson.go @@ -14,6 +14,6 @@ func NewSchemaFromFile(path string) (*schema.Table, error) { return schema.NewTable(data) } -func NewSchemaFromJSON(json []byte) (*schema.Table, error) { +func NewSchema(json []byte) (*schema.Table, error) { return schema.NewTable(json) } diff --git a/query.go b/query.go new file mode 100644 index 0000000..626541a --- /dev/null +++ b/query.go @@ -0,0 +1,13 @@ +package pgson + +import ( + "git.secnex.io/secnex/pgson/build" + "git.secnex.io/secnex/pgson/schema" +) + +func CreateSQL(s *schema.Table) (string, error) { + if err := s.Validate(); err != nil { + return "", err + } + return build.Create(s) +} diff --git a/schema/table.go b/schema/table.go index 1bd2113..017299c 100644 --- a/schema/table.go +++ b/schema/table.go @@ -3,7 +3,6 @@ package schema import ( "encoding/json" "fmt" - "strings" ) type Table struct { @@ -96,15 +95,3 @@ func (f *Field) SQLReferences() string { } return fmt.Sprintf(" REFERENCES %s(%s)", f.References.Table, f.References.Column) } - -func (t *Table) CreateSQL() (string, error) { - if err := t.Validate(); err != nil { - return "", err - } - schemaParts := make([]string, len(t.Schema)) - for i, field := range t.Schema { - schemaParts[i] = field.SQL() - } - query := fmt.Sprintf("CREATE TABLE %s (%s)", t.Name, strings.Join(schemaParts, ",\n")) - return query, nil -}