Initial source commit

This commit is contained in:
2026-05-03 19:23:27 +02:00
parent b27b9add45
commit 106e5c3dc5
10 changed files with 511 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
// 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}
}
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()
}