Commit 6d822213 authored by Faizal Aziz's avatar Faizal Aziz

adding foreign key and improve db gen

parent cc05862e
...@@ -3,6 +3,7 @@ package dbcon ...@@ -3,6 +3,7 @@ package dbcon
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"reflect" "reflect"
"strings" "strings"
"time" "time"
...@@ -32,6 +33,8 @@ type ( ...@@ -32,6 +33,8 @@ type (
MaxLifeTimeConnection time.Duration MaxLifeTimeConnection time.Duration
MaxIdleConnection, MaxOpenConnection int MaxIdleConnection, MaxOpenConnection int
Logger log.Logger Logger log.Logger
AutoGenerateDDL bool
EntitiesPath string
} }
) )
...@@ -314,6 +317,22 @@ func NewPostgreSql(option *PostgreSqlOption) (ORM, error) { ...@@ -314,6 +317,22 @@ func NewPostgreSql(option *PostgreSqlOption) (ORM, error) {
sql.SetMaxOpenConns(option.MaxOpenConnection) sql.SetMaxOpenConns(option.MaxOpenConnection)
sql.SetMaxIdleConns(option.MaxIdleConnection) sql.SetMaxIdleConns(option.MaxIdleConnection)
dirs, err := os.ReadDir(option.EntitiesPath)
if option.AutoGenerateDDL && len(dirs) > 0 {
var typeRegistry = make(map[string]reflect.Type)
for _, data := range dirs {
instances := reflect.New(typeRegistry[data.Name()]).Elem()
test := instances.MethodByName("TableName")
if !test.IsNil() {
i, _ := test.TryRecv()
generateSQLFromStorage(i.String(), instances)
}
}
}
return &postgresqldb{db: db}, nil return &postgresqldb{db: db}, nil
} }
...@@ -359,7 +378,12 @@ func generateSQLFromStorage(tableName string, columnAndValues interface{}) strin ...@@ -359,7 +378,12 @@ func generateSQLFromStorage(tableName string, columnAndValues interface{}) strin
argName = val argName = val
} }
} }
argName = fmt.Sprintf("constraint %s_%s", strings.Replace(keysName, " ", "_", 1), argName) if argName != "" && strings.Contains(argName, "pk") || strings.Contains(argName, "unique") {
argName = getPKConstraint(tableName, columnName, argName, keysName)
} else if argName != "" && strings.Contains(argName, "fk") {
argName, keysName = getFKConstraint(tableName, columnName, argName, keysName)
}
} }
} }
...@@ -370,3 +394,14 @@ func generateSQLFromStorage(tableName string, columnAndValues interface{}) strin ...@@ -370,3 +394,14 @@ func generateSQLFromStorage(tableName string, columnAndValues interface{}) strin
return fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s);", tableName, columnNamesStr) return fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s);", tableName, columnNamesStr)
} }
func getPKConstraint(tableName, columnName, argsName, keysName string) string {
return fmt.Sprintf("constraint %s_%s_%s_%s", strings.Replace(keysName, " ", "_", 1), tableName, columnName, argsName)
}
func getFKConstraint(tableName, columnName, argsName, keysName string) (args, keys string) {
args = fmt.Sprintf("constraint %s_%s_%s_%s", strings.Replace(keysName, " ", "_", 1), tableName, columnName, argsName)
keysArgs := strings.Split(keys, ";")
keys = fmt.Sprintf(" references %s %s", keysArgs[1], keysArgs[2])
return
}
...@@ -4,7 +4,7 @@ const ( ...@@ -4,7 +4,7 @@ const (
ContentType = "Content-Type" ContentType = "Content-Type"
ApplicationJSON = "application/json" ApplicationJSON = "application/json"
UserAgent = "User-Agent" UserAgent = "User-Agent"
UserAgentValue = "https://digdayatech.id" UserAgentValue = "https://ursabyte.com"
) )
const ( const (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment