WIP: Refactor
This commit is contained in:
@@ -6,22 +6,26 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"git.gdulai.com/gdulai/simpleorm/cache"
|
||||
"git.gdulai.com/gdulai/simpleorm/parser"
|
||||
"git.gdulai.com/gdulai/simpleorm/schema"
|
||||
|
||||
log "gitlab.com/gdulai/simpleloglvl"
|
||||
)
|
||||
|
||||
// Type to access the ORM functionalities in a structured manner-
|
||||
type ORM struct {
|
||||
cache *OrmCache
|
||||
cache *cache.SchemaCache
|
||||
}
|
||||
|
||||
// Inits the ORM library.
|
||||
// Param objs is an array which should be an array of the types which describe the tables.
|
||||
func NewORM(objs ...any) *ORM {
|
||||
var parsers []*Parser
|
||||
var tables []*Table
|
||||
var parsers []*parser.Parser
|
||||
var tables []*schema.Table
|
||||
for _, obj := range objs {
|
||||
log.LogDebug("[ORM] Mapping type for: %s", reflect.TypeOf(obj).Name())
|
||||
parser := NewParser(obj)
|
||||
parser := parser.NewParser(obj)
|
||||
parsers = append(parsers, parser)
|
||||
tables = append(tables, parser.ParseColumns())
|
||||
}
|
||||
@@ -29,7 +33,7 @@ func NewORM(objs ...any) *ORM {
|
||||
log.LogDebug("[ORM] Tables initiated, creating cache.")
|
||||
|
||||
// Create cache with the initialized tables
|
||||
cache := NewOrmCache(tables)
|
||||
cache := cache.NewSchemaCache(tables)
|
||||
|
||||
log.LogDebug("[ORM] Cache created.")
|
||||
|
||||
@@ -44,7 +48,7 @@ func NewORM(objs ...any) *ORM {
|
||||
}
|
||||
|
||||
// Builds the DDL and returns it as a string
|
||||
func (orm ORM) CreateDDL() string {
|
||||
func (orm *ORM) CreateSchema() (string, error) {
|
||||
var ddl strings.Builder
|
||||
|
||||
tables := orm.cache.GetAll()
|
||||
@@ -53,8 +57,15 @@ func (orm ORM) CreateDDL() string {
|
||||
if i != 0 {
|
||||
ddl.WriteString("\n")
|
||||
}
|
||||
ddl.WriteString(table.ToDDL())
|
||||
tableDdl, err := table.GetDDL()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
ddl.WriteString(tableDdl)
|
||||
}
|
||||
return ddl.String(), nil
|
||||
}
|
||||
|
||||
return ddl.String()
|
||||
func (orm *ORM) Cache() *cache.SchemaCache {
|
||||
return orm.cache
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user