Files
simpleorm/orm.go
T
gdulai 01336bb911
Go Tests / test (push) Failing after 6s
Implement DML func and related tests
2026-05-04 21:33:29 +02:00

61 lines
1.5 KiB
Go

// Package simpleorm provies a simple, basic ORM functionalities for making
// interaction with relational databses easier.
package simpleorm
import (
"reflect"
"strings"
log "gitlab.com/gdulai/simpleloglvl"
)
// Type to access the ORM functionalities in a structured manner-
type ORM struct {
cache *OrmCache
}
// 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
for _, obj := range objs {
log.LogDebug("[ORM] Mapping type for: %s", reflect.TypeOf(obj).Name())
parser := NewParser(obj)
parsers = append(parsers, parser)
tables = append(tables, parser.ParseColumns())
}
log.LogDebug("[ORM] Tables initiated, creating cache.")
// Create cache with the initialized tables
cache := NewOrmCache(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)
}
return &ORM{cache: cache}
}
// Builds the DDL and returns it as a string
func (orm ORM) CreateDDL() string {
var ddl strings.Builder
tables := orm.cache.GetAll()
for i, table := range tables {
if i != 0 {
ddl.WriteString("\n")
}
ddl.WriteString(table.ToDDL())
}
return ddl.String()
}