Compare commits
4 Commits
7f27579050
...
v1.1.12
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f5d1775943 | ||
|
|
8e45a20b6a | ||
|
|
dd4840b694 | ||
|
|
705a215707 |
24
dbHandler.go
24
dbHandler.go
@@ -65,11 +65,14 @@ func NewDBHandler(name, path string, logger *logging.Logger) (dH *DBHandler, err
|
||||
if err := os.MkdirAll(path, 0666); err != err {
|
||||
return nil, err
|
||||
}
|
||||
dH.NewCreatedDB = true
|
||||
}
|
||||
}
|
||||
|
||||
dbPath := filepath.Join(path, name)
|
||||
|
||||
//indicator if database exists
|
||||
_, err = os.Stat(dbPath)
|
||||
dH.NewCreatedDB = err != nil
|
||||
logger.Debug("NewDBHandler", "open database "+dbPath)
|
||||
dH.db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
return
|
||||
@@ -179,8 +182,22 @@ func (dH *DBHandler) GetByKey(model any, key string, value any, LikeSearch bool,
|
||||
query = query.Preload(relation)
|
||||
}
|
||||
if LikeSearch {
|
||||
if vals, ok := value.([]string); ok {
|
||||
subQuery := dH.db
|
||||
for i, v := range vals {
|
||||
v = strings.ReplaceAll(v, "*", "%")
|
||||
if i == 0 {
|
||||
subQuery = subQuery.Where(key+" LIKE ?", v)
|
||||
} else {
|
||||
subQuery = subQuery.Or(key+" LIKE ?", v)
|
||||
}
|
||||
}
|
||||
return query.Where(subQuery).Find(model).Error
|
||||
}
|
||||
|
||||
value = strings.ReplaceAll(fmt.Sprint(value), "*", "%")
|
||||
dH.logger.Debug("getByKey", "find like key "+key+" value "+fmt.Sprint(value))
|
||||
fmt.Println(465, "find like key "+key+" value "+fmt.Sprint(value))
|
||||
return query.Where(key+" LIKE ?", value).Find(model).Error
|
||||
}
|
||||
|
||||
@@ -370,3 +387,8 @@ func (dH *DBHandler) AddRelation(model, relation any, relationName string) error
|
||||
func (dH *DBHandler) DeleteRelation(model, relation any, relationName string) error {
|
||||
return dH.db.Model(model).Association(relationName).Delete(relation)
|
||||
}
|
||||
|
||||
func (dH *DBHandler) ReplaceRelation(model any, relationName string, data any) error {
|
||||
// .Replace() handles the "Delete missing / Add new" logic for you
|
||||
return dH.db.Model(model).Association(relationName).Replace(data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user