// 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() }