feat: Change CreateSchemaFromJson to CreateSchema

This commit is contained in:
Björn Benouarets
2025-11-05 15:06:17 +01:00
parent e006490de2
commit 89246cae77
4 changed files with 31 additions and 14 deletions

17
build/create.go Normal file
View File

@@ -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
}

View File

@@ -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)
}

13
query.go Normal file
View File

@@ -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)
}

View File

@@ -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
}