Refactor parsing & select order by impl (#6)
Go Tests / test (push) Successful in 1m3s

Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
2026-06-05 15:22:41 +00:00
parent d10d0f8414
commit 238361b066
11 changed files with 792 additions and 528 deletions
+26 -16
View File
@@ -7,7 +7,6 @@ import (
"strings"
"git.gdulai.com/gdulai/simpleorm/cache"
"git.gdulai.com/gdulai/simpleorm/parser"
"git.gdulai.com/gdulai/simpleorm/schema"
log "gitlab.com/gdulai/simpleloglvl"
@@ -21,28 +20,39 @@ type ORM struct {
// 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.Parser
var tables []*schema.Table
typeParsers := make(map[string]*schema.Parser)
log.LogDebug("[ORM] Parsing entities to tables...")
log.LogDebug("[ORM] Step 1: Parsing table columns")
for _, obj := range objs {
log.LogDebug("[ORM] Mapping type for: %s", reflect.TypeOf(obj).Name())
parser := parser.NewParser(obj)
parsers = append(parsers, parser)
tables = append(tables, parser.ParseColumns())
typ := reflect.TypeOf(obj)
log.LogDebug("[ORM] Mapping type for: %s", typ.Name())
parser := schema.NewParser(obj)
parser.ParseColumns()
typeParsers[typ.Name()] = parser
}
log.LogDebug("[ORM] Tables initiated, creating cache.")
log.LogDebug("[ORM] Step 1: Finished parsing columns!")
log.LogDebug("[ORM] Step 2: Parsing constraints...")
for _, parser := range typeParsers {
parser.ParseConstraints(typeParsers)
}
log.LogDebug("[ORM] Step 2: Finished parsing contraints!")
log.LogDebug("[ORM] Step 3: Creating and caching schema...")
var tables []*schema.Table
for _, parser := range typeParsers {
tables = append(tables, parser.ParseTable())
}
// Create cache with the initialized tables
cache := cache.NewSchemaCache(tables)
log.LogDebug("[ORM] Cache created.")
// Finish the parsing with the constraints and add the to the tables
for _, parser := range parsers {
log.LogDebug("[ORM] Parsing constraing for: %s", parser.Table.Name)
parser.ParseConstraints(cache)
log.LogDebug("[ORM] Parsed constraints for: %s", parser.Table.Name)
}
log.LogDebug("[ORM] Step 3: Cache created.")
return &ORM{cache: cache}
}